Top Java Interview Questions – Part 2

Posted on Posted in Beginner, Online Java Training

top java interview questions : Merit Campus


Top Java Interview Questions 2017 – Part 2:

Since last year as I became more active & started conducting frequent surveys among Java communities, I observed that there are different types of active learners –

  • Beginners/College students who want to learn basics of Java.
  • Professionals who want to improve their Java skills.
  • Learners who are keen to practice more Java programs.


But there was one query which was common among everyone – ‘Java Interview Questions’. In my previous Linkedin post I curated list of top Java programs asked in technical interviews. In this blog I would be focussing on Java Interview questions which are asked to check the concepts and logical ability of the candidate. If your concepts for the below given questions are crystal clear, solving Java programs in real-time will never be a problem. Here it goes:


1. Difference between various data types in Java.

The data types are broadly classified into two types – Primitive and Non-Primitive data types.

The primitive data types are inherently supported by programming languages whereas non-primitive data types are defined using the primitive ones.The below snapshot shows categorisation of different data types in detail. Read further here



2. How to find factorial using recursion in Java?

class FactorialUsingRecursion


   public static void main(String s[])


       System.out.println(“Factorial of 5 is “ + factorial(5) );


   public static int factorial(int i)


       if( i == 1 )    // LINE A


           return 1;


       return i * factorial(i 1); // LINE B



Check the output and reasoning here ..



3. What is the difference between for, while and do-while loops?

–> While loop checks for the condition first. so it may not even enter into the loop, if the condition is false.

–> do while loop, execute the statements in the loop first before checks for the condition. At least one iteration takes places, even if the condition is false.

–> for loop is similar to while loop except that

  • initialization statement, usually the counter variable initialization
  • a statement that will be executed after each and every iteration in the loop, usually counter variable increment or decrement.

Understand the difference, check out the programs here .



4. Explain various types of sorting on Arrays.

Bubble Sort, Insertion Sort and Selection Sort are few important types of arrays you must know. The algorithm for Bubble and Selection sort is given below:


–> Bubblesort

  • compare 1st and 2nd elements
  • if 1st larger than 2nd, swap
  • compare 2nd and 3rd, and swap if necessary
  • continue until compare the last two elements
  • the largest element is now the last element in the array.
  • repeat starting from the beginning until no swaps are performed (i.e.,
  • the array is sorted)
  • each time you go through the elements bubbling up the largest element
  • no need to try the last i elements for the ith run since the end
  • elements are already sorted


–> Selection Sort

  • array to be sorted: A
  • array to be returned: B
  • find smallest element in A and put in B
  • mark space in A with null so it won’t be chosen again
  • repeat last two steps until B is sorted array

See example of How to use Bubble Sort?



5. Define Class Hierarchy.

The class hierarchy defines the inheritance relationships between classes. Refer the program to understand it better:



6. Explain class composition.

Refer the program here to understand it better.



7. Define Abstract Classes vs Interfaces and give their examples.

Read in detail here.



8. Exceptions – How does a try, catch, finally block work?

Exception causes abnormal termination of program and consume lot of resources compared to the normal code of execution. The difference between error and exception is explained in the diagram given below.

Please refer the program here.



9. Threads + I/O – File Copy from one directory to another using threads.

Thread groups are created to avoid confusion between various individual threads and help them identify better. The thread methods can be understood better here.


Leave a Reply

Your email address will not be published. Required fields are marked *