100 most asked Java Interview Questions and Answers

Hello guys, welcome back. In this module, I will be discussing the 100 most asked Java Interview Questions and Answers. If you want to know more about Java Programming Language, you can learn from our Java programming course. Let us now talk about it.

Java Interview Questions and Answers

Table of Contents

100 most asked Java Interview Questions and Answers

1. What makes Java a platform-independent language?

This is the first question on the list of 100 most asked Java Interview Questions and Answers. Java is called platform-independent because the byte code generated by the JDK (Java Development Kit) can be executed by the JVM (Java Virtual Machine) on any platform regardless of the operating system.

2. Why is Java not entirely object-oriented?

This is the second question on the list of 100 most asked Java Interview Questions and Answers. Java is not entirely object-oriented since it uses eight primitive data types that are not objects: boolean, byte, char, int, float, double, long, and short.

3. Mention some differences between JDK, JRE, and JVM.

JDK JRE JVM
It stands for Java Development Kit. It stands for Java Runtime Environment. It stands for Java Virtual Machine.
It is a software development kit used to compile and develop Java Applets and Applications. It is a runtime environment in which a Java program is executed. It is a virtual or abstract machine used to execute the byte code of Java.
Platform-dependent. Platform-dependent. Platform-independent.
It includes development tools and JRE. It contains libraries and files that are utilized by the JVM during runtime. It includes specification, implementation, and instance.

4. Why is Java referred to as a robust language?

This is the 4th question on the list of 100 most asked Java Interview Questions and Answers. Java is known as a robust language since it utilizes strong memory management. Instead of using pointers like in C++, Java utilizes automatic garbage collection.

5. Why is the main() method in Java made static?

This is the 5th question on the list of 100 most asked Java Interview Questions and Answers. The main() method is declared as static so that it can be directly called by the JVM (Java Virtual Machine) without creating an object or instance of the class in which it is declared.

6. Define Java wrapper classes.

This is the 6th question on the list of 100 most asked Java Interview Questions and Answers. In Java, every primitive data type such as int, the float has a corresponding class to it known as wrapper classes. They are called wrappers because they wrap or convert the primitive data type into objects.

7. What do you mean by the constructor in Java?

This is the 7th question on the list of 100 most asked Java Interview Questions and Answers. A constructor in Java is a class special member function that is used for initializing data members of an object of a class. The constructor’s and class’s names must be the same. When a class object or instance is created, it is automatically invoked or called.

8. What are the differences between arrays and ArrayList in Java?

Arrays ArrayList
It is a static data structure which means that its size is fixed and cannot be changed. It is a variable-length or dynamic size array in the Collection framework which means that its size can be changed according to requirements.
It can store both objects and primitive types. It can store or hold only objects but no primitive types.
We can easily access the element by providing its index inside square brackets [ ]. We can access the element by providing its index number inside the get() method.

9. Why isn’t Java using pointers?

This is the 9th question on the list of 100 most asked Java Interview Questions and Answers. Java does not use pointers since they are unsafe and also increase the program’s complexity. Moreover, pointers are avoided in Java to avoid giving the user direct memory access.

10. What are the different kinds of access modifiers in Java?

This is the 10th question on the list of 100 most asked Java Interview Questions and Answers. In Java, access modifiers are the keywords used for specifying the access of a class, and methods outside a class or in another class. In Java, the four different types of access modifiers are:

  1. Default
  2. Public
  3. Private
  4. Protected

11. What do you mean by JIT in Java?

This is the 11th question on the list of 100 most asked Java Interview Questions and Answers. JIT is an abbreviation for a Just-In-Time compiler in Java. By converting bytecode into native machine code at run time, the JIT compiler helps to improve the performance of Java programs.

12. What is the final keyword in Java?

This is the 12th question on the list of 100 most asked Java Interview Questions and Answers. The final keyword in Java is used to represent constants. It can be used with variables, classes and methods.

  • If the final keyword is used with variables, you cannot modify or change its value.
  • If the final keyword is used with methods, you cannot override that method.
  • If you declare the class as final, the class cannot be extended or inherited by a subclass.

13. What are the main concepts of OOPs or Object-Oriented Programming in Java?

This is the 13th question on the list of 100 most asked Java Interview Questions and Answers. There are 4 main concepts used in object-oriented programming or OOPs in Java:

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

14. What is a singleton class in Java, and how do we make a class a singleton?

This is the 14th question on the list of 100 most asked Java Interview Questions and Answers. In Java, a singleton class is a class having only one instance or object. By making the private constructor, we can make a class a singleton.

15. What are the differences between local & instance variables in Java?

This is the 15th question on the list of 100 most asked Java Interview Questions and Answers. A local variable is defined within a block or method. The scope of a local variable is within a block and destroyed outside it.

An instance variable of a class is a variable that is defined inside a class but outside a method. It is accessed by creating an object of a class and destroyed when the object is destroyed.

16. What is OOP or Object-Oriented Programming?

This is the 16th question on the list of 100 most asked Java Interview Questions and Answers. Object-Oriented Programming or OOPs is a programming paradigm used to develop programs or applications using the concept of Objects. It aims to implement real-world entities in programming such as inheritance, hiding, polymorphism, and so on.

17. What makes Java Strings immutable?

This is the 17th question on the list of 100 most asked Java Interview Questions and Answers. Strings in Java are immutable which means that once a string object is created it cannot be updated but a new string object is created. This is because the string objects are cached in the string pool.

18. Describe the distinctions between Java’s Interface and Abstract classes.

Interface Abstract Class
It can have only abstract methods. It is possible to have both abstract and non-abstract methods.
A subclass implements the interface by using the implements keyword. A subclass or derived class inherits the abstract by using extends keywords.
It can only possess public static methods with no implementation. It can possess methods without or with implementation.
All methods of the interface must be implemented by the class that implements it. A class that inherits from an abstract class is not required to implement all of its superclass’s methods.

19. What do you mean by Polymorphism and what are its types?

This is the 19th question on the list of 100 most asked Java Interview Questions and Answers. Polymorphism in Java refers to an object’s ability to take more than one form. It can also be defined as a single task or action that can be done in many different ways.
Polymorphism is classified into two types:

  1. Compile-time polymorphism
  2. Runtime polymorphism

20. How compile-time and runtime polymorphism is achieved in Java?

This is the 20th question on the list of 100 most asked Java Interview Questions and Answers. In Java, compile-time polymorphism is achieved through method overloading and runtime polymorphism is achieved through method overriding.

21. What do you mean by abstraction and how it is achieved in Java?

This is the 21st question on the list of 100 most asked Java Interview Questions and Answers. An abstraction is an act of representing essential information without showing internal or implementation details. In Java, it is achieved by using abstract classes and interfaces.

22. What exactly are interfaces in Java?

This is the 22nd question on the list of 100 most asked Java Interview Questions and Answers. An interface in Java is a collection of abstract methods i.e., only function declarations but no implementation. We cannot create objects or instances of an interface. An interface cannot have constructors. A class can implement multiple interfaces by using the implements keyword in Java.

23. What are the types of inheritance in Java?

This is the 23rd question on the list of 100 most asked Java Interview Questions and Answers. In Java, we use extended keywords for inheriting attributes and methods of a super or base class. There are four types of inheritance:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance through Interface
  5. Hybrid Inheritance

24. Why doesn’t Java allow Multiple Inheritance?

This is the 24th question on the list of 100 most asked Java Interview Questions and Answers. Java does not support multiple inheritances to prevent ambiguity. For example,
Suppose there are 2 superclasses A and B and a subclass C that inherits both A & B. There is a show() method present in both the classes A & B. If we call the method show() method by creating an object of class C, the compiler produces an error since it is ambiguous which class show() method should be executed.

25. What are the differences between method overloading & overriding?

Method Overloading Method Overriding
Compile-time polymorphism. Runtime polymorphism.
Two or more methods in the same class with the same function or method name but distinct signatures. Two or more methods having the same function or method name and signature in different classes.

26. Is it possible to override a static or private method in Java?

This is the 26th question on the list of 100 most asked Java Interview Questions and Answers. A private method is not accessible outside the class so it will not be overridden by the sub-class. Hence, it cannot be overridden. Similarly, a static method cannot also be overridden because if we make static methods with the same method name and signature, both in base and derived classes, the derived class hides methods in the base class. This is known as method hiding.

27. What is constructor overloading in Java?

This is the 27th question on the list of 100 most asked Java Interview Questions and Answers. In Java, a constructor is said to be overloaded if a class has more than one constructor but with a different parameter list.

28. Can we define a class Abstract even if it does not have any abstract methods?

This is the 28th question on the list of 100 most asked Java Interview Questions and Answers. Yes, we can create an abstract class even if it doesn’t have any abstract method. However, if a class contains even one abstract method, it must be defined as abstract, or else an error will occur.

29. Mention some differences between C++ and Java.

C++ Java
Platform-dependent Platform-independent
Supports multiple inheritances. Using an interface, you may achieve multiple inheritances.
It supports operator overloading. It does not support operator overloading.
C++ uses pointers. Pointers are not used in Java.
It only makes use of the compiler. It utilizes both an interpreter and a compiler.
C++ uses virtual to override the function. Java does not use virtual keywords.

30. What is the use of static variables and methods?

This is the 30th question on the list of 100 most asked Java Interview Questions and Answers. We use static members (methods & variables) when we want to share a common method or variable among all the objects of the class instead of making several copies of each object.

For example, in a class employee, the company name can be made static because all the objects will have the same company name.

31. What is the result of the Java program below?

class Demo {
    public static void main(String[] args) {
      System.out.println(21 + 30 + "UseMynotes");
      System.out.println("UseMynotes" + 21 + 30);
    }
}
Output
//51Usemynotes
//Usemynotes2130

32. In Java, how many various types of constructors are there?

In Java, there 2 types of constructors:

  1. Default constructor – It does not take any parameters.
  2. Parameterized constructor – It accepts at least one parameter.

33. Does Java use a copy constructor?

No, there is no copy constructor in Java like C++.

34. Mention the differences between constructor and methods in Java.

Constructor Method
It is mandatory the constructor name and class name must be the same. The name of the method may or may not be the same as the class name.
It cannot have any return type. It is required to have a return type.
Constructors can’t be static. The method may or may not be static.
It is invoked or called automatically when an object or instance of a class is created. It is called or invoked by using object name along with dot (.) symbol.
It is used for initializing the state of an object. It is used to define the behavior of an object.

35. Can a program be executed without using the main() method?

This is the 35th question on the list of 100 most asked Java Interview Questions and Answers. No, a program cannot be executed without the main() method in Java because the execution of the program starts from the main() method.

36. What is this keyword in Java?

This is the 36th question on the list of 100 most asked Java Interview Questions and Answers. This keyword in Java is a reference variable used to refer to the current object called the method or variable.

37. Is it possible to use this keyword in Java to refer to the static members?

This is the 37th question on the list of 100 most asked Java Interview Questions and Answers. Yes, because it is a reference variable to the current object, we may use this keyword to refer to static members.

38. Can we make the main() method in Java private instead of public?

This is the 38th question on the list of 100 most asked Java Interview Questions and Answers. We can make the main() method private but we will be getting a runtime error and hence, the program will not execute.

39. What do you understand about super keywords in Java?

In Java, the super keyword is a reference variable that is used to refer to the object of the immediate parent class.

40. Mention the differences between vector and array.

Vector Array
It is a dynamically sized array. It is a fixed-sized data structure.
Vector is synchronized. The array is not synchronized.
It is slow compared to the array. It is fast.
It can only hold or store Java objects. It is used to store both primitive types and Java objects.
We can use the size() method to know the length of the vector. We can use the length property to know the length of the array.

41. What is multithreading in Java?

This is the 41st question on the list of 100 most asked Java Interview Questions and Answers. Multithreading is a process of executing multiple tasks concurrently within a single program. This is done to achieve maximum CPU utilization.

42. What are the different ways of implementing multithreading in Java?

In Java, multi-threading can be implemented in 2 ways:
1. By implementing the Runnable Interface in java.lang.Runnable.
2. By extending the Thread class in java.lang.Thread.

43. Predict the outcome of the Java program below.

class BaseClass{
  public final void show(){
    System.out.println("Base Class");
  }
}

public class DerivedClass extends BaseClass{
  public final void show(){
    System.out.println("Derived class");
  }
  public static void main(String[] args){
    BaseClass obj = new BaseClass();
    obj.show();
  }
}
//Output
//There will be a compilation error because a final method cannot be overridden.

44. Mention some differences between super and this keyword in Java.

super this
It is used to refer to parent class objects. It is used to refer to the current object of the class.
It is used to access the methods of the parent or base class. It is used to access the current class’s method.
It calls or invokes the default constructor of the base class. It calls the default constructor of the current class.

45. What are the differences between subclass and inner class?

Sub Class Inner Class
It is a class that inherits the properties and methods of a base or parent class. It is a class that is nested within another class.
It can access all the public and protected members of the super or base class. It has access to all members of the outer class.

46. What is a class loader in Java?

This is the 46th question on the list of 100 most asked Java Interview Questions and Answers. A Java class loader is a component of the Java Runtime Environment or JRE that is used to load the classes of Java into the Java Virtual Machine, or JVM, during runtime.

47. Is it necessary for a Try Block to be followed by a Catch Blockin Java Exception handling?

This is the 47th question on the list of 100 most asked Java Interview Questions and Answers. Yes, it is mandatory to use a catch block because any exception thrown by the try block must be caught in the catch block.

48. Name the types of exceptions in Java.

Exceptions in Java are of two types:

  1. Unchecked exception
  2. Checked exception

49. Mention the different states of thread.

The four states of thread in Java are:

  1. New thread
  2. Runnable
  3. Non-Runnable
  4. Dead or Terminated

50. Define NullPointerException in Java.

This is the 50th question on the list of 100 most asked Java Interview Questions and Answers. When a user tries to access or alter the values of a null object, the NullPointerException arises.

51. What are the types of keywords used in java exception handling?

  1. try
  2. catch
  3. finally
  4. throw
  5. throws

52. Is it allowed to overload the main() method in Java?

This is the 52nd question on the list of 100 most asked Java Interview Questions and Answers. Yes, the main() method can be overloaded but the JVM or Java Virtual Machine only calls the original main() method.

53. Can the main() method be overridden in Java?

This is the 53rd question on the list of 100 most asked Java Interview Questions and Answers. No, because the main() method is a static method and static methods cannot be overridden.

54. In Java, distinguish between equals() and ==.

This is the 54th question on the list of 100 most asked Java Interview Questions and Answers. In Java, equals() is a method used for comparing the values of two objects or variables whereas == is a comparison operator used for comparing the addresses or memory locations of two more objects or variables.

55. Mention some of the features of Java.

  1. Platform-independent
  2. Robust
  3. Object-oriented language
  4. Interpreted
  5. Simple and safe

56. Can a class constructor return any value?

No, a constructor does not have any return type so it cannot return any value.

57. Define aggregation.

This is the 57th question on the list of 100 most asked Java Interview Questions and Answers. An aggregation is a form of association that is defined as a “has-a” relationship between two classes. It is done to reuse the code. It is a one-way or unidirectional relationship between two classes.

58. What do you mean by association?

This is the 58th question on the list of 100 most asked Java Interview Questions and Answers. An association defines a relationship between two classes with the help of objects. The relation or association can be one-to-one, one-to-many, many-to-many, and many-to-one. Aggregation & Composition are the 2 forms of association.

59. What do you mean by composition?

A composition is a form of association in which two classes are dependent on each other.

60. Differentiate between dynamic and static binding.

This is the 60th question on the list of 100 most asked Java Interview Questions and Answers. In dynamic binding, the decision of binding a method to an object is taken at run time. Late binding is another name for dynamic binding.

In static binding, the decision of binding a method to an object is taken at compile time. Static binding is another name for early binding.

61. What will be the result of the below Java program?

class BaseClass{
  void display(){
   System.out.println("BaseClass display () method");
  }
}
public class DerivedClass extends BaseClass{
  void display(){
    System.out.println("DerivedClass display () method");
  }
  public static void main (String arr[]){
    BaseClass obj = new DerivedClass();
    obj.display();
  }
}
//Output
//DerivedClass display () method

62. Mention the difference between encapsulation and abstraction.

In encapsulation, data and methods are wrapped or bound into a single unit known as class.

In abstraction, only essential information is shown and hiding the internal or implementation details.

63. In Java, what is an abstract class?

This is the 63rd question on the list of 100 most asked Java Interview Questions and Answers. An abstract class is a class that cannot be instantiated but we can create a reference variable. A class is declared as abstract by using abstract keywords. It includes both abstract and non-abstract methods.

64. Can we use both final & abstract keywords with a method?

This is the 64th question on the list of 100 most asked Java Interview Questions and Answers. No, we cannot use both final and static methods with a method because an abstract method needs to be overridden by the sub-class but the final method cannot be overridden.

65. Can we declare a method as final in an interface?

This is the 65th question on the list of 100 most asked Java Interview Questions and Answers. No, we cannot declare a method as final because all the methods in the interface are abstract so it needs to be implemented by subclass but the final method cannot be overridden.

66. Mention some differences between unchecked and checked exceptions in Java.

Unchecked Exception Checked Exception
It is checked or validated at run time. It is checked or validated at compile time.
NullPointerException, ArithmeticException, ArraysOutOfBound exception are some of the exceptions checked at run time. SQLException, IOException, FIleNotFoundException are some of the exceptions checked at compile time.

67. Name the base class of all the Java exception classes.

The base class for all the exceptions is java.lang.Throwable.

68. Is it possible to create several catch blocks within a single try block?

Yes, we can create multiple catch blocks for a single try block.

69. Identify the two environment variables that must be specified to run any Java program.

  1. Path variable
  2. Classpath variable

70. Is it possible to use the destructor in Java?

No, we cannot use destructors in Java.

71. What do you mean by anonymous class?

An anonymous inner class in Java does not have a name and produces only one object.

72. Give the Java method name that must be implemented for all threads.

Run() method

73. Distinguish between break and continue in Java.

Break Continue
It is used to terminate or end the loop if a certain condition is met. It is used to skip the execution of the loop for the current iteration based on some condition.
It can be used inside a loop and switch. It is used inside the loop only.

74. What do you understand about string pools in Java?

This is the 74th question on the list of 100 most asked Java Interview Questions and Answers. A string pool in Java is defined as a collection of strings stored in heap memory.

75. What are the differences between the throw and throws keywords in Java?

Throw Throws
It is used for throwing an exception explicitly. It is used to declare an exception that may be thrown by the method during the execution.
We cannot throw more than an exception. Using throws keywords, we may declare multiple exceptions.
It is followed by an instance or object of exception to be thrown. It is followed by the name of a class of exception to be thrown.

76. Is it possible to call a constructor inside another constructor of the same class?

Yes, we can call a constructor of the same class inside another constructor by using this() as the first line.

77. Is it allowed to compare String Buffer and String class in Java?

No, we cannot compare the objects of String Buffer and String. By doing so, we will be getting an error.

78. In inheritance, what is the sequence of calls of constructors?

First, the parent or base class constructor is called, and then the derived class constructor is called.

79. Can we name the constructor differently from a class name?

No, the constructor and class name must be the same.

80. What exactly do you mean by Java packages?

This is the 80th question on the list of 100 most asked Java Interview Questions and Answers. A package is defined as a collection of classes and interfaces that are grouped.

81. What will be the output of the Java program given below?

public class Demo{
  public static void main(String []str) {
    String str1 = new String("UseMyNotes");
    String str2 = "UseMyNotes";
    System.out.println(str1==str2);
    System.out.println(str1.equals(str2));
  }
}
//Output
//false
//true

82. In Java, distinguish between the String and StringBuffer classes.

String StringBuffer
The String class is immutable. StringBuffer class is mutable.
It is slow. It is fast.
It uses more memory while concatenating many strings. It uses less memory while concatenating many strings.

83. Is it possible to overload the static methods in Java?

This is the 83th question on the list of 100 most asked Java Interview Questions and Answers. Yes, we can overload the static methods in Java by passing different function signatures.

84. Which component of memory, the stack or the heap, is cleaned during the garbage collection process?

Heap

85. Which of the following string classes is to be used when frequent updates need to be done in the data?

This is the 85th question on the list of 100 most asked Java Interview Questions and Answers. StringBuffer class should be used in case of frequent updates because it is mutable and hence, the string pool will not be overloaded.

86. What will happen when a static keyword is not used with the main() method in Java?

This is the 86th question on the list of 100 most asked Java Interview Questions and Answers. There will be no compilation error but we will get the “NoSuchMethodError” error at runtime.

87. Will the final block be performed if a return statement is placed at the end of the try-and-catch blocks?

This is the 87th question on the list of 100 most asked Java Interview Questions and Answers. Yes, the final block will always be executed regardless of whether or not there is an exception.

88. What is the result of the Java program shown below?

class Demo{
  public static void main(String[] str) {
    System.out.println('a' + 'b' + 'c');
  }
}
//Output
//294

89. Is it possible to have many public classes in a java source file?

No, we can only have one public class in the Java source file.

90. Can an interface extend another interface?

This is the 90th question on the list of 100 most asked Java Interview Questions and Answers. Yes, one interface can extend another interface by using extends keyword in Java.

list of 100 most asked Java Interview Questions and Answers

91. Name the different types of classloaders in Java.

  1. Bootstrap ClassLoader
  2. Extensions ClassLoader
  3. System ClassLoader

92. Define ternary operator in Java.

This is the 92nd question on the list of 100 most asked Java Interview Questions and Answers. A ternary operator is used as a replacement for the if-else statement in Java. It is the only conditional operator that contains 3 operands.

93. In Java, define garbage collection.

This is the 93rd question on the list of 100 most asked Java Interview Questions and Answers. Java garbage collection is the technique through which Java applications maintain their memory automatically by deleting or cleaning up unused objects and packages in memory.

94. What is the usage of the default constructor in Java?

This is the 94th question on the list of 100 most asked Java Interview Questions and Answers. In Java, a default constructor is used for initializing data members or member variables of an object with their default values. It is automatically created by the compiler when there are no other constructors in the program.

95. Mention the difference between path & Classpath in Java.

This is the 95th question on the list of 100 most asked Java Interview Questions and Answers. The classpath defines the location of bytecode files, i.e., .class files.
The path defines the location of .exe files.

96. Mention different data types in Java.

In Java, there are 8 data types.

  1. short
  2. byte
  3. char
  4. int
  5. float
  6. double
  7. long
  8. boolean

97. What are the four OOPs concepts in Java?

  • Encapsulation
  • Abstraction
  • Polymorphism
  • Inheritance

98. Can we assign a null value to this keyword in Java?

No.

99. What do you mean by static block in Java?

This is the 99th question on the list of 100 most asked Java Interview Questions and Answers. A static block in Java is used for initializing the static variables of a class. It is only executed once: when the class is loaded into memory for the first time.

100. Define exception. Which package contains all of the exception class definitions in Java?

This is the 100th question on the list of 100 most asked Java Interview Questions and Answers. Exceptions are abnormal circumstances that occur during program execution. It may arise as a result of incorrect user input or incorrect logic provided by the coder. The java.lang.Exception package contains all of the exception class definitions in Java.

I hope this module will be very helpful (Java Interview Questions and Answers) for you in your interview preparation. There is no limit of questions to be asked in interviews but I have mentioned some of the most frequently asked questions related to Java. Stay tuned for more informative and engaging modules like this.