Encapsulation Interview Questions

    Question 1JAVA OOPS - Encapsulation Basics

    Which of the following statements is true about encapsulation?

    Question 2JAVA OOPS - Encapsulation Example

    What will be printed?

    class Person { private String name; public void setName(String n) { name = n; } public String getName() { return name; } } public class Main { public static void main(String[] args) { Person p = new Person(); p.setName("Alice"); System.out.println(p.getName()); } }

    Question 3JAVA OOPS - Access Modifier Importance

    Why are private access modifiers important in encapsulation?

    Question 4JAVA OOPS - Read-only Property

    How can you make a class property read-only?

    class Demo { private int value; public int getValue() { return value; } }

    Question 5JAVA OOPS - Write-only Property

    Which statement is correct?

    Question 6JAVA OOPS - Multiple Objects Encapsulation

    What will be printed?

    class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } public class Main { public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); c1.increment(); System.out.println(c2.getCount()); } }

    Question 7JAVA OOPS - Changing Internal Representation

    Which is true?

    Question 8JAVA OOPS - Encapsulation with Validation

    Which is correct for validating input using encapsulation?

    class Account { private int balance; public void setBalance(int b) { if(b >= 0) balance = b; } public int getBalance() { return balance; } }

    Question 9JAVA OOPS - Encapsulation Benefits

    Which of the following is not a benefit of encapsulation?

    Question 10JAVA OOPS - Encapsulation in Practice

    Which statement about encapsulation is correct?