Search Tutorials


Java Access Modifiers MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Lambda Expressions MCQ - Multiple Choice Questions

Q. What is an access modifier in Java?

A. A keyword that specifies the access level of a class, method, or variable
B. A keyword that specifies the data type of a variable
C. A keyword that specifies the return type of a method
D. A keyword that specifies the number of arguments in a method

Q. Which of the following is an access modifier in Java?

A. Public
B. Integer
C. Boolean
D. String

Q. Which access modifier restricts access to the same package and subclasses only?

A. Private
B. Public
C. Protected
D. Default

Q. Which access modifier is the most restrictive in Java?

A. Private
B. Protected
C. Public
D. Default

Q. Which access modifier allows access from any other class or package?

A. Private
B. Protected
C. Public
D. Default

Q. Which access modifier is the default in Java if none is specified?

A. Private
B. Protected
C. Public
D. Default

Q. Which access modifier allows access within the same class only?

A. Private
B. Protected
C. Public
D. Default

Q. Which access modifier allows access within the same package?

A. Public
B. Private
C. Protected
D. Default

Q. What does the "protected" access modifier allow?

A. Access within the same class only
B. Access within the same package only
C. Access from any other class or package
D. No access at all

Q. Which of the following is an example of a default access modifier in Java?

A. Private
B. Public
C. Protected
D. Default

Q. What is the default access modifier for class members in Java?

A. public
B. private
C. protected
D. package-private

Q. Which of the following code snippets demonstrates the correct usage of the private access modifier in Java?

A.
public class MyClass {
    private int number;

    public void setNumber(int num) {
        this.number = num;
    }

    public int getNumber() {
        return number;
    }
}
B.
public class MyClass {
    int number;

    private void setNumber(int num) {
        this.number = num;
    }

    private int getNumber() {
        return number;
    }
}
C.
public class MyClass {
    private int number;

    private void setNumber(int num) {
        this.number = num;
    }

    public int getNumber() {
        return number;
    }
}
D.
public class MyClass {
    int number;

    void setNumber(int num) {
        this.number = num;
    }

    void getNumber() {
        return number;
    }
}





Q. What happens when a class member is declared as "protected" in Java?

A. The member can be accessed only within the same package.
B. The member can be accessed by any class in the same package or subclasses in different packages.
C. The member can be accessed only within the same class.
D. The member can be accessed by any class in the same package.

Q. Which of the following code snippets demonstrates the correct usage of the "public" access modifier in Java?

A.
public class MyClass {
    public int number;

    public void setNumber(int num) {
        this.number = num;
    }

    public int getNumber() {
        return number;
    }
}
B.
public class MyClass {
    int number;

    void setNumber(int num) {
        this.number = num;
    }

    int getNumber() {
        return number;
    }
}
C.
public class MyClass {
    public int number;

    void setNumber(int num) {
        this.number = num;
    }

    int getNumber() {
        return number;
    }
}
D.
public class MyClass {
    int number;

    public void setNumber(int num) {
        this.number = num;
    }

    public int getNumber() {
        return number;
    }
}

Q. What is the output of the following Java code snippet?

public class AccessModifiersExample {
    private int number = 10;

    public void displayNumber() {
        System.out.println(number);
    }

    public static void main(String[] args) {
        AccessModifiersExample example = new AccessModifiersExample();
        example.number = 20;
        example.displayNumber();
    }
}
A. 10
B. 20
C. This code will not compile due to a compilation error.
D. This code will throw a runtime exception.

Q. What happens when a class member is declared as "private" in Java?

A. The member can be accessed only within the same package.
B. The member can be accessed by any class in the same package or subclasses in different packages.
C. The member can be accessed only within the same class.
D. The member can be accessed by any class in the same package.

Q. Which access modifier allows access to a class member from any class in the same package or subclasses in different packages?

A. public
B. private
C. protected
D. default (package-private)

Q. What is the output of the following Java code snippet?

public class Parent {
    protected int number = 10;
}

public class Child extends Parent {
    public void displayNumber() {
        System.out.println(number);
    }

    public static void main(String[] args) {
        Child child = new Child();
        child.displayNumber();
    }
}
A. 0
B. 10
C. This code will not compile due to a compilation error.
D. This code will throw a runtime exception.

Q. Which of the following is not a valid access modifier in Java?

A. public
B. private
C. internal
D. protected

Q. What will be the output of the following Java code snippet?

class MyClass {
    private int number = 5;
    
    private void displayNumber() {
        System.out.println(number);
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.displayNumber();
    }
}
A. 0
B. 5
C. This code will not compile due to a compilation error.
D. This code will throw a runtime exception.