Java Viva
Java Viva
How to Answer: Explain that abstract classes can have both abstract and concrete methods,
while interfaces only have abstract methods. Discuss when to use each and provide examples.
Sample Answer: "An abstract class is a class that can have both abstract (unimplemented)
and concrete (implemented) methods, while an interface is a collection of abstract methods
only. Abstract classes are used when you want to provide a common base for multiple related
classes, and interfaces are used to define a contract that multiple classes must adhere to."
What to Look For: Look for an understanding of when to use abstract classes and interfaces,
and how they are implemented in Java.
2. What are the key differences between the equals() and == operators in Java?
How to Answer: Explain that == compares object references, while equals() compares the
contents of objects. Provide examples to illustrate the differences.
Sample Answer: "The == operator in Java compares object references, checking if two
references point to the same memory location. On the other hand, the equals() method
compares the contents of objects to determine if they are equal. It's important to note
that equals() can be overridden by classes to provide custom comparison logic."
What to Look For: Look for a clear explanation of the differences between == and equals(),
along with the ability to provide examples.
3. What is the purpose of the static keyword in Java, and how is it used?
How to Answer: Describe that the static keyword is used to create class-level variables and
methods that can be accessed without creating an instance of the class. Provide examples of
static variables and methods.
Sample Answer: "The static keyword in Java is used to create class-level variables and
methods. Class-level variables are shared among all instances of the class, while class-level
methods can be called without creating an instance of the class. For example, the Math class
in Java has static methods like Math.sqrt() that can be called directly without creating
a Math object."
What to Look For: Look for a clear understanding of how the static keyword is used and its
purpose in Java.
public T getValue() {
return value;
}
}
2. Wildcards
Wildcards allow you to write more flexible and reusable code when working with generic
types. There are three types of wildcards: ?, ? extends T, and ? super T.
Example (Using Wildcards):
List<? extends Number> numbers = new ArrayList<>();
3. Generic Methods
You can also create generic methods within non-generic classes. Generic methods allow you
to parameterize methods independently of the class.
Example (Generic Method):
public <T> T getFirst(List<T> list) {
if (list.isEmpty()) {
return null;
}
return list.get(0);
}
Understanding and mastering these core Java concepts will significantly boost your
confidence and success in advanced Java interviews. These fundamentals serve as the
building blocks for more complex topics you'll encounter in your Java journey.