Top 50 Java Interview Question and Answers pdf Download: In Java, the primary advantage is that it enables developers to write code that runs everywhere (WORM); meaning there isn’t a need for recompilation. Regardless of an architecture of a system, Java compiled code can be run on any JVM (Java Virtual Machine), making it one of the most widely used computer programming language. It is class based and object-oriented.
1. Define Class in Java.
In Java, a class is a template used to create objects and define the data type. It acts as a building block for Java language-oriented systems.
2. What Do JDK, JRE, and JVM Stand for?
• JVM stands for Java Virtual Machine
• JRE stands for Java Runtime Environment
• JDK stands for Java Development Kit
You might like : Top 20 MongoDB Interview Questions and Answers
3. What Is the Difference Between Static and Dynamic Loading?
Static class loading involves the creation of objects and instances using new keywords, and dynamic class loading is done when the name of the class is not known at compile time.
4. Does Java Use Pointers?
No. Java doesn’t use pointers. It has a tough security. Instead of pointers, references are used in Java as they are safer and more secure when compared to a pointer.
5. Explain public static void main(String args).
- public : Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
- static : It is a keyword in java which identifies it is class based i.e it can be accessed without creating the instance of a Class.
- void : It is the return type of the method. Void defines the method which will not return any value.
- main: It is the name of the methodwhich is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
- String args : It is the parameter passed to the main method.
Q6. What are constructors in Java?
In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.
There are two types of constructors:
- Default constructor
- Parameterized constructor
Q7. What is singleton class and how can we make a class singleton?
Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.
Q8. What is the difference between Array list and vector?
|Array List is not synchronized.||Vector is synchronized.|
|Array List is fast as it’s non-synchronized.||Vector is slow as it is thread safe.|
|If an element is inserted into the Array List, it increases its Array size by 50%.||Vector defaults to doubling size of its array.|
|Array List does not define the increment size.||Vector defines the increment size.|
|Array List can only use Iterator for traversing an Array List.||Except Hashtable, Vector is the only other class which uses both Enumeration and Iterator.|
Q9. What is the difference between equals() and == ?
Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.
“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.Top 50 Java Interview Question and Answers pdf Download
Q10. What are the differences between Heap and Stack Memory?
The major difference between Heap and Stack memory are:
|Memory||Stack memory is used only by one thread of execution.||Heap memory is used by all the parts of the application.|
|Access||Stack memory can’t be accessed by other threads.||Objects stored in the heap are globally accessible.|
|Memory Management||Follows LIFO manner to free memory.||Memory management is based on generation associated to each object.|
|Lifetime||Exists until the end of execution of the thread.||Heap memory lives from the start till the end of application execution.|
|Usage||Stack memory only contains local primitive and reference variables to objects in heap space.||Whenever an object is created, it’s always stored in the Heap space.|
11. What Is a Package?
A package is a collection of related classes and interfaces.
12. What’s the Base Class of all Exception Classes?
Java.lang.Throwable is the superclass of all exception classes, and all exception classes are derived from this base class.
13. What Is the Difference Between
= = ?
Equals() method is used for checking the equality of two objects defined by business logic.
== or the equality operator is used to compare primitives and objects.
14. State Two Differences Between an Inner Class and a Subclass
While Inner classes are in the same file, subclasses can be in another file. With that in mind, while subclasses have the methods of their parent class, inner classes get the methods they want.
15. How Are Destructors Defined in Java?
Since Java has its own garbage collection, no destructors are required to be defined. Destruction of objects is automatically carried by the garbage collection mechanism.
16. Define JSON.
17. Name the Most Important Feature of Java
Java is a platform independent language.
18. What Is an Anonymous Class?
The class defined without a name in a single line of code using new keyword is known as an anonymous class.
19. What Is a JVM?
JVM is the Java Virtual Machine, which is a runtime environment for compiled Java class files.
20. Can a Dead Thread Be Started Again?
No, a thread that is in the dead state can’t be started again.
21. What is runtime polymorphism or dynamic method dispatch?
In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. Let’s take a look at the example below to understand it better.
22. What is the difference between abstract classes and interfaces?
|An abstract class can provide complete, default code and/or just the details that have to be overridden.||An interface cannot provide any code at all,just the signature.|
|In case of abstract class, a class may extend only one abstract class.||A Class may implement several interfaces.|
|An abstract class can have non-abstract methods.||All methods of an Interface are abstract.|
|An abstract class can have instance variables.||An Interface cannot have instance variables|
|An abstract class can have any visibility: public, private, protected.||An Interface visibility must be public (or) none.|
|If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly||If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method|
|An abstract class can contain constructors||An Interface cannot contain constructors|
|Abstract classes are fast||Interfaces are slow as it requires extra indirection to find corresponding method in the actual class|
23. What is method overloading and method overriding?
Method Overloading :
- In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order.
- Method Overloading is to “add” or “extend” more to method’s behavior.
- It is a compile time polymorphism.
- The methods must have different signature.
- It may or may not need inheritance in Method Overloading.
Let’s take a look at the example below to understand it better.
- In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.
- Method Overriding is to “Change” existing behavior of method.
- It is a run time polymorphism.
- The methods must have same signature.
- It always requires inheritance in Method Overriding.
Let’s take a look at the example below to understand it better.
24. Can you override a private or static method in Java?
You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it’s not accessible there. What you can do is create another private method with the same name in the child class. Let’s take a look at the example below to understand it better.
25. What is association?
Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationship can be one to one, One to many, many to one and many to many.
26. What do you mean by aggregation?
Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy.
27. What is composition in Java?
Composition is again specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.
28. Explain Garbage Collection in Java.
In Java, when an object is no longer used or referenced, garbage collection is called and the object is destroyed automatically.
29. What’s the Difference Between Stack and Queue?
The difference between a stack and a queue is that the stack is based on the Last in First out (LIFO) principle, and a queue is based on FIFO (First In, First Out) principle.
30. What are the differences between forward() method and sendRedirect() methods?
|Forward() method||SendRedirect() method|
|forward() sends the same request to another resource.||sendRedirect() method sends new request always because it uses the URL bar of the browser.|
|forward() method works at server side.||sendRedirect() method works at client side.|
|forward() method works within the server only.||sendRedirect() method works within and outside the server.|
Additional Interview Tips
Top 50 Java Interview Question and Answers pdf Download: Having discussed the phases of an interview and common interview questions, here we have listed some quick interview tips to help you ace the interview:
• Be punctual and show up on time
• Be ready to talk about your achievements — both personal and professional
• Keep a check on your language and avoid using fillers
• Go through the job description and find key requirements you can talk about
• Listen carefully and take time to gather your thoughts before answering
• Dress and behave professionally.
• Avoid interrupting the interviewer
• Carry all the essential documents
• Do a brief research on the company and understand their needs