Java is a platform-independent programming language. Sun Microsystems introduced Java. It is a high-level programming language that follows Object Oriented Programming concept.
The following tutorial has compiled the most asked 50+ Java interview questions, which are popular in 2021. It will help beginners, and experienced developers to prepare for the interviewer’s questions during the interview.
The tutorial lists commonly asked Java questions in an interview with detailed answers to learn the concepts quickly. You can learn detailed concepts of Java programming language at codeleaks.io.
The following questions are designed in order to make you understand the nature of questions in the job interview of Java Programming Language. You will ace a Java interview quite easily if you go through all these questions.
Q.01: How will you define Java Programming Language?
Ans: Java was developed by Sun Microsystems in 1995. It is high level and independent programming language. Java is Objected Oriented Programming Language.
Q.02: List the OOP concepts with the basic knowledge.
Q.03: How can we say Java is a Platform Independent Language?
Ans: Java is a platform-independent language because it runs on every system regardless of the operating system. It works on a variety of platforms, be it Windows/ Linux/ Mac OS, etc.
Q.04: What do we call a top-level class? Private or Protected?
Ans: Java Top-level classes are neither private nor protected because if they are personal, nobody will be able to use, them and it is senseless to make them a protected class. Only the Inner class is either saved or private.
Q.05: Define Java Scanner class briefly.
Ans: Java Scanner class works as a user input collector. The scanner class does not need any libraries to work as it is a part of Java.util package.
Q.06: How would you differentiate between Inner Class and SubClass?
Ans: When one class is nested inside another class, it is called an Inner Class in Java. Inner Class can access all the methods and variables in the outer class.
Whereas a class that is inherited from another class is known as SubClass. It can access the Super class’s public methods as well as protected methods.
Q.07: Define Wrapper class?
Ans: The classes which allow us to access primitive data types as objects are known as the Wrapper class. Primitive data types in Java are Integer, double, Boolean, character, etc.
Q.08: What is the Java ‘this’ keyword?
Ans: ‘This’ keyword refers to the current object of the method or a constructor that is being called. It is used for the reference of the current class instance variable.
Q.09: What is Java’s Final Keyword?
Ans: We can limit method, class, and variable usage in Java using Final Keyword.
For example, a Final Method cannot be overridden, and a Final class cannot be inherited, etc.
Q.10: What method is used to create random numbers in Java?
Ans: We can generate random numbers using the Math.random( ) method in the specified range.
Also, we can use the Random class in Java.util package.
Q.11: Can you name any typical IDE’s used for Java?
Ans: Java IDE’s are: NetBeans & Eclipse; there are many more. It depends upon the developer’s preferences.
Q.12: Differentiate Java double and float variables?
Ans: Double and Float both are floating-point variables, but double provides more precision than float variables. Also, float occupy 4 bytes in memory, whereas double occupies 8 bytes in memory.
Q.13: Name the three loops used in Java.
Ans: We know loops are used to execute a code block repeatedly.
- For Loops: We can execute a code block repeatedly for the specified number of times.
- While Loops: A code block is executed repeatedly till the specified condition is fulfilled.
- Do while Loops: It is just like a while loop, but in the do-while loop, the set of statements will be executed anyhow for once, no matter condition is true or false.
Q.14: How will you define Infinite Loop?
Ans: As the name suggests, the infinite loop runs infinitely. It means there is no condition for an infinite loop. You have to define one code-breaking logic to stop the loop.
Q.15: Explain the ternary operator briefly.
Ans: Ternary operator uses Boolean results, i.e., true/false, to decide what value could be assigned to a variable. It is also known as the conditional operator.
Q.16: Define Singleton class.
Ans: Java class having only one instance is known as Singleton class. The methods and variables in the singleton class contain only one instance.
Q.17: Name the Java base class which is used to derive all other classes.
Ans: The java base class is “java.lang.object”, where all java classes can be derived.
Q.18: Define switch-case statements.
Ans: When there is no match in any switch conditions, the default code will be executed with Switch Case. The switch case default case is optional. You can declare your switch cases.
Q.19: Can you define Abstract Class briefly?
Ans: When one method is initiated but there is no implementation, it is known as Abstract Method. The classes which are consist of one or more abstract forms, these classes are called Abstract classes.
Q.20: Explain Java ‘super’ keyword?
Ans: Super Keyword is also known as a reference variable. Parent class objects are referred to by super keyword in Java. It is used to refer to the parent class method, constructor, and instance variable.
Q.21: If it is possible to create an Abstract class without an abstract method?
Ans: An Abstract class does not require an abstract method to get initiated. That’s why we can create an abstract class with an abstract keyword before the class name, even with no abstract method. But it is not the same case when we want an abstract method in a class; that class must be declared abstract.
Q.22: Is it allowed to overload/ override the Java static method?
Ans: We can overload Java static methods, but there should be differences in the input parameters provided.
As far as Overriding is concerned, there is no polymorphism at run-time, i.e., it got created with the same subclass signature. Therefore, we cannot override static methods in Java.
Q.23: Is there any performance difference between interface and abstract class.?
Ans: Interface works slower than Abstract class as it contains indirections. Also, interface use causes a burden as it requires the execution of every interface method in the class.
Q.24: How do you explain object cloning?
Ans: To create an exact copy of any object is known as Object Cloning. You have to implement Java.lang if you require object cloning in class.
The syntax is
protected Object clone() throws CloneNotSupportedException
Q.25: Name the base class used in Java for Exception?
Ans: All classes of exceptions are derived from Java.Lang.Throwable. It is the superclass for all the exceptions in Java.
Q.26: Is there any difference between equals and == operator?
Ans: When we want to compare string objects’ content, we use the equals( ) method between two strings.
Whereas == operator is used for comparison of two string objects references.
Q.27: Differentiate between HashMap and Hashtable in Java?
Ans: When we talk about thread synchronization, we have HashMap and HashTable in Java. HashMap is not used because it is not synchronized and allows one null key. Whereas HashTable is used in thread synchronization as it is synchronized and does not allow any null value.
Q.28: Why can we not use multiple inheritances in Java?
Ans: Java is an Object-Oriented Programming Language. To keep the simplicity of OOP in Java, it does not allow multiple inheritances in classes, but you can implement various inheritances through Java interfaces.
Q.29. Can you differentiate between throws and throws in Exception Handling of Java?
Ans: We use the throw keyword to throw an Exception. But Throws keyword points out what Exception could be thrown by a specific method.
You can see the example below,
Throw new Exception (“File is not valid!”)
Throws ArithmeticException, RemoteException
Q.30: In Java Exception Handling, is it necessary for Catch block to follow Try block?
Ans: In Java Exception handling, we need to catch the error in catch block, so Try block must be followed by a catch block or finally block when there are statements that should be executed before exiting.
Q.31: What do you know about the OOP Inheritance concept?
Ans: In inheritance, we can use codes from one class into another class. Simply, one class can extend the other class.
We cannot inherit private members of the class; only public and protected members can be inherited.
Let’s study syntax,
class Subclass extends superclass
Q.32: Can you define Encapsulation briefly?
- Encapsulation is used to maintain the code. In Encapsulation, we wrap the variables and methods in the code together.
- Encapsulations require the getters and setters for the instance variables that are private.
- Variables of one class are hidden from the other class. We can access the variable via methods of the current class.
Q.33: What is the main difference between set and list interface?
Ans: In the Java Collection interface, we have set and List as child interfaces.
When you enter the data in the List, the order is preserved, but it is not maintained in the set interface.
Also, the set cannot be used to keep duplicate values, whereas List allows saving.
Q.34: Explain the interface briefly.
Ans: Interface is introduced to overcome the issues of Multi-Inheritance as Java doesn’t allow multiple inheritances. We declare methods in the interface, and there is no implementation for these methods. Classes provide methods declarations.
Q.35: What do you know about collections in Java?
Ans: A Collection in Java is used as storage for objects. The collection is a framework in Java that is used for design manipulation as well.
We do the following operations in JAVA collections.
Insertion-Deletion-Sorting- Searching- Manipulation
Q.36: How do you differentiate between Public and Private access specifiers?
Ans: As we know that, members are methods and instance variables in Java.
Public access specifiers are accessible to the same class as well as the outside class.
Whereas Private access specifiers are accessible in the same package only and not in the outside class. It remains invisible.
Q.37: List down all the interfaces, along with the available classes in Java
- Tree Set
- Hash Set
- Linked Hash set
- Hash Table
- Hash Map
- Priority Queue
- Linked List
- Array List
- Sorted Map
- Sorted Set
Q.38: What is Priority Queue?
Ans: Queue is an interface that can be handled with the help of a linked list. There lies a relative priority in the elements stored. The elements are usually ordered initially.
Q.39: Can you define Java thread?
Ans: The flow of operations is known as Thread in Java. JVM creates at least one main thread in each of the Java programs. Java executes threads concurrently. One can create own threads in their Java program by extending the Thread class.
Q.40: Name the states a Thread goes through in its life cycle.
Ans: Thread’s states in the Life cycle are:
- Terminated (Dead)
Q.41: Differentiate between Thread class and Runnable interface?
Ans: When one class extends another class and doesn’t extend a thread, we can work with a runnable interface.
On the other hand, if no class is extended, you can easily extend the thread class as Java allows only one class to be extended.
Q.42: What happens if you use the yield() method in the Thread class?
Ans: Using the yield( ) method in the Thread class makes other threads executable by moving the current running threads in a runnable state. This method does not make any thread sleep or block state.
Q.43: What does notify( ) and notifyall( ) methods do?
Ans: If one thread is waiting, we will be using notify() method to wake up that thread.
If we need to wake up all the threads in the waiting state, notifyall() will be used.
Q.44: Briefly define Serialization in Java.
Ans: When converting a file into a stream of bytes occurs, we call it serialization. The serialization is done for security purposes. Java.io.serializable will be implemented, and no other method needs to be initialized.
Q.45: Why transient variable is used?
Ans: In the process of serialization and deserialization, we use a transient variable. It does not go with serialization. The transient variables are default in deserialization as they cannot be used with static variables.
Q.46: Briefly define synchronization.
Ans: In synchronization, we access a single block of code through one thread at a time. On the other hand, if many threads will be accessing the code block at one time, it might become a conflict in output results. To avoid this problem, we need to use synchronization for the block of codes.
Q.47: Write two main methods used in serialization and deserialization?
Ans: ObjectOutputStream.writeObject and ObjectOutputStream.readObject are two methods used for serialization and deserialization respectively.
Q.48: Why do we use volatile variables?
Ans: Volatile variables are used in the process of synchronization. These variables come from the main memory and not from the thread cache’s memory.
Q.49: Why main() method is declared void?
Ans: Java does not allow the main method to return any type of data. That’s why the main method is always initiated with return type void.
Q.50: Explain the importance of the Java Packages.
Ans: Java packages consist of classes and interfaces. Packages allow us to have a divide and conquer approach. In simple words, we can use modularization of code, and we can re-use the code efficiently.
Q.51: Is there any way to pass the argument without using pass-by-value?
Ans: No, there is no way to pass the function argument in Java except for value. It cannot be done by using reference.
Q.52: In Java, how to invoke the constructor of the class?
Ans: Whenever you initiate an object with a New keyword, every time it invokes the constructor.
Q.53: Differentiate between Stack and Queue.
Ans: Stack and Queue are part of data structures in Java. Simple, Stacks work based on LIFO (Last In First Out) structure whereas Queue works as FIFO (First In First Out) design.
Q.54: Name the compulsory environment variables to be used for Java?
Ans: First, one has to set two environment variables properly to execute Java programs in their systems.
Q.55: how can you define a string in Java?
Ans: In Java, String is not a data type but an object of Java.Lang.String class. We can use the built-in methods of this class on all of the string objects.
Q.56: Why do we call strings Immutable in Java?
Ans: In Java, the assigned value of a string cannot be changed. In case if the value of the string is changed, another new object is created. That’s why Strings are known as immutable in Java.
Q.57: Define the concept of pointers in Java.
Ans: In Java, there are no pointers. Hence there is no concept of pointers in Java class.
Q.58: Can we increase the size of an Array even after declaration?
Ans: As we know, Array is a static structure and does not allow any changes after being declared once. If it is needed to change the size afterward, one should use vectors.
Q.59: What do you know about Vector?
Ans: Vector is a type of Java Array that is dynamic. As it is a dynamic array, you can change the size of the vector array even after the declaration. Vector has no limit for size.
Q.60: Explain garbage collection in Java briefly?
Ans: When we stop referencing an object in Java, it gets destroyed in the process of garbage collection. Java uses two methods to start garbage collection automatically.
So, this was all about 50+ Java-based tricky questions and answers that an interviewer might ask you. We have put our best efforts in gathering these questions and answers. Now, we hope you all are well-versed with Java questions. In case, you face any issue, please contact our Codersera team of developers. We would love to serve you with our services. Good luck with your interview.