Java Encapsulation & Abstraction in OOP Quiz
35 in-depth questions (15 with code) covering encapsulation principles, getters/setters implementation, abstraction concepts, abstracting behavior, and design benefits in object-oriented programming.
Question 1
What is the primary purpose of encapsulation in object-oriented programming?
Question 2
Which access modifier provides the highest level of encapsulation for a class field?
Question 3
What is the main benefit of using getters and setters instead of public fields?
Question 4
What does abstraction mean in the context of object-oriented design?
Question 5
Which of the following best describes the difference between encapsulation and abstraction?
Question 6
When should you use an abstract class instead of a concrete class?
Question 7
What is the key benefit of abstraction in software design?
Question 8
In encapsulation, why is it important to make fields private and provide public getters/setters?
Question 9
What is an abstract method?
Question 10
How does encapsulation contribute to better software maintenance?
Question 11
What is the output of this encapsulation example?
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public double getBalance() { return balance; }
}
public class Test {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.balance = -1000; // Direct access
System.out.println(acc.getBalance());
}
}Question 12
Which scenario demonstrates good use of abstraction?
Question 13
What happens when you try to instantiate an abstract class directly?
Question 14
Why are getters and setters considered a form of encapsulation?
Question 15
What is the result of this abstraction example?
abstract class Shape {
abstract double area();
void display() { System.out.println("Shape"); }
}
class Circle extends Shape {
double radius;
Circle(double r) { radius = r; }
double area() { return Math.PI * radius * radius; }
}
public class Test {
public static void main(String[] args) {
Shape s = new Circle(5);
System.out.println(s.area());
}
}Question 16
How does encapsulation improve code reusability?
Question 17
What is the difference between an abstract class and an interface in terms of abstraction?
Question 18
Why should you avoid public fields in a class?
Question 19
What is the output of this getter/setter example?
class Person {
private String name;
public String getName() { return name; }
public void setName(String name) {
if (name != null && !name.trim().isEmpty()) {
this.name = name;
}
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.setName(" ");
System.out.println("'" + p.getName() + "'");
}
}Question 20
How does abstraction help in managing large software projects?
Question 21
What is the benefit of using interfaces for abstraction?
Question 22
What happens in this encapsulation violation example?
class Employee {
public int salary;
}
public class Test {
public static void main(String[] args) {
Employee e = new Employee();
e.salary = -50000;
System.out.println("Salary: " + e.salary);
}
}Question 23
When should you make a method abstract?
Question 24
How does encapsulation support the single responsibility principle?
Question 25
What is the result of this abstract method implementation?
abstract class Animal {
abstract void makeSound();
void sleep() { System.out.println("Sleeping"); }
}
class Dog extends Animal {
void makeSound() { System.out.println("Woof"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound();
a.sleep();
}
}Question 26
Why is abstraction important for testing?
Question 27
What is a common anti-pattern that violates encapsulation?
Question 28
How does abstraction improve code maintainability?
Question 29
What is the output of this interface abstraction example?
interface Drawable {
void draw();
default void erase() { System.out.println("Erasing"); }
}
class Circle implements Drawable {
public void draw() { System.out.println("Drawing circle"); }
}
public class Test {
public static void main(String[] args) {
Drawable d = new Circle();
d.draw();
d.erase();
}
}Question 30
Why is encapsulation crucial for thread safety?
Question 31
What is the key difference between encapsulation and information hiding?
Question 32
How does abstraction support the open-closed principle?
Question 33
What is the result of this encapsulation with validation?
class Account {
private int balance;
public void setBalance(int balance) {
if (balance >= 0) {
this.balance = balance;
}
}
public int getBalance() { return balance; }
}
public class Test {
public static void main(String[] args) {
Account a = new Account();
a.setBalance(-100);
System.out.println(a.getBalance());
}
}Question 34
Why are abstract classes useful for framework design?
Question 35
Mastering encapsulation and abstraction means you now understand that professional OOP code:
