Java - Interview Questions and Answers for Junior / Entry Level Java Developer

Q1.  What is the difference between List, Set and Map ?

Ans. List - Members are stored in sequence in memory and can be accessed through index. 
Set - There is no relevance of sequence and index. Sets doesn't contain duplicates whereas multiset can have duplicates. Map - Contains Key , Value pairs.

Q2.  Difference between Public, Private, Default and Protected ?

Ans. Private - Not accessible outside object scope. 
Public - Accessible from anywhere. 
Default - Accessible from anywhere within same package.
Protected - Accessible from object and the sub class objects.

Q3.  Difference between HashMap and Hashtable?

Ans. Hashtable is synchronized whereas HashMap is not.

HashMap allows null values whereas Hashtable doesn’t allow null values.

Q4.  What is casting?

Ans. There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference

Q5.  What is difference between Encapsulation And Abstraction?

Ans. 1.Abstraction solves the problem at design level while encapsulation solves the problem at implementation level

2.Abstraction is used for hiding the unwanted data and giving relevant data. while Encapsulation means hiding the code and data into a single unit to protect the data from outside world.

3. Abstraction lets you focus on what the object does instead of how it does it while Encapsulation means hiding the internal details or mechanics of how an object does something.

4.For example: Outer Look of a Television, like it has a display screen and channel buttons to change channel it explains Abstraction but Inner Implementation detail of a Television how CRT and Display Screen are connect with each other using different circuits , it explains Encapsulation.

Q6.  Difference between Overloading and Overriding ?

Ans. Overloading - Similar Signature but different definition , like function overloading. 

Overriding - Overriding the Definition of base class in the derived class.

Q7.  Difference between Vector and ArrayList ?

Ans. Vectors are synchronized whereas Array lists are not.

Q8.  What is a final variable ?

Ans. Final variable is a constant variable. Variable value can't be changed after instantiation.

Q9.  What is a Final Method ?

Ans. A Method that cannot be overriden in the sub class.

Q10.  What is a Final Class ?

Ans. A Class that cannot be sub classed.

Q11.  Name few Java Exceptions ?

Ans. IndexOutofBound , NoClassDefFound , OutOfMemory , IllegalArgument.

Q12.  What is "super" used for ?

Ans. Used to access members of the base class.

Q13.  What is "this" keyword used for ?

Ans. Used to represent an instance of the class in which it appears.

Q14.  What is a finalize method ?

Ans. finalize() method is called just before an object is destroyed.

Q15.  Difference between Process and Thread ?

Ans. Process is a program in execution whereas thread is a separate path of execution in a program.

Q16.  what is the difference between collections class vs collections interface ?

Ans. Collections class is a utility class having static methods for doing operations on objects of classes which implement the Collection interface. For example, Collections has methods for finding the max element in a Collection.

Q17.  Explain Thread States ?

Ans. Runnable - waiting for its turn to be picked for execution by the thread schedular based on thread priorities.

Running - The processor is actively executing the thread code. It runs until it becomes blocked, or voluntarily gives up its turn. 

Waiting: A thread is in a blocked state while it waits for some external processing such as file I/O to finish.

Sleeping - Java threads are forcibly put to sleep (suspended) with Thread.sleep. they can resume using Thread.resume method.

Blocked on I/O - Will move to runnable after I/O condition like reading bytes of data etc changes.

Blocked on synchronization - Will move to Runnable when a lock is acquired.

Dead - The thread is finished working.

Q18.  What is the advantage of using arrays over variables ?

Ans. Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.

Q19.  Advantage of Collection classes over Arrays ?

Ans. Collections are re-sizable in nature. We can increase or decrease the size as per recruitment.
Collections can hold both homogeneous and heterogeneous data's.
Every collection follows some standard data structures.
Collection provides many useful built in methods for traversing,sorting and search. 

Q20.  What is the difference between float and double?

Ans. Float can represent up to 7 digits accurately after decimal point, where as double can represent up to 15 digits accurately after decimal point.

Q21.  What is the difference between System.out ,System.err and

Ans. System.out and System.err both represent the monitor by default and hence can be used to send data or results to the monitor. But System.out is used to display normal messages and results whereas System.err is used to display error messages and represents InputStream object, which by default represents standard input device, i.e., keyboard.

Q22.  Why is Java considered Portable Language ?

Ans. Java is a portable-language because without any modification we can use Java byte-code in any platform(which supports Java). So this byte-code is portable and we can use in any other major platforms.

Q23.  Difference between throw and throws ?

Ans. throw is used to explicitly throw an exception especially custom exceptions, whereas throws is used to declare that the method can throw an exception.

We cannot throw multiple exceptions using throw statement but we can declare that a method can throw multiple exceptions using throws and comma separator.

Q24.  What is an Object ?

Ans. Object is a run time entity whose state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Q25.  What is a Class ?

Ans. A class is a blue print or Mold using which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

Q26.  How finally used under Exception Handling?

Ans. The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

Q27.  What is the difference between a break statement and a continue statement?

Ans. Break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

Q28.  What are the features of encapsulation ?

Ans. Combine the data of our application and its manipulation at one place. 

Encapsulation Allow the state of an object to be accessed and modified through behaviors.

Reduce the coupling of modules and increase the cohesion inside them.

Q29.  Difference between Composition and Inheritance ?

Ans. Inheritance means a object inheriting reusable properties of the base class. Compositions means that an abject holds other objects. 

In Inheritance there is only one object in memory ( derived object ) whereas in Composition , parent object holds references of all composed objects. 

From Design perspective - Inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.