QuizzesJava Access Modifiers
beginner
java

Java Access Modifiers

Master public, protected, default, and private access modifiers, their scope, and best practices for controlling visibility in Java.

20 minutes 20 questions Pass at 70%
Time Limit
20 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Access Modifiers. Work through them here, then start the quiz to answer them and see your score.

  1. 1.How many access modifiers does Java have?

    • A3
    • B5
    • C2
    • D4
  2. 2.Which access modifier makes a member accessible from anywhere?

    • Aprotected
    • Bdefault
    • Cpublic
    • Dprivate
  3. 3.Which access modifier restricts access to only the declaring class itself?

    • Adefault
    • Bpublic
    • Cprotected
    • Dprivate
  4. 4.What is "default" (package-private) access in Java?

    • AAccess available to all classes everywhere
    • BSame as protected
    • CAccess available only within the same package
    • DAccess available to subclasses anywhere
  5. 5.Which access modifier allows access within the same package AND by subclasses in other packages?

    • Aprotected
    • Bprivate
    • Cdefault
    • Dpublic
  6. 6.What does this code produce?

    // File: com/example/Parent.java
    package com.example;
    public class Parent {
        private int x = 10;
        protected int y = 20;
        public int z = 30;
    }
    
    // File: com/other/Child.java
    package com.other;
    import com.example.Parent;
    public class Child extends Parent {
        void show() {
            // System.out.println(x); // Line A
            System.out.println(y);   // Line B
            System.out.println(z);   // Line C
        }
    }
    • AAll lines compile fine
    • BLine A would error; Lines B and C compile fine
    • CLines A and B would error; Line C compiles fine
    • DCompilation error on all lines
  7. 7.Can a top-level class be declared with the private modifier?

    • AYes, if they have no methods
    • BNo, top-level classes can only be public or default
    • CYes, but only abstract classes
    • DYes, always
  8. 8.What access modifier should a constructor have if you want to prevent instantiation from outside the class (e.g., for a Singleton)?

    • Apublic
    • Bdefault
    • Cprotected
    • Dprivate
  9. 9.What does this code print?

    class Config {
        private static Config instance;
        private int value = 42;
        private Config() {}
        public static Config getInstance() {
            if (instance == null) instance = new Config();
            return instance;
        }
        public int getValue() { return value; }
    }
    public class Main {
        public static void main(String[] args) {
            Config c1 = Config.getInstance();
            Config c2 = Config.getInstance();
            System.out.println(c1 == c2);
            System.out.println(c1.getValue());
        }
    }
    • Afalse 42
    • Btrue 42
    • Cfalse 0
    • DCompilation error
  10. 10.In which order are access modifiers ranked from most to least restrictive?

    • Apublic > protected > default > private
    • Bprivate > default > protected > public
    • Cprivate > protected > default > public
    • Ddefault > private > protected > public
  11. 11.Can a subclass reduce the access level of an overridden method?

    • AYes, it can make it more restrictive
    • BYes, but only from public to protected
    • COnly if the parent method is default
    • DNo, overriding methods cannot have a more restrictive access modifier
  12. 12.What does this code print?

    class Outer {
        private int secret = 99;
        class Inner {
            void reveal() {
                System.out.println(secret);
            }
        }
    }
    public class Main {
        public static void main(String[] args) {
            Outer outer = new Outer();
            Outer.Inner inner = outer.new Inner();
            inner.reveal();
        }
    }
    • ACompilation error — Inner cannot access private members
    • Bnull
    • C99
    • D0
  13. 13.Which of the following is the best practice for field visibility?

    • AMake all fields public for easy access
    • BUse default visibility to share within the package
    • CMake fields protected so subclasses can use them
    • DMake all fields private and expose only what is necessary via methods
  14. 14.What is the visibility of a member with no access modifier inside an interface?

    • Adefault (package-private)
    • Bprotected
    • Cpublic — all interface members are implicitly public
    • Dprivate
  15. 15.Can a protected method be accessed by a non-subclass in a different package?

    • ANo, outside the package only subclasses can access protected members
    • BYes, if the class is imported
    • CYes, protected is visible everywhere outside the class
    • DOnly if the method is also static
  16. 16.What does this code produce?

    class Animal {
        protected void breathe() {
            System.out.println("Breathing");
        }
    }
    class Fish extends Animal {
        @Override
        public void breathe() {
            System.out.println("Breathing underwater");
        }
    }
    public class Main {
        public static void main(String[] args) {
            Animal a = new Fish();
            a.breathe();
        }
    }
    • ACompilation error — cannot override protected with public
    • BBreathing
    • CCompilation error — cannot widen access
    • DBreathing underwater
  17. 17.What does the module system (Java 9+) add on top of access modifiers?

    • AModule-level encapsulation: packages can be kept internal even if classes are public
    • BAutomatic public access for all classes
    • CNothing new — access modifiers are unchanged
    • DA new "module-private" keyword
  18. 18.Which access modifier is appropriate for a utility helper method used only within the same class?

    • Aprivate
    • Bprotected
    • Cdefault
    • Dpublic
  19. 19.What is the access level of a field declared in a Java enum?

    • AAlways protected
    • BDepends on the declared modifier — same rules as class fields apply
    • CAlways public
    • DAlways private
  20. 20.What is the principle of "least privilege" in the context of access modifiers?

    • ANever use public methods in a class
    • BUse private for everything including class declarations
    • CGive each member the minimum access level it needs to fulfil its role
    • DAlways use the most open access modifier to simplify code