Core Java Interview Questions
Core Java Interview Questions
Interview Questions
Answers
Created by Foram_Chudasama
1. What is Java?
In the year 1991, a small group of engineers called ‘Green Team’ led by James
Gosling, worked a lot and introduced a new programming language called
“Java”. This language is created in such a way that it is going to revolutionize
the world.
In today’s World, Java is not only invading the internet, but also it is an
invisible force behind many of the operations, devices, and applications.
C- Language Java
Functions play a major role in the Objects play a major role in the
C language. Java language.
Created by Foram_Chudasama
It is a middle-level language It is a high-level language.
Java is the first programming language that is used to write code on a virtual
machine, so that is the reason why it is called JVM (Java Virtual Machine). This
JVM is a new concept that is introduced in Java. It also provides a new feature
called code reusability which is not possible in C.
Java C++
Created by Foram_Chudasama
Goto statement is not supported by
C++ supports the goto statement
Java
Java doesn’t support structures and C++ does support unions and
unions structures
Java is used for building applications C++ is used for system programming
Created by Foram_Chudasama
• Portable: Java provides no implementation aspects for the specifications
which make Java portable.
• Multithreaded: By this feature, we can perform multiple tasks
simultaneously in Java.
• High-Performance: With the built-in Just-in-Time compiler, Java provides
high performance.
• Robust: Java makes more efforts to eliminate errors by concentrating more
on runtime checking and compile-time check.
The instance of a class is called an object. Every object in Java has both state
and behavior. The state of the object is stored in fields and the behavior of the
objects is defined by methods.
class Mindmajix
System.out.println("Hello World");
Created by Foram_Chudasama
10. Define JVM?
JVM is the abbreviation for Java Virtual Machine. It is a virtual machine that
provides a runtime environment to write code. JVM is a part of JRE (Java
Runtime Environment) and is used to convert the bytecode into machine-level
language. This machine is responsible for allocating memory.
11. Name the memory areas that are allocated by JVM and explain the
class loader in JVM?
There are totally five memory areas that are allocated by the JVM, and they are:
• Class Area
• Heap
• Stack
• Native Method Stack
• PC Register
• Application Classloader
• Bootstrap Classloader
• Extension Classloader
Created by Foram_Chudasama
12. What is JDK?
Java Development Kit is one of the three prominent technology packages used
in Java programming. JDK is used as a standalone component to run the Java
programs by JVM and JRE. This kit is used to implement Java platform
specifications, including class libraries and compiler.
14. What is the significant difference between JVM, JRE, and JDK?
We can understand the difference between JVM, JDK, and JRE by the
following diagram:
• JVM: Java Virtual Machine is the main part of Java programming, which
provides platform independence. JRE and JDK both contain JVM in order
to run our Java programs.
• JDK: This development kit is mainly used for developing programs.
• JRE: Java Runtime Environment is mainly used for running Java
programs.
Just In Time Compiler is the component of JRE, which is used to compile the
bytecodes of the particular method into the native machine code. This compiled
code of the method is directly called by JVM without interpreting it
Created by Foram_Chudasama
16. Define variables in Java and explain with example?
For Example:
int a = 10;
There are mainly three different types of variables available in Java, and they
are:
• Static Variables
• Local Variables
• Instance Variables
Static Variables: A variable that is declared with the static keyword is called a
static variable. A static variable cannot be a local variable, and the memory is
allocated only once for these variables.
Local Variables: A variable that is declared inside the body of the method
within the class is called a local variable. A local variable cannot be declared
using the static keyword.
Instance Variables: The variable declared inside the class but outside the body
of the method is called the instance variable. This variable cannot be declared as
static and its value is instance-specific and cannot be shared among others.
Example:
Created by Foram_Chudasama
class A{
void method(){
}//end of class
byte -> short -> char -> int -> long -> float -> double
double -> float -> long -> int -> char -> short -> byte
Type conversion can be defined as converting one data type to another data type
automatically by the compiler.
Created by Foram_Chudasama
20. What are the data types in Java?
Datatypes in Java specify the values and sizes that can be stored in the
variables. There are mainly two types of data types; they are:
Primitive data types in Java are the major constituents of data manipulation.
These are the most basic data types that are available in Java. The primitive data
types include int, char, byte, float, double, long, short, and boolean.
22. What are the default values and sizes of primitive data types?
int 4 bytes 0
byte 1 byte 0
byte 2 bytes 0
long 8 bytes 0L
Created by Foram_Chudasama
23. What are non-primitive data types?
The non-primitive data types are something that is different from primitive data
types, and these non-primitive data types include String, arrays, and structures.
24. Why char uses 2 bytes in Java and what is this ‘u0000’ notation called?
This is only due to the reason that Java uses the Unicode system. The notation
‘u0000’ is the lowest range of the Unicode system, and it is the default value of
the char data type.
For example:
Created by Foram_Chudasama
It is because the Java compiler converts the code into byte code which is the
main transitional language between machine code and source code. This byte
code is not platform-dependent so that it can be compiled and executed on any
platform.
29. Is exit, null, delete, main, and next are the keywords in Java?
Both the compilation and execution of the programs are done correctly because
in Java specifiers order doesn’t matter.
1. Arithmetic operators
2. Assignment operators
3. Logical operators
4. Relational operators
5. Bitwise operators
6. Unary operators
7. Ternary operators
8. Shift operators
Created by Foram_Chudasama
Java provides a set of rules and regulations for particularly specifying the order
in which operators are evaluated. If the expression has many numbers of
operators then the operator precedence comes into action. This operator
precedence evaluates the operators present in the expressions based on the
priority. For example, multiplication has the highest priority when compared to
addition and subtraction.
Operator Description
Created by Foram_Chudasama
This type of operator has only one operand and is mainly used to perform
various operations including negating an expression, either incrementing/
decrementing the value by one, and invention on boolean values.
class UnaryExample{
int x=15;
System.out.println(x++);//15 (16)
System.out.println(++x);//17
System.out.println(x--);//12 (16)
System.out.println(--x);//15
}}
36. What is the difference between ++a and a++ increment operators?
++a is a prefix increment and a++ is the postfix increment. The prefix increment
is used to return the value after incrementing the present value. Whereas in
postfix increment, the value is returned before incrementing it.
Left Shift: This left shift is a bitwise operator in which bits are moved towards
the left side and zeros are placed at the rightmost places.
Example:
Created by Foram_Chudasama
public static void main(String[] args) {
int a=2;//
int i;
i=a<<1;//4
Output: 4
Right Shift: It is also of the bitwise operator in which bits are moved towards
the right-hand side and zeros are places at the leftmost places.
Example:
int a=2;
int i;
Created by Foram_Chudasama
i=a>>1;
Output: 1
Bitwise operators are mainly used to work on bits and these operators continue
to work on bit-by-bit operations.
The following are the bitwise operators in Java, and they are:
• Bitwise OR (A&B)
• Bitwise AND (A|B)
• Bitwise XOR (A^B)
• Bitwise Complement (~A)
• Left shift ( A<<2)
• Right shift (A>>2)
• Unsigned left shift ( <<<)
• Unsigned right shift (>>>)
Created by Foram_Chudasama
variable= (expression) ? expression true : expression false
Yes, Java allows us to save our Java file by .java only, we need to compile it by
javac.java and run it by Java class name.
For example:
class A{
System.out.println("Hello Mindmajix");
Java keywords are also called “Reserved keywords” that act as a key to a code.
Keywords in Java are predefined that cannot be used as an object name or
variable. There are many keywords in Java, and some of them are:
• abstract
• default
• new
• This
• static
• Extends and many more
Created by Foram_Chudasama
There are four access specifiers present in Java, and they are:
1. Public: The methods, classes, and variables that are defined as the public
can be accessed by any class or method.
2. Private: The methods or classes which are declared as private can be
accessible within the same class only.
3. Protected: The variables, methods, and classes which are defined as private
can be accessed within the same class of the same package or by the
subclass of the same class.
4. Default: By default, all the classes, variables, and methods are of default
scope. The default is accessible within the package only.
The following are the advantages of packages in Java, and they are:
class Java
System.out.println(10 * 50 + "Mindmajix");
System.out.println("Mindmajix" + 10 * 50);
Created by Foram_Chudasama
}
Output:
500Mindmajix
Mindmajix500
In Java control statements are divided into three types. They are:
• Selection Statements
• Iterative/looping Statements
• Jump Statements.
• If statement
• If-else statement
• Switch statements
The iterative statements in Java are also called looping statements, these
statements are the set of statements that repeat continuously until the condition
for the termination is not met.
Created by Foram_Chudasama
Looping/iterative statements in Java include:
• For loop
• While loop
• Do-while loop
In Java, jump statements are mainly used to transfer control to another part of
our program depending on the condition. Moreover, these statements are used to
jump directly to other statements.
• Break and Continue are the two jump statements present in Java.
For-each is another kind of array traversing technique in Java which is the same
as that of for loop, while loop. It is most commonly used to iterate over a
collection or an array such as ArrayList. An example for for-each loop is as
follows:
class ForEachPro{
//declaring an array
int arr[]={12,13,14,44};
for(int i:arr){
System.out.println(i);
Created by Foram_Chudasama
}
Output:
12
12
14
44
50. What is the difference between a while loop and a do-while loop
In the case of a while loop the condition is tested first and then if the condition
is true then the loop continues if not it stops the execution. Whereas in the case
of the do-while loop first the condition is executed and at the end of the loop,
the condition is tested.
while(condition){
//code to be executed
Java comments are statements that are not executed by the interpreter and
compiler. These are used to provide information about the class, variables,
methods, and any statements. Comments are mainly used to hide program code
for a specific time.
There are mainly three types of comments in Java, and they are:
• Documentation comment
Created by Foram_Chudasama
• Multi-line comment
• Single-line comment
The following are the OOPs concepts that are included in Java:
• Class
• Object
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Binding data and code together into a single unit are called encapsulation. The
capsule is the best example of encapsulation.
Created by Foram_Chudasama
When an object of child class has the ability to acquire the properties of a parent
class then it is called inheritance. It is mainly used to acquire runtime
polymorphism and also it provides code reusability.
59. What will be an initial value for an object reference that is defined as
an instance variable?
Created by Foram_Chudasama
All the object references in Java are initialized to null.
Created by Foram_Chudasama
{
//code snippet
64. What all the rules must be followed while creating a constructor in
Java?
• The constructor should have the same name as its class name.
• There is no explicit return type for a constructor.
• In Java, a constructor cannot be synchronized, abstract, final, and static.
• Default constructor
• Parameterized constructor
Created by Foram_Chudasama
Parameterized constructor: A parameterized constructor is one type of
constructor which is mainly used to initialize the instance variables with the
given values. In simple words, the constructor that accepts arguments is called a
parameterized constructor.
class Mindmajix1{
Mindmajix1()
System.out.println("Welcome to Mindmajix");
//main method
Created by Foram_Chudasama
//Java Program to demonstrate the use of the parameterized constructor.
class Training{
int id;
String name;
id = i;
name = n;
void display()
System.out.println(id+" "+name);
t1.display();
t2.display();
Created by Foram_Chudasama
Output:
111 DevOps
222 Oracle
Yes, the constructor will return the current or present instance of the class.
71. Is there any Constructor class available in Java and what is its purpose
of it?
Yes, there is a class called Constructor class available in Java. The purpose of
the Constructor class is to get the internal information of the constructor in the
class. It is present in java.lang.reflect package.
Created by Foram_Chudasama
72. What is the use of a copy constructor in Java?
There is no copy constructor in Java, we can copy the values from one object to
another same as that of a copy constructor in C++. In Java, there are many ways
to copy the values of one object to another and they are:
In Java method is defined as a set of code that is represented by a name and can
be invoked at any point in a program with the help of the method name. Each
and every method in the program has its own name which is not the same as that
of a class name.
Constructor Method
The constructor should have the same The method name is not the same as that
name as the class name of the class name
A constructor has no return type The method must have a return type
Created by Foram_Chudasama
The constructor is used to initialize the A method is used to expose the behavior
state of the object of an object
In Java method signature is given the specified format followed by the method
name, type, and order of its parameters. Exceptions are not considered as a part
of the method signature.
//code
Keyword static in Java is mainly used for memory management and we can
declare block, variable, method, and nested class as static.
• A static variable can be mainly used to refer to all the common properties of
objects.
• Memory for the static variable is assigned only once in the class area at the
time of class loading
Created by Foram_Chudasama
Example for static variable:
class Mindmajix1{
int ID;
ID = i;
employee name = n;
void display ()
m1.display();
m2.display();
Output:
Created by Foram_Chudasama
346 Pranaya Appmajix
If we declare a method as static, the following operations take place, and they
are:
79. What are the cons observed if we declare a method as static in Java?
• A static method cannot access non-static members and also cannot call the
non-static method directly.
• This and super keywords cannot be used in the context of the static method
as they are non-static.
The main reason is that the object is not required to call for a static method so, if
we declare the main method as non-static we need to create an object first and
then call the main() method. In order to save memory, we declare the main
method as static in Java.
Created by Foram_Chudasama
82. What is a static block in Java?
Static block in Java is mainly used to initialize the static data members. The
specialty of the static block is that it is executed before the main method at the
time of class loading.
Class Mindmajix{
static{System.out.println("static block");
System.out.println("Hello World");
Output:
static block
Hello World
Yes, we can execute the program in Java without the main method using a static
block. It was possible only till JDK 1.6 and from JDK 1.7 it is not possible to
execute the program without the main method in Java.
84. What happens if we remove the static modifier from the signature of
the main() method?
Created by Foram_Chudasama
The program will be compiled, but at runtime, it throws NoSuchMethodError
error.
As we know that the static context is suitable for only class, variable, and
method, not for the object. So the constructors are invoked only when an object
is created, so there is no possibility to declare the constructor as static in Java.
No, if we declare abstract methods as static it becomes a part of the class, and
we can directly call it which is not required. Calling an undefined method is
unnecessary. Therefore declaring an abstract method as static is not allowed.
87. Can we declare static methods and variables in the abstract class?
Yes, as we know that there is no need for an object to access the static block,
therefore we can access static methods and variables declared inside the abstract
class by using the abstract class name. Consider the following example.
Created by Foram_Chudasama
{
Check.CheckMethod();
System.out.println("i = "+Check.i);
Output:
i = 113
The main use of this keyword is to refer to the current object in Java
programming.
The following are the usage provided by this keyword in Java, and they are:
90. Can this keyword be used to refer to the current class instance
variable?
Created by Foram_Chudasama
Yes, this keyword is used to refer to the current class instance variable and it is
also used to initiate or invoke the current class constructor.
91. Which class is the superclass for all the classes in Java?
Singleton class can be defined as the class that consists of only one single
instance. In this class, all the methods and variables belong to only one instance.
Singleton class is mainly used in a situation where we need to limit the objects
for a class.
In Java, we use inheritance mainly for two uses, and they are:
//code
Created by Foram_Chudasama
By using the Math.random() method we can generate random numbers in Java
ranging from 0.1 to less than 1.0. Moreover, the random numbers can be
generated by using the Random class present in java. util package.
The main() method in Java doesn’t return any data because it is declared with a
void return type.
In Java, the package is defined as a collection of interfaces and classes that are
bundled together and related to each other. Usage of packages in the program
will help developers to group the code for proper re-use. In Java, packages are
used by importing them into different classes.
98. What is the reason why multiple inheritances are not supported in
Java?
Java doesn’t support multiple inheritances in order to reduce the complexity and
simplify the language. An example of multiple inheritances in Java is given
below:
class X{
void msg()
System.out.println("Hello");}
class Y{
void msg()
Created by Foram_Chudasama
{
System.out.println("Welcome");}
Z obj=new Z();
Output:
Compile-time error
So, the above example shows that Java doesn’t support multiple inheritances.
In Java, main() method must be always public static to run any application or
program correctly. Suppose, if the main method is declared as private there will
be no complications but it will give a runtime error.
Yes, the classes present in Java can have multiple constructors with different
parameters.
If a class in Java has multiple numbers methods with the same name and
different parameters, then it is called as method overloading. The main
Created by Foram_Chudasama
advantage of method overloading is that it increases the readability score of a
program.
There are two different ways to overload a method, and they are:
class Addition{
return x+y;
return x+y+z;
class TestOverloading1{
System.out.println(Adder.add(10,20));
System.out.println(Adder.add(10,20,30));
Created by Foram_Chudasama
}}
Output:
30
60
class Addition{
return x+y;
return x+y;
class TestOverloading2{
System.out.println(Adder.add(22,22));
System.out.println(Adder.add(12.5,12.5));
}}
Output:
Created by Foram_Chudasama
44
25
105. Why method overloading is not possible only by changing the return
type of method only?
Yes, we can overload the main() method in Java by using method overloading
but, JVM only calls the main() method that receives string array as arguments
only.
If the subclass in the program has the same method as declared in superclass
then it is known as method overriding.
109. What are the rules we need to follow during method overriding?
• The method must have the same parameters as present in the parent class.
• There must be an IS-A relationship which is called inheritance.
• The method should have the name same as that of the class name.
Created by Foram_Chudasama
110. Write a program to demonstrate method overriding?
Class Shape{
//defining a method
void run()
System.out.println("Shape is ready");
void run()
System.out.println("Rectangle is drawn");
obj.run();//calling method
Output:
Created by Foram_Chudasama
Rectangle is drawn
Pointer is a variable that mainly refers to the memory address. Java doesn’t
support pointers because they are complex to understand and unsecured.
Super keyword in Java is used to conjure immediate prompt parent class object.
It is also called a reference variable.
Created by Foram_Chudasama
• Super can be used to conjure an immediate parent class constructor.
Created by Foram_Chudasama