The most common question asks in java interview
Here are a most important Java interview questions
1. What is the difference between an abstract class and an interface in Java?
An abstract class is a class that can have both abstract (without a body) and non-abstract (with a body) methods, whereas an interface can only have abstract methods. A class can extend only one abstract class, but it can implement multiple interfaces. An abstract class can have instance variables and a constructor, while an interface cannot have any of those.
Here are some key differences between an abstract class and an interface in Java:
- An abstract class can have instance variables, a constructor, and non-abstract methods, while an interface cannot.
- A class can extend only one abstract class, but it can implement multiple interfaces.
- An abstract class can have any visibility (public, private, etc.), while an interface is always public.
- An abstract class can have any number of abstract and non-abstract methods, while an interface can have only abstract methods (and also constants, since Java 8).
- An abstract class can have inner classes (including non-static and static ones), while an interface cannot have inner classes.
2.What is the difference between a constructor and a method in Java?
A constructor is a special type of method in Java that is used to initialize an object. It has the same name as the class and is invoked when an object of the class is created. A constructor does not have a return type, not even void.
On the other hand, a method is a block of code that performs a specific task and may or may not return a value. A method has a return type or void if it doesn’t return a value.
Here are some key differences between a constructor and a method in Java:
- A constructor has the same name as the class and is used to create an object of the class, while a method has a different name and is used to perform a specific task.
- A constructor does not have a return type, not even void, while a method has a return type or void if it doesn’t return a value.
- A constructor is invoked automatically when an object of the class is created, while a method is called explicitly by the code.
- A constructor is used to initialize the state of an object, while a method is used to perform a specific task.
3.Can a class be both abstract and final in Java?
A final class is a class that cannot be subclassed, which means that it cannot be extended by any other class. On the other hand, an abstract class is a class that cannot be instantiated and is meant to be extended (subclassed) by other classes.
If you try to declare a class as both abstract and final, the compiler will throw an error because the two are mutually exclusive. An abstract class is meant to be extended, while a final class is not meant to be extended.
For example:
However, it is possible to have an abstract method in a final class. In this case, the final class cannot be subclassed, but the abstract method must be implemented in a subclass if the subclass is not also declared as final.
4.What is the difference between a static method and a non-static method in Java?
In Java, a static method is a method that belongs to a class rather than an instance of the class. It can be invoked without creating an instance of the class and can only access static variables of the class.
On the other hand, a non-static method (also called an instance method) is a method that belongs to an instance of a class. It requires an instance of the class to be created before it can be invoked and can access both static and non-static variables of the class.
Here are some key differences between static and non-static methods in Java:
- A static method belongs to a class and can be invoked without creating an instance of the class, while a non-static method belongs to an instance of a class and requires an instance to be created before it can be invoked.
- A static method can only access static variables of the class, while a non-static method can access both static and non-static variables.
- A static method is declared with the
static
keyword, while a non-static method is not declared with thestatic
keyword.
Here’s an example of a static method:
And here’s an example of a non-static method:
What is the difference between a while loop and a do-while loop in Java?
In Java, a while
loop is a control flow statement that allows you to execute a block of code repeatedly as long as a boolean condition is true. The loop checks the condition before the block of code is executed.
On the other hand, a do-while
loop is also a control flow statement that allows you to execute a block of code repeatedly. However, the loop checks the condition after the block of code is executed. This means that the block of code will be executed at least once, even if the condition is false.
Here are some key differences between a while
loop and a do-while
loop in Java:
- A
while
loop checks the condition before the block of code is executed, while ado-while
loop checks the condition after the block of code is executed. - A
while
loop will not execute the block of code at all if the condition is initially false, while ado-while
loop will execute the block of code at least once.
Here’s an example of a while
‘loop’
And here’s an example of a do-while
loop:
5.What is the difference between a for loop and a while loop in Java?
In Java, a for
loop is a control flow statement that allows you to execute a block of code repeatedly for a given number of times. It consists of three parts: an initialization statement, a boolean condition, and an increment/decrement statement.
On the other hand, a while
loop is a control flow statement that allows you to execute a block of code repeatedly as long as a boolean condition is true. It consists of only one part: a boolean condition.
Here are some key differences between a for
loop and a while
loop in Java:
- A
for
loop has three parts: an initialization statement, a boolean condition, and an increment/decrement statement, while awhile
loop has only one part: a boolean condition. - A
for
loop is used when the number of iterations is known beforehand, while awhile
loop is used when the number of iterations is not known beforehand or when the loop needs to run an indefinite number of times.
Here’s an example of a for
loop:
And here’s an example of a while
loop:
6.What is the difference between a public class and a default class in Java?
In Java, a class can have one of four access levels: public
, private
, protected
, or default (no access modifier).
A public class is a class that can be accessed from anywhere, regardless of the package it is in. It is declared with the public
keyword.
On the other hand, a default class (also called a package-private class) is a class that can only be accessed from within the package it is in. It does not have an access modifier.
Here are some key differences between a public class and a default class in Java:
- A public class can be accessed from anywhere, while a default class can only be accessed from within the package it is in.
- A public class is declared with the
public
keyword, while a default class does not have an access modifier.
Here’s an example of a public class:
And here’s an example of a default class:
7.What is the difference between a try-catch block and a try-with-resources block in Java?
In Java, a try-catch
block is a control flow statement that allows you to handle exceptions that may be thrown in a block of code. It consists of a try
block, which contains the code that may throw an exception, and one or more catch
blocks, which handle the exception if it is thrown.
On the other hand, a try-with-resources
block is similar to a try-catch
block, but it also includes a mechanism for automati
cally closing any resources that are opened within the try
block. The resources are declared in a try
statement and are automatically closed when the block completes, whether it completes normally or abruptly due to an exception.
Here are some key differences between a try-catch
block and a try-with-resources
block in Java:
- A
try-catch
block handles exceptions that may be thrown in a block of code, while atry-with-resources
block also includes a mechanism for automatically closing resources that are opened within thetry
block. - A
try-with-resources
block can only be used with resources that implement theAutoCloseable
interface, while atry-catch
block can be used with any code that may throw an exception.
Here’s an example of a try-catch
block:
And here’s an example of a try-with-resources
block:
8.What is the difference between a Java array and an ArrayList in Java?
In Java, an array is a fixed-size data structure that can store a homogeneous (elements of the same type) collection of elements. An array is created with a specified size and cannot change its size once created.
On the other hand, an ArrayList
is a resizable-array implementation of the List
interface. It is an object that can store a collection of elements and can dynamically increase or decrease its size as elements are added or removed.
Here are some key differences between an array and an ArrayList
in Java:
- An array has a fixed size and cannot change its size once created, while an
ArrayList
has a dynamic size and can change its size as elements are added or removed. - An array can store only homogeneous elements, while an
ArrayList
can store both homogeneous and heterogeneous elements (elements of different types). - An array has faster access times compared to an
ArrayList
, but it is more difficult to use and requires manual resizing. AnArrayList
is easier to use but has slower access times compared to an array.
Here’s an example of creating and initializing an array:
And here’s an example of creating and initializing an ArrayList
:
9.What is the difference between a HashMap and a Hashtable in Java?
In Java, a HashMap
is a class that implements the Map
interface and stores key-value pairs in a hash table. It is an unordered collection and allows null values and null keys.
On the other hand, a Hashtable
is a legacy class that was introduced in Java 1.0 and implements the Map
interface. It is similar to a HashMap
, but it is synchronized (thread-safe) and does not allow null values or null keys.
Here are some key differences between a HashMap
and a Hashtable
in Java:
- A
HashMap
is unsynchronized (not thread-safe) and allows null values and null keys, while aHashtable
is synchronized (thread-safe) and does not allow null values or null keys. - A
HashMap
is faster and more efficient than aHashtable
, but it is not thread-safe. AHashtable
is slower and less efficient than aHashMap
, but it is thread-safe. - A
HashMap
is the recommended implementation of theMap
interface, unless thread-safety is required.
Here’s an example of creating and initializing a HashMap
:
And here’s an example of creating and initializing a Hashtable
: