Java Interview Questions for 5 to 6 Years Experience
1. What is the unique feature of Java?Platform independence is a unique feature in Java. The "Write once run anywhere" feature means that the compiled Java bytecodes can run in any operating system irrespective of the operating systems. This unique feature makes Java different from other programming languages.
You can find it in detail here.
2. What is an instance and a local variable?
An instance variable is a variable that is declared within a class and outside the method or a constructor or a block.
A local variable is a variable that is declared within a method or a constructor or a block. The scope of the variable is finished within a method or a block.
You can find about types of variables in Java in detail here.
3. Difference between String and String pool?
A string pool is a storage area in heap memory where string literals are stored. When we create a string literal, the JVM checks for the string in the String pool. If the string literal is already found in the pool, it returns a reference of that string. If the string is not found, it creates a new string literal in the String pool.
4. Difference between == and equals() method in java?
In simple words, we can say that the == operator compares the object reference variables i.e. checks whether the objects refer to the same memory location and the equals() methods compare the values of the objects. You can find it in detail here.
public class EqualsMethod {
public static void main(String[] args)
{
String s1 = "hello world";
String s2 = "hello world";
String s3 = new String("hello world");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
}
}
Output:
true
false
true
true
5. List some of the features in Java 8?
• Lambda expressions.
• Stream API.
• forEach method.
• Optional class.
• Functional interfaces.
• Default methods inside interfaces.
• Date/Time API.
• Collectors and StringJoiner class.
• IO and JDBC enhancements.
You can find the features of Java8 in detail here.
6. What is a Lambda expression and its syntax?
A lambda expression is a short block of code that takes in parameters and returns a value. It is similar to the method but doesn't contain a name and be implemented in the body of the method.
Lambda Expression Syntax:
(argument-list) -> {body}
A lambda expression consists of three elements,
1. Argument-list: It can be empty or non-empty.
2. Arrow-token: It is used to link the argument lists and body of expression.
3. Body: It contains expressions and statements for lambda expressions.
Use a lambda expression in the ArrayList's forEach() method to print each element in the list:
import java.util.ArrayList;
public class LambdaExp {
public static void main(String[] args) {
ArrayList numberList = new ArrayList();
numberList.add(5);
numberList.add(8);
numberList.forEach( (number) -> { System.out.print(number); } );
}
}
Output:
58
7. What is the difference between Collection and Streams in Java?
Collection is used to store the data in a particular data structure like List, Set, or Map. But, streams are used to perform operations like filtering, matching, mapping, etc. on stored data such as arrays, a collection like lists, or I/O resources.
Example:
import java.util.List;
import java.util.ArrayList;
public class CollectionStream{
public static void main(String args[]) {
List names = new ArrayList<>();
names.add("Kamal");
names.add("Ram");
names.add("Madhavi");
names.add("Kamal");
// Use Streams to select unique names
names.stream().distinct().forEach(System.out::println);
}
}
Output:
Kamal
Ram
Madhavi
8. What are the advantages of design patterns in Java?
• The design patterns provide a solution to define a software architecture.
• The design patterns are reusable in multiple projects.
• It is an outcome of experienced software developers.
9. What are functional or SAM interfaces?
The functional interface is an interface with only one abstract method. So, it is also called the Single Abstract Method (SAM) interface. Functional interfaces can have any number of default, static, and overridden methods. To declare a functional interface, we can use @FunctionalInterface annotation but it is optional. If this annotation is used for interfaces with more than one abstract method, it will generate a compiler error.
10. What is the method reference in Java 8?
Method reference is used to refer method of functional interface. It is an easy way of a lambda expression. We can replace the lambda expression with a method reference.
Syntax:
class::methodname
Types of method references in Java,
• Reference to a static method.
• Reference to an instance method.
• Reference to a constructor.
11. Why String is called an immutable object?
Strings are immutable as their values cannot be changed. The Strings are used in many Java applications to store sensitive information like username, password, etc. The purpose of making string immutable is to ensure caching, security, synchronization, and performance of the application.
12. What is an Optional class in Java?
Java 8 has introduced a new class Optional in java.util package. This class is introduced to avoid NullPointerException in java. It helps developers to write good code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run.
13. What does the peek() method do? When should you use it?
The peek() method of Stream class allows us to see through a Stream pipeline. We can peek through each step and print meaningful messages. It is generally used for debugging issues related to lambda expression and Stream processing.
14. What are serialization and deserialization in java?
Serialization allows us to convert the state of an object into a byte stream, which then can be saved into a file on the local disk or sent over the network to any other machine. Deserialization allows us to reverse the process which means reconverts the serialized byte stream to an object again.
Example Server and Client application.
15. Is it important to implement a Serializable interface if we want to serialize any object?
Yes, it is necessary to implement a Serializable interface if we want to serialize any object.
16. Can you explain the concept of serialVersionUID?
serialVersionUID is used to ensure that the same class which was used during Serialization is loaded during deserialization. serialVersionUID is used for version control of the object.
You can find the serialVersionUID in detail here.
That's all for today. Please mention in the comments in case you have faced Java interview questions for 5-6 years of experience that is not present in the above list.