Java Declaration Initialization and Access Control Quiz 3
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 3 contains 10 single choice questions. This 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 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%
Language Basics0%
OOPs0%
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{
boolean b;
public static void main(String[] args){
Test t = new Test();
System.out.println(t.b);
}
}
Correct answer.
Option 3 is the correct choice. Since boolean variable b is declared as an instance variable, it will be automatically initialized with the default value which is false for the boolean type. The code will print false when compiled and executed.
Incorrect answer.
Option 3 is the correct choice. Since boolean variable b is declared as an instance variable, it will be automatically initialized with the default value which is false for the boolean type. The code will print false when compiled and executed.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
public class Test{
Integer i;
public static void main(String[] args){
Test t = new Test();
System.out.println(t.i);
}
}
Correct answer.
Option 4 is the correct choice. Since Integer reference i is declared as an instance variable, it will be initialized with the default value which is null for any object type. The code will print null when executed. Do not confuse it with 0 which is default value of an int primitive type.
Incorrect answer.
Option 4 is the correct choice. Since Integer reference i is declared as an instance variable, it will be initialized with the default value which is null for any object type. The code will print null when executed. Do not confuse it with 0 which is default value of an int primitive type.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
Integer i;
System.out.println(i);
}
}
Correct answer.
Option 3 is the correct choice. The Integer reference i is declared as a local variable inside main method. Since it is a local reference, it will not be initialized with the default value unlike as instance variable. Hence, we must initialize it before using it.
The code will give compilation error “The local variable i may not have been initialized”.
Incorrect answer.
Option 3 is the correct choice. The Integer reference i is declared as a local variable inside main method. Since it is a local reference, it will not be initialized with the default value unlike as instance variable. Hence, we must initialize it before using it.
The code will give compilation error “The local variable i may not have been initialized”.
Question 4 of 10
4. Question
Which of the below given variable names are valid in Java?
Correct answer.
Options 1, 2, 3, 5, 6 and 7 are the correct choices. A variable name must begin with a letter, the dollar sign ($) or the underscore character (_). Subsequent characters may be letters, digits, $ or _. Any other special characters are not allowed in the variable name.
Incorrect answer.
Options 1, 2, 3, 5, 6 and 7 are the correct choices. A variable name must begin with a letter, the dollar sign ($) or the underscore character (_). Subsequent characters may be letters, digits, $ or _. Any other special characters are not allowed in the variable name.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
class One{
public One(){
System.out.print("One");
}
}
class Two extends One{
public Two(){
System.out.print("Two");
}
}
class Three{
Two two = new Two();
public Three(){
System.out.print("Three");
}
}
public class Test{
public static void main(String[] args) {
Three three = new Three();
}
}
Correct answer.
Option 3 is the correct choice. When the class object is created, its constructor gets called. However, before the constructor body is executed, the static variables and initializer blocks are executed after which the super class constructor is called. Once that is done, all the instance variables and initializers are executed for that class. Once it is done, rest of the class constructor body is executed.
So, in our case, when the code creates an object of the class Three, its constructor is called. But before its body is executed, the object of the class Two is created which calls the constructor of the class Two. The constructor of the class Two calls the constructor of the class One first which will print “One” followed by “Two” by the constructor of the class Two. Once it is done, the code written in the constructor of the class Three will be executed and will print “Three”.
Option 3 is the correct choice. When the class object is created, its constructor gets called. However, before the constructor body is executed, the static variables and initializer blocks are executed after which the super class constructor is called. Once that is done, all the instance variables and initializers are executed for that class. Once it is done, rest of the class constructor body is executed.
So, in our case, when the code creates an object of the class Three, its constructor is called. But before its body is executed, the object of the class Two is created which calls the constructor of the class Two. The constructor of the class Two calls the constructor of the class One first which will print “One” followed by “Two” by the constructor of the class Two. Once it is done, the code written in the constructor of the class Three will be executed and will print “Three”.
Can you override public constructor declared in a base class to private in a child class?
Correct answer.
No is the correct choice. Constructor cannot be overridden at all since they are not inherited by the child class.
Incorrect answer.
No is the correct choice. Constructor cannot be overridden at all since they are not inherited by the child class.
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 += 1;
}
public static void main(String[] args){
System.out.println(i);
}
static{
i += 1;
}
}
Correct answer.
Option 3 is the correct choice. All the static initializers are executed in the order in which they are defined in the class when the class loader loads the class for the first time. It happens before the main method is invoked.
The test class has defined two static initializer blocks, so they are executed when the Test class loads and increment the value of variable i to 2. So the code will print 2 when run.
Incorrect answer.
Option 3 is the correct choice. All the static initializers are executed in the order in which they are defined in the class when the class loader loads the class for the first time. It happens before the main method is invoked.
The test class has defined two static initializer blocks, so they are executed when the Test class loads and increment the value of variable i to 2. So the code will print 2 when run.
Question 8 of 10
8. Question
All the methods declared in an interface are implicitly public.
Correct answer.
True is the correct choice. All the methods declared in an interface is by default public whether you use public in method declaration or not. Moreover, they are also implicitly abstract.
Incorrect answer.
True is the correct choice. All the methods declared in an interface is by default public whether you use public in method declaration or not. Moreover, they are also implicitly abstract.
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 = 0;
System.out.println(i);
}
public static void changeMe(int i){
i++;
}
}
Correct answer.
Option 1 is the correct choice. In Java, the method parameters are passed by value. So even if the method changes the parameter value, the original variable in the caller method remains unchanged.
Incorrect answer.
Option 1 is the correct choice. In Java, the method parameters are passed by value. So even if the method changes the parameter value, the original variable in the caller method remains unchanged.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
static int i = 0;
{
i += 1;
}
public static void main(String[] args){
System.out.print(i);
Test t = new Test();
System.out.print(i);
}
static{
i += 1;
}
}
Correct answer.
Option 1 is the correct choice. All the static initializers are run in order in which they are defined in the class when the class loads for the first time. It happens before the main method is invoked. The code defines one static initializer which increments value of variable i to 1 from 0.
So when main method prints the value of i, it is 1. All the instance initializers are called in the order in which they are defined when an object of the class is created. The code has one instance initializer block which increments the value of variable i to 2. It will be called when the main method creates and object of the Test class. So the second System.out.println statement will print 2. Thus output of the program will be 12.
Incorrect answer.
Option 1 is the correct choice. All the static initializers are run in order in which they are defined in the class when the class loads for the first time. It happens before the main method is invoked. The code defines one static initializer which increments value of variable i to 1 from 0.
So when main method prints the value of i, it is 1. All the instance initializers are called in the order in which they are defined when an object of the class is created. The code has one instance initializer block which increments the value of variable i to 2. It will be called when the main method creates and object of the Test class. So the second System.out.println statement will print 2. Thus output of the program will be 12.