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.
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)
Average score
Your score
Category Score
Declaration, Initialization and Access Control0%
For Loop0%
Language Basics0%
Switch0%
While Do While Loop0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
What will happen when you compile and run the following code?
public class Test{
static int i;
public static void main(String[] args) {
Test t = null;
System.out.println(t.i);
}
}
Correct 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.
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.
The code will output 0 when run.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
class One{
public One(){
System.out.print("One");
}
}
class Two{
public Two(){
System.out.print("Two");
}
}
class Three{
public Three(){
System.out.print("Three");
}
}
public class Test{
static Three three = new Three();
Two two = new Two();
static One one = new One();
public static void main(String[] args) {
System.out.print("Hello");
Test t = new Test();
}
}
Correct 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”.
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”.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
class One{
public One(){
System.out.println("One");
}
}
public class Test{
One one = new One();
public static void main(String[] args) {
System.out.println("Hello");
}
}
Correct 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”.
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”.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 97;
switch(i){
case 95 :
System.out.print("95");
case 96 :
System.out.print("96");
case 'a' :
System.out.print("a");
case 'b' :
System.out.print("b");
case 'c' :
System.out.print("c");
}
}
}
Correct 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”.
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”.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0;
while(a + 2 < 10){
a += 2;
int b = 10;
while(b - 2 > 0){
b -= 2;
}
}
System.out.println(a + " " + b);
}
}
Correct 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”.
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”.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
for(int i = 0 ; i < 10; i++){
}
System.out.println(i);
}
}
Correct answer.
Option 4 is correct choice. Variable i is declared inside the for loop hence it cannot be accessed outside of it. Hence code will give compilation error “i cannot be resolved to a variable”.
Incorrect answer.
Option 4 is correct choice. Variable i is declared inside the for loop hence it cannot be accessed outside of it. Hence code will give compilation error “i cannot be resolved to a variable”.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
double d1 = Double.NaN;
double d2 = d1;
if(d1 == d2)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
Correct 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”.
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”.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
interface TestInterface{
void sayHello();
}
public class Test implements TestInterface{
void sayHello(){
System.out.println("Hello");
}
public static void main(String[] args) {
Test t = new Test();
t.sayHello();
}
}
Correct 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”.
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”.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 5, j = null;
System.out.println( i % j );
}
}
Correct 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”.
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”.
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){
int i = 10;
int j = 25;
System.out.println(i + ' ' + j);
}
}
Correct 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.
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.