Here are some basic Java Interview questions and answers.
1. What is the Java Development Kit (JDK)?
JDK is a software development kit that includes tools and libraries necessary for developing java applications.
2. What does Java Runtime Environment(JRE) do?
JRE refers to a runtime environment where Java bytecode is executed. JRE supports files and libraries for a runtime environment.
3. What does the Java Virtual Machine(JVM) do?
JVM is an abstract machine that provides a runtime environment in which java bytecode can be executed. Learn preface to java virtual machine and architecture.
4. Java is Platform-Independent. Why?
In any other programming language source code is compiled into executable code and this code cannot be run across all platforms.
Using JVM we can make byte code understandable to any platform and this bytecode is platform-independent.
Meanwhile, JVM is different for each platform and java is platform-independent because it does not depend on any type of platform. Hence, java is platform-independent.
5. Java is not 100% object-oriented. Why?
Because java uses eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.
6. What are the constructors in java?
Constructor is a block of code used to initialize objects.
Syntax:
class DemoClass
{
// constructor name is same as class name
DemoClass()
{
....
}
}
// calls DemoClass() constructor
DemoClass obj = new DemoClass();
For example:
public class ConstructorDemo
{
int a; // class attribute
// create constructor for class ConstructorDemo
ConstructorDemo()
{
a = 26; // initial value for class attribute 'a'
}
public static void main(String[] args)
{
// creating object for ConstructorDemo class
// here we're calling constructor ConstructorDemo()
ConstructorDemo obj = new ConstructorDemo();
System.out.println(obj.a);
}
}
Output:
26
7. What does a singleton class do?
Singleton classes can have only one object (an instance of the class) at a time. After object is created and we try to instantiate singleton class, new variable also points to first object created.
8. What are wrapper classes in java?
wrapper classes allow primitive data types a way to convert into object and vice-versa.
For example:
int a = 7; // using primitive datatype
Integer a = new Integer(7); // using wrapper class
9. What is the difference between == operator and equals method in java?
In general == is an operator and equals() is a method. We use == operator for reference comparison of objects in heap.
Because in java we don’t have concept of operator overloading. Meanwhile == operator is used for checking address of two objects is same or not.
That is, == operator checks if both objects point to the same memory location or not.
Also == operator is used to compare object types and primitive types like boolean. While equals() method of String class compares the content of two objects.
10. What are the OOP’s concepts in java?
Abstraction is defined as hiding internal implementation and showing only necessary information.
Inheritance is a procedure of acquiring all the properties and behaviour of a parent class (super class) into child class (subclass).
encapsulation is a procedure of binding data or variables and methods together.
Polymorphism literally means many forms. Polymorphism is the ability of a method to perform different tasks. It can be achieved through methods.
11. Why main() method is always static in java?
Because to call static method object is not required. If main() were to be a non-static method Java Virtual Machine have to create its object first and then call main() method which will lead to the extra memory allocation.
12. Why strings are immutable in java?
Strings are immutable in java because String objects are cached in string constant pool.
Learn strings and String constant pool here.
13. What are Arrays and ArrayList in java?
Array is an object which holds fixed number of elements of similar type.
ArrayList is part of collection framework.Arraylist implements list interface.ArrayList is a re-sizable array that grows dynamically when elements are added and decreased when elements are deleted.For frequent retrieving operation java ArrayList is the best. Because ArrayList elements are stored in consecutive memory locations.ArrayList cannot hold primitive data types such as int, double, char, and long.ArrayList can hold String and wrapper class objects (Double, Integer).ArrayList allows duplicate elements.ArrayList preserves insertion order.ArrayList is widely used because of its functionality and flexibility. It is designed to hold heterogeneous collections of objects.ArrayList can have any number of null values.
14. Difference between hashset and hashmap in java?
In HashSet,
We can store objects in HashSet. For example HashSet:{“Hello”, “World”}insertion order is not preserved. It is based on hashcode.has add() method.implements Set interface.do not allow duplicate elements.allows single null value.
In HashMap,
In HashMap we can store key and value pairs. For example {1 ->”Hello”, 2 ->”World”}does not maintain insertion order. It is based on Hash function.has put() method.implements Map interface.allows duplicate values. Does not allow duplicate keys.allows single null key and any number of null values.
16. Difference between this and super keyword in java?
this keyword is the reference variable that refers to the current object.this keyword can be used to invoke current class method implicitly.this() keyword used to invoke current class constructor.this keyword can be used to pass as an argument in method call.
super keyword is the reference variable which is used to refer immediate parent class object.super keyword can be used to invoke immediate parent class method.super keyword can be used to invoke immediate parent class constructor.super keyword used to access methods of the base class.
17. What is break and continue statement in java?
break statement encountered in a loop, that loop will terminate and the control will shift to next statement of the same loop.
Example:
public class Example
{
public static void main(String[] args)
{
for(int a = 1; a <= 10; a++)
{
if(a == 3)
{
// breaking loop
break;
}
System.out.println(a);
}
}
}
Output:
1
2
continue statement jumps to next iteration of a loop based on specific condition.
Example:
public class Example
{
public static void main(String[] args)
{
for(int a = 1; a <= 10; a++)
{
if(a % 2 != 0)
{
continue;
}
System.out.println(a + " ");
}
}
}
Output:
2
4
6
8
10
18. What are access modifiers in java?
Access modifiers define the limit or scope of a variable, constructor, class or a method. There are four types of access modifiers in java; private access modifier, protected access modifier, public access modifier and default access modifier.
19. Difference between for and for each loop in java?
for loop,
There is no sequence in execution. Here in for loop we can change counter as per our wish.was introduced from start, JDK 1.no need of implementing interface.can have access to index. Hence can replace element in an array.counter can increment and decrement.
for each loop,
Executes in sequence. Counter is increased by one.was introduced from JDK 5 onwards.To loop over containers using for each loop, container should implement Iterable interface.can’t replace element at given index since there is no access to array index.we can only iterate in incremental order cannot decrement.
Example on difference between foreach and for loop.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DifferenceBetween
{
public static void main(String[] args)
{
List<String> players = new ArrayList<>(Arrays.asList("Virat", "Rohit", "Dhoni"));
// iterate over List using for loop
System.out.println("using for loop: ");
for(int a = 0; a < players.size(); a++)
{
System.out.println(players.get(a));
}
// iterate over List using enhanced for loop
System.out.println("using for each loop: ");
for(String str : players)
{
System.out.println(str);
}
}
}
Output:
using for loop:
Virat
Rohit
Dhoni
using for each loop:
Virat
Rohit
Dhoni
Also published on: https://replit.com/talk/learn/Java-interview-questions-and-answers-2021/143902