Java Declaration Initialization and Access Control Quiz 5
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 5 contains 10 single 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%
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?
abstract class TestParent{
abstract void Test();
static int i = 0;
}
public class Test extends TestParent{
static int i = 1;
public static void main(String[] args) {
TestParent t = new Test();
System.out.println(t.i);
}
}
Correct answer.
Option 3 is the correct choice. The abstract class TestParent declares an abstract method named Test. The Test class extends the TestParent class, but does not implement the abstract method Test. The child class must either implement the abstract method declared in the parent class or declare itself as abstract.
Hence, the program will give compile error “The type Test must implement the inherited abstract method TestParent.Test()”.
Incorrect answer.
Option 3 is the correct choice. The abstract class TestParent declares an abstract method named Test. The Test class extends the TestParent class, but does not implement the abstract method Test. The child class must either implement the abstract method declared in the parent class or declare itself as abstract.
Hence, the program will give compile error “The type Test must implement the inherited abstract method TestParent.Test()”.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
boolean b1 = true, b2 = False;
if(b1 & b2)
System.out.println("1");
else
System.out.println("2");
}
}
Correct answer.
Option 3 is the correct choice. The boolean literal “false” is incorrectly spelled as “False”. Hence the program will give compilation error “False cannot be resolved to a variable”.
Incorrect answer.
Option 3 is the correct choice. The boolean literal “false” is incorrectly spelled as “False”. Hence the program will give compilation error “False cannot be resolved to a variable”.
Question 3 of 10
3. Question
A subclass must implement all the abstract methods declared by its parent abstract class.
Correct answer.
False is the correct choice. A subclass must implement all the abstract methods of the parent abstract class or it can declare itself as an abstract.
Incorrect answer.
False is the correct choice. A subclass must implement all the abstract methods of the parent abstract class or it can declare itself as an abstract.
Question 4 of 10
4. Question
Will this code compile successfully?
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
ArrayList aList = new ArrayList();
aList.add(1);
}
}
Correct answer.
True is the correct choice. Java automatically converts int value to Integer wrapper class object (Java automatically converts primitive values to the corresponding wrapper class objects, this process is known as autoboxing). Hence, program will compile without an error.
Incorrect answer.
True is the correct choice. Java automatically converts int value to Integer wrapper class object (Java automatically converts primitive values to the corresponding wrapper class objects, this process is known as autoboxing). Hence, program will compile without an error.
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) {
boolean hot, cold=false;
System.out.println(hot && cold);
}
}
Correct answer.
Option 2 is the correct choice. Local variables must be initialized before they can be used. Here the program declares two variables hot and cold. While the cold variable is initialized with false, variable hot is not initialized. Hence, the program will give compilation error “The local variable hot may not have been initialized”
Incorrect answer.
Option 2 is the correct choice. Local variables must be initialized before they can be used. Here the program declares two variables hot and cold. While the cold variable is initialized with false, variable hot is not initialized. Hence, the program will give compilation error “The local variable hot may not have been initialized”
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) {
Integer i1 = new Integer(2), i2 = new Integer(2);
if(i1 == i2)
System.out.print("true");
else
System.out.print("false");
}
}
Correct answer.
Option 4 is the correct choice. The i1 and i2 are references of Integer class created with new keyword. Since they both refer to different objects, == returns false.
Incorrect answer.
Option 4 is the correct choice. The i1 and i2 are references of Integer class created with new keyword. Since they both refer to different objects, == returns false.
Question 7 of 10
7. 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 t1 = new Test();
Test t2 = new Test();
System.out.println(i);
}
static{
i++;
}
}
Correct answer.
Option 4 is the correct choice. Since the variable i is declared as a class level static member it will be assigned the default value 0. The variable i is incremented in the static initializer before main method is invoked.
However, remember that the static initializer is called only once (when the class is loaded first time) no matter how many objects are created. So program will print 1 when run.
Incorrect answer.
Option 4 is the correct choice. Since the variable i is declared as a class level static member it will be assigned the default value 0. The variable i is incremented in the static initializer before main method is invoked.
However, remember that the static initializer is called only once (when the class is loaded first time) no matter how many objects are created. So program will print 1 when run.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
final int i;
public static void main(String[] args){
System.out.println(i);
}
}
Correct answer.
Option 3 is the correct choice. The final variables need to be initialized before they can be accessed.
The final instance variable can be initialized either at the time of the declaration or in the constructor of the class. The final static variable can be initialized either at the time of the declaration or in the static initializer block. The local final variables can be initialized either at the time of declaration or assigned the value only once after declaration before it can be accessed.
The code will give compilation error “The blank final field i may not have been initialized”.
Incorrect answer.
Option 3 is the correct choice. The final variables need to be initialized before they can be accessed.
The final instance variable can be initialized either at the time of the declaration or in the constructor of the class. The final static variable can be initialized either at the time of the declaration or in the static initializer block. The local final variables can be initialized either at the time of declaration or assigned the value only once after declaration before it can be accessed.
The code will give compilation error “The blank final field i may not have been initialized”.
Question 9 of 10
9. Question
A variable declared by an interface can be changed by the class implementing it.
Correct answer.
False is the correct choice. All variables declared inside an interface are implicitly static and final. Its value cannot be changed by the class implementing it.
Incorrect answer.
False is the correct choice. All variables declared inside an interface are implicitly static and final. Its value cannot be changed by the class implementing it.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
static String str = "false";
public static void main(String[] args) {
if(str == false)
System.out.println("Nothing");
}
}
Correct answer.
Option 1 is the correct choice. The if statement compares string object with boolean literal which is not legal. Had it been str == “false”, the code would have printed “Nothing”. The code will give compilation error “Incompatible operand types String and boolean”.
Incorrect answer.
Option 1 is the correct choice. The if statement compares string object with boolean literal which is not legal. Had it been str == “false”, the code would have printed “Nothing”. The code will give compilation error “Incompatible operand types String and boolean”.