Most Popular Java Interview Questions
Most Popular Java Interview Questions
Made by:
Smrutimayee Mishra
A/23, NILAKANTHANAGAR, BEHIND PASSPORT OFFICE, NAYAPALLI, BHUBNESWAR www.theexcellency.in
Page 1
Java Important Questions & Answers
Contents
Q#1)What is Java? .................................................................................................................................. 6
Q #2) What are the features in JAVA? ................................................................................................... 6
Q #3) How does Java enable high performance? .................................................................................... 6
Q #4) What are the Java IDE’s?.............................................................................................................. 6
Q #5) What do you mean by Constructor? ............................................................................................. 6
Q #6) What is meant by Local variable and Instance variable? .............................................................. 7
Q #7) What is a Class? ............................................................................................................................ 7
Q #8) What is an Object? ........................................................................................................................ 7
Q #9)What are the Oops concepts? ......................................................................................................... 7
Q #10) What is Inheritance? ................................................................................................................... 8
Q #11) What is Encapsulation?.............................................................................................................. 8
Q #12) What is Polymorphism? ............................................................................................................. 9
Q #13) What is meant by Method Overriding?...................................................................................... 9
Q #14) What is meant by Overloading?................................................................................................ 10
Q #15) What is meant by Interface? ..................................................................................................... 11
Q #16) What is meant by Abstract class? ............................................................................................. 11
Q #17) Difference between Array and Array List. .............................................................................. 12
Q #18) Difference between String, String Builder, and String Buffer................................................. 12
Q #19) Explain about Public and Private access specifiers. ................................................................. 13
Q #20) Difference between Default and Protected access specifiers. ................................................... 14
Q #21) Difference between HashMap and HashTable. ........................................................................ 15
Q #22) Difference between HashSet and TreeSet................................................................................. 15
Q #23) Difference between Abstract class and Interface. ..................................................................... 15
Q #24) What is mean by Collections in Java? ..................................................................................... 16
Q #25) What are all the Classes and Interfaces that are available in the collections? .......................... 16
Q #26) What is meant by Ordered and Sorted in collections? .............................................................. 17
Q #27) Explain about the different lists available in the collection. ..................................................... 17
Q #29). Explain about Map and their types. ......................................................................................... 20
Q #30) Explain the Priority Queue. ...................................................................................................... 21
Q #31) What is mean by Exception? .................................................................................................... 22
Q #32) What are the types of Exceptions? ............................................................................................ 22
Q #33) What are the different ways to handle exceptions? .................................................................. 22
Q #34) What are the Advantages of Exception handling? .................................................................... 23
A/23, NILAKANTHANAGAR, BEHIND PASSPORT OFFICE, NAYAPALLI, BHUBNESWAR www.theexcellency.in
Page 2
Java Important Questions & Answers
Page 3
Java Important Questions & Answers
Page 4
Java Important Questions & Answers
Page 5
Java Important Questions & Answers
Q#1)What is Java?
Ans: Java is a high-level programming language and is platform independent.
Java is a collection of objects. It was developed by Sun Microsystems. There are a lot of
applications, websites and Games that are developed using Java.
Page 6
Java Important Questions & Answers
If the user created a constructor with a parameter then he should create another
constructor explicitly without a parameter.
3 int b= 5;
4 public void add(){ //Method declaration
5 int c = a+b;
6}
7}
Page 7
Java Important Questions & Answers
Abstraction
Interface
Page 8
Java Important Questions & Answers
}
}
For encapsulation, we need to make all the instance variables as private and create setter and
getter for those variables. Which in turn will force others to call the setters rather than access the
data directly.
9 Manipulation addition = new Addition();//Manipulation is reference type and Addition is reference type
10 addition.add();
11 }
12 }
Using Manipulation reference type we can call the Addition class “add()” method. This ability is
known as Polymorphism.
Polymorphism is applicable for overriding and not for overloading.
Page 9
Java Important Questions & Answers
}
Public static void main(String args[]){
Addition addition = new Addition();
addition.add();
}
}
Here the add() method having different parameters in the Addition class is overloaded in the same
class as well as with the super class.
Note: Polymorphism is not applicable for method overloading.
Page 10
Java Important Questions & Answers
Page 11
Java Important Questions & Answers
\Ans: String: String variables are stored in “constant string pool”. Once the string reference
changes the old value that exists in the “constant string pool”, it cannot be erased.
Example:
String name = “book”;
Constant string pool
.
If the name value has changed from “book” to “pen”.
Constant string pool
Page 12
Java Important Questions & Answers
Once the name value has been changed to “pen” then the “book” is erased in the stack.
String Builder:
This is same as String Buffer except for the String Builder which is not threaded safety that is not
synchronized. So obviously performance is fast.
Public members in Class A are visible to Class B (Same package) as well as Class C (Different
package).
Private:
Private members are visible in the same class only and not for the other classes in the same
package as well as classes in the outside packages.
Page 13
Java Important Questions & Answers
Private members in class A is visible only in that class. It is invisible for class B as well as class
C.
Default members in Class A are visible to the other classes which are inside the package and
invisible to the classes which are outside the package.
So Class A members are visible to the Class B and invisible to the Class C.
Protected:
.
Protected is same as Default but if a class extends then it is visible even if it is outside the
package.
Class A members are visible to Class B because it is inside the package. For Class C it is invisible
but if Class C extends Class A then the members are visible to the Class C even if it is outside the
package.
Page 14
Java Important Questions & Answers
HashMap HashTable
Iterator is used to iterate the values Enumerator is used to iterate the values
Allows one null key and multiple null values Doesn’t allow anything that is null
HashSet TreeSet
Inserted elements are in random order Maintains the elements in the sorted order
Page 15
Java Important Questions & Answers
Q #25) What are all the Classes and Interfaces that are available in the
collections?
Ans: Given below are the Classes and Interfaces that are available in Collections:
Interfaces:
Collection
List
Set
Map
Sorted Set
Sorted Map
Queue
Classes:
Lists:
Array List
Vector
Linked List
Sets:
Hash set
Linked Hash Set
Tree Set
Maps:
Hash Map
Hash Table
Tree Map
Linked Hashed Map
Queue:
Priority Queue
Page 16
Java Important Questions & Answers
Page 17
Java Important Questions & Answers
names.add (“apple”);
names.add (“banana”);
names.add (“kiwi”);
names.add (“apple”);
System.out.println (“names”);
}
}
Output:
[cherry,apple,banana,kiwi,apple]
Vector also maintains the insertion order and accepts the duplicates.
Linked List:
Elements are doubly linked to one another.
Performance is slow than Array list.
Good choice for insertion and deletion.
In Java 5.0 it supports common queue methods peek( ), Pool ( ), Offer ( ) etc.
Example:
public class Fruit {
public static void main (String [ ] args){
Linkedlist <String> names = new linkedlist <String> ( ) ;
names.add(“banana”);
names.add(“cherry”);
names.add(“apple”);
names.add(“kiwi”);
names.add(“banana”);
System.out.println (names);
}
}
Output
[ banana,cherry,apple,kiwi,banana]
Maintains the insertion order and accepts the duplicates.
Q #28) Explain about Set and their types in a collection?
Ans: Set cares about uniqueness. It doesn’t allow duplications. Here “equals ( )” method is used
to determine whether two objects are identical or not.
Hash Set:
Unordered and unsorted.
Uses the hash code of the object to insert the values.
Use this when the requirement is “no duplicates and don’t care about the order”.
Example:
public class Fruit {
public static void main (String[ ] args){
HashSet<String> names = new HashSet <=String>( ) ;
names.add(“banana”);
names.add(“cherry”);
names.add(“apple”);
names.add(“kiwi”);
names.add(“banana”);
System.out.println (names);
A/23, NILAKANTHANAGAR, BEHIND PASSPORT OFFICE, NAYAPALLI, BHUBNESWAR www.theexcellency.in
Page 18
Java Important Questions & Answers
}
}
Output:
[banana, cherry, kiwi, apple]
Maintains the insertion order in which they have been added to the Set. Duplicates are not
allowed.
Tree Set:
It is one of the two sorted collections.
Uses “Read-Black” tree structure and guarantees that the elements will be in an ascending
order.
We can construct a tree set with the constructor by using comparable (or) comparator.
Example:
public class Fruits{
public static void main (String[ ]args) {
Treeset<String> names= new TreeSet<String>( ) ;
names.add(“cherry”);
names.add(“banana”);
names.add(“apple”);
names.add(“kiwi”);
names.add(“cherry”);
System.out.println(names);
}
}
Output:
[apple, banana, cherry, kiwi]
TreeSet sorts the elements in an ascending order. And duplicates are not allowed.
A/23, NILAKANTHANAGAR, BEHIND PASSPORT OFFICE, NAYAPALLI, BHUBNESWAR www.theexcellency.in
Page 19
Java Important Questions & Answers
Hash Table:
Like vector key, methods of the class are synchronized.
Thread safety and therefore slows the performance.
Doesn’t allow anything that is null.
Example:
public class Fruit{
public static void main(String[ ]args){
Hashtable<Sting,String> names =new Hashtable<String,String>( );
names.put(“key1”,“cherry”);
names.put(“key2”,“apple”);
names.put(“key3”,“banana”);
names.put(“key4”,“kiwi”);
names.put(“key2”,“orange”);
System.out.println(names);
}
}
Output:
{key2=apple, key1=cherry,key4=kiwi, key3=banana}
Page 20
Java Important Questions & Answers
TreeMap:
Sorted Map.
Like Tree set, we can construct a sort order with the constructor.
Example:
public class Fruit{
public static void main(String[ ]args){
TreeMap<Sting,String> names =new TreeMap<String,String>( );
names.put(“key1”,“cherry”);
names.put(“key2”,“banana”);
names.put(“key3”,“apple”);
names.put(“key4”,“kiwi”);
names.put(“key2”,“orange”);
System.out.println(names);
}
}
Output:
{key1=cherry, key2=banana, key3 =apple, key4=kiwi}
It is sorted in ascending order based on the key. Duplicate keys are not allowed.
Page 21
Java Important Questions & Answers
Checked Exceptions must either declare the exception using throes keyword (or) surrounded by
appropriate try/catch.
It includes:
Arithmetic Exception
ArrayIndexOutOfBounds Exception
Example:
1 class Manipulation{
2 public static void main(String[] args){
3 add();
4}
5 Public void add(){
6 try{
Page 22
Java Important Questions & Answers
7 addition();
8 }catch(Exception e){
9 e.printStacktrace();
10 }
11 }
12 }
#2) By declaring throws keyword:
At the end of the method, we can declare the exception using throws keyword.
Example:
1 class Manipulation{
2 public static void main(String[] args){
3 add();
4}
5 public void add() throws Exception{
6 addition();
7}
8}
catch:
This is followed by try block. Exceptions are caught here.
finally:
This is followed either by try block (or) catch block. This block gets executed regardless of an
exception. So generally clean up codes are provided here.
Example:
A/23, NILAKANTHANAGAR, BEHIND PASSPORT OFFICE, NAYAPALLI, BHUBNESWAR www.theexcellency.in
Page 23
Java Important Questions & Answers
If an exception occurred in the addition() method is not caught, then it moves to the
method add(). Then it is moved to the main() method and then it will stop the flow of execution.
It is called Exception Propagation.
Example:
final int = 12;
Final method:
A final keyword in a method that couldn’t be overridden. If a method is marked as a final, then it
can’t be overridden by the subclass.
Final class:
If a class is declared as final, then the class couldn’t be subclassed. No class can extend the final
class.
Page 24
Java Important Questions & Answers
Example:
1 Public class Addition extends Thread {
2 public void run () {
3}
4}
The disadvantage of using a thread class is that we cannot extend any other classes because we
have already extend the thread class. We can overload the run () method in our class.
Example:
1 Public class Addition implements Runnable {
2 public void run () {
3}
4}
Example:
1 public static void main (String[] args){
Page 25
Java Important Questions & Answers
Wait() and the other above-mentioned methods do not give the lock on the object immediately
until the currently executing thread completes the synchronized code. It is mostly used in
synchronization.
Example:
1 public static void main (String[] args){
2 Thread t = new Thread ();
3 t.start ();
4 Synchronized (t) {
5 Wait();
6}
7}
This method is used to send a signal to wake up a This method sends the signal to wake up all the
single thread in the waiting pool. threads in a waiting spool.
Page 26
Java Important Questions & Answers
Waiting
Blocked
Sleep:
Sleep () method is used to sleep the currently executing thread for the given amount of time.
Once the thread is wake up it can move to the runnable state. So sleep () method is used to delay
the execution for some period.
It is a static method.
Example:
Thread. Sleep (2000)
So it delays the thread to sleep 2 milliseconds. Sleep () method throws an uninterrupted
exception, hence we need to surround the block with try/catch.
Page 27
Java Important Questions & Answers
Once the execution reaches, t.start () line then a new thread is created and the new stack for the
thread is also created. Now JVM switches to the new thread and the main thread are back to the
runnable state.
The two stacks look as shown below.
Now, the user thread executed the code inside the run() method.
Once the run() method has completed, then JVM switches back to the main thread and the User
thread has completed the task and the stack was disappeared.
JVM switches between each thread until both the threads are completed. This is called Multi-
threading.
Page 28
Java Important Questions & Answers
New
Runnable
Running
Non-runnable (Blocked)
Terminated
New:
In New state, Thread instance has been created but start () method is not yet invoked. Now the
thread is not considered alive.
Runnable:
The Thread is in runnable state after invocation of the start () method, but before the run ()
method is invoked. But a thread can also return to the runnable state from waiting/sleeping. In
this state the thread is considered alive.
Running:
The thread is in running state after it calls the run () method. Now the thread begins the
execution.
Non-Runnable(Blocked):
The thread is alive but it is not eligible to run. It is not in runnable state but also, it will return to
runnable state after some time.
Locks are per objects. Every Java object has a lock. A lock has only one key. A thread can access
a synchronized method only if the thread can get the key to the objects lock.
Example:
public class ExampleThread implements Runnable{
A/23, NILAKANTHANAGAR, BEHIND PASSPORT OFFICE, NAYAPALLI, BHUBNESWAR www.theexcellency.in
Page 29
Java Important Questions & Answers
Page 30
Java Important Questions & Answers
Serialization is the process which is used to Deserialization is the opposite process of serialization
convert the objects into byte stream where we can get the objects back from the byte stream.
1.What is JVM?
The Java interpreter along with the runtime environment required to run the Java application
in called as Java virtual machine(JVM)
Page 31
Java Important Questions & Answers
Page 32
Java Important Questions & Answers
17. What is a layout manager and what are different types of layout
managers available in java AWT?
A layout manager is an object that is used to organize components in a container. The
different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout and
GridBagLayout
Page 33
Java Important Questions & Answers
Page 34
Java Important Questions & Answers
developing an RMI object are: a) Define the interfaces b) Implementing these interfaces c)
Compile the interfaces and their implementations with the java compiler d) Compile the
server implementation with RMI compiler e) Run the RMI registry f) Run the application.
Page 35
Java Important Questions & Answers
Page 36
Java Important Questions & Answers
50. What is the difference between the prefix and postfix forms of the
++ operator?
The prefix form performs the increment operation and returns the value of the increment
operation. The postfix form returns the current value all of the expression and then performs
the increment operation on that value.
Page 37
Java Important Questions & Answers
Page 38
Java Important Questions & Answers
64. How many objects are created in the following piece of code?
MyClass c1, c2, c3;
c1 = new MyClass ();
c3 = new MyClass ();
Answer: Only 2 objects are created, c1 and c3. The reference c2 is only declared and not
initialized.
65.What is UNICODE?
Unicode is used for internal representation of characters and strings and it uses 16 bits to
represent each other.
Page 39