Top 101 Java Interview Questions And Answers

Java Interview Questions

1. What do you understand by Java?

2. What is the difference between JDK and JRE?

3. What are the features of the Java Programming language?

  • Object-oriented: Java follows an object-oriented technique and employs a collection of objects from a class to perform any operation.
  • Secure: It is extremely secure because it does not produce any security problems such as stack overflow. This is due to the fact that it does not employ the concept of pointers.
  • Robust: Java is strong and dependable because it detects mistakes early on and avoids concerns like garbage collection, memory allocation, and exception handling.
  • Simple: Java is simple to learn and does not necessitate the use of advanced programming concepts such as pointers or multiple inheritance.
  • Platform-independent: Because the code we compile and execute on any operating system is the same, Java is platform-independent. This means that the code can be compiled on one system and run on another.
  • Multithreaded: Supports the multithreading idea, which means we can run two or more programmes at the same time, maximising CPU utilisation.
  • Great performance: Java makes advantage of a Just-in-Time compiler, which ensures high performance.
  • Dynamic: Java is more dynamic when we compare to other programming languages like C or C++. This is because it can carry a large amount of run time information which we can use to verify runtime object access.

4. What are the different types of memory areas allocated by JVM?

  • ClassLoader: It is a component of JVM used to load class files.
  • Class (Method) Area: It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods.
  • Heap: Heap has created a runtime and it contains the runtime data area in which objects are allocated.
  • Stack: Stack stores local variables, reference variable,s and partial results at runtime. It also helps in method invocation and return value. Each thread creates a private JVM stack at the time of thread creation.
  • Program Counter Register: This memory area contains the address of the Java virtual machine instruction that is currently being executed.
  • Native Method Stack: This area is reserved for all the native methods used in the application.

5. How Java platform is different from other platforms?

6. Can we write the main method as public void static instead of public static void?

7. What is core java?

8. What is an array in java?

9. What is the difference between byte and char data types in Java?

10. Why do we need a constructor in Java?

11. How many types of constructors does Java support?

  • Default or non-parametrized Constructors
  • Parameterized Constructors
  • Copy constructor

12. Why constructors cannot be final, static, or abstract in Java?

13. How to implement constructor chaining using the Java super keyword?

Output :

14. How aggregation and composition are different concepts?

Aggregation Code

Composition

15. What is an association?

16. If there are no pointers in Java, then why do we get NullPointerException?

17. What is object cloning in Java?

18. Is it possible to use this() and super() both in the same constructor?

19. What is the difference between this() and super() in Java?

20. What is the difference between break and continue statements?

21. What is a copy constructor in Java?

22. Is it possible to use a non-static variable in a static context?

23. Why do we mark main method as static in Java?

24. What is Inheritance?

25. What are the different types of inheritance in Java?

  • Single Inheritance: In single inheritance, one class inherits the properties of another, implying that there will only be one parent and one child class or derived class.
  • Multilevel Inheritance: This sort of inheritance occurs when a class is derived from another class that is itself derived from another class, i.e. when a class has more than one parent class but at different levels.
  • Hierarchical Inheritance: Hierarchical inheritance occurs when a class has many child classes (subclasses), or when more than one child class inherits from the same parent class.
  • Hybrid Inheritance: A hybrid inheritance is one that combines two or more inheritance types.

26. Why is multiple inheritance not supported in java?

27. How will you implement method overloading in Java?

  • By changing the number of parameters.
  • By changing the data type of parameter.

28. What is Polymorphism?

  • Compile Time Polymorphism: Method overloading is compile-time polymorphism
  • Run time polymorphism or Dynamic Polymorphism: Inheritance and interfaces are used to implement runtime time polymorphism.

29. Can we change the scope of the overridden methods in the subclass?

  • Protected, default, and public can all be changed from private.
  • Protected can be changed to default or public.
  • It is possible to alter a default modifier to public.
  • Only public can be declared with the public modifier.

30. What is the difference between method overloading and method overriding in Java?

31. What is encapsulation in Java?

  • Declaring a class’s variables as private; and
  • Providing public setter and getter methods to edit and inspect the variables’ values.

32. What is an abstraction in Java?

  • Classes that are abstract (0–100 percent of abstraction can be achieved)
  • Interfaces (it is possible to achieve 100% abstraction).

33. How to create singleton class in java?

34. Can you override a private or static method in Java?

35. Explain the difference between static and dynamic binding

36. What do you mean by an interface in Java?

37. Is it allowed to mark a method abstract as well as a final?

38. Is it allowed to mark an interface method as static?

39. What is the difference between abstract class and interface in Java?

40. What are the main uses of ‘this’ keyword?

  • This keyword can be used in the following ways.
  • This can be used to refer to the current instance variable of the current class.
  • This can be used to call the method of the current class (implicitly)
  • This() can be used to call the constructor of the current class.
  • In the method call, this can be given as an argument.
  • This can be provided to the constructor as an argument.
  • This can be used to get the current instance of the class from a method.

41. Can a class be marked final in java?

42. Is it allowed to declare main method as final?

43. What is the purpose of package in Java?

44. What is a static import in Java?

45. What is the purpose of serialization?

  • Communication: It is used to send data between two machines across a network.
  • Persistence: We can save the state of an object in a database and recover it later.
  • Caching: To boost performance, serialization can be utilized for caching. It could take us 10 minutes for object creation, but just 10 seconds to de-serialize it.
  • Cross JVM Synchronization: It can be utilized in the same way across multiple JVMs with various architectures.

46. What are the uses of Reflection in Java?

47. What is Garbage Collection in Java?

48. What are the different types of garbage collectors in Java?

  • Serial Garbage Collector
  • Parallel Garbage Collector
  • CMS Garbage Collector
  • G1 Garbage Collector

49. Why do we use finalize() method in Java?

50. What is the difference between a Inner class and sub class in Java?

51. What is Java String Pool?

52. Why a String object is considered immutable in java?

53. What is bufferreader in java?

54. What is the basic difference between a String and StringBuffer object?

55. What is the use of toString() method in java

56. Difference between String Buffer and StringBuilder

57. What is the difference between Error and Exception?

58. In Java, what are the differences between a Checked and Unchecked Exception?

  • Checked Exception: Checked exceptions are all classes that extend the Throwable class except RuntimeException and Error. Checked exceptions are checked during the compilation process. IOException, SQLException, and other exceptions are examples.
  • Unchecked Exception: Unchecked exceptions are the classes that extend RuntimeException. At compilation time, unchecked exceptions are not checked. ArithmeticException, NullPointerException, and other exceptions are examples.

59. What are the differences between throw and throws?

60. What is the use of finally block in Java?

61. What is the exception hierarchy in java?

62. Do we have to always put a catch block after a try block?

63. How Multi-threading works in Java?

64. What is a Thread? What are the two ways to create a thread?

65. What is a Thread’s priority and how it is used in scheduling?

66. Explain the thread lifecycle in Java?

  • New: After it has been generated, the thread is in a new state. Until you start the execution procedure, it will remain in the new state.
  • Running: When a thread is in the runnable state, it is ready to run at any time or has already started running.
  • Waiting: Another thread may be running in the system while the current thread is waiting to execute. As a result, the thread enters the waiting state.
  • Dead: When a thread’s execution is complete, its status is converted to dead, which indicates it is no longer considered active.

67. What is synchronization?

68. What is a collection class in Java? List down its methods and interfaces

  • Classes
  • Methods
  • Interfaces

69. What is the difference between Collection and Collections Framework in Java?

70. What are the differences between a List and Set collection in Java?

71. In Java, how will you decide when to use a List, Set or a Map collection

72. What is the difference between Arraylist and vector in Java?

73. What are the differences between Comparable and Comparator?

74. What are the differences between a HashMap and a Hashtable in Java?

75. What are the differences between a HashMap and a TreeMap?

76. What are Wrapper classes in Java?

77. What is the purpose of the native method in Java?

78. What is the difference between Shallow Copy and Deep Copy in Java?

79. What is the difference between Collection and Collections Framework in Java?

80. Why Map interface does not extend the Collection interface in the Java Collections Framework?

81. What is the difference between an Iterator and ListIterator in Java?

82. What is the difference between a Set and a Map in Java?

83. What is Hash Collision? How Java handles hash-collision in HashMap?

84. What is the difference between Queue and Stack data structures?

85. What is a classloader?

86. What is the covariant return type?

87. How to create an immutable class in java?

  • Make the class final to prevent it from being extended.
  • Make all fields private in order to prevent direct access.
  • Variables should not have any setter methods.
  • Make all changeable fields final, so that their value may only be changed once.
  • Create a constructor that does a deep copy and initialises all fields.
  • In the getter methods, clone objects to return a copy instead of the original object reference.

88. Can you have virtual functions in Java?

89. Mention the uses of the synchronized block

  • It aids in the locking of an object for each shared resource.
    The synchronized block has a smaller scope than the method.

90. Distinguish between static loading and dynamic class loading?

91. What are different scenarios causing “Exception in thread main”?

  • Exception in thread main java.lang.UnsupportedClassVersionError: This exception occurs when you try to run a java class that was compiled with a different JDK version.
  • Exception in thread main java.lang.NoClassDefFoundError: This exception comes in two flavors. The first is where you give the whole name of the class, including the.class extension. When Class is not identified, the second situation occurs.
  • Exception in thread main java.lang.NoSuchMethodError: main: When you try to launch a class that doesn’t have a main function, you’ll get this exception.
  • Exception in thread “main” java.lang.ArithmeticException: When an exception is thrown from the main method, the exception is sent to the console. The first section specifies that the main method throws an exception, the second part publishes the exception class name, and the third part displays the exception message after a colon.

92. Explain the Externalizable interface.

93. Can we execute any code, even before the main method? Explain.

94. What is the difference between transient and volatile variables in Java?

95. What is a functional interface in java?

96. What are the uses of the Java super keyword?

  • Access the parent class variable
  • To invoke the parent class method
  • To invoke the parent class constructors with and without argument.

97. What is method overloading with type promotion?

98. What is Java instanceOf operator?

99. What is the use of System class and Runtime class?

100. What is the difference between abstraction and encapsulation?

101. What are the different types of access specifiers in Java?

  • public: Any class in any package can see it. For a class, variable, or method, we can declare a public access type.
  • protected: It can be accessed from any class in the same package or from the subclass of the declared class. We can gain access to the outside world through inheritance.
  • default: The scope is contained within the package and does not require the use of any keywords.
  • private: It can only be accessible by members of the same class.

86. What is the covariant return type?

87. How to create an immutable class in java?

  • Make the class final to prevent it from being extended.
  • Make all fields private in order to prevent direct access.
  • Variables should not have any setter methods.
  • Make all changeable fields final, so that their value may only be changed once.
  • Create a constructor that does a deep copy and initialises all fields.
  • In the getter methods, clone objects to return a copy instead of the original object reference.

88. Can you have virtual functions in Java?

89. Mention the uses of the synchronized block

  • It aids in the locking of an object for each shared resource.
    The synchronized block has a smaller scope than the method.

90. Distinguish between static loading and dynamic class loading?

91. What are different scenarios causing “Exception in thread main”?

  • Exception in thread main java.lang.UnsupportedClassVersionError: This exception occurs when you try to run a java class that was compiled with a different JDK version.
  • Exception in thread main java.lang.NoClassDefFoundError: This exception comes in two flavors. The first is where you give the whole name of the class, including the.class extension. When Class is not identified, the second situation occurs.
  • Exception in thread main java.lang.NoSuchMethodError: main: When you try to launch a class that doesn’t have a main function, you’ll get this exception.
  • Exception in thread “main” java.lang.ArithmeticException: When an exception is thrown from the main method, the exception is sent to the console. The first section specifies that the main method throws an exception, the second part publishes the exception class name, and the third part displays the exception message after a colon.

92. Explain the Externalizable interface.

93. Can we execute any code, even before the main method? Explain.

94. What is the difference between transient and volatile variables in Java?

95. What is a functional interface in java?

96. What are the uses of the Java super keyword?

  • Access the parent class variable
  • To invoke the parent class method
  • To invoke the parent class constructors with and without argument.

97. What is method overloading with type promotion?

98. What is Java instanceOf operator?

99. What is the use of System class and Runtime class?

100. What is the difference between abstraction and encapsulation?

101. What are the different types of access specifiers in Java?

  • public: Any class in any package can see it. For a class, variable, or method, we can declare a public access type.
  • protected: It can be accessed from any class in the same package or from the subclass of the declared class. We can gain access to the outside world through inheritance.
  • default: The scope is contained within the package and does not require the use of any keywords.
  • private: It can only be accessible by members of the same class.

--

--

Unstop (formerly Dare2Compete) enables companies to engage with candidates in the most interactive way to discover, assess, and hire the best talent.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Unstop

Unstop

Unstop (formerly Dare2Compete) enables companies to engage with candidates in the most interactive way to discover, assess, and hire the best talent.