Java 70 Sheet
Java 70 Sheet
JAVA
70+ interview questions/answers
Used for method frames, local Used for storing objects and
Purpose variables, method call stack dynamically allocated memory
Thread
Safety Thread-safe Not thread-safe
Memory
Overhead Less memory overhead More memory overhead
// Superclass
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclasses
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
// Polymorphic method
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.sound(); // Output: Dog barks
cat.sound(); // Output: Cat meows
}
}
14. What is encapsulation in Java? Provide an example.
Answer:
Encapsulation in Java is the mechanism of wrapping data (variables) and code
(methods) together as a single unit (class) and restricting access to the internal
state of the object.
It is achieved by declaring the variables of a class as private and providing
public getter and setter methods to access and modify the data.
Example:
// Encapsulated class
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// Usage
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("John");
System.out.println("Name: " + person.getName());
// Output: Name: John
}
}
15. Describe abstraction in Java and provide an example.
Answer:
Abstraction in Java is the process of hiding the implementation details and
showing only the essential features of an object.
It allows the creation of abstract classes and interfaces with abstract methods.
Abstraction is achieved using abstract classes and interfaces in Java.
Example:
// Abstract class
abstract class Shape {
abstract void draw(); // Abstract method
}
// Concrete subclasses
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing a rectangle");
}
}
// Usage
public class Main {
public static void main(String[] args) {
Shape circle = new Circle();
Shape rectangle = new Rectangle();
circle.draw(); // Output: Drawing a circle
rectangle.draw(); // Output: Drawing a rectangle
}
}
16. What are abstract classes and interfaces? How do they differ?
Answer:
Access Modifiers Can have any access modifiers All methods are implicitly public
java public void add(int a, int java class Animal { void sound()
b) {} java public void add(int { } } java class Dog extends
Example a, int b, int c) {} Animal { void sound() { } }
19.Explain the try-catch block in Java with an example.
Answer: The try-catch block in Java is used for exception handling. Code that
may throw an exception is enclosed within the try block, and the potential
exceptions are caught and handled in the catch block.
Here's an example:
try { // Code that may throw an exception
int result = 10 / 0; // Division by zero
} catch (ArithmeticException e) { // Exception handling
System.out.println("Cannot divide by zero");
}
20.What is the difference between checked and unchecked exceptions?
Answer:
FileNotFoundException, NullPointerException,
Examples IOException ArrayIndexOutOfBoundsException