Top Java Interview Questions

Java Interview Questions
Top Java Interview Questions

a) What is JAVA?
Answer: Java is a high-level programming language and is platform-independent. It is a collection of objects and was developed by Sun Microsystems. There are a lot of applications, websites, and games that are developed using Java.

b) What are the features of JAVA?
Answer: Features of Java are mentioned below:
OOP concepts

i) Platform independent: A single program works on different platforms without any modification.
ii) High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into machine language and then JVM starts the execution.
iii) Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called the main thread. The user can create multiple threads by extending the thread class or by implementing the Runnable interface.

c) What is a singleton class in Java and how can we make a class singleton?
Answer: Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

d) Why are pointers not used in Java?
Answer: Java doesn’t use pointers because they are unsafe and increase the complexity of the program. Since Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user,  pointers are discouraged in Java.

e) What is a JIT compiler in Java?
Answer: JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.

f) What is Object-Oriented Programming?
Answer: Object-oriented programming or popularly known as OOPs is a programming model or approach where the programs are organized around objects rather than logic and functions. In other words, OOP mainly focuses on the objects that are required to be manipulated instead of logic. This approach is ideal for the program’s large and complex codes and needs to be actively updated or maintained.

g)  What are the main concepts of OOPs in Java?
Answer: Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:
a) Inheritance: Inheritance is a process where one class acquires the properties of another.
b) Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
c) Abstraction: Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.
d) Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms.

j) What is the final keyword in Java?
Answer: Final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:
Final variable- When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

final method- When a method is declared final then it can’t be overridden by the inheriting class.

Final class- When a class is declared as final in Java, it can’t be extended by any subclass but it can extend other class.

i) Why are Java Strings immutable in nature?
Answer: In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances the security, caching, synchronization, and performance of the application. 

Gyani Labs