Basic Java Interview Questions

Modified Date:- Published Date:-

Categories: JAVA

Q.1. Explain the term “Double Brace Initialization” in Java?

 

Ans. Double Brace Initialization is a Java term that refers to the combination of two independent processes. There are two braces used in this. The first brace creates an anonymous inner class. The second brace is an initialization block. When these both are used together, it is known as Double Brace Initialization. The inner class has a reference to the enclosing outer class, generally using the ‘this’ pointer. It is used to do both creation and initialization in a single statement. It is generally used to initialize collections. It reduces the code and also makes it more readable.

 

Q.2. Why is it said that the length() method of String class doesn’t return accurate results?

 

Ans. The length() method of String class doesn’t return accurate results because it simply takes into account the number of characters within in the String. In other words, code points outside of the BMP (Basic Multilingual Plane), that is, code points having a value of U+10000 or above, will be ignored.

The reason for this is historical. One of Java’s original goals was to consider all text as Unicode; yet, Unicode did not define code points outside of the BMP at the time. It was too late to modify char by the time Unicode specified such code points.

 

Q.3. What are the differences between Heap and Stack Memory in Java?

 

Ans. The major difference between Heap and Stack memory are:

Features

Stack

Heap

Memory

Stack memory is used only by one thread of execution.

Heap memory is used by all the parts of the application.

Access

Stack memory can’t be accessed by other threads.

Objects stored in the heap are globally accessible.

Memory Management

Follows LIFO manner to free memory.

Memory management is based on the generation associated with each object.

Lifetime

Exists until the end of execution of the thread.

Heap memory lives from the start till the end of application execution.

Usage

Stack memory only contains local primitive and reference variables to objects in heap space.

Whenever an object is created, it's always stored in the Heap space.

 

Q.4. What is a package in Java? List down various advantages of packages.

 

Ans. Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below a few of its advantages:

(i) Packages help in avoiding name clashes

(ii) They provide easier access control on the code

(iii) Packages can also contain hidden classes which are not visible to the outer classes and only used within the package

(iv) Creates a proper hierarchical structure which makes it easier to locate the related classes

 

Q.5. Why pointers are not used in Java?

 

Ans. Java doesn’t use pointers because they are unsafe and increases 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.

 

Q.6. What is JIT compiler in Java?

 

Ans. 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.

 

Q.7. What are access modifiers in Java?

 

Ans. In Java, access modifiers are special keywords which are used to restrict the access of a class, constructor, data member and method in another class. Java supports four types of access modifiers:

(i) Default

(ii) Private

(iii) Protected

(iv) Public

 

Q.8. What is an object in Java and how is it created?

 

Ans. An object is a real-world entity that has a state and behavior. An object has three characteristics:

  1. State
  2. Behavior
  3. Identity

An object is created using the ‘new’ keyword. For example:

ClassName obj = new ClassName();

 

Q.9. What is Object Oriented Programming?

 

Ans. Object-oriented programming, or OOP, is a programming model or approach in which programs are organized around objects rather than logic and functions. In other words, OOP mainly focuses on the objects that need to be manipulated instead of logic. This approach is ideal for large and complex programs and needs to be actively updated or maintained.

 

Q.10. What are the main concepts of OOPs in Java?

 

Ans. Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:

(i) Inheritance: Inheritance is a process where one class acquires the properties of another.

(ii) Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.

(iii) Abstraction: Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.

(iv) Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms.