100+ Java Interview Questions and Answers
100+ Java Interview Questions and Answers
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.
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.
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.
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.
Q8. What is the difference between Array list and vector in Java?
ArrayList Vector
Array List is not synchronized. Vector is synchronized.
Array List is fast as it’s non-synchronized. Vector is slow as it is thread safe.
If an element is inserted into the Array List, it increases its Array
Vector defaults to doubling size of its array.
size by 50%.
Array List does not define the increment size. Vector defines the increment size.
Array List can only use Iterator for traversing an Array List. Vector can use both Enumeration and Iterator for traversing.
“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and
objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to
compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two
objects.
Q10. What are the differences between Heap and Stack Memory in Java?
The major difference between Heap and Stack memory are:
1. Default
2. Private
3. Protected
4. Public
1 class Abc {
2 member variables // class body
3 methods}
1. State
2. Behavior
3. Identity
1. Inheritance: Inheritance is a process where one class acquires the properties of another.
2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
3. Abstraction: Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to
the users.
4. Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms.
Q19. What is the difference between a local variable and an instance variable?
In Java, a local variable is typically used inside a method, constructor, or a block and has only local scope. Thus, this variable can be
used only within the scope of a block. The best benefit of having a local variable is that other methods in the class won’t be even aware of
that variable.
Example
Whereas, an instance variable in Java, is a variable which is bounded to its object itself. These variables are declared within a class, but
outside a method. Every object of that class will create it’s own copy of the variable while using it. Thus, any changes made to the variable
won’t reflect in any other instances of that class and will be bound to that particular instance only.
1 class Test{
2 public String EmpName;
3 public int empAge;
4 }
Methods Constructors
1. Used to represent the behavior of an object 1. Used to initialize the state of an object
2. Must have a return type 2. Do not have any return type
3. Needs to be invoked explicitly 3. Is invoked implicitly
4. No default method is provided by the compiler 4. A default constructor is provided by the compiler if the class has none
5. Method name may or may not be same as class name 5. Constructor name must always be the same as the class name
final variable
When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been
assigned to the final variable then using only the class constructor a value can be assigned to it.
final method
When a method is declared final then it can’t be overridden by the inheriting class.
final class
When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.
Powered by Edureka
BOOK A SLOT
2. It causes the switch or loop statements to terminate the 2. It doesn’t terminate the loop but causes the loop to jump to the
moment it is executed next iteration
3. A continue within a loop nested with a switch will cause the next
3. It terminates the innermost enclosing loop or switch immediately
loop iteration to execute
Example break:
Example continue:
For example:
this() super()
1. this() represents the current instance of a class 1. super() represents the current instance of a parent/base class
2. Used to call the default constructor of the same class 2. Used to call the default constructor of the parent/base class
3. Used to access methods of the current class 3. Used to access methods of the base class
4. Used for pointing the current class instance 4. Used for pointing the superclass instance
5. Must be the first line of a block 5. Must be the first line of a block
1. Bootstrap ClassLoader
2. Extension ClassLoader
3. System/Application ClassLoader
Array ArrayList
Cannot contain values of different data types Can contain values of different data types.
Size must be defined at the time of declaration Size can be dynamically changed
Need to specify the index in order to add data No need to specify the index
Arrays are not type parameterized Arraylists are type
Arrays can contain primitive data types as well as objects Arraylists can contain only objects, no primitive data types are allowed
Q33. What is collection class in Java? List down its methods and interfaces.
In Java, the collection is a framework that acts as an architecture for storing and manipulating a group of objects. Using Collections you
can perform various tasks like searching, sorting, insertion, manipulation, deletion, etc. Java collection framework includes the following:
Interfaces
Classes
Methods
The below image shows the complete hierarchy of the Java Collection.
In case you are facing any challenges with these java interview questions, please comment on your problems in the section below.
Compile time polymorphism is method overloading whereas Runtime time polymorphism is done
using inheritance and interface.
1 class Car {
2 void run()
3 {
4 System.out.println(“car is running”);
5 }
6 }
7 class Audi extends Car {
8 void run()
9 {
10 System.out.prinltn(“Audi is running safely with 100km”);
11 }
12 public static void main(String args[])
13 {
14 Car b= new Audi(); //upcasting
15 b.run();
16 }
17 }
Inheritance in Java is the concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish
a relationship between different classes. Inheritance is performed between two types of classes:
A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.
1. Single Inheritance: In single inheritance, one class inherits the properties of another i.e there will be only one parent as well as one
child class.
2. Multilevel Inheritance: When a class is derived from a class which is also derived from another class, i.e. a class having more than
one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.
3. Hierarchical Inheritance: When a class has more than one child classes (subclasses) or in other words, more than one child
classes have the same parent class, then such kind of inheritance is known as hierarchical.
4. Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance.
Method Overriding:
In Method Overriding, the subclass has the same method with the same name and exactly the same number and type of parameters
and same return type as a superclass.
Method Overriding is to “Change” existing behavior of the method.
It is a run time polymorphism.
The methods must have the same signature.
It always requires inheritance in Method Overriding.
1 class Car {
2 void run(){
3 System.out.println(“car is running”);
4 }
5 Class Audi extends Car{
6 void run()
7 {
8 System.out.prinltn("Audi is running safely with 100km");
9 }
10 public static void main( String args[])
11 {
12 Car b=new Audi();
13 b.run();
14 }
15 }
1 class Base {
2 private static void display() {
3 System.out.println("Static or class method from Base");
4 }
5 public void print() {
6 System.out.println("Non-static or instance method from Base");
7 }
8 class Derived extends Base {
9 private static void display() {
10 System.out.println("Static or class method from Derived");
11 }
12 public void print() {
13 System.out.println("Non-static or instance method from Derived");
14 }
15 public class test {
16 public static void main(String args[])
17 {
18 Base obj= new Derived();
19 obj1.display();
20 obj1.print();
21 }
22 }
The problem with multiple inheritance is that if multiple parent classes have the same method name, then at runtime it becomes difficult for
the compiler to decide which method to execute from the child class.
Therefore, Java doesn’t support multiple inheritance. The problem is commonly referred to as Diamond Problem.
A Marker interface can be defined as the interface having no data member and member functions. In simpler terms, an empty interface is
called the Marker interface. The most common examples of Marker interface in Java are Serializable, Cloneable etc. The marker interface
can be declared as follows.
Object cloning in Java is the process of creating an exact copy of an object. It basically means the ability to create an object with a similar
state as the original object. To achieve this, Java provides a method clone() to make use of this functionality. This method creates a new
instance of the class of the current object and then initializes all its fields with the exact same contents of corresponding fields. To object
clone(), the marker interface java.lang.Cloneable must be implemented to avoid any runtime exceptions. One thing you must note is
Object clone() is a protected method, thus you need to override it.
1 class Demo
2 {
3 int i;
4 public Demo(int a)
5 {
6 i=k;
7 }
8 public Demo(int a, int b)
9 {
10 //body
11 }
12 }
In case you are facing any challenges with these java interview questions, please comment on your problems in the section below. Apart
from this Java Interview Questions Blog, if you want to get trained from professionals on this technology, you can opt for a structured
training from edureka!