Top 50 Java Coding Interview Questions

May 10, 2022
Xblog 100InterviewQuestions


The job interview is one of the most integral parts of the recruitment process and the questions asked in the interviews for the Java developer position could be tricky to answer despite having adequate knowledge of Java. This article contains the 50 most important Java coding interview questions (and their answers) for newbies as well as experienced programmers to help them prepare for the java job roles

Top 50 Java Coding Interview Questions

1. Explain the main method() in Java.

Ans. In Java programs, main() method is the point from where the program starts its execution or simply the entry point of Java programs, making it one of the most important methods.


2. Explain the term ‘public’ in Java.

Ans. Public is an Access modifier, which implies from where and who can access this method. It also makes the main() method globally available.


3. What is meant by the term ‘static’ in Java?

Ans. The static keyword makes a method class-related. The main() method is static so that allows JVM to invoke it without instantiating the class.


4. What is meant by the term ‘void’ in Java?

Ans. Void is a keyword used to specify that a method does not return any value indicating that the main method does not return anything.


5. What is meant by (String args[]) in the main method in Java?

Ans. String[] args stores Java command-line arguments and is an array of type java.lang.String class. Here, the name of the String array is args but it is not fixed and users are allowed to use any name in place of it.


6. Why Java is an OOP language?

Ans. Java is purely an object-oriented programming language as everything in java is an object, all the program codes and data reside within classes and objects.


7. What is a singleton class in Java?

Ans. A Singleton class in Java allows only a single object to be created and provides global access to all other classes through that single object or instance.


8. Can we use a variable in Java without initializing it?

Ans. Yes, If the variable is a class variable or Boolean type then it is not necessary to initialize them. The default value for the class variable is 0, and in the case of Boolean, it is false. But, if a variable is declared inside any functions or in constructors then it must be initialized, else the compile will throw an error.


9. How to manually throw an exception in Java?

Ans. Java allows throwing a user-defined exception explicitly using the ‘throw’ keyword.


10. How objects are stored in Java?

Ans. As all the objects are created dynamically in Java, they are stored in heap memory.


11. How to find the actual size of an object on the heap?

Ans. One way to find the estimated size of an object in Java is by using the getObjectSize(Object) method of the Instrumentation interface.


12. What are access modifiers in Java?

Ans. Access modifiers are used to set the accessibility of classes, constructors, methods, and other members of Java. Java provides four access modifiers i.e., default, public, private, and protected.


13. There are two classes, class01 has 2 methods, 4 variables, and no objects whereas class02 has 3 methods, 6 variables, and no objects. Which of these classes will have more memory allocated?

Ans. Memory is never allocated before the creation of objects. Since both classes have no objects created so no memory is allocated on the heap for either of the class.


14. What will happen if an exception is not handled in a code?

Ans. the program will terminate abruptly and the code past the line that caused the exception will not be executed.


15. If you have multiple constructors defined in a class. Can you call a constructor from another constructor’s body?

Ans. Yes, any number of constructors can be present in a class and they can be called by a constructor from another constructor using this() method. It is called constructor overloading.


16. Can we use primitive data types as objects in Java?

Ans. No, primitive data types such as int, float, Boolean cannot be used as objects.


17. Which types of exceptions are caught at compile time?

Ans. Checked type of exceptions that are caught at compile time.


18. What are the different states of a thread in Java?

Ans. We got 6 different states of a thread,

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated


19. Is it possible to use a default constructor of a class even if an explicit constructor is defined?

Ans. Java provides a default no-argument constructor for classes only when they don’t have a constructor explicitly defined for them. Once a constructor is defined by the developer, the default constructor cannot be used.


20. Can you override a method by using the same method name and parameters but different return types?

Ans. Yes. overridden methods can have the same name and parameters but a different return type.


21. Is it possible to compile a java class successfully without even having the main method in it?

Ans. Technically, yes you can compile and execute a class without a main method by using static block. Although, after the static block will get executed, you will get an error saying “no main method found”.


22. Can you call a non-static method from inside a static method?

Ans. A static method provides no reference to an object of its class hence, no, you cannot call a non-static method inside a static one.


23. Name the two environment variables that must be set to run a Java code?

Ans. The PATH and CLASSPATH are the two most important environment variables that must be set to run a code.


24. Can a class be inherited from more than one class in Java?

Ans. When one class is inherited from more than one class, it is called multiple inheritances and Java does not allow multiple inheritances.


25. Can a constructor have a different name than a Class name in Java?

Ans. No, the name of the constructor must be the same as the name of the class, you cannot use a different name.


26. What is the difference between Round() and Ceil() functions?

Ans. ceil() always rounds the number up regardless of the value whereas round() rounds correctly depending on the number.


27. Can you use goto in Java to go to a particular line?

Ans. Java does not support goto, although it is reserved as a keyword just in case, they wanted to add it in a later Java version.


28. What is an anonymous class?

Ans. An anonymous class in Java is a class not given a name and is both declared and instantiated in a single statement. You should consider using an anonymous class whenever you need to create a class that will be instantiated only once.


29. Is String a primitive data type in java?

Ans. No, it is a non-primitive data type but it is predefined in java.


30. Why Strings are also called Immutable in Java?

Ans. They are called immutable because they provide no methods that modify the state of an existing String object. The only provided method is for creating new String objects based on the content of existing ones.


31. What is the difference between an array and a Vector?

Ans. An array is a data structure that stores a fixed number of elements of the same type in a  sequence Whereas a Vector is a sequential-based container and has a dynamic size i.e they can resize themselves.


32. What is meant by multi-threading?

Ans. Multithreading in Java refers to the process of executing multiple threads simultaneously for maximum utilization of the CPU. It is easily implemented as Thread is a very lightweight process in Java.


33. When do we use Runnable Interface in Java?

Ans. A runnable interface is supposed to be implemented by any class when its objects are intended to be executed by a thread.


34. How many ways are there to implement multi-threading in Java?

Ans. There are two ways to implement multithreading in java. By extending the Thread class and by implementing a Runnable interface.


35. When you need to make a lot of changes in data, which data type should be used, String or StringBuffer, and why?

Ans. String Buffer is a more preferred choice as the StringBuffer class is used to represent characters that can be modified. StringBuffer is also faster than String when performing changes like simple concatenations.


36. What is the purpose of using the Break in Switch Statement?

Ans. The break statement is used to end the processing of a particular labeled statement within the switch statement. It branches directly to the end of the switch statement. Without break, the program will continue to go till the next labeled statement or till the end of the statement.


37. What is meant by garbage collection in Java?

Ans. Java garbage collection is the process of automatic memory management in Java such as creating and destroying objects automatically.


38. Can a class be a superclass as well as a sub-class at the same time?

Ans. Yes. This implementation is called multilevel inheritance and it is possible in Java. The last class to be inherited will have the properties of both classes above it.


39. How objects of a class are created if no constructor is defined in the class?

Ans. If no constructor is defined then the compiler will generate a default constructor. i.e., a zero number parameter constructor.


40. How can you ensure in multi-threading that a resource is not used by multiple threads simultaneously?

Ans. The synchronized keyword is used to ensure that only one thread is using a shared resource at a time and other threads can get control of the resource only once it has become free from the other threads using it.


41. Can you call the constructor method more than once?

Ans. No, it is called only once for an object at the time of object creation hence, you cannot invoke the constructor more than once for an object after it is created.


42. Can you make two methods in a class with the same name?

Ans. No, you cannot declare more than one method with the same name because the compiler cannot be able to tell them apart.


43. How can you make a copy of a java object?

Ans. In Java, there is no operator to create a copy of an object. You can use the assignment operator-like or clone() method but it will just create a copy of the reference variable and not the object.


44. What is the benefit of using inheritance in Java?

Ans. Inheritance increases the code reusability as well as makes your code manageable and improves the readability.


45. How to use Pointers in Java class?

Ans. Java does not support pointers explicitly, but it uses pointers, implicitly. Java uses pointers for manipulations of references but these pointers are not available for outside use.


46. Can you restrict a class so that no class can be inherited from it?

Ans. You can restrict a class from being inherited by using the final keyword in the declaration of the class.


47. What is the access scope of the Protected Access specifier?

Ans. The protected declared members of a class are only accessible to a class derived from it.


48. How to increase the size of an array after its declaration?

Ans. It is not possible to increase the size of an array after its declaration as it is a static data structure.


49. What is a Local class in Java?

Ans. A local class is locally declared within a method in Java, instead of as a member of a class. The main feature of it is that as local classes are associated with a containing instance, users can access any members, including private members, of the containing class.


50. What is the ternary operator in Java?

Ans. A Java ternary operator allows you to write an if statement in one line of code. A ternary operator can either evaluate to true or false.

See Also: 10 Best Java Books For Advanced Programmers

Write a Java Code

These java coding interview questions mentioned above are some straight verbal questions that you can expect. Along with that, you could also be asked to write a code. The duration of the interview is not very extended that is why the interviewers usually ask such questions that would not take a lot of time to be answered but also is enough to judge the coding skills of the candidate. Such tasks could include,

  • Implement a stack using an array or Linked list.
  • Output the second largest number in an array.
  • Implement a queue using a given array.
  • Implement a doubly linked list.
  • Reverse the given linked list.
  • Find the nth element from a linked list.
  • Implement selection sort/ insertion sort/bubble sort/quicksort/shell sort?
  • Find the longest common substring from the given two strings.
  • Reverse a String in java without using any java inbuilt methods.
  • Write an algorithm to do a depth-first/breadth-first search in a graph.
  • Output the number of leaf nodes of a binary tree.

Things to remember:

· Confusing Questions:

You will find some questions that would not make sense like “how to use Pointers in Java class?” or “how to change the size on an array after declaration?”. Being a Java developer, you should know that there are no pointers in Java. Such questions are just asked to test the concepts of the candidate and to some extend confuse them. You should be confident and have a firm understanding of all the concepts to answer such questions.

· Give a suitable example:

In certain cases, you can be asked to explain your answer with an example. Be prepared with some clear examples in your mind to explain your answer.

· Examine the given code

You can also be given a piece of code and you will be asked to answer the questions like,

  • Find the error in the code.
  • What will be the output of this code?
  • How can you make it more efficient?
  • You can also be given some data and their expected outcome and based on that you will have to find the error in the code.


These were the top 50 Java coding interview questions that you must be prepared for. The articles also included some noteworthy tips to successfully pass a  Java coding interview. These common questions included various points that usually get skipped while learning Java development but these are some very important points that must be known to a Java coder to be considered a top Java developer.

new Java jobs



Candidate signup

Create a free profile and find your next great opportunity.


Employer signup

Sign up and find a perfect match for your team.


How it works

Xperti vets skilled professionals with its unique talent-matching process.


Join our community

Connect and engage with technology enthusiasts.