Java Interview
Java Interview
1. What is Java?
Java is a popular object-oriented programming language. It is defined as a complete collection
of objects. By using Java, we can develop lots of applications such as gaming, mobile apps, and
websites.
2. Explain in brief the history of 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.
3. What is the difference between C and Java?
The differences between C and Java are as follows:
4. What is the reason why Java came into existence?
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.
Dynamic: Java is more dynamic when compared to C++ and C. Java is designed in such a way
that it is adaptable to any the evolving environments.
Simple: Java is very easy to learn and code.
Object-oriented: In Java, everything is based on objects.
Secure: Java provides a secure platform to develop safe and virus-free applications.
Platform Independent: Unlike C and C++, when we compile Java code it is compiled into
platform-independent bytecode rather than the platform-specific machine code.
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.
MindMajix YouTube Channel
Top 10 Programming Languages that you need to watch out for to boost your career in 2023
Java Interview Questions for Freshers
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
ClassLoader: class loader is a subschema of JVM which is used to load class files. Whenever we
run Java programs, the data will be first loaded from the classloader. There are mainly three in-
built classloaders in JVM, they are:
Application Classloader
Bootstrap Classloader
Extension Classloader
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.
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.
[Related Blog: Java stream interview questions]
For Example:
int a = 10;
17. What are the different types of variables in Java?
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:
class A{
int num=30;//instance variable
static char name=pranaya;//static variable
void method(){
int n=90;//local variable
}
}//end of class
18. What is Typecasting?
Typecasting in Java is done explicitly by the programmer; this is done to convert one data type
into another data type.
Widening (automatically) - conversion of a smaller data type to a larger data type size.
byte -> short -> char -> int -> long -> float -> double
Narrowing (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
19. What is Type Conversion?
Type conversion can be defined as converting one data type to another data type automatically
by the compiler.
22. What are the default values and sizes of primitive data types?
Data Type Default Size Default Value
int 4 bytes0
char 2 bytes‘u0000’
byte 1 byte 0
byte 2 bytes0
long 8 bytes0L
float 4 bytes0.0f
double 8 bytes0.0d
boolean 1 bit false
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:
29. Is exit, null, delete, main, and next are the keywords in Java?
No, they are no such keywords in Java.
Arithmetic operators
Assignment operators
Logical operators
Relational operators
Bitwise operators
Unary operators
Ternary operators
Shift operators
33. What is operator precedence in Java?
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.
class UnaryExample{
public static void main(String args[]){
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
}}
Java Interview Questions for Experienced
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.
Example:
int a=2;//
int i;
i=a<<1;//4
System.out.println("the value of a before left shift is: " +a);
System.out.println("the value of a after applying left shift is: " +i);
}
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:
public class RightShiftOperator {
}
Output: 1
38. What are the bitwise operators in Java?
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 (>>>)
39. What is a ternary operator?
Ternary operator in Java is used to replace the if-else statement. The representation or the
syntax for the ternary operator is given as:
For example:
abstract
default
new
This
static
Extends and many more
42. What are various access specifiers present in Java?
There are four access specifiers present in Java, and they are:
Public: The methods, classes, and variables that are defined as the public can be accessed by
any class or method.
Private: The methods or classes which are declared as private can be accessible within the same
class only.
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.
Default: By default, all the classes, variables, and methods are of default scope. The default is
accessible within the package only.
43. What are the advantages of packages in Java?
The following are the advantages of packages in Java, and they are:
class Java
{
public static void main (String args[])
{
System.out.println(10 * 50 + "Mindmajix");
System.out.println("Mindmajix" + 10 * 50);
}
}
Output:
500Mindmajix
Mindmajix500
45. List out the control statements in Java?
In Java control statements are divided into three types. They are:
Selection Statements
Iterative/looping Statements
Jump Statements.
[Related Blog: Control Statements in Java]
If statement
If-else statement
Switch statements
47. What are the different types of iterative 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.
Break and Continue are the two jump statements present in Java.
49. Define for each loop 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{
public static void main(String args[]){
//declaring an array
int arr[]={12,13,14,44};
//traversing the array with for-each loop
for(int i:arr){
System.out.println(i);
}
}
}
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
}
51. What are the comments in Java?
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
Multi-line comment
Single-line comment
52. Describe in brief OOPs concepts?
OOPs is an abbreviation for Object-Oriented Programming Language which entirely deals with
an object. The main aim of OOPs is to implement real-world entities such as objects, classes,
inheritance, polymorphism, and so on. Simula is considered the first object-oriented
programming language. The most popular programming languages are Java, Python, PHP, C++,
and many others.
The following are the OOPs concepts that are included in Java:
Class
Object
Abstraction
Encapsulation
Inheritance
Polymorphism
53. What is Abstraction?
In simple words, abstraction can be defined as hiding unnecessary data and showing or
executing necessary data. In technical terms, abstraction can be defined as hiding internal
processes and showing only the functionality.
59. What will be an initial value for an object reference that is defined as an instance variable?
All the object references in Java are initialized to null.
64. What all the rules must be followed while creating a constructor in Java?
The rules we need to follow while creating a constructor are:
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.
65. What are the different types of constructors available in Java?
There are two constructors in Java, and they are:
Default constructor
Parameterized constructor
Default constructor: It is also called a no-argument constructor and it is mainly used to initialize
the instance variable with the default value. Moreover, it is also used to perform some useful
tasks on object creation. This default constructor is implicitly invoked by the compiler if there is
no constructor for a particular class.
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.
66. Give examples for both a default constructor and a parameterized constructor?
An example of a default constructor is as follows:
111 DevOps
222 Oracle
67. Does the constructor return any value?
Yes, the constructor will return the current or present instance of the class.
68. Can the constructor be inherited?
No, the constructor cannot be inherited.
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.
Constructor Method
The constructor should have the same name as the class name The method name is not the
same as that of the class name
A constructor has no return type The method must have a return type
It can be invoked implicitly It can be invoked explicitly
The constructor is used to initialize the state of the object A method is used to expose the
behavior of an object
Default constructor can be provided by the compiler The default method is not provided
by the Java compiler
75. What is the method signature in Java?
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.
m1.display();
m2.display();
}
}
Output:
346 Pranaya Appmajix
222 Lilly Appmajix
78. What happens if we declare a method as static?
If we declare a method as static, the following operations take place, and they are:
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.
80. Why is the main method in Java is 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.
Class Mindmajix{
static{System.out.println("static block");
}
public static void main(String args[]){
System.out.println("Hello World");
}
}
Output:
static block
Hello World
83. Can we execute a program in Java without the main() method?
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?
The program will be compiled, but at runtime, it throws NoSuchMethodError error.
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.
91. Which class is the superclass for all the classes in Java?
An object class is a superclass for all the classes in Java.
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()
{
System.out.println("Welcome");}
}
class Z extends X,Y{//suppose if it were
public static void main(String args[]){
Z obj=new Z();
obj.msg();//Now which msg() method would be invoked?
}
}
Output:
Compile-time error
So, the above example shows that Java doesn’t support multiple inheritances.
30
60
104. Write a program to demonstrate the method overloading by changing a number of
arguments?
class Addition{
static int add(int x, int y)
{
return x+y;
}
static double add(double x, double y)
{
return x+y;
}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(22,22));
System.out.println(Adder.add(12.5,12.5));
}}
Output:
44
25
105. Why method overloading is not possible only by changing the return type of method only?
Ambiguity is the main reason why method overloading is not possible by changing the return
type of method only.
109. What are the rules we need to follow during method overriding?
The rules are as follows:
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.
110. Write a program to demonstrate method overriding?
//Java Program to illustrate the use of Java Method Overriding
//Creating a parent class.
Class Shape{
//defining a method
void run()
{
System.out.println("Shape is ready");
}
}
//Creating a child class
class Rectangle extends Shape{
//defining the same method as in the parent class
void run()
{
System.out.println("Rectangle is drawn");
}
public static void main(String args[]){
Rectangle obj = new Rectangle();//creating object
obj.run();//calling method
}
}
Output:
Rectangle is drawn