dridhOn

Java Interview Questions and Answers

Java Interview Questions and Answers

1. What makes Java a platform-agnostic language?
Because the compiler compiles the code and then converts it to platform-independent byte code that can be run on many platforms, the Java language was designed to be independent of any hardware or software.

The sole need for running the byte code is that the machine is equipped with a Java runtime environment (JRE).

2. What makes Java different from other object-oriented languages?
Java is not a pure object-oriented language because it supports primitive data types such as byte, boolean, char, short, int, float, long, and double.

 

3. In Java, what is the difference between heap and stack memory? And how Java makes use of it.
Stack memory is a type of memory that is used to store data. Each program was given a certain amount of memory. And the problem was resolved. Heap memory, on the other hand, is the piece of memory that was not allocated to the java program but will be accessible for use by the java program when it is needed, which is usually during the program's runtime.

 

4. Can java be described as an object-oriented programming language in its entirety?
If we say that java is the entire object-oriented programming language, we are not wrong. Because classes are the foundation of Java. We can gain access to this by constructing objects.

However, because it supports primitive data types such as int, float, char, boolean, double, and others, we can claim that java is not a fully object-oriented programming language.

Is Java an entirely object-oriented programming language? Because it allows direct access to primitive data types, we can say that Java is not a pure object-oriented programming language. Furthermore, these primitive data types are not directly related to the Integer classes.

 

5. What distinguishes Java from C++?
Java is both a compiled and an interpreted language, whereas C++ is solely a compiled language.
Java applications run on any machine, whereas C++ programs can only execute on the machine where they were compiled.
In C++, users can use pointers in their programs. Java, on the other hand, does not enable it. Internally, Java makes use of pointers.
Multiple inheritances are supported in C++, however, they are not supported in Java. The diamond dilemma arises from the need to avoid the complexities of name ambiguity.

6. In C/C++, pointers are used. Why is it that Java doesn't use pointers?
Beginner programmers should avoid using pointers because they are fairly difficult. The use of pointers might be useful in Java because it focuses on code simplicity. 

Make it difficult. The use of a pointer might also lead to mistakes. Furthermore, when pointers are utilized, security is undermined since pointers allow people to directly access memory.

By not including pointers in Java, a certain amount of abstraction is provided. Furthermore, the use of pointers might make garbage collection time-consuming and inaccurate. References are used in Java because, unlike pointers, they cannot be changed.

 

7. Can you explain what an instance variable and a local variable are?
Instance variables are variables that are available to all of the class's methods. They are declared both outsides and inside the methods. These variables describe an object's attributes and are inextricably linked to it.

All of the class's objects will have a copy of the variables to use. If any changes are made to these variables, just that instance will be affected, while all other class instances would stay unchanged.

 

8. In Java, what are the default values for variables and instances?
In Java, no default values are assigned to variables. Before we can use the value, we must first initialize it. Otherwise, a compilation error will be thrown (the Variable might not be initialized).
However, if we build the object, the default value will be set by the default function Object() { [native code] }, which will be determined by the data type.
If the value is a reference, it will be set to null.
If it's a number, it'll be assigned to 0.
If the value is a boolean, it will be set to false.

 

9. What exactly do you mean when you say "data encapsulation"?
Data encapsulation is an Object-Oriented Programming paradigm that encapsulates data properties and behaviors into a single unit.
It aids developers in adhering to modularity when designing software by ensuring that each object is self-contained, with its methods, characteristics, and functionalities.
It is used to protect an object's private properties and so serves the aim of data concealing.

 

9. What exactly do you mean when you say "data encapsulation"?
Data encapsulation is an Object-Oriented Programming paradigm that encapsulates data properties and behaviors into a single unit.
It aids developers in adhering to modularity when designing software by ensuring that each object is self-contained, with its methods, characteristics, and functionalities.
It is used to protect an object's private properties and so serves the aim of data concealing.

 

10. Tell us about the JIT compiler.
JIT stands for Just-In-Time, and it is a performance optimization technique that is used to improve efficiency during runtime. Its job is to compile bits of byte code with similar functionality at the same time, minimizing the amount of time the code takes to compile and run.
The compiler is nothing more than a tool for converting source code into machine-readable code. But what makes the JIT compiler unique? Let's take a look at how it works:
The javac compiler is used to convert Java source code (.java) to byte code (.class) for the first time.
The. class files are then loaded by JVM at runtime and translated to machine-readable code with the help of an interpreter.
The JIT compiler (just-in-time compiler) is a component of the JVM When the JIT compiler is enabled, the JVM analyses and compiles method calls in.class files to produce more efficient and native code. It also ensures that the method calls that are prioritized are optimized.
After completing the preceding step, the JVM executes the optimized code directly rather than reinterpreting it. This improves the execution's efficiency and speed.

 

10. Describe function Object() { [native code] } overloading in a few words.

Constructor overloading is the process of creating numerous constructors with the same name but different function Object() { [native code] } parameters in the same class. The compiler distinguishes the different types of constructors based on the number of parameters and their related types.

 

11. How is an infinite loop declared in Java?

Infinite loops are those loops that run infinitely without any breaking conditions. Some examples of consciously declaring an infinite loop are:

Using For Loop:
for (;;)
{
// Business logic
// Any break logic
}
Using while loop:
while(true){
// Business logic
// Any break logic
}
Using the do-while loop:
do{
// Business logic
// Any break logic
}while(true);

13. Briefly explain the concept of constructor overloading
Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending upon the number of parameters and their corresponding types, distinguishing the different types of constructors is done by the compiler.

class Hospital {
int variable1, variable2;
double variable3;
public Hospital(int doctors, int nurses) {
variable1 = doctors;
variable2 = nurses;
}
public Hospital(int doctors) {
variable1 = doctors;
}
public Hospital(double salaries) {
variable3 = salaries

14. Define Copy constructor in java.
Copy Constructor is the constructor used when we want to initialize the value to the new object from the old object of the same class.

class InterviewBit{
String department;
String service;
InterviewBit(InterviewBit ib){
this.departments = ib.departments;
this.services = ib.services;
}
}
Here we are initializing the new object value from the old object value in the constructor. Although, this can also be achieved with the help of object cloning.

Follow Us on!

How can we help you?

To request a quote or want to meet up for a course discussion, contact us directly or fill out the form and we will get back to you promptly.