Java is a high-level programming language used to build various apps by developers for laptops, game consoles, mobile phones, and other devices. In other words, it is a strongly-typed, general purpose, and object-oriented coding language.
The following Java Interview Questions and Answers are compiled for you to make you familiar with the types of questions you may encounter in your interview. These questions are useful for basic to medium and advanced level interviews.
Ans: Java is a high-level object-oriented coding language developed & released by Sun Microsystems in 1995. Java runs on multiple platforms like Windows, Mac OS, etc.
Ans: Java language supports multiple platforms like Windows, Mac OS, and the other editions of UNIX/Linux such as HP-Unix, Redhat Linux, Ubuntu, CentOS, and so on.
Ans: Java’s compiler produces an object file format that is architecture-neutral. It makes the arranged code to be executable on several processors including a Java runtime system.
Become a Gerrit Certified With HKR. Gerrit Training now
Ans: The language uses a Just-In-Time (JIT) compiler which is an integral part of JVM to enable high and faster performance. JIT compiler turns Java bytecode to speed up the Java application. This is a type of program that includes certain instructions that must be converted into orders that can be sent directly to the processor.
Ans: Local variables in Java are defined within the body including methods, constructors, or blocks. You can declare and initialize the variable within the same method only. And it will be removed after completion of that method.
Ans: The top features of Java include Object Oriented, Robust, Portable, Platform Independent, and Multi-threaded.
Ans: Java programming language is considered more dynamic as it is designed to adapt to an evolving environment. Java programs can move a large amount of run-time data. This data is useful to verify and work out access to objects on run-time. Further, it supports dynamic loading of various classes which are loaded on-demand basis.
Ans: Two popular Java IDEs are- Netbeans and Eclipse.
Ans: There are some useful Java keywords such as import, super, finally, for, abstract, byte, case, etc.
Ans: Instance variables are non-static variables defined inside a class but outside any method or block. Moreover, these variables are instantiated or expressed when the class is loaded.
Ans: When we compile a Java program, it is not compiled into the platform-specific system but rather into platform-independent byte code. This byte code is allocated over the web and converted by JVM which is a specification and a runtime instance, on whatever platform it runs actually.
Ans: A class in a Java program is a basic building block through which individual objects are built. Moreover, a class can include certain fields and methods that explain the object’s (variable’s) behavior.
Wish to make a career in the world of Java? Start with HKR'S Java Training !
Ans: A class in Java can contain Local variables, instance variables, and class variables.
Ans: An object is a runtime entity and a member of the Java class. It consists of a state and behavior such as a chair, bike, etc. Moreover, the object includes three different features such as state, behavior, and identity. Further, an object can be physical or logical in nature.
Ans: These variables are declared within a class, but outside of any method using the static keyword in the Java program.
Ans: A Constructor is a block of several codes which is equal to the method. It is called when a new object or instance of a class is created. Every class in Java consists of a constructor. If we do not clearly define or write a constructor for a class the java compiler then creates a default constructor for the same class.
Ans: The creation of an object includes- declaration, instantiation, and initialization.
Ans: The default byte datatype value in a Java program is 0 (zero).
Ans: A Singleton class in Java is a design pattern that makes sure that a class should contain only a single object. It controls the number to one but allows the flexibility to build more objects if the condition alters. Moreover, it is useful when a single instance of a class needs to control the actions in the entire execution.
Ans: The following are the default values of float and double data types:- for float the value is 0.0f and for double data type the value is 0.0d.
Ans: A byte data type is useful to save space in large arrays. This is mainly used in the place of integers. But a byte is 4 times smaller than an integer in reality.
Ans: The static variables have another name: Class variables that are declared with the static keyword in a class but outside of a method, or a block.
Ans: Access modifiers are useful to set access levels for classes, methods, variables, and constructors. Further, these modifiers mention the accessibility or scope of a method or a field.
Ans: The protected modifiers’ access level is within and outside the package through the child class in Java. In case, if you don’t build the child class, you can access it from outside the package.
Ans: The String class in Java is immutable or cannot be altered as once you create a String object it cannot be changed. Also, you can easily share this between many threads, and is viewed as important for multithreaded programming.
Ans: The String class is viewed as immutable as once created, it cannot be altered. If there is a need to make a lot of changes to Strings of characters then StringBuffer is highly useful.
Ans: An exception is a problem or error that occurs at the time of execution of a program. These errors/exceptions are grabbed by handlers located along the thread's method citation stack.
Ans: Inheritance is a type of process where one object obtains another object’s properties. By using inheritance, the data is made viable in a hierarchical order.
Ans: The term refers to the capability to make a class abstract in object-oriented programming. It minimizes the complexity and helps to improve the system’s maintainability.
Ans: It is the technique to integrate code and data into a single unit. Here, we can build a completely encapsulated Java class by ensuring all the data members of the class are private. Java Bean is an example of this type of Java class.
Ans: A local variable is a variable that can be defined inside a method and the scope of that variable is accessible only to its method but not outside the method. An instance variable is a variable which is defined inside a class and outside a method and the scope of that variable is accessible all throughout the class.
Ans: Pointers increases the complexity of a program and becomes unsafe. Hence the java code is designed with simplicity by avoiding the pointers that will become contradicting. JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.
Ans: The features are.
1. The concepts of OOPs that include
2. Platform Independent - A single program runs on different platforms without any modifications.
3. High Performance - JIT (Just in Time compiler) enables high performance which converts the bytecode into machine language and then JVM starts the execution.
4. Multi-threading - Thread is a flow of execution. JVM creates a thread that is called the main thread. A user can able to create multiple threads by extending the thread class or by implementing the Runnable interface.
Ans: The concepts of OOPs are:
Inheritance: It is a process by which one class acquires the properties of another class.
Encapsulation: It is a mechanism of wrapping the code and data as a single unit.
Abstraction: It is a methodology for hiding the implementation details and proving only the functionality to the users.
Polymorphism: It is the ability of a variable, function or object to take multiple forms.
Difference between Public and Private access specifiers is as follows:
Public access specifier:
When we declare any variable, class or method with ‘public’, then they can be accessed anywhere. There is no scope restriction for public access modifiers. They can be accessed within the same package and also in other packages.
public class Main {
public String mailid = "[email protected]";
public int age = 26;
}
class Second {
public static void main(String[] args) {
Main myObj = new Main();
System.out.println("mailid: " + myObj.mailid);
System.out.println("Age: " + myObj.age);
}
}
Output:
mailid: [email protected]
Age: 26
Private access specifier:
When we declare a method or variable as ‘private’, we cannot access them outside that class. It can be accessed only within that declared class.
public class Main {
private String name = "Anu";
private String surname = "Wills";
private int age = 26;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println("Name: " + myObj.name + " " + myObj.surname);
System.out.println("Age: " + myObj.age);
}
}
Output:
Name: Anu Wills
Age: 26
Difference between Default and Protected access specifiers is as follows:
Default access specifier:
When an access specifier is not specified for a class, data member or method, then by default it will have a ‘default’ access modifier. They can be accessible within the same package. It cannot be accessed in other packages.
class Person {
String mailid = "[email protected]";
int age = 26;
public static void main(String[] args) {
Person myObj = new Person();
System.out.println("mailid: " + myObj.mailid);
System.out.println("Age: " + myObj.age);
}
}
Output:
mailid: [email protected]
Age: 26
Protected access specifier: Whenever we specify a data member or a method with ‘protected’, then they can be accessed within the same class and same package. Classes or interfaces cannot be declared as protected in Java.
class Person {
protected String name = "Anu";
protected int age = 26;
}
class Student extends Person {
private int graduatedin = 2016;
public static void main(String[] args) {
Student myObj = new Student();
System.out.println("Name: " + myObj.name);
System.out.println("Age: " + myObj.age);
System.out.println("Graduation Year: " + myObj.graduatedin);
}
}
Output:
Name: Anu
Age: 26
Graduation Year: 2016
Ans:
Break:
Example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
System.out.println(i);
}
Continue:
Example:
for (int i = 0; i < 10; i++)
{
if(i == 5)
{
continue;
}
System.out.println(i);
}
Ans: The final keyword is used as a non-access modifier. A final variable is used in different contexts of a class, method and a variable.
Ans:
Ans:
Methods:
Constructors:
Ans:
Abstract class:
Interface:
Ans: No. You cannot override private or static methods. If a similar method is created with the same return type and the same method arguments in child class then it hides the superclass method which is also called as method hiding. Similarly, the private method is not overridable in sub-class which is not accessible there. You can create another private method with the same name in the child class as in this example.
Example:
class Base_class {
private static void display() {
System.out.println("Static or class method from Base class");
}
public void print() {
System.out.println("Non-static or instance method from Base class");
}
class Derived_class extends Base_class {
private static void display() {
System.out.println("Static or class method from Derived class");
}
public void print() {
System.out.println("Non-static or instance method from Derived class");
}
public class test {
public static void main(String args[])
{
Base obj= new Derived_class();
obj.display();
obj.print();
}
}
Ans: An error is an unrecoverable condition that occurs at runtime such as “OutofMemory” error. These JVM errors cannot be repaired at runtime. Even though such kind of errors is caught in the catch block, the application execution will be halted and becomes unrecoverable.
The exceptions are the conditions which applied for tracking the bad input to human error while executing a program. For example “FileNotFoundException” is an exception which is thrown to handle if the specified file doesn’t exist.
Ans:
Serialization:
Deserialization:
Ans: When a class is needed for extending some other classes other than a thread then it is recommended to implement the runnable interface because java can extend only one class. If there is no need for extending any class than at the time it is recommended to extend the properties of a thread class.
Ans:
HashMap:
HashTable:
Ans:
HashSet:
TreeSet:
Ans: Collections are the framework which is designed to store and manipulate the objects. The collections perform the following operations.
A group of objects are known as collections. All the collection classes and interfaces are available in the util package.
Ans: Ordered collections are values which are stored in a collection that is based on the values that are added to the collection. These values can be iterated from the collection in a specific order.
The sorted collection mechanism is applied internally or externally such that the group of objects sorted in a particular collection is based on the properties of objects.
Ans:
ArrayList:
Vector:
Ans:
Array:
ArrayList:
Ans:
Get method:
Post method:
Ans: Session is a state of a conversation between client and server and consists of multiple requests and responses. As HTTP and Web Server are stateless, the session maintains the unique information as session-id that is passed between client and server in every request and response. The methods of session management in servlets are:
Ans: JDBC is a software component which enables the application to interact with the database. There are mainly four types of JDBC drivers.
Ans: These are the statements which are used to send SQL commands to the database and also retrieve the data from the database. The methods like execute(), executeUpdate() and executeQuery() are provided by JDBC to interact with the database.
JDBC supports three types of statements.
Ans: There are three lifecycle methods in JSP.
Ans: JSP is a technology of the server’s side programming with simple content generation. JSP’s are document-centric while Servlets are programs. A JSP page contains the fragments of java programming code which executes and instantiates Java classes. However, they occur inside an HTML template file. It provides the framework for the development of a Web Application.
Ans: Synchronization is about handling only one thread for accessing a block of code at a time. While multiple threads access the block of code then there are chances for inaccurate results, in the end, to avoid this the synchronization is provided for the sensitive block of code. The keyword “synchronized” means that a thread requires a key to access the synchronized code.
It locks as per object. Each Java object has a lock which has only one key. A thread can access a synchronized method only if the thread can get a key to the objects to lock. Towards this, the “Synchronization” keyword will be used.
Example:
public class ExampleThread implements Runnable{
public static void main (String[] args){
Thread t = new Thread ();
t.start ();
}
public void run(){
synchronized(object){
{
}
}
Ans: The modules of spring framework include.
Ans: Dispatcher Servlet is a front controller in Spring MVC application which loads the spring bean configuration file and initializes all beans which are configured. By enabling the annotations it allows scanning the packages to configure any bean annotated with @Component, @Controller, @Repository or @Service annotations.
ContextLoaderListner is a listener that can start and shut the “WebApplicationContext” in spring root. Its main function ties the lifecycle of Application Context to the lifecycle of the ServletContext and automating the creation of ApplicationContext.
Batch starts on 26th Sep 2023, Weekday batch
Batch starts on 30th Sep 2023, Weekend batch
Batch starts on 4th Oct 2023, Weekday batch