As a Fresher, you have either just attended an interview or planning to attend one soon. An Entry Level jobseeker looking to grow your career in software programming, you may be nervous about your upcoming interviews. All of us have those moments of panic where we blank out and might even forget what a thread is. We will simplify it for you, all you need to do it take a deep breath and check the questions that are most likely to be asked.
You can’t avoid panicking, but you can definitely prepare yourself so that when you step in that interview room. You are confident and know you can handle anything the interviewer might throw at you.
Here is a compiled a list of comprehensive 21 Java Interview Questions with Answers (latest 2019) that will help you nail that confidence, and ensure you sail through the interview.
1. What all does JVM comprise of?
JVM, short for Java Virtual Machine is required by any system to run Java programs. Its architecture essentially comprises of:
● Classloader: It is a subsystem of JVM and its main function is to load class files whenever a Java program is run.
● Heap: it is the runtime data that is used for allocating objects.
● Class area: it holds the class level of each class file such as static variables, metadata, and constant run pool.
● Stack: used for storing temporary variables.
● Register: the register contains the address of the JVM instruction currently being executed
● Execution engine: the EE consists of a virtual processor, an interpreter that executes instructions after reading the bytecode, and a JIT compiler which improves performance when the rate of execution is slow.
● Java Native Interface: it acts as the communication medium for interacting with another application developed in C, C++, etc.
2. What is object-oriented programming? Is Java an object-oriented language?
Essentially, object-oriented programming is a programming paradigm that works on the concept of objects. Simply put, objects are containers – they contain data in the form of fields and code in the form of procedures. Following that logic, an object-oriented language is a language that works on objects and procedure.
Since Java utilizes 8 primitive datatypes — boolean, byte, char, int, float, long, short, double — which are not objects, Java cannot be considered a 100% object-oriented language.
3. What do you understand by Aggregation in context of Java?
Aggregation is a form of association in which each object is assigned its own lifecycle. But, there is ownership in this, and the child object cannot belong to any other parent object in any manner.
4. Name the superclass in Java.
Java.lang. All different non-primitive are inherited directly or indirectly from this class.
5. Explain the difference between ‘finally’ and ‘finalize’ in Java?
Used with the try-catch block, the ‘finally’ block is used to ensure that a particular piece of code is always executed, even if the execution is thrown by the try-catch block.
In contrast, finalize() is a special method in the object class. It is generally overridden to release system resources when garbage value is collected from the object.
6. What is an anonymous inner class? How is it different from an inner class?
Any local inner class which has no name is known as an anonymous inner class. Since it doesn’t have any name, it is impossible to create its constructor. It always either extends a class or implements an interface, and is defined and instantiated in a single statement.
A non-static nested class is called an inner class. Inner classes are associated with the objects of the class and they can access all methods and variables of the outer class.
7. What is a system class?
It is a core class in Java. Since the class is final, we cannot override its behavior through inheritance. Neither can we instantiate this class since it doesn’t provide any public constructors. Hence, all of its methods are static.
8. How to create daemon thread in Java?
We use the class setDaemon(true) to create this thread. We call this method before the start() method, else we get IllegalThreadStateException.
9. Does Java support global variables? Why/Why not?
No, Java doesn’t support global variables. This is primarily because of two reasons:
● They create collisions in the namespace.
● They break the referential transparency.
10. How is an RMI object developed?
The following steps can be taken to develop an RMI object:
● Define the interface
● Implement the interface
● Compile the interface and their implementations with the java compiler
● Compile server implementation with RMI compiler
● Run RMI registry
● Run application
11. Explain the differences between time slicing and preemptive scheduling?
In case of time slicing, a task executes for a specified time frame – also known as a slice. After that, it enters the ready queue — a pool of ‘ready’ tasks. The scheduler then picks the next task to be executed based on the priority and other factors.
Whereas under preemptive scheduling, the task with the highest priority is executed either until it enters dead or warning states or if another higher priority task comes along.
12. Garbage collector thread is what kind of a thread?
It is a daemon thread.
13. What is the lifecycle of a thread in Java?
Any thread in Java goes through the following stages in its lifecycle:
● Non-runnable (blocked)
14. State the methods used during deserialization and serialization process.
Reads the file and deserializes the object.
Serialize the object and write the serialized object to a file.
15. What are volatile variables and what is their purpose?
Volatile variables are variables that always read from the main memory, and not from thread’s cache memory. These are generally used during synchronization.
16. What are wrapper classes in Java?
All primitive data types in Java have a class associated with them – known as wrapper classes. They’re known as wrapper classes because they ‘wrap’ the primitive data type into an object for the class. In short, they convert Java primitives into objects.
17. How can we make a singleton class?
By making its constructor private.
18. What are the important methods of Exception Class in Java?
● string getMessage()
● string toString()
● void printStackTrace()
● synchronized Throwable getCause()
● public StackTraceElement getStackTrace()
19. How can we make a thread in Java?
We can follow either of the two ways to make a thread in Java:
● By extending Thread Class
The disadvantage of this method is that we cannot extend any other classes since the thread class has already been extended.
● By implementing Runnable interface
20. Explain the differences between get() and load() methods.
The get() and load() methods have the following differences:
● get() returns null if the object is not found, whereas load() throws the ObjectNotFound exception.
● get() always returns a real object, whereas load() returns a proxy object.
● get() method always hits the database whereas load() doesn’t.
● get() should be used if you aren’t sure about the existence of an instance, whereas load() should be used if you are sure that the instance exists.
21. What is the default value of the local variables?
They aren’t initialized to any default value. Neither are primitives or object references.
The above Java interview questions will provide a good start for preparing for the interview. Practice your coding skills, too, though, and make sure to be thorough in these questions and their related concepts so that when the interviewer fires a Q, you are ready to win the round with your A. Oh, and don’t forget 3 (inconspicuous) breaths when you present yourself before the interviewer.
For further reference, you can also download the PDF version of Java Interview Questions and Answers mentioned below.
All the best!