1. What are the main features of Java?
Answer:
Java is a high-level, object-oriented, platform-independent programming language. Some key features include:
Answer:
==
and .equals()
in Java?Answer:
==
→ Compares memory addresses (checks if both objects reference the same memory)..equals()
→ Compares the contents of the object (logical equality).Example:
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
Answer:
Access modifiers in Java control the visibility of classes, methods, and variables.
Modifier | Scope | Accessible in Same Class | Accessible in Same Package | Accessible in Subclass | Accessible Outside Package |
---|---|---|---|---|---|
private | Only within the class | ✅ | ❌ | ❌ | ❌ |
default | Within the package | ✅ | ✅ | ❌ | ❌ |
protected | Package + subclass | ✅ | ✅ | ✅ | ❌ |
public | Everywhere | ✅ | ✅ | ✅ | ✅ |
static
and final
keywords?Answer:
static
→ Used for class-level variables and methods (shared among all instances).final
→ Used to declare constants, prevent method overriding, and class inheritance.Example:
class Example {
static int count = 0; // Shared among all objects
final int MAX = 100; // Constant value
final void display() { // Cannot be overridden
System.out.println("Hello");
}
}
Answer:
Feature | Method Overloading | Method Overriding |
---|---|---|
Definition | Multiple methods with the same name but different parameters in the same class. | A child class provides a new implementation for a method from the parent class. |
Parameters | Must be different (number, type, or order). | Must be the same. |
Return Type | Can be different. | Must be the same or covariant. |
Access Modifier | No restrictions. | Cannot have a more restrictive access modifier than the parent class. |
Static Methods | Can be overloaded. | Cannot be overridden. |
Example of Overloading:
class OverloadingExample {
void show(int a) {
System.out.println("Integer: " + a);
}
void show(double a) {
System.out.println("Double: " + a);
}
}
Example of Overriding:
javaCopyEditclass Parent {
void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child display");
}
}
Answer:
Feature | Abstract Class | Interface |
---|---|---|
Usage | Used to provide a base class with some implemented methods. | Defines only abstract methods (before Java 8). |
Methods | Can have both abstract & concrete methods. | Can only have abstract methods (Java 8+ allows default and static methods). |
Variables | Can have instance variables. | Only contains public static final variables. |
Constructor | Can have constructors. | Cannot have constructors. |
Access Modifiers | Methods can have any access modifier. | Methods are public by default. |
Multiple Inheritance | Cannot support multiple inheritance. | Supports multiple inheritance. |
Example of Abstract Class:
abstract class Animal {
abstract void makeSound();
void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark!");
}
}
Example of Interface:
interface Animal {
void makeSound(); // Abstract method
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark!");
}
}
Answer:
Exceptions are unexpected runtime errors. Java has two types of exceptions:
try-catch
or throws
.IOException
, SQLException
NullPointerException
, ArrayIndexOutOfBoundsException
Example of Exception Handling:
try {
int result = 10 / 0; // This will cause ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("Execution completed.");
}
Answer:
Multithreading allows a program to run multiple threads simultaneously for better performance.
Two ways to create a thread:
Thread
class:class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
}
}
public class Main {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
}
}
Runnable
interface:class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running...");
}
}
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
t1.start();
}
}
Answer:
The Java Collections Framework (JCF) provides ready-to-use data structures and algorithms for handling groups of objects.
Type | Interface | Implementations |
---|---|---|
List (Ordered, Duplicates Allowed) | List<E> | ArrayList , LinkedList , Vector , Stack |
Set (Unique Elements) | Set<E> | HashSet , LinkedHashSet , TreeSet |
Queue (FIFO Order) | Queue<E> | PriorityQueue , Deque , LinkedList |
Map (Key-Value Pairs) | Map<K,V> | HashMap , LinkedHashMap , TreeMap , Hashtable |
Example: Using ArrayList
import java.util.ArrayList;
public class ListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names);
}
}
String
, StringBuilder
, and StringBuffer
?Answer:
Feature | String | StringBuffer | StringBuilder |
---|---|---|---|
Mutability | Immutable (Creates new object) | Mutable (Thread-safe) | Mutable (Faster, but not thread-safe) |
Performance | Slow (because new objects are created) | Fast (synchronized) | Fastest (no synchronization) |
Thread Safety | ✅ Safe | ✅ Safe | ❌ Not Safe |
Example:
String str = "Hello";
str = str + " World"; // Creates new object
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World"); // Modifies existing object
HashMap
and Hashtable
?Answer:
Feature | HashMap | Hashtable |
---|---|---|
Synchronization | ❌ Not synchronized (Faster) | ✅ Synchronized (Thread-safe) |
Performance | Fast | Slow |
Null Keys/Values | Allows 1 null key, multiple null values | ❌ Does not allow null keys or values |
Iteration | Uses Iterator (Fail-fast) | Uses Enumeration (Not fail-fast) |
Example:
import java.util.*;
public class MapExample {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
System.out.println(map);
}
}
Comparable
and Comparator
?Answer:
Feature | Comparable | Comparator |
---|---|---|
Purpose | Used for natural sorting of objects. | Used for custom sorting. |
Method | compareTo(T obj) | compare(T obj1, T obj2) |
Example Usage | TreeSet , TreeMap | Collections sort() method |
Example using Comparator
:
import java.util.*;
class Student {
int age;
Student(int age) { this.age = age; }
}
class AgeComparator implements Comparator<Student> {
public int compare(Student s1, Student s2) {
return s1.age - s2.age;
}
}
public class ComparatorExample {
public static void main(String[] args) {
List<Student> students = Arrays.asList(new Student(25), new Student(20));
Collections.sort(students, new AgeComparator());
}
}
If you’re serious about placements, feel free to attend our FREE Campus Placement Evaluation Test! https://faceprep.in/article/campus-placement-evaluation-test/
Conclusion
Mastering Java interview questions is essential for securing a job in software development. In this set, we covered fundamental and frequently asked Java interview questions that assess your understanding of core concepts such as OOP principles, exception handling, collections, multithreading, and more.
Click here to know more our program!