Thursday, August 8, 2024

Access specifiers (public, private, protected, default) in Java MCQ

 Here are multiple-choice questions (MCQs) on access specifiers (public, private, protected, default) in Java:


Question 1

Which access specifier allows members to be accessed from any other class?


A) private


B) protected


C) public


D) default


Answer: C) public


Question 2

Which access specifier restricts access to members within the same class only?


A) private


B) protected


C) public


D) default


Answer: A) private


Question 3

Which access specifier allows access to members within the same package and subclasses?


A) private


B) protected


C) public


D) default


Answer: B) protected


Question 4

What is the default access level when no access specifier is explicitly declared in Java?


A) private


B) protected


C) public


D) Package-private (default)


Answer: D) Package-private (default)


Question 5

Which of the following access specifiers can be applied to a class in Java?


A) public


B) private


C) protected


D) default


Answer: A) public


Question 6

Which access specifier can be used for a method that should only be accessible within its own class and not by any other class?


A) private


B) protected


C) public


D) default


Answer: A) private


Question 7

In which of the following cases can a protected member be accessed?


A) Within the same class only


B) Within the same package only


C) Within the same package and subclasses


D) From any class


Answer: C) Within the same package and subclasses


Question 8

Which of the following is not true about the private access specifier?


A) It can be applied to variables.


B) It can be applied to methods.


C) It can be applied to classes.


D) It restricts access to the defining class only.


Answer: C) It can be applied to classes.


Question 9

What will happen if a member variable is declared without any access specifier?


A) It will be private.


B) It will be protected.


C) It will be public.


D) It will have default (package-private) access.


Answer: D) It will have default (package-private) access.


Question 10

Which access specifier should be used for a method that can be overridden by any subclass but not accessible by objects outside of the package?


A) private


B) protected


C) public


D) default


Answer: B) protected


Question 11

What does the term "package-private" (default access) mean in Java?


A) Accessible only within the same class


B) Accessible only within the same package


C) Accessible by subclasses in other packages


D) Accessible from any class


Answer: B) Accessible only within the same package


Question 12

Which access specifier provides the least restrictive access?


A) private


B) protected


C) public


D) default


Answer: C) public


Question 13

What is the result of the following code snippet?


java

Copy code

class Example {

    private int num;

    public int getNum() {

        return num;

    }

    public void setNum(int num) {

        this.num = num;

    }

}

A) num can be accessed from any class.


B) num can only be accessed within the Example class.


C) num can be accessed from classes in the same package.


D) num can be accessed from subclasses.


Answer: B) num can only be accessed within the Example class.


Question 14

Which access specifier can be used to prevent other classes from accessing the num variable in the following code?


java

Copy code

public class Example {

    int num;

}

A) private


B) protected


C) public


D) default


Answer: A) private


Question 15

What does the following code demonstrate about access specifiers?


java

Copy code

class Parent {

    protected void display() {

        System.out.println("Protected method");

    }

}


public class Child extends Parent {

    public static void main(String[] args) {

        Child obj = new Child();

        obj.display();

    }

}

A) A protected method can be accessed from a subclass.


B) A protected method can be accessed from any class.


C) A protected method cannot be accessed from a subclass.


D) A protected method can only be accessed within the same package.


Answer: A) A protected method can be accessed from a subclass.


Question 16

Which access specifier allows a member to be accessed from within the same package but not by subclasses in other packages?


A) private


B) protected


C) public


D) default


Answer: D) default


Question 17

Which access specifier should be used if a class wants to expose a method to all classes in the package and all subclasses?


A) private


B) protected


C) public


D) default


Answer: B) protected


Question 18

Can a private method be overridden in Java?


A) Yes, in the same class.


B) Yes, in the subclass.


C) No, private methods cannot be overridden.


D) Yes, if it is declared as final.


Answer: C) No, private methods cannot be overridden.


Question 19

What is the main purpose of using access specifiers in Java?


A) To control the visibility of class members.


B) To improve the performance of the application.


C) To define the data types of class members.


D) To enforce the use of object-oriented programming.


Answer: A) To control the visibility of class members.


Question 20

In the following code, which access specifier can be used to make the display() method accessible only within the Example class?


java

Copy code

public class Example {

    void display() {

        System.out.println("Hello");

    }

}

A) private


B) protected


C) public


D) default


Answer: A) private

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...