Skip to content
QuiznExam

Declarations and Access Control in Java Quiz – 15 Questions with Answers

Test your knowledge of declarations and access control in Java with 15 questions covering variables, methods, classes, interfaces, and Java access modifiers.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which keyword is used to declare a variable in Java?

    • A. var
    • B. define
    • C. Both A and a data type can be used
    • D. declare
    Show answer

    C. Both A and a data type can be used

    Java variables can be declared using an explicit data type, such as int age, and local variables can also use var when type inference is supported.

  2. 2. Which access modifier makes a member accessible from any class?

    • A. private
    • B. protected
    • C. public
    • D. default
    Show answer

    C. public

    A public member can be accessed from classes in the same package as well as classes in other packages, subject to normal Java access rules.

  3. 3. Which access modifier restricts access to the declaring class?

    • A. public
    • B. protected
    • C. private
    • D. default
    Show answer

    C. private

    A private member is accessible only within the top-level class or the class that declares it.

  4. 4. What is the access level of a member when no access modifier is specified?

    • A. public
    • B. private
    • C. protected
    • D. Package-private
    Show answer

    D. Package-private

    When no access modifier is specified, the member has package-private access and can be accessed by classes in the same package.

  5. 5. What does the protected access modifier allow?

    • A. Access only within the declaring class
    • B. Access within the same package and through inheritance in other packages
    • C. Access only from other packages
    • D. Access from every class without restriction
    Show answer

    B. Access within the same package and through inheritance in other packages

    A protected member is accessible within the same package and also to subclasses in other packages, subject to Java's protected-access rules.

  6. 6. Which access modifier can be applied to top-level classes in Java?

    • A. private only
    • B. protected only
    • C. public or package-private
    • D. private or protected
    Show answer

    C. public or package-private

    A top-level class can be declared public or have no access modifier, which gives it package-private access. Top-level classes cannot be private or protected.

  7. 7. Which declaration correctly declares an integer variable named count?

    • A. integer count;
    • B. int count;
    • C. count int;
    • D. declare int count;
    Show answer

    B. int count;

    In Java, the data type is written before the variable name, so int count; is a valid declaration.

  8. 8. Which keyword is used to declare a constant variable in Java?

    • A. constant
    • B. static
    • C. final
    • D. const
    Show answer

    C. final

    The final keyword prevents a variable from being assigned a new value after initialization.

  9. 9. Which declaration correctly defines a public method that returns an int?

    • A. public int calculate() { }
    • B. int public calculate() { }
    • C. public calculate int() { }
    • D. method public int calculate()
    Show answer

    A. public int calculate() { }

    A Java method declaration places the access modifier before the return type, followed by the method name and parameter list.

  10. 10. Which access modifier provides the most restrictive access to a class member?

    • A. public
    • B. protected
    • C. private
    • D. package-private
    Show answer

    C. private

    Private members have the most restricted normal access because they are accessible only within their declaring class.

  11. 11. Which statement about private members is correct?

    • A. They can be accessed directly from any class
    • B. They can be accessed directly by subclasses in all cases
    • C. They are directly accessible only within their declaring class
    • D. They are accessible only from other packages
    Show answer

    C. They are directly accessible only within their declaring class

    A private field or method cannot be directly accessed from unrelated classes or subclasses.

  12. 12. Which access level allows a member to be accessed by any class in the same package?

    • A. private
    • B. package-private
    • C. protected only from subclasses
    • D. public only
    Show answer

    B. package-private

    Package-private access, obtained by omitting an access modifier, allows access from classes in the same package.

  13. 13. Which statement about Java interface methods is correct for a normal interface method declared without a body?

    • A. It must be private
    • B. It is implicitly public and abstract
    • C. It is implicitly protected
    • D. It is always static
    Show answer

    B. It is implicitly public and abstract

    A traditional interface method declared without a body is implicitly public and abstract. Interfaces can also contain default, static, and private methods with bodies.

  14. 14. Which keyword is used to declare a class-level variable or method shared by the class rather than by each instance?

    • A. shared
    • B. global
    • C. static
    • D. common
    Show answer

    C. static

    The static keyword declares a class member that belongs to the class rather than to individual objects.

  15. 15. Why are access modifiers used in Java?

    • A. To control visibility and access to classes and members
    • B. To automatically increase program speed
    • C. To convert variables into constants
    • D. To prevent all runtime exceptions
    Show answer

    A. To control visibility and access to classes and members

    Access modifiers control where classes, fields, methods, and constructors can be accessed, helping enforce encapsulation and organize program structure.