Interview Q & A
Basic Questions
1. What are 4 major OOP concepts?
Abstraction, Polymorphism, Inheritance, Encapsulation
2. What is polymorphism?
Polymorphism is the ability of an object to take on many forms. In Java, there are two types of polymorphism such as Compile Time Polymorphism (or Static Polymorphism) & Run time Polymorphism (or Dynamic Polymorphism)
Compile Time Polymorphism — Method Overloading Run Time Polymorphism — Method Overriding
3. What is Method Overloading & Overriding?
Overloading: Methods have same names but different number or type or order of parameters. Overloading is determined at the compile time.
E.g. display(String name) display(String name, int number) display(int number, String name)
Overriding: A child class implements the method with same signature as a method in a parent class.
4. Why Method Overriding is called dynamic polymorphism?
In method overriding both the super & sub class have same method signature, compile doesn’t figure out which method to call at compile-time. In this case, JVM decides which method to call at runtime that’s why it’s called dynamic polymorphism.
5. Will return type of method considered for overloading? No, method won’t be considered overloaded, if the return type is the only different between two methods.
For Overloading, method name should be same and either of the following should be true. Change in number of parameter Change in type of parameter Change in order of parameter
6. What is data encapsulation and whats the significance ?
Data encapsulation is binding data and function together. Its significance is we can hide implementation details from user.
7. Difference between Break and Continue Statement
Break skips the loop completely and execute next statement after loop. Whereas Continue skips only the current iteration of loop and continue to execute the next iteration of loop.
8. What is the use of final keyword in Java ?
Final have different meaning depends on the context it’s been used. In class : Final class can’t be inherited further..simply there cant be any subclass for a final class. String is the example for final class in Java. In Property: If a class property is declared as final, it will be treated as constant.. so value can’t be changes further (so needs a value while creating itself). In Method: if method declared as final, it can’t be override
9. Whether the program get compile, if public static void main changed to static public void main?
Compilation will get successful
10. In How many ways you can create a Java String object?
a.) Assign String value directly
String name =”Vicky AV”;
b.) Using new operator
String name= new String(“Vicky AV”);
c.) Using character array
char[] nameArray = {‘v’,’i’,’c’,’k’,’y’,’ ‘,’A’,’V’}; String name= new String(nameArray);
11. What is the use of toString() method in Java
Whenever you print an object using toString() method, it will give textual representation of the object.
We can override toString() method to give meaningful textual representation for an object. Used in debugging and logging.
12. What are all access specifier in Java ? State their purposes.
Private: Only the same class can use it Public: Any class can use it Protected: Same class as well as inherited class can use it default: Only classes in same package can use it
13. Can you make constructor final?
No, Constructor can’t be final.
14. What is constructor overloading and what is purpose of default constructor ?
Like method overloading, constructor also can be overloaded. For overloading, we have same method name (here method name is same as class name since constructor) but difference in either of the following
Number of parameters Type of parameter Order of parameters
15. How many constructor a class can have ?
Constructor is a special method with class name as method name. Java supports constructor overloading. So we can have any number of constructors in Java. But If you are not specifying any constructor, compiler will add a default constructor to your class (constructor without parameter). So there will be at least one constructor in a Java class.
Difference Questions
Abstract Class vs Interface
Abstract class | Interface |
---|---|
Can have abstract and non-abstract methods. | Can have only abstract methods. |
Doesn't support multiple inheritance. | Supports multiple inheritance. |
Can have final, non-final, static and non-static variables. | Only static and final variables. |
Achieves only partial abstraction | Achieves 100% abstraction |
Overloading vs Overriding
Overloading | Overriding |
---|---|
Parameter must be different. | Parameters are same |
Overloaded method can be in same class or in sub class | Overrided method should be in sub class |
Purpose: To add more behaviours | Purpose: To override existing behavior. |
Final, static & private methods can be overloaded | Final, static & private methods can’t be overrided |
ArrayList vs LinkedList
ArrayList | LinkedList |
---|---|
Used array in back end | Uses Doubly linked list in back end |
Better for frequent reading | Better for frequent add and delete |
HashMap vs HashTable
HashMap | HashTable |
---|---|
Not Synchronized so fast | Synchronized so slow |
Not a legacy class | Legacy Class (From Java 1.1) |
Uses less memory | Used more memory |
To iterate elements, iterator is used | Enumerator is used |
Can contain one null key and value | Null not allowed |
Comparable vs Comparator
Comparable | Comparator |
---|---|
Comparable provides only one way of sorting | Provides multiple way of sorting. |
Method - compareTo(). | Method - compare(). |
java.lang package. | java.util package. |
Actual class is modified. | Actual class is not modified. |
Constructor vs Method
Constructor | Method |
---|---|
initialize the state of object | Define behavior of object |
No return type | Have return type |
Name same as class name | Developer choice |
Constructor called with new operator | Method called with dot operator |
Can be private, protected, public | Can be private, protected, public, abstract, static, final |
StringBuffer vs StringBuilder
StringBuffer | StringBuilder |
---|---|
Mutable | Mutable |
Synchronized so thread safe | Not Synchronized |
Less efficient | More efficient |
Throw vs Throws
Throw | Throws |
---|---|
Used inside method block | Used in method declaration |
Throw is followed by an exception instance variable | Followed by Exception class name |
You cant throw more than one Exception | Can specify more than one Exception in Throws |