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
Explanation:

To determine which option will cause a compiler error, we need to understand the syntax and rules of arrays in programming languages.

An array is a data structure that allows you to store multiple values of the same type under a single variable name. In most programming languages, arrays have a fixed size and can only store elements of the same type.

Now, let's analyze each option to identify if it will cause a compiler error:

A. int[ ] scores = {3, 5, 7}; This option is correct. It declares an integer array named "scores" and initializes it with three values: 3, 5, and 7. This code will not cause a compiler error.

B. int [ ][ ] scores = {2,7,6}, {9,3,45}; This option is incorrect. It declares a two-dimensional integer array named "scores" and initializes it with two rows and three columns. However, the array initializer is incorrect. Each row of a two-dimensional array should be enclosed in curly braces. The correct syntax should be: int [ ][ ] scores = {{2,7,6}, {9,3,45}}; The given code will cause a compiler error.

C. String cats[ ] = {"Fluffy", "Spot", "Zeus"}; This option is correct. It declares a String array named "cats" and initializes it with three strings: "Fluffy", "Spot", and "Zeus". This code will not cause a compiler error.

D. boolean results[ ] = new boolean [] {true, false, true}; This option is correct. It declares a boolean array named "results" and initializes it with three boolean values: true, false, and true. This code will not cause a compiler error.

Therefore, the option that will cause a compiler error is:

The Answer is: B

  1. public

  2. private

  3. protected

  4. default access


Correct Option: D
Explanation:
  • Public access is the least restrictive access modifier. Members with public access can be accessed from anywhere in the program.
  • Private access is the most restrictive access modifier. Members with private access can only be accessed from within the class in which they are declared.
  • Protected access is a middle ground between public and private access. Members with protected access can be accessed from within the class in which they are declared, and from subclasses of that class.
  • Default access is also known as package-private access. Members with default access can be accessed from any class in the same package.

So, the most restrictive access modifier that allows a class in the same package to access members of another class in the same package is Default.

Here is a table that summarizes the different access modifiers in Java:

Access modifier Visibility
public Anywhere in the program
private Only within the class in which it is declared
protected Within the class in which it is declared, and from subclasses of that class
default Only within the same package

The difference between protected and default access is that protected allows access from subclasses in other packages, while default does not. For example, if you have a class Animal in package A, and a class Dog in package B that extends Animal, then the class Dog can access the protected members of Animal, but not the default members. However, if you have a class Cat in package A that extends Animal, then the class Cat can access both the protected and the default members of Animal.

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
  1. protected int a;

  2. transient int b = 3;

  3. private synchronized int e;

  4. volatile int d;


Correct Option: C

Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

  1. final

  2. static

  3. private

  4. protected


Correct Option: C
Explanation:

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

  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
- Hide questions