Java Declaration Initialization and Access Control Quiz 10

Java declaration, initialization and access control quiz 10 contains 10 single and multiple choice questions. Declaration, initialization and access control quiz questions are designed in such a way that it will help you understand how to declare and initialize variables in Java as well as how to properly define access control for class, member variables and methods. At the end of the quiz, result will be displayed along with your score and quiz answers.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java Declaration, Initialization and Access Control quiz online.

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading.

You must sign in or sign up to start the quiz.

You have to finish following quiz, to start this quiz:

Result

0 out of 10 questions were answered correctly.

Time has elapsed

Your score is 0 out of 0 , ( 0 )

Category Score

  1. Declaration, Initialization and Access Control 0%
Review Answers
  1. Answered
  2. Review
Question 1 of 10
1 . Question
A top level class can be private, protected or public. Correct answer.

False is the correct choice. A top level class can either be public without any modifiers, it cannot be private or protected. Remember, this applies only to the top level class.

Incorrect answer.

False is the correct choice. A top level class can either be public without any modifiers, it cannot be private or protected. Remember, this applies only to the top level class.

Question 2 of 10
2 . Question

A private member variable or method is only accessible in the same class as well as in the sub-classes.

Correct answer.

False is the correct choice. A private member or method is accessible only within the class it is defined.

Incorrect answer.

False is the correct choice. A private member or method is accessible only within the class it is defined.

Question 3 of 10
3 . Question

A member variable or method declared without any access modifier is only accessible in the same class and all its sub-classes.

Correct answer.

False is the correct choice. A member variable or method defined without any modifier is accesible in the same class and all the classes defined in the same package.

Remember the method or variable defined with default modifier is NOT accessible in the sub-classes defined in other package. That’s the only difference between protected and default modifier.

Incorrect answer.

False is the correct choice. A member variable or method defined without any modifier is accesible in the same class and all the classes defined in the same package.

Remember the method or variable defined with default modifier is NOT accessible in the sub-classes defined in other package. That’s the only difference between protected and default modifier.

Question 4 of 10
4 . Question

A member variable or method declared with the public access modifier is only accessible in all the classes in the same package.

Correct answer.

False is the correct choice. A member variable or method defined with public modifier is accessible everywhere regardless of the package.

Incorrect answer.

False is the correct choice. A member variable or method defined with public modifier is accessible everywhere regardless of the package.

Question 5 of 10
5 . Question

A member variable or method declared with protected access modifier is only accessible in all the sub-classes in the same package.

Correct answer.

False is the correct choice. A protected member or method is accessible in all the sub-classes regardless of the package.

Incorrect answer.

False is the correct choice. A protected member or method is accessible in all the sub-classes regardless of the package.

Question 6 of 10
6 . Question
What will happen when you compile and run the following code?
public class Test < static int i = 0; public static void main(String[] args) < System.out.println(i); >static < i = 1; >> 
Correct answer.

Option 2 is the correct choice. The static initializer block runs when the class is first loaded. So value of variable i will become 1 before main method is invoked. The code will print 1 when run.

Incorrect answer.

Option 2 is the correct choice. The static initializer block runs when the class is first loaded. So value of variable i will become 1 before main method is invoked. The code will print 1 when run.

Question 7 of 10
7 . Question
What will happen when you compile and run the following code?
public class Test < static int i = 0; static< i++; >public static void main(String[] args) < System.out.println(i); >static < i++; >> 
Correct answer.

Option 2 is the correct choice. The static initializer blocks run when the class is loaded first time. They are run in the sequence in which they are defined in the code. The first block increments variable i to 1 and second block increments it again to 2. The main method will be invoked which will print 2.

Incorrect answer.

Option 2 is the correct choice. The static initializer blocks run when the class is loaded first time. They are run in the sequence in which they are defined in the code. The first block increments variable i to 1 and second block increments it again to 2. The main method will be invoked which will print 2.

Question 8 of 10
8 . Question
What will happen when you compile and run the following code?
public class Test < int i = 0; public static void main(String[] args) < Test t = new Test(); System.out.println(t.i); > < i = 1; >> 
Correct answer.

Option 2 is the correct choice. Instance intializer blocks are executed before main method is invoked. Hence the code will print 1 when run.

Incorrect answer.

Option 2 is the correct choice. Instance intializer blocks are executed before main method is invoked. Hence the code will print 1 when run.

Question 9 of 10
9 . Question
What will happen when you compile and run the following code?
public class Test < int i = 5, j = 7; static< i = i * j; >public static void main(String[] args) < Test t = new Test(); System.out.println(t.i); >> 
Correct answer.

Option 3 is the correct choice. Static initializer blocks cannot access non static members of the class. The code will give compilation error “Cannot make a static reference to the non-static field i”.

Incorrect answer.

Option 3 is the correct choice. Static initializer blocks cannot access non static members of the class. The code will give compilation error “Cannot make a static reference to the non-static field i”.

Question 10 of 10
10 . Question
What will happen when you compile and run the following code?
public class Test < public static void main(String[] args)< Long l1 = new Long(7), l2 = new Long(7); if(l1 == l2) System.out.print("true "); else System.out.print("false "); if(l1.equals(l2)) System.out.print("true"); else System.out.print("false"); >> 
Correct answer.

Option 1 is the correct choice. The == operator compares whether the references point to the same object which are different in this case so false will be printed. The equals method compares the value they represent which is same so true will be printed.

Incorrect answer.

Option 1 is the correct choice. The == operator compares whether the references point to the same object which are different in this case so false will be printed. The equals method compares the value they represent which is same so true will be printed.