Java is the most popular platform in today’s competitive world. Whether you are a fresher or highly experienced professional, Java opens up endless opportunities and benefits. Time is changing, and so is the round of java interview. You can brush up your skills by choosing the most popular certification in Java.

Worldwide, Java is used by more than 10 million developers to develop software and applications. Here is the huge list of core Java interview questions and answers that should be in your to-do list before appearing for an interview.

Q1. What are the differences between C++ and Java?

Ans. Differences between C++ and Java are:

Features C++ Java
Platform dependent C++ is platform dependent Java is platform-independent
Multiple inheritance It supports multiple inheritance. It doesn’t support multiple inheritance through the class. Interfaces can obtain it in Java.
Pointers It supports pointers. You can write a program using a pointer in C++. It supports pointer internally and has restricted pointer support. Java does not allow to write pointer program. 

 

Thread Support It doesn’t have built-in support for threads. 

It depends on third-party libraries for thread support.

It has built-in thread support.
Call by Value and Call by reference It supports both call by value and call by reference. It supports call by value only. There is no call by reference in Java.
Object-oriented It is an object-oriented language. It is also an object-oriented language. 

 

Q2. What do you understand by Java virtual machine?

Ans. Java Virtual Machine is a virtual machine that enables the computer to run the Java program. The Java code is compiled by JVM to be a Bytecode, which is machine-independent and close to the native code.

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

Ans. Different types of memory areas allocated by JVM are:

  • Stack
  • Class
  • Program counter register
  • Native method stack
  • Heap

Q4. What is the difference between StringBuilder and StringBuffer?

Ans. StringBuffer

  • Synchronized means two threads can’t call the methods of StringBuffer simultaneously.
  • Less proficient than StringBuilder

StringBuilder

  • Non-synchronized means two threads can call the threads of StringBuilder simultaneously
  • More efficient than StringBuffer

Q5. Which are the four principle concepts upon which object-oriented design and programming rest?

Ans. Following are four principle concepts upon which object-oriented design and programming rest:

  • Encapsulation
  • Abstraction
  • Polymorphism
  • Inheritance

Q6. What do you mean by loops?

Ans. Loop is used in programming to execute a statement or a block of statement repeatedly.

Q7. How can you prevent a method from being overridden?

Ans. Use the final modifier on the method declaration to prevent a method from being overridden.

Public final void examplemethod ()

{

// method statements

}

Q8. Is it necessary to create an object for an interface?

Ans. Yes.

Q9. Give an example of a ternary operator?

Ans. Example:

Public class conditiontest

{

Public static void main (string args [])

{

String status;

Int rank;

Status= (rank ==1) “Done”: “Pending”;

}

}

Q10. Name the modifiers allowed for methods in an interface?

Ans. Public and abstract modifiers.

Q11. What do you mean by super?

Ans. It is a keyword used to access the method variables from the superclass.

Q12. When should we use abstract classes?

Ans. We use interfaces when something in our design changes frequently.

Q13. What is the base class of all classes?

Ans. Java.lang.object

Q14. Define a constant variable?

Ans. A constant variable should be declared as final and static. E.g.:

Static final int MAX_LENGTH=50;

Q15. How can you implement the singleton pattern?

Ans. You can implement singleton pattern by following common concepts which are:

  • A private static variable of the same class that is the only instance of the class
  • Private constructor to restrict instantiation of the class from other classes
  • Public static method that returns the instance of the class

Q16. Give one difference between the continue and break statement?

Ans. Continue statement is used to end the current loop iteration while when the break statement is used inside a loop, the loop gets terminated and return at the next statement.

Q17. What do you understand by thread-safe?

Ans. It is simply the program that behaves correctly when multiple simultaneous threads are using a resource.

Q18. What is the use of the default constructor?

Ans. A constructor is used to initialize the object. E.g.:

Class Bike1

{

Bike1()

}

System.out.println(“Bike is created”);

}

Public static void main(string args[])

{

Bike1 b= new Bike1();

}

}

Q19. Can you make a constructor final?

Ans. No

Q20. What is object cloning?

Ans. It is a way to create an exact copy of an object. Clone() is defined in the object class.

Q21. Which package is imported by default?

Ans. Java.lang package is imported by default.

Q22. Can the non-static variable be accessed in a static context?

Ans. No.

Q23. Can you make an array volatile?

Ans. Yes, you can make an array volatile but only the reference, which is pointing an array.

Q24. Which operator is considered to be with the highest precedence?

Ans. Postfix operators.

Q25. What is the advantage of Encapsulation?

Ans. The main advantage is the ability to modify implemented code without breaking the code of others.

Q26. Can a source file have more than one class declaration?

Ans. Yes, a source file can have more than one class declaration.

Q27. What is the difference between throw and throws?

Ans. Throw is used to trigger an exception, while throws is used in the declaration of exception.

Q28. What is the JAR file?

Ans. A JAR (Java Archive files) holds java classes in a library.

Q29. Define the JIT compiler?

Ans. JIT (just-in-time compiler) is enabled by default and is activated when a java method is called.

Q30. Is Empty .java file name a valid source file name?

Ans. Yes, Empty .java file name a valid source file name as Java allows us to save java file by .java only. First, we need to compile it by using javac .java and run by using java classname. 

Example:

//save by .java only

class Demo

{

public static void main(String args[])

{

System.out.println(“Naukri Learning”);

}

}

//compile by javac.java

//run by     java Demo

compile it by javac .java

run it by java Demo

Q31. Is delete, next, main, exit, or null keyword used in Java?

Ans. No, these keywords are not used in Java.

Q32. What is the output of the following Java program?

class Naukri 

{  

public static void main (String args[])   

{  

System.out.println(20 + 20 + “Javatpoint”);   

System.out.println(“Javatpoint” + 20 + 20);  

}  

 

Ans. Output:

40Javatpoint

Javatpoint2020

Q33. What is the output of the following Java program?

class NL  

 public static void main (String args[])   

{  

for(int i=0; 0; i++)  

 {  

System.out.println(“Hello Learners”);  

 }  

}  

}  

Ans. Here, the above code will give the compile-time error because there is a need for integer value in the second part of for loop where we are putting ‘0’.

Q34. Mention what will be the initial value of an object reference, which is defined as an instance variable?

Ans. In Java, all object references are initialized to null.

Q35. Why is a default constructor used in Java?

Ans. The default constructor is used to assign the default value to the objects. It is created by the Java compiler implicitly if there is no constructor in the class.

class Learner

{  

int id;  

String name;  

void display(){System.out.println(id+” “+name);}    

public static void main(String args[])

{  

Learner l1=new Learner(); 

 Learner l2=new Learner();  

l1.display();  

l2.display();  

}  

}  

Output:

0 null

0 null

Q36. What is the output of the following Java program?

public class Test   
{  
Test(int x, int y)  {  System.out.println(“x = “+x+” y = “+y);  }  Test(int x, float y)  {  System.out.println(“x = “+x+” y = “+y);  

}  

public static void main (String args[])  

{  

byte x = 10;  

 byte y = 15;  

Test test = new Test(x,y);  

}  

}  

Output:

X = 10 y = 15

In this program, the data type of the variables x and y, i.e., byte gets promoted to int, and the first parameterized constructor with the two integer parameters is called.

Q37. What is the output of the following Java program?

class Naukri  

{  

    int a;   

}

class Main   

{  

 public static void main (String args[])       

{          

Naukri naukri = new Naukri();           

System.out.println(naukri.a);      

}  

}  

Ans. Output: 0

Here, the variable a is initialized to 0 internally where a default constructor implied in the class, the variable i is initialized to 0 since there is no constructor in the class.

Q38. What is the static method in Java?

Ans. Three steps to define a static method:

  • A static method specifically belongs to the class, not from the object.
  • A static method can be used to access and change the value of the static variable.
  • There is no need to create an object to call static methods.

Example:

class Demo

    // static variable 

    static int x = 10; 

    static int y; 

      

    // static block 

    static { 

        System.out.println(“Static block initialized.”); 

        y = x * 4; 

    } 

  public static void main(String[] args) 

    { 

       System.out.println(“from main”); 

       System.out.println(“Value of x : “+x); 

       System.out.println(“Value of y : “+y); 

    } 

}

Output:

Static block initialized.

from main

Value of x: 10

Value of y: 40

Q39. Is it possible to override the static methods?

Ans. No, It is not possible to override static methods.

Q40. Explain what the static block is?

Ans. A static block is used to initialize the static data member in the program. It is executed before the main method.

class Test

{  

static{System.out.println(“static block is invoked”);

}  

public static void main(String args[])

{  

System.out.println(“Hello Learners”);  

}  

}  

Output: 

static block is invoked

Hello Learners

Q41. Is it possible to execute a program without main() method?

Ans. Yes, it is possible to execute a program without main() method where one can execute the program using a static block. 

Q42. What if the static modifier is removed from the signature of the main method?

Ans. If the static modifier is removed from the signature of the main method, then program compiles. Whereas, at runtime, It throws an error “NoSuchMethodError.”

Q43. Is it possible to declare the static variables and methods in an abstract class?

Ans. Yes, It is possible to declare static variables and methods in an abstract method. There is no need to make the object to access the static context. We can access the static context declared inside the abstract class by using the name of the abstract class.

Example:

abstract class Learning 

{  

    static int i = 2020;  

    static void LearningMethod()  

    {  

        System.out.println(“Naukri”);  

    }  

}  

public class LearningClass extends Learning    

{  

    public static void main (String args[])  

    {  

        Learning.LearningMethod();  

        System.out.println(“i = “+Learning.i);  

    }  

}  

Output

Naukri

i = 2020

Q44.  How do we do constructor chaining be done using this keyword?

Ans. Constructor chaining is used to call one constructor from another constructor of the class as per the current class object. This keyword is used to perform constructor chaining within the same class. 

Example:

public class Student 

{  

    int id,age;   

    String name, address;  

    public Student (int age)  

    {  

        this.age = age;  

    }  

    public Student(int id, int age)  

    {  

        this(age);  

        this.id = id;  

    }  

    public Student(int id, int age, String name, String address)  

    {  

        this(id, age);  

        this.name = name;   

        this.address = address;   

    }  

    public static void main (String args[])  

    {  

        Student stu = new Student (10121, 22, “Ron”, “Boston”);  

        System.out.println(“ID: “+stu.id+” Name:”+stu.name+” age:”+stu.age+” address: “+stu.address);  

    }  

      

}  

Output

ID: 10121 Name:Ron age:22 address: Boston

Q45. Explain what is super in Java?

Ans. The super keyword in Java is used to refer to the immediate parent class object as it is a reference variable. When there is any instance created of the subclass, then the instance of the parent class is created implicitly, which is referred by a super reference variable. 

class Student

{  

Student()

{

System.out.println(“Student is created”);}  

}  

class Jack extends Student{  

Jack(){  

System.out.println(“Jack is created”);  

}  

}  

class TestSuper4{  

public static void main(String args[]){  

Jack j=new Jack();  

}  

}  

Output:

Student is created

Jack is created

Q46. Is it possible to use this() and super() both in a constructor?

Ans. No, because this() and super() must be the first statement in the class constructor.

Example:

public class Demo

{    

  Demo()  

     {  

         super();   

         this(); 

          System.out.println(“Demo class object is created”);  

     }  

     public static void main(String []args){  

     Demo d = new Demo();  

     }  

}  

Output:

Demo.java:5: error: the call to this must be the first statement in the constructor

Q47. Is it possible to override private methods?

Ans. No, it is not possible to override the private methods because the scope of private methods is limited to the class, and we cannot access these methods outside of the class.

Q48. Explain what the covariant return type is?

Ans. In Java, it is possible to override any method by altering the return type if the return type of the subclass overriding method is the subclass type, which is known as a covariant return type. The covariant return type defines that the return type may vary in the same direction as the subclass.

class Test

{  

Test get(){return this;}  

}  

 class Test1 extends Test{  

Test1 get(){return this;}  

void message(){System.out.println(“Welcome to the covariant return type”);}  

 public static void main(String args[]){  

new Test1().get().message();  

}  

}  

Output: Welcome to the covariant return type

Q49. Is it possible to initialize the final blank variable?

Ans. Yes, it is possible to initialize the final blank variable in the constructor, if it is not static. If it is a static blank final variable, it can be initialized only in the static block.

Q50. Define immutable objects?

Ans. Immutable objects are simply objects which can’t be modified once created.

 

// An immutable class

public final class Student

{

    final String name;

    final int regNo;

    public Student(String name, int regNo)
    {

        this.name = name;

        this.regNo = regNo;

    }

    public String getName()

    {

        return name;

    }

    public int getRegNo()

    {

        return regNo;

// Driver class

class Test

{

    public static void main(String args[])

    {

        Student s = new Student(“ABC”, 101);

        System.out.println(s.getName());

        System.out.println(s.getRegNo());

        // Uncommenting below line causes error

        // s.regNo = 102;

    }

}

Output:

ABC

101

Conclusion

These are some of the frequently asked core Java interview questions and answers. Your conceptual understanding will make a big difference on whether or not you get through a job interview. Be confident, energetic, and happy!