Skip to main content

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 classInterface
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 abstractionAchieves 100% abstraction

Overloading vs Overriding

OverloadingOverriding
Parameter must be different.Parameters are same
Overloaded method can be in same class or in sub classOverrided method should be in sub class
Purpose: To add more behavioursPurpose: To override existing behavior.
Final, static & private methods can be overloadedFinal, static & private methods can’t be overrided

ArrayList vs LinkedList

ArrayListLinkedList
Used array in back endUses Doubly linked list in back end
Better for frequent readingBetter for frequent add and delete

HashMap vs HashTable

HashMapHashTable
Not Synchronized so fastSynchronized so slow
Not a legacy classLegacy Class (From Java 1.1)
Uses less memoryUsed more memory
To iterate elements, iterator is usedEnumerator is used
Can contain one null key and valueNull not allowed

Comparable vs Comparator

ComparableComparator
Comparable provides only one way of sortingProvides 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

ConstructorMethod
initialize the state of objectDefine behavior of object
No return typeHave return type
Name same as class nameDeveloper choice
Constructor called with new operatorMethod called with dot operator
Can be private, protected, publicCan be private, protected, public, abstract, static, final

StringBuffer vs StringBuilder

StringBufferStringBuilder
MutableMutable
Synchronized so thread safeNot Synchronized
Less efficientMore efficient

Throw vs Throws

ThrowThrows
Used inside method blockUsed in method declaration
Throw is followed by an exception instance variableFollowed by Exception class name
You cant throw more than one ExceptionCan specify more than one Exception in Throws