preloader
Java Interview Questions

Top Core Java Interview Questions for 2022 Job Interviews

author image

Core Java is a very important part of the Java programming language. So if you are appearing for a technical job interview then you need to well prepare the core java topics. To help you out, here we have provided a list of Core Java interview questions and answers from which you can prepare for your interview. Go through each question given here and prepare it well, here we have covered basic, advanced, coding-based core java interview questions for experienced and freshers.

About Core Java: It is a part of the Java programming language which is used to develop or create general-purpose online applications and mobile applications. Without Core Java, you are not able to develop any advanced java applications. Core java programming covers many important things like awt, swings, thread concept, socket, collection object, and classes.

Also, prepare for Java Collection interview questions, Networking interview questions, and other language interview questions from here.

Core Java Interview Questions

1. Explain Java?

2. Explain some features of the Java Programming language.

3. Explain Java virtual machine?

4. Tell the different types of memory areas are assigned by JVM?

5. Explain the JIT compiler?

6. Explain the platform?

7. Differentiate between Java and other platforms?

8. From where does this 'write once and run anywhere' nature in Java?

9. Explain classloader?

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

11. What if I mention a static public void than a public static void?

12. Tell the default value of the local variables?

13. Explain various access specifiers in Java?

14. Tell some advantages of Packages in Java?

15. Explain the object-oriented paradigm?

16. Explain an object?

17. Explain constructor?

18. Explain the types of constructors?

19. Does the constructor bring back any value?

20. Explain the static method?

21. Explain the restrictions applied on static methods in Java?

22. Explain the reason for being the main method static?

23. Explain static block?

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

25. What happens when the static modifier is detached from the signature of the main method?

26. Are we able to make constructors static?

27. Can we create the abstract methods static in Java?

28. Explain this keyword in java?

29. Explain the uses of this keyword?

30. Can we allocate the reference to this variable?

31. Can this keyword be used to refer to static members?

32. Using this keyword, how constructor chaining can be done?

33. Tell the benefit of passing this keyword into a method rather than the current class object itself?

34. Explain Inheritance?

35. Explain composition?

36. Differentiate aggregation and composition?

37. Why is the reason Java does not support pointers?

38. Explain the super keyword in java?

39. Show by coding how super keyword can be used to do constructor chaining?

40. Explain the uses of the super keyword?

41. Differentiate between this and super keyword?

42. Can we use super() & this() both in a constructor?

43. Explain object cloning?

44. Explain method overloading?

45. Why do method overloading won’t work by changing the return type in java?

46. How can we overload the main() method?

47. Explain rules of method overriding?

48. Why are we not able to override the static method?

49. Are we able to override the private methods?

50. In subclass can we modify the scope of the overridden method?


Learn More Interview Questions Here:


Core Java Interview Questions For Freshers

1. Explain Java?

Java is the programming language that is robust, high-level, secure, object-oriented, Multithreaded, platform-independent, high performance, and portable. James Gosling developed this programming language in June 1991. It also has its own platform as it gives its own JRE and API.

2. Explain some features of the Java Programming language.

  • Simple: Java Programming language is easy to learn. The Java syntax is based on C++ in which we can easily write the program.
  • Portable: Java has a read-once-write-anywhere approach. We can run the Java program on any machine by converting the Java program (.java) to bytecode (.class).
  • Platform Independent: Java doesn’t depend upon any other operating system to be executed and comes with its platform on which its code is executed.
  • Secured: Java uses ByteCode and Exception handling which makes it more secured.
  • Robust: Java uses strong memory management. The concepts such as Exception handling, Automatic garbage collection, etc. make it more robust.
  • Architecture Neutral: Java is not dependent on the architecture. In C language, the size of the architecture (32 bit or 64 bit) varies that doesn’t exist in Java.
  • Interpreted: Java uses (JIT) interpreter along with the compiler to execute the program.
  • High Performance: Java is faster than other traditional programming languages because Java bytecode is “close” to native code but still a little bit slower than a compiled language (e.g., C++).
  • Dynamic: Java Programming language is dynamic. It allows the dynamic loading of classes and loads on demand. It also sustains functions from its native languages, i.e., C and C++.

3. Explain Java virtual machine?

Java Virtual Machine (JVM) is a virtual machine that allows the computer to execute the Java program. JVM works as a run-time engine that calls the main method available in the Java code. Java Virtual Machine is the requirement that must be employed in the computer system. The Java code is assembled by JVM to become a Bytecode that is machine-independent and close to the native code.

4. Tell the different types of memory areas are assigned by JVM?

  • Class(Method) Area: It stores per-class structures such as the runtime constant pool, method data, field, and the code for methods.
  • Heap: It is the runtime data area in which allocated the memory to the objects
  • Stack: Java Stack supplies frames. It carries local variables and partial results, and plays a part in method invocation and return. Each thread creates a private JVM stack. A new frame is made every time a method is invoked. A frame is destroyed when its method invocation finishes.
  • Program Counter Register: PC (program counter) register includes the address of the JVM instruction currently being implemented.
  • Native Method Stack: It includes all the native methods used in the application.

5. Explain the JIT compiler?

Just-In-Time(JIT) compiler: It is used to enhance performance. Just-In-Time compiles parts of the bytecode that have the same functionality at the exact time and hence decreases the amount of time required for compilation. The “compiler” terms to a translator from the instruction set of a JVM ( Java virtual machine) to the instruction set of a particular CPU.

6. Explain the platform?

It is the software or hardware environment in which a component of the software is run. There are two different types of platforms available i.e. software-based and hardware-based. Java gives a software-based platform

7. Differentiate between Java and other platforms?

  • Java is only a software-based platform whereas other platforms can be software-based platforms or hardware-based platforms.
  • Java is directed on top of other hardware platforms whereas other platforms can only work on the hardware components.

8. From where does this 'write once and run anywhere' nature in Java?

The bytecode provides this ‘write once and run anywhere’ nature to Java. Java compiler transforms the Java programs into the class file (Byte Code) which is the connecting language between source code and machine code. This bytecode is not platform-based and can be run on any computer.

9. Explain classloader?

Classloader is a subsystem of Java Virtual Machine used to carry class files. When a java program is executed, it is firstly loaded by the classloader. There are 3 built-in classloaders in Java.

  • Bootstrap ClassLoader: it is the superclass of the Extension classloader as it loads the rt.jar file that includes all class files of Java Standard Edition like java.util package classes, java.lang package classes, java.io package classes, java.net package classes, java.sql package classes, etc.
  • Extension ClassLoader: For Bootstrap it is the child classloader and parent classloader of the System classloader. It loads the jar files placed inside $JAVA_HOME/jre/lib/ext directory.
  • System/Application ClassLoader: It loads the class files from the classpath. By default, the classpath is set to the existing directory. You can alter the classpath using “-cp” or “-classpath” switch. It is also called the Application classloader.

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

Yes, Java authorizes us to save our java file by .java only, after that we need to assemble it by javac .java and operate by java class name. For example:

//save by .java only

class A{

public static void main(String args[]){

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

}

}

//compile by javac .java

//run by java A

compile it by javac .java

run it by java A

11. What if I mention a static public void than a public static void?

The program smoothly compiles and runs properly as the order of specifiers doesn’t matter in Java.

12. Tell the default value of the local variables?

The local variables are not set to any default value, neither primitives nor object references.

13. Explain various access specifiers in Java?

Access specifiers are the keywords that show the access scope of the method, class, or variable.

  • Public: Any classes, methods, or variables can be accessed under public.
  • Protected: It can be accessed by the class or the sub-class of the same package.
  • Default: Default is accessible in the set package only.
  • Private: The class, methods, or variables mentioned as private can be called up within that class only.

14. Tell some advantages of Packages in Java?

  • Packages evade the name clashes.
  • The Package gives easier access control.
  • Packages also use the hidden classes which are not visible outside.
  • It easily locates the related classes.

15. Explain the object-oriented paradigm?

It is a programming paradigm that depends on objects having data and methods mentioned in the class it belongs to. The Object-oriented paradigm takes the benefit of modularity and reusability. Objects are the representatives of classes that interact with different design applications and programs. Some features of the object-oriented paradigm are:

  • Follows the bottom-up approach in program design.
  • Focus on data with methods to operate upon the object’s data
  • Consist the concept like Encapsulation and abstraction which only show functionality to the user and hide the complexities.
  • Implements the real-time approach such as inheritance, abstraction, etc.
  • C++, Simula, Smalltalk, Python, C#, etc. are some examples of the object-oriented paradigm.

Core Java Interview Questions For Experienced

16. Explain an object?

It is a real-time commodity that has some state and behavior. An object is a representative of the class which has the instance variables like the state or the behavior of the object. The object of a class is made by using the new keyword.

17. Explain constructor?

It is a special type of method used to set the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object. Whenever an object is made using the new keyword, it calls the default constructor of the class. The class and the constructor must have the same name.

18. Explain the types of constructors?

  • Default Constructor: It is the one that doesn’t take any value. It is mainly used to set the instance variable with the default values. Default Constructor is also used for executing some important tasks on object creation. A default constructor is invoked indirectly by the compiler if there is no constructor mentioned in the class.
  • Parameterized Constructor: It is the one that uses the given values to initialize the instance variables. Generally, the constructors which take the arguments are called parameterized constructors.

19. Does the constructor bring back any value?

Yes, the constructor implicitly brings back the current instance of the class

20. Explain the static method?

  • A static method fits in the class better rather than the object.
  • You do not require creating an object to call the static methods.
  • A static method can manage and modify the value of the static variable.

Follow Simple Steps to apply in these MNC’s:

21. Explain the restrictions applied on static methods in Java?

There are two main restrictions imposed on the static methods.

  • The static method is not able to use non-static data members or directly call the non-static method.
  • this and super are non-static so they cannot be used in a static context.

22. Explain the reason for being the main method static?

It is because the objects are not required to be called in the static method. If it is non-static, then the JVM firstly needs to create its object and then call the main() method which consumes extra memory.

23. Explain static block?

It is used to set the static data member and executed prior to the main method, at the time of classloading.

class A2{

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

public static void main(String args[]){

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

}

}

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

No, After JDK 1.7 it is not possible, but it was possible before JDK 1.7 using the static block.

25. What happens when the static modifier is detached from the signature of the main method?

Initially, Program compiles but at runtime, It shows an error “NoSuchMethodError.”

Know the Interview Criteria of these MNCs!!!

Core Java Interview Questions And Answers For Experienced

26. Are we able to make constructors static?

It is already known that the static context (block, method, or variable) goes to the class and not to the object. Meanwhile, Constructor is only invoked when the object is made then there is no point in making the constructor static. But still, if you try then the compiler will show an error.

27. Can we create the abstract methods static in Java?

If we create the abstract methods static, it belongs to the class, can be directly called which is not required at all. Calling an undefined method is unnecessary or useless so therefore in Java it is not allowed.

28. Explain this keyword in java?

this keyword is a reference variable that points towards the current object. The keyword can be used to mention the current class properties like constructors, instance methods, variables, etc. In methods or constructors, it can pass as an argument. It can come back from the method as the current class instance.

29. Explain the uses of this keyword?

  • this points to the current class instance variable.
  • this can also implicitly invoke the current class method
  • this() keyword can also invoke the current class constructor.
  • In the method call, this keyword can be passed as an argument.
  • In constructor call also this keyword can be passed as an argument
  • this keyword can return to the current class instance.

Core Java Interview Questions For 3 Years Experience

30. Can we allocate the reference to this variable?

No, this variable is not able to be assigned to any value as it always refers to the current class object and in Java, this is the final reference. The compiler will show an error if we try to do so. For example.

public class Test

{

public Test()

{

this = null;

System.out.println(“Test class constructor called”);

}

public static void main (String args[])

{

Test t = new Test();

}

}

Output

Test.java:5: error: cannot assign a value to final variable this

this = null;

^

1 error

31. Can this keyword be used to refer to static members?

this keyword is just a reference variable that points to the current class object. Nevertheless, it is useless to access static variables through objects, therefore, we don’t use this to refer to static members. For example:

public class Test

{

static int i = 10;

public Test ()

{

System.out.println(this.i);

}

public static void main (String args[])

{

Test t = new Test();

}

}

Output

10

32. Using this keyword, how constructor chaining can be done?

Constructor chaining allows us to call one constructor from another constructor of the class w.r.t. the current class object. this keyword can be used to perform constructor chaining in the same class. Check the below-given example:

public class Employee

{

int id,age;

String name, address;

public Employee (int age)

{

this.age = age;

}

public Employee(int id, int age)

{

this(age);

this.id = id;

}

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

{

this(id, age);

this.name = name;

this.address = address;

}

public static void main (String args[])

{

Employee emp = new Employee(105, 22, “Vikas”, “Delhi”);

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

}

}

Output

ID: 105 Name:Vikas age:22 address: Delhi

33. Tell the benefit of passing this keyword into a method rather than the current class object itself?

  • this is a final variable so it cannot be allocated to any new value whereas the current class object can be modified.
  • this can also be used in the synchronized block.

34. Explain Inheritance?

It is a mechanism through which a single object can acquire all the behavior and properties of another object of another class. Inheritance is used for Method Overriding and Code Reusability. The idea used behind inheritance is that you can make new classes that are made upon existing classes. When you come from an existing class, you can utilize methods and fields of the parent class and also add new methods and fields in your present class. It shows the IS-A relationship that is also called the parent-child relationship.

5 types of inheritance in Java are:

  • Single-level inheritance
  • Multi-level inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Multiple inheritances are not supported in Java through the class.

35. Explain composition?

Holding the reference of one class within another class is called composition. When an object holds the other object, and if the held object cannot exist without the presence of a container object, then it makes a composition. In simple words, a composition is the specific case of aggregation which shows a stronger relationship between two objects.

36. Differentiate aggregation and composition?

Aggregation shows the weak relationship whereas composition displays the strong relationship. For example, a motorbike has an indicator (shows aggregation), but the motorbike has an engine (shows composition).

37. Why is the reason Java does not support pointers?

The pointer is a variable that points to the memory address. They are unsafe (unsecured) to use in Java and also complex to understand.

38. Explain the super keyword in java?

It is basically a variable that always points to the immediate parent class object. When you make the subclass instance then an instance of the parent class is made implicitly which referred to the super keyword reference variable. The super() automatically becomes the class constructor by the compiler when there is no super or this keyword is used.

class Animal{

Animal(){System.out.println(“animal is created”);}

}

class Dog extends Animal{

Dog(){

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

}

}

class TestSuper4{

public static void main(String args[]){

Dog d=new Dog();

}

}

Output:

animal is created

dog is created

39. Show by coding how super keyword can be used to do constructor chaining?

class Person

{

String name,address;

int age;

public Person(int age, String name, String address)

{

this.age = age;

this.name = name;

this.address = address;

}

}

class Employee extends Person

{

float salary;

public Employee(int age, String name, String address, float salary)

{

super(age,name,address);

this.salary = salary;

}

}

public class Test

{

public static void main (String args[])

{

Employee e = new Employee(22, “Mukesh”, “Delhi”, 90000);

System.out.println(“Name: “+e.name+” Salary: “+e.salary+” Age: “+e.age+” Address: “+e.address);

}

}

Output

Name: Mukesh Salary: 90000.0 Age: 22 Address: Delhi

40. Explain the uses of the super keyword?

  • It is used to point to the immediate parent class instance variable.
  • It can invoke the parent class method immediately.
  • It is also used to invoke immediate parent class constructor.

You may also prepare:

Core Java Interview Questions For 5 Years Experience

41. Differentiate between this and super keyword?

  • this keyword goes to the current class context whereas The super keyword goes to the parent class contexts.
  • this keyword tells the difference between local and instance variables when it goes through the class constructor whereas the super keyword initializes the base class variables in the derived class constructor
  • They are both super and this should be the first statement in the class constructor else the compiler shows an error.

42. Can we use super() & this() both in a constructor?

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

Example:

public class Test{

Test()

{

super();

this();

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

}

public static void main(String []args){

Test t = new Test();

}

}

Output:

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

43. Explain object cloning?

Object cloning is used to make a clone, an exact copy of an object. The Object class uses the clone() method to clone an object. By the object class java.lang.Cloneable interface needed to be implemented on those objects which are cloned. If the cloneable interface is not implement then, clone() method create CloneNotSupportedException.

protected Object clone() throws CloneNotSupportedException

44. Explain method overloading?

Method overloading is the polymorphism method that permits you to generate multiple methods with the same name but dissimilar signatures. Through these ways, you can achieve method overloading.

  • By Changing the number of arguments
  • By changing the data type of arguments

It enhances the readability of the program and it is performed to work out the program quickly.

45. Why do method overloading won’t work by changing the return type in java?

In Java programming, method overloading is not workable by changing the return type of the program because of avoiding ambiguity.

class Adder{

static int add(int a,int b){return a+b;}

static double add(int a,int b){return a+b;}

}

class TestOverloading3{

public static void main(String[] args){

System.out.println(Adder.add(11,11));//ambiguity

}}

Output:

Compile Time Error: method add(int, int) is already defined in class Adder

46. How can we overload the main() method?

We can overload the main() method by using method overloading that can have any number of main methods in a Java program.

47. Explain rules of method overriding?

  • The method and parent class must have the same name.
  • The method and parent class must have the same signature.
  • The method and parent classes must have an IS-A relationship between them.

48. Why are we not able to override the static method?

It reason behind it is the static method is the part of the class and bound with it on the other hand instance method is bound with the object, so the static method gets memory in the class area, whereas the instance method gets memory in a heap.

49. Are we able to override the private methods?

No, we are not able to override the private methods because the private methods have limited scope to the class and cannot be accessed outside of the class.

50. In subclass can we modify the scope of the overridden method?

Yes, in subclass we can modify the scope of the overridden method but, we cannot reduce the accessibility of the method. The following point must be kept in mind:

  • The private scope can be transformed to protected, public, or default.
  • The protected scope can be transformed to public or default.
  • The default can be transformed to public.
  • The public will not be changed and always remain public.

If you want to prepare for more programming languages other than core java interview questions and answers for experienced and freshers then you can take help from the below-given interview questions list. If you have any concerns and queries related to core java interview questions and answers then reach us through the comment box given below.

Want to prepare for these languages:

Recent Articles