0

Java Declarations and Access Control Quiz

Description: Java Declarations and Access Control Quiz
Number of Questions: 8
Created by:
Tags: java
Attempted 0/8 Correct 0 Score 0

You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

  1. public

  2. protected

  3. private

  4. transient


Correct Option: B
Explanation:

protected makes a member accessible only to classes in the same package or subclass of the class

What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?

  1. public

  2. abstract

  3. protected

  4. synchronized

  5. default access


Correct Option: E
Explanation:

default access is the "package oriented" access modifier.

Which cause a compiler error?

  1. int[ ] scores = {3, 5, 7};

  2. int [ ][ ] scores = {2,7,6}, {9,3,45};

  3. String cats[ ] = {"Fluffy", "Spot", "Zeus"};

  4. boolean results[ ] = new boolean [] {true, false, true};


Correct Option: B

You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

  1. public

  2. private

  3. protected

  4. default access


Correct Option: D

Which one creates an instance of an array?

  1. int[ ] ia = new int[15];

  2. float fa = new float[20];

  3. char[ ] ca = "Some String";

  4. int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };


Correct Option: A

Which of the following class level (nonlocal) variable declarations will not compile?

  1. protected int a;

  2. transient int b = 3;

  3. private synchronized int e;

  4. volatile int d;


Correct Option: C

AI Explanation

To answer this question, we need to understand the rules and restrictions for declaring class-level (nonlocal) variables in Java.

Let's go through each option to understand why it is correct or incorrect:

Option A) protected int a; This option is correct because there are no syntax errors or restrictions with declaring a protected class-level variable in Java.

Option B) transient int b = 3; This option is correct because there are no syntax errors or restrictions with declaring a transient class-level variable in Java.

Option C) private synchronized int e; This option is incorrect because it violates the rules for declaring class-level variables. In Java, the synchronized modifier is not allowed for class-level variables. It can only be used for instance-level methods or blocks.

Option D) volatile int d; This option is correct because there are no syntax errors or restrictions with declaring a volatile class-level variable in Java.

Therefore, the correct answer is C) private synchronized int e. This option will not compile because the synchronized modifier is not allowed for class-level variables in Java.

Please select the most appropriate option.

  1. final

  2. static

  3. private

  4. protected


Correct Option: C
Explanation:

The private access modifier limits access to members of the same class.

Which is a valid declaration within an interface?

  1. public static short stop = 23;

  2. protected short stop = 23;

  3. transient short stop = 23;

  4. final void madness(short stop);


Correct Option: A

AI Explanation

To answer this question, you need to understand the rules for declaring variables and methods within an interface. Let's go through each option to understand why it is correct or incorrect:

Option A) public static short stop = 23; - This option is correct. In an interface, you can declare variables with the "public static" modifiers. The variable "stop" is declared as a public static short and assigned the value 23.

Option B) protected short stop = 23; - This option is incorrect. In an interface, variables cannot have access modifiers like "protected" or "private". They are implicitly public and static.

Option C) transient short stop = 23; - This option is incorrect. In an interface, variables cannot have modifiers like "transient". They can only have "public" and "static" modifiers.

Option D) final void madness(short stop); - This option is incorrect. In an interface, you can only declare method signatures, not method implementations. The "void" return type and the presence of the method body make this option invalid.

The correct answer is Option A) public static short stop = 23;. This option is correct because it follows the rules for declaring variables within an interface.

Therefore, the correct answer is A) public static short stop = 23;

- Hide questions