In This post will cover all the popular Core Java interview questions for freshers and experienced candidates in depth. Go through all the questions to enhance your chances of performing well in the interviews. The questions will revolve around the basic and core fundamentals of Java.
Java was developed as an open-source and platform-independent programming language by Sun Microsystems in the year of 1995.
For Downloading this OOP'S interview questions as a PDF please Click
1. What are the key differences between C++ and Java?
C++ |
Java |
1) C++ is platform dependent. |
1) Java is platform-independent. |
2) C++ writes structural programs without
using classes and objects. |
2) Java is a pure object-oriented language except
for the primitive variables. |
3) C++ doesn’t support documentation comments. |
3) Java supports documentation comment to
create documentation for java code. |
4) C++ fully supports pointers. |
4) In Java, there is no concept of pointers. |
5) C++ supports multiple inheritance. |
5) Java doesn’t support multiple inheritance. |
2. List the features of the Java Programming language?
A few of the significant features of Java Programming Language are:
- Easy: Java is a language that is considered easy to learn. One fundamental concept of OOP Java has a catch to understand.
- Secured Feature: Java has a secured feature that helps develop a virus-free and tamper-free system for the users.
- OOP: OOP stands for Object-Oriented Programming language. OOP signifies that, in Java, everything is considered an object.
- Independent Platform: Java is not compiled into a platform-specific machine; instead, it is compiled into platform-independent bytecode. This code is interpreted by the Virtual Machine on which the platform runs.
3. Why is Java a platform independent language?
Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.
The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it.
4. Why is Java not a pure object oriented language?
Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object oriented language.
5. Difference Between JDK And JRE ?
JDK |
JRE |
Abbreviation for Java Development Kit |
Abbreviation for Java Runtime Environment |
JDK is a dedicated kit for solely
software development |
JRE is a set of software and library designed for executing Java Programs |
Unlike JVM, JDK is Platform Dependent |
Unlike JVM, JRE is also Platform Dependent |
JDK package is a set of tools for debugging and Developing |
JRE Package is one that only supports files and libraries for a
runtime environment |
JDK package will be provided with an installer file |
JRE Package does not get an installer but has only a runtime
environment |
6. Which class is a superclass of all classes?
7. What are finally and finalize in Java?
8. What do you understand by an instance variable and a local variable?
9. Can you tell the difference between equals() method and equality operator (==) in Java?
equals() |
== |
This is a method
defined in the Object class. |
It is a binary
operator in Java. |
This
method is used for checking the equality of contents between two objects as
per the specified business logic. |
This operator is
used for comparing addresses (or references), i.e checks if both the objects
are pointing to the same memory location. |
11. What is JDBC?
12. What is Spring?
13. What is the relationship between a class and an object?
14. Can == be used on enum?
15. What are the Memory Allocations available in Java ?
- Class Memory
- Heap Memory
- Stack Memory
- Program Counter-Memory
- Native Method Stack Memory
16. What is break and continue statement?
17. What is Type casting in Java?
18. Define Copy Constructor in Java ?
19. What are the differences between Heap and Stack Memory in Java?
20. Why is Java not completely object-oriented?
21. What is Object Cloning?
22. Define Wrapper Classes in Java?
23. What is an object-oriented paradigm?
24. Explain the use of final keyword in variable, method and class ?
25. Define Singleton Classes in Java?
26. What happens when an exception is thrown by the main method?
27. Do final, finally and finalize keywords have the same function?
28. Can the static methods be overloaded?
29. Can the static methods be overridden?
- No! Declaration of static methods having the same signature can be done in the subclass but run time polymorphism can not take place in such cases.
- Overriding or dynamic polymorphism occurs during the runtime, but the static methods are loaded and looked up at the compile time statically. Hence, these methods cant be overridden.
30. How would you differentiate between a String, StringBuffer, and a StringBuilder?
31. What part of memory - Stack or Heap - is cleaned in garbage collection process?
32. What is the main objective of garbage collection?
33. Using relevant properties highlight the differences between interfaces and abstract classes ?
- Availability of methods: Only abstract methods are available in interfaces, whereas non-abstract methods can be present along with abstract methods in abstract classes.
- Variable types: Static and final variables can only be declared in the case of interfaces, whereas abstract classes can also have non-static and non-final variables.
- Inheritance: Multiple inheritances are facilitated by interfaces, whereas abstract classes do not promote multiple inheritances.
- Data member accessibility: By default, the class data members of interfaces are of the public- type. Conversely, the class members for an abstract class can be protected or private also.
- Implementation: With the help of an abstract class, the implementation of an interface is easily possible. However, the converse is not true;
34. Define package in Java ?
35. Differentiate between instance and local variables ?
36. Can you implement pointers in a Java Program ?
37. What is an Exception ?
38. Explain Java String Pool ?
39. What is the final keyword in Java ?
40. What makes a HashSet different from a TreeSet ?
- Implementation: For a HashSet, the hash table is utilized for storing the elements in an unordered manner. However, TreeSet makes use of the red-black tree to store the elements in a sorted manner.
- Complexity/ Performance: For adding, retrieving, and deleting elements, the time amortized complexity is O(1) for a HashSet. The time complexity for performing the same operations is a bit higher for TreeSet and is equal to O(log n). Overall, the performance of HashSet is faster in comparison to TreeSet.
- Methods: hashCode() and equals() are the methods utilized by HashSet for making comparisons between the objects. Conversely, compareTo() and compare() methods are utilized by TreeSet to facilitate object comparisons.
- Objects type: Heterogeneous and null objects can be stored with the help of HashSet. In the case of a TreeSet, runtime exception occurs while inserting heterogeneous objects or null objects.
41. What are the differences between JVM, JRE and JDK in Java?
Criteria |
JDK |
JRE |
JVM |
Abbreviation |
Java Development Kit |
Java Runtime
Environment |
Java Virtual
Machine |
Definition |
JDK is a complete
software development kit for developing Java applications. It comprises JRE,
JavaDoc, compiler, debuggers, etc. |
JRE is a software
package providing Java class libraries, JVM and all the required components
to run the Java applications. |
JVM is a
platform-dependent, abstract machine comprising of 3 specifications -
document describing the JVM implementation requirements, computer program
meeting the JVM requirements and instance object for executing the Java byte
code and provide the runtime environment for execution |
Tools provided |
JDK provides tools
like compiler, debuggers, etc for code development |
JRE provides
libraries and classes required by JVM to run the program. |
JVM does not
include any tools, but instead, it provides the specification for
implementation. |
Main Purpose |
JDK is mainly used
for code development and execution. |
JRE is mainly used
for environment creation to execute the code. |
JVM provides
specifications for all the implementations to JRE. |
42. What are the differences between HashMap and HashTable in Java?
HashMap |
HashTable |
HashMap is not
synchronized thereby making it better for non-threaded applications. |
HashTable is
synchronized and hence it is suitable for threaded applications. |
Supports order of
insertion by making use of its subclass LinkedHashMap. |
Order of insertion
is not guaranteed in HashTable. |
Allows only one
null key but any number of null in the values. |
This does not allow
null in both keys or values. |
43. What are the differences between constructor and method of a class in Java?
Constructor |
Method |
Constructor is used
for initializing the object state. |
Method is used for
exposing the object's behavior. |
Constructor gets
invoked implicitly. |
Method has to be
invoked on the object explicitly. |
Constructor has no
return type. |
Method should have
a return type. Even if it does not return anything, return type is void. |
If the constructor
is not defined, then a default constructor is provided by the java compiler. |
If a method is not
defined, then the compiler does not provide it. |
The constructor
name should be equal to the class name. |
The name of the
method can have any name or have a class name too. |
44. Can you call a constructor of a class inside the another constructor?
Yes, the concept can be termed as constructor chaining and can be achieved using this().
45. Can you explain the Java thread lifecycle?
- New :- When the instance of the thread is created and the start() method has not been invoked, the thread is considered to be alive and hence in the NEW state.
- Runnable :- Once the start() method is invoked, before the run() method is called by JVM, the thread is said to be in RUNNABLE (ready to run) state. This state can also be entered from the Waiting or Sleeping state of the thread.
- Running :- When the run() method has been invoked and the thread starts its execution, the thread is said to be in a RUNNING state.
- Non-Runnable (Blocked/Waiting) :- When the thread is not able to run despite the fact of its aliveness, the thread is said to be in a NON-RUNNABLE state. Ideally, after some time of its aliveness, the thread should go to a runnable state.
- A thread is said to be in a Blocked state if it wants to enter synchronized code but it is unable to as another thread is operating in that synchronized block on the same object. The first thread has to wait until the other thread exits the synchronized block.
- A thread is said to be in a Waiting state if it is waiting for the signal to execute from another thread, i.e it waits for work until the signal is received.
- Terminated :- Once the run() method execution is completed, the thread is said to enter the TERMINATED step and is considered to not be alive.
46.What is the difference between JDK, JRE, and JVM?
47. Explain Access Specifiers and Types of Access Specifiers?
- Public Access Specifier
- Private Access Specifier
- Protected Access Specifier
- Default Access Specifier
48. How many types of constructors are used in Java?
- Parameterized Constructors:
- Default constructors:
- Parameterized Constructors:
49. Explain ‘super’ keyword in Java?
50. Can a constructor return a value?
51. Explain Method Overloading in Java.
- Varying the number of arguments.
- Changing the return type of the Method .
52. Define Late Binding ?
53. Explain ‘this’ keyword in Java ?
54. Can we overload a static method?
55. Give a briefing on the life cycle of a thread ?
- New Born State
- Runnable State
- Running State
- Blocked State
- Dead State
56. Define Dynamic Method Dispatch ?
57. Explain the difference between >> and >>> operators ?
- >> operator does the job of right shifting the sign bits
- >>> operator is used in shifting out the zero-filled bits