Java Declaration Initialization and Access Control Quiz 1

Time limit: 0

Quiz-summary

0 of 10 questions completed

Questions:

1

2

3

4

5

6

7

8

9

10

Information

Java declaration, initialization and access control quiz 1 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.

Option 4 is the correct choice. Since the variable i is declared as a static variable, it will be initialized with the default value which is 0 for the int type. Additionally, since it is static (defined at the class level), it is accessible even if the class reference t has not been assigned an object to it.

The code will output 0 when run.

Incorrect answer.

Option 4 is the correct choice. Since the variable i is declared as a static variable, it will be initialized with the default value which is 0 for the int type. Additionally, since it is static (defined at the class level), it is accessible even if the class reference t has not been assigned an object to it.

Option 4 is the correct choice. All the static fields defined by the class are loaded when the class loader loads the class for first time. The class Test has defined two static references so they will be initialized first and will print “Three” and “One” respectively.

After that the main method will be called which will print “Hello”. Now, the main method creates an instance of the Test class. All the instance variables are initialized when the object of the class is created. So reference of the Two class will be initialized which will print “Two”.

Incorrect answer.

Option 4 is the correct choice. All the static fields defined by the class are loaded when the class loader loads the class for first time. The class Test has defined two static references so they will be initialized first and will print “Three” and “One” respectively.

After that the main method will be called which will print “Hello”. Now, the main method creates an instance of the Test class. All the instance variables are initialized when the object of the class is created. So reference of the Two class will be initialized which will print “Two”.

Option 3 is the correct choice. Class instance variables are initialized when an object of the class is created. Here, no object is getting created of the Test class. So the reference One is never initialized. The code will execute the main method which will print “Hello”.

Incorrect answer.

Option 3 is the correct choice. Class instance variables are initialized when an object of the class is created. Here, no object is getting created of the Test class. So the reference One is never initialized. The code will execute the main method which will print “Hello”.

Option 4 is the correct choice. All byte, short and char values are promoted to integer values so ‘a’ is represented by ASCII 97 value, ‘b’ is 98 and so on. Since there is a matching case, “a” will be printed followed by “b” and “c” in the absence of break statements. Output of the program will be “abc”.

Incorrect answer.

Option 4 is the correct choice. All byte, short and char values are promoted to integer values so ‘a’ is represented by ASCII 97 value, ‘b’ is 98 and so on. Since there is a matching case, “a” will be printed followed by “b” and “c” in the absence of break statements. Output of the program will be “abc”.

Option 4 is the correct choice. Since variable b is declared inside inner while loop, it is local to that block only and cannot be accessed outside that loop. Hence, program will give compilation error “b cannot be resolved to a variable”.

Incorrect answer.

Option 4 is the correct choice. Since variable b is declared inside inner while loop, it is local to that block only and cannot be accessed outside that loop. Hence, program will give compilation error “b cannot be resolved to a variable”.

Option 2 is the correct choice. Comparing NaN value with another NaN value returns false. Additionally, comparing NaN value with another non NaN value also returns false. Output of the above program will be “Not Equal”.

Incorrect answer.

Option 2 is the correct choice. Comparing NaN value with another NaN value returns false. Additionally, comparing NaN value with another non NaN value also returns false. Output of the above program will be “Not Equal”.

Option 3 is the correct choice. All the methods declared in an interface are implicitly public and abstract. When a class implements an interface, it cannot reduce the visibility of the method it is implementing from an interface. The Test class implements TestInterface and defines method sayHello without access modifier. So basically it is reducing the visibility of the method from public to default.

The code will give compilation error “Cannot reduce the visibility of the inherited method from TestInterface”.

Incorrect answer.

Option 3 is the correct choice. All the methods declared in an interface are implicitly public and abstract. When a class implements an interface, it cannot reduce the visibility of the method it is implementing from an interface. The Test class implements TestInterface and defines method sayHello without access modifier. So basically it is reducing the visibility of the method from public to default.

The code will give compilation error “Cannot reduce the visibility of the inherited method from TestInterface”.

Option 3 is the correct choice. The int type is a primitive type and cannot have null value, only object reference can. The code will give compilation error “Type mismatch: cannot convert from null to int”.

Incorrect answer.

Option 3 is the correct choice. The int type is a primitive type and cannot have null value, only object reference can. The code will give compilation error “Type mismatch: cannot convert from null to int”.

Option 3 is the correct choice. Java treats value in single quotes as characters, so they are promoted to int value while using arithmetic operations. ASCII value of space character is 32. So arithmetic expression will be like 10 + 32 + 25 = 67.

Incorrect answer.

Option 3 is the correct choice. Java treats value in single quotes as characters, so they are promoted to int value while using arithmetic operations. ASCII value of space character is 32. So arithmetic expression will be like 10 + 32 + 25 = 67.