[2022] Core Java Interview Questions

Written by

Vaaruni Agarwal


Java is a high-level, object-oriented programming language. It is one of the most popular programming languages because it is robust, secure, platform-independent, multithreaded has a high performance and a portable programming
language.


It was developed by James Gosling in June 1991. It is also known as the platform independent language and provides its own runtime environment (JRE). JAVA is used for application programming and is used in windows, enterprise
based and mobile applications.

Let us look at the JAVA Interview Questions:

  1. Who is the father of JAVA?

James Gosling developed JAVA in June 1991.

  1. What do you understand by Java virtual machine?


Java Virtual Machine is a virtual machine that enables the computers to run the Java program. JVM is a run-time engine, which calls the main method present in the Java code. The Java code is compiled by JVM into a Bytecode. This
byte code is machine independent.

  1. Explain what is JRE in JAVA?


JRE stands for Java Runtime Environment. It is the implementation of the JVM. The JRE is a set of software tools used for developing the Java applications. It is used to provide the runtime environment. JRE is a set of libraries and
other files that JVM uses at runtime.

  1. What do you understand by the term JDK?

JDK stands for Java Development Kit. It is a software development environment, used to develop Java applications and applets. It contains JRE and other development tools. It has a physical existence.

  1. What is JIT compiler?


Just in Time or the JIT compiler is used to improve the performance of a JAVA program. JIT compiles parts of the bytecode with a similar functionality to reduce the amount of time needed for compilation. The term “compiler”, over
here refers to a translator that translates instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

  1. What do you understand by the ‘write once and run anywhere’ nature of a JAVA program?


The bytecode in Java is machine independent and can be executed on any machine easily. JVM converts the Java programs into the class file or byte code. It is the intermediate language between source code and machine code.

  1. What do you mean by javac ?

javac is actually a java compiler. It is a compiler that compiles the source code of your program and generates byte code, which is platform independent. The JVM, then executes the bytecode to run the program.

  1. Is .java a valid source file name?

Yes, .java is a valid java file name as a valid java file has .java extension. To compile it, javac .java is used and run by java class_name.

  1. What are the different access specifiers in Java?

In Java, four access specifiers are used to specify the scope of a class, method or variable. These access specifiers are:

  • Public can be accessed by any class or method.
  • Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
  • Default accessible within the package only.
  • Private accessible within the class only.

  1. What are static methods and variables in JAVA?


The static methods or variables are shared among all the objects of the class. They are a part of the class and not of the object and are stored in the class area. To define variables or methods, which are common to all the objects
of the class, they are declared static.

  1. What are the advantages of Packages in Java?

Advantages of packages are as follows:

  • They the name clashes.
  • Provides easier access control
  • Hidden classes accessible only through packages
  • Locating related classes becomes easier

  1. What is object-oriented paradigm?


OOP or object oriented programming paradigm is based on objects having data and methods defined in the class. It uses the advantages of modularity and reusability. Objects are the instances of classes, which have a real world
existence.

  1. What is an object?

An object is an instance of a class and is a real-time entity having some state and behavior. This state and behavior is defined in a class. The object of a class in JAVA can be created by using the new keyword.

  1. What is a class in JAVA?


A class in JAVA is as a collection of methods and related data under a single name. It is a blueprint of objects. A JAVA program can have any number of classes. There must be at least one class in every JAVA program, as it is
necessary for execution.

  1. What will be the initial value of an object reference, which is defined as an instance variable in JAVA?

All object references are initialized to null in Java.

  1. What is a constructor?


A constructor is a special type of method that is used to initialize the state of an object. It is invoked as soon as the class is instantiated, and the memory is allocated for the object. That is whenever the new keyword is used
the constructor is automatically called. The name of the constructor is the same as the class name.

  1. How many types of constructors are there in Java?

There are two types of constructors in Java:

Default Constructor:
 Default has no parameters. It is mainly used to initialize the instance variable with the default values. It is invoked implicitly by the compiler even if there is no constructor defined in the class.

Parameterized Constructor:
 The parameterized constructor initializes the instance variables with the values given in form of parameters. A parameterized constructor initializes all the instance variables with the parameter values.

  1. Why is the main method static in Java?


No object is required to call the static method. If the main method becomes non-static, JVM must create its object first and then call main() method. This will result in extra memory allocation. Thus main method is always declared
as static in Java.

public static void main(String args[]){  

}

  1. What are instance methods in Java?


A method that is not declared as static, in a class is known as the instance method. An object is required to call the instance methods. Static variables and non-static variables both can be accessed in instance methods.

  1. Can we make constructors static?


A static method, block, or variable belongs to the class and an object is not required in invoking it. The Constructors are invoked only when the object is created, so there is no sense to make the constructors static. Also, the
compiler will show the compiler error, if one tries so.

  1. What is this keyword in java?


this keyword refers to the current object, and acts as a reference variable in Java. This keyword can be used to refer to current class properties. It can also be passed as an argument into the methods or constructors and
returned from the method as the current class instance.

  1. What is the concept of Inheritance?


Through inheritance, one object acquires all the properties and behavior of another object of another class. It offers features of Code Reusability and Method Overriding. The advantage of inheritance is new classes are built upon
the existing classes. New methods and fields can be added in the current class along with those of existing classes. Inheritance represents IS-A relationship, also known as the parent-child relationship between classes.

  1. State the different types of Inheritance in JAVA.

There are six types of inheritance in Java.

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

All the above inheritances follow IS – A relationship.

  • Containership another types of inheritance, supports HAS – A relationship.

Multiple inheritance is not supported in Java through classes, because of the Diamond Problem.

  1. State Diamond Problem in JAVA.


Suppose, a scenario where A, B, C and D are four classes. Here D, inherits C and B classes, and A is the superclass of both B and C. If B and C classes have the same method then it can be called, from D, there will be ambiguity to
call the method of B or C class. This is called diamond problem, and that is why multiple inheritance is not supported in JAVA.

  1. What is aggregation in JAVA?


Aggregation also known as Containership can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as HAS-A relationship.

Eg:

Address Class: (Containee)

public class Address {  

String city,state,country;  

public Address(String city, String state, String country) {  

    this.city = city;  

    this.state = state;  

    this.country = country;  

}  

}  

Employee Class: (Container)

public class Emp {  

int id;  

String name;  

Address address;  

  public Emp(int id, String name,Address address) {  

    this.id = id;  

    this.name = name;  

    this.address=address;  

}  

void display(){  

System.out.println(id+" "+name);  

System.out.println(address.city+" "+address.state+" "+address.country);  

}

  1. What is the super keyword in java?


The super keyword is used to refer to the immediate parent class, it is a reference variable. Whenever an instance of the subclass is created, an instance of the parent class is created implicitly which is referred by super
reference variable.

  1. What is object cloning?

The object cloning creates an exact copy of an object. The clone() method is used to clone an object. The java.lang.Cloneable
interface must be implemented by the class whose object clone is to be created, otherwise CloneNotSupportedException is generated.

  1. What is method overloading?

Method overloading is an example of polymorphism, which allows creation of multiple methods with the same name but different signature. There are two ways to overload methods in JAVA:

  • Changing the number of arguments
  • Changing the return type

  1. What is method overriding in JAVA?


When a subclass provides implementation of a method that is already specified by its parent class, it is known as Method Overriding. Thus, presence of inheritance among the classes, is a must for method overriding to work. It is
used for runtime polymorphism, and the methods must have same names and signatures in both the classes.

  1. Can the private methods be overridden in JAVA?

No, this is not possible because private methods can only be accessed in the class they are declared.

  1. What is a final variable?


A final variable restricts the user from updating itself. On initializing the final variable, its value cannot change. The final variable which is not assigned to any value on initialization, can only be assigned through the class
constructor.

  1. Can an abstract method be declared as final in JAVA?

An abstract method cannot be final as we need to override them in the subclass to give its definition.

  1. What is the difference between import java.util.Date and java.util.* ?


The second form, with an asterisk includes all the classes of the package and that may increase the compilation time. Thus, it is advisable to import only the required classes from the package by using first statement. However
importing all classes at once doesn’t have any effect run-time performance of the code.

  1. Explain the concept of Garbage collection in java?


When the objects are dynamically allocated by using the new operator, the java automatically de – allocates the memory, once there are no references for a long time to that object. This whole process is called garbage collection. It
is essential for efficient memory management.

  1. What is an exception?


Abonrmal conditions that arise during execution of the program, are known as exceptions. It may occur due to wrong user input or wrong logic written by programmer. It is essential to handle exceptions beforehand as it may cause
abrupt program termination.

  1. Which package has definitions for all the exception classes in JAVA?

Java.lang.Exception

This package contains the required definitions for all the Exceptions in JAVA.

  1. What are the different types of exceptions?

There are two types of exceptions:

Checked exceptions: must be handled by programmer otherwise the program would throw a compilation error, with abrupt program termination.

Unchecked exceptions: These do not cause any compilation errors, so it is up to the programmer to write the code whether to avoid unchecked exceptions or not.

  1. State the difference between throw and throws keywords in JAVA.

The throw keyword is used for throwing a pre-defined exception or a user defined exception in JAVA.

throws keyword appears at the end of a method’s signature and in case a method is unable to handle a checked exception, then the corresponding method must declare it using throws keyword.

  1. What is a finally block?


A Finally block is a piece of code that always executes, whether the program is running normally or abnormally. It always follows the try – catch block. No matter whether, the exception is thrown or not the code inside finally block
will always execute.

  1. What is Multithreading?

Multithreading is a process through which execution of two or more parts of a program take place simultaneously. Each of these parts is known as a thread. This is related to the concept of parallel processing.

  1. What is the advantage of multithreading in JAVA?

Multithreading ensures the maximizing usage of CPU and all other available resources. Apart from this it also reduces the CPU idle time, thus giving out quick results.

  1. What is the difference between by yield and sleep in JAVA?

yield() – causes the currently executing thread object to temporarily pause itself, and instead allows other threads to execute in its place.

sleep() – causes the current thread to sleep or suspend its execution for a specified period of time. In sleep state a thread does not releases the lock.

  1. What is a daemon thread?


A daemon thread is a special thread. It keeps JVM running even when the program finishes but the thread is still running. Example:  garbage collection that is carried out after program finishes its execution.

  1. What is Starvation of resources in threads?


Starvation is a situation in which a thread is unable to gain access to shared resources. This means the thread is unable to make any progress. This usually happens when shared resources are made unavailable for long periods by the
threads requiring an ample of resources, i.e. greedy threads.

  1. What is a deadlock condition?


Deadlock condition is a condition where two or more threads are blocked forever, waiting for each other to complete its execution. For Example, a thread A holding resource 1 requires resource 2, which in turn is held by thread B,
who requires resource 1 for execution, then this situation is a deadlock. As both of the threads are unable to continue their executions.

  1. What is the process of Serialization and de-serialization?


Serialization is a process of converting an object and its corresponding attributes to a stream of bytes. De-serialization is the reverse process of serialization that is it recreates the object from a stream of bytes.

  1. What is the difference between StringBuffer and StringBuilder class?

While, StringBuffer is thread-safe but StringBuilder is not.

StringBuilder class  is faster than StringBuffer.

StringBuffer is synchronized on the other hand StringBuilder is not synchronized.

  1. What is the difference between and Array List and a Linked List?


A Linked List stores elements within a doubly-linked list data structure. While, an Array List stores elements within a dynamically resizing array. A Linked List is preferred for performing add and update operations while an Array
List is a good choice for search operations.

  1. What is the difference between Iterator and Enumeration?


An Iterator allows to remove elements from a collection during the iteration using its remove() method. However, one cannot add or remove elements from a collection when using an enumerator. The once created the structure of an
enumerator cannot change in any case, however, no such condition exists for an iterator.

  1. What is Event handling in Java?


The process of handling any action performed by the user in an applet is known as Event Handling. There are various events associated with a particular control, and the code written to perform an action by the machine, in response
to users action is known as Event Handler code.

[2022] Core Java Interview Questions