0% found this document useful (0 votes)
5 views

Basic Java Interview Questions

The document provides a series of basic Java interview questions and answers covering key concepts such as JDK, JRE, JVM, the main method, platform independence, object-oriented principles, wrapper classes, constructors, singleton classes, JIT compiler, access specifiers, static methods, and the definition of an object. It explains the roles and characteristics of these concepts in Java programming. The content is structured as a Q&A format, making it a useful resource for interview preparation.

Uploaded by

luxmirajput290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basic Java Interview Questions

The document provides a series of basic Java interview questions and answers covering key concepts such as JDK, JRE, JVM, the main method, platform independence, object-oriented principles, wrapper classes, constructors, singleton classes, JIT compiler, access specifiers, static methods, and the definition of an object. It explains the roles and characteristics of these concepts in Java programming. The content is structured as a Q&A format, making it a useful resource for interview preparation.

Uploaded by

luxmirajput290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Basic Java Interview Questions

Q1. Explain JDK, JRE and JVM?

JDK vs JRE vs JVM

JDK JRE JVM

It stands for
It stands for
Java
Java Runtime It stands for Java Virtual Machine.
Development
Environment.
Kit.
It is the tool JRE refers to a
necessary to runtime It is an abstract machine. It is a
compile, environment in specification that provides a run-time
document and which Java environment in which Java bytecode can
package Java bytecode can be be executed.
programs. executed.
It’s an
It contains JRE
implementation JVM follows three notations:
+
of the JVM which Specification, Implementation, and Ru
development
physically ntime Instance.
tools.
exists.
Q2. Explain public static void main(String args[]) in Java.

main() in Java is the entry point for any Java program. It is always written
as public static void main(String[] args).

 public: Public is an access modifier, which is used to specify who can


access this method. Public means that this Method will be accessible
by any Class.
 static: It is a keyword in java which identifies it is class-based. main()
is made static in Java so that it can be accessed without creating the
instance of a Class. In case, main is not made static then the compiler
will throw an error as main() is called by the JVM before any objects
are made and only static methods can be directly invoked via the
class.
 void: It is the return type of the method. Void defines the method
which will not return any value.
 main: It is the name of the method which is searched by JVM as a
starting point for an application with a particular signature only. It is
the method where the main execution occurs.
 String args[]: It is the parameter passed to the main method.
Q3. Why Java is platform independent?

Java is called platform independent because of its byte codes which can run
on any system irrespective of its underlying operating system.

Q4. Why Java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive


data types such as boolean, byte, char, int, float, double, long, short which
are not objects.

Q5. What are wrapper classes in Java?

Wrapper classes convert the Java primitives into the reference types
(objects). Every primitive data type has a class dedicated to it. These are
known as wrapper classes because they “wrap” the primitive data type into
an object of that class. Refer to the below image which displays different
primitive type, wrapper class and constructor argument.

Q6. What are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an


object. It must have the same name as that of the class. Also, it has no
return type and it is automatically called when an object is created.

There are two types of constructors:

1. Default Constructor: In Java, a default constructor is the one which


does not take any inputs. In other words, default constructors are the
no argument constructors which will be created by default in case you
no other constructor is defined by the user. Its main purpose is to
initialize the instance variables with the default values. Also, it is
majorly used for object creation.
2. Parameterized Constructor: The parameterized constructor in Java,
is the constructor which is capable of initializing the instance variables
with the provided values. In other words, the constructors which take
the arguments are called parameterized constructors.

Q7. What is singleton class in Java and how can we make a class
singleton?

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.
Q 8.What is JIT compiler?

Just-In-Time(JIT) compiler: It is used to improve the performance. JIT


compiles parts of the bytecode that have similar functionality at the same
time, and hence reduces the amount of time needed for compilation. Here
the term “compiler” refers to a translator from the instruction set of a Java
virtual machine (JVM) to the instruction set of a specific CPU.

Q9 If I don't provide any arguments on the command line, then


what will the value stored in the String array passed into the main()
method, empty or NULL?

It is empty, but not null.

Q10 What if I write static public void instead of public static void?

The program compiles and runs correctly because the order of specifiers
doesn't matter in Java.

Q11. What is the default value of the local variables?

The local variables are not initialized to any default value, neither primitives
nor object references.

Q12. What are the various access specifiers in Java?

In Java, access specifiers are the keywords which are used to define the
access scope of the method, class, or a variable. In Java, there are four
access specifiers given below.

Public :The classes, methods, or variables which are defined as public, can
be accessed by any class or method.

Protected :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 : Default are accessible within the package only. By default, all the
classes, methods, and variables are of default scope.

Private : The private class, methods, or variables defined as private can be


accessed within the class only.

Q13. What is the purpose of static methods and variables?


The methods or variables defined as static are shared among all the objects
of the class. The static is the part of the class and not of the object. The
static variables are stored in the class area, and we do not need to create
the object to access such variables. Therefore, static is used in the case,
where we need to define variables or methods which are common to all the
objects of the class.

For example, In the class simulating the collection of the students in a


college, the name of the college is the common attribute to all the students.
Therefore, the college name will be defined as static.

Q 14. What is an object?

The Object is the real-time entity having some state and behavior. In Java,
Object is an instance of the class having the instance variables as the state
of the object and the methods as the behavior of the object. The object of a
class can be created by using the new keyword.

Q15. What is the constructor?

The constructor can be defined as the special type of method that is used to
initialize the state of an object. It is invoked when the class is instantiated,
and the memory is allocated for the object. Every time, an object is created
using the new keyword, the default constructor of the class is called. The
name of the constructor must be similar to the class name. The constructor
must not have an explicit return type.

You might also like