Java Declaration Initialization and Access Control Quiz 7
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 7 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%
Language Basics0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
Will this code compile?
public class Test{
final int i;
}
Correct answer.
No is the correct choice. Class level final variables must be initialized either at the time of declaration or in the constructor of the class. The code will give compilation error “The blank final field i may not have been initialized”.
Incorrect answer.
No is the correct choice. Class level final variables must be initialized either at the time of declaration or in the constructor of the class. The code will give compilation error “The blank final field i may not have been initialized”.
Question 2 of 10
2. Question
Will this code compile?
public class Test{
final int i;
Test(){
i = 5;
}
}
Correct answer.
Yes is the correct choice. Final member variables must be initialized either at declaration time or in the constructor. Here, the code initializes the final int i in the constructor so there will be no compilation error.
Incorrect answer.
Yes is the correct choice. Final member variables must be initialized either at declaration time or in the constructor. Here, the code initializes the final int i in the constructor so there will be no compilation error.
Question 3 of 10
3. Question
Will this code compile?
public class Test{
final int i;
int j;
Test(){
i = 5;
}
Test(int intvalue){
j = intvalue;
}
}
Correct answer.
No is the correct choice. Final member variable must be initialized either along with the declaration or in the constructor. If the class has more than one constructor, it must be initialized in all of them. Here, the constructor with int parameter does not initialize the variable i.
So the compiler will give error “The blank final field i may not have been initialized”.
Incorrect answer.
No is the correct choice. Final member variable must be initialized either along with the declaration or in the constructor. If the class has more than one constructor, it must be initialized in all of them. Here, the constructor with int parameter does not initialize the variable i.
So the compiler will give error “The blank final field i may not have been initialized”.
Question 4 of 10
4. Question
Will this code compile?
public class Test{
static final int i;
Test(){
Test.i = 5;
}
}
Correct answer.
No is the correct choice. The static final variables must be intialized in the static block if they are not initialized along with the declaration. The code does not have any static block which initializes the static int i. Hence the compiler will give error “The blank final field i may not have been initialized”.
Incorrect answer.
No is the correct choice. The static final variables must be intialized in the static block if they are not initialized along with the declaration. The code does not have any static block which initializes the static int i. Hence the compiler will give error “The blank final field i may not have been initialized”.
Question 5 of 10
5. Question
Will this code compile?
public class Test{
static final int i;
static{
i = 5;
}
}
Correct answer.
Yes is the correct choice. Static final variables must be initialized along with declaration or in the static block. Here code initializes the static variable in a static block so there will be no compilation error.
Incorrect answer.
Yes is the correct choice. Static final variables must be initialized along with declaration or in the static block. Here code initializes the static variable in a static block so there will be no compilation error.
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){
int i = 010;
System.out.println(i);
}
}
Correct answer.
Option 3 is the correct choice. The code will not give any compile time or run time error. If the number starts with 0, it is considered to be an octal number in Java. 010 is 8 in octal, so when you run the code, it will print 8 instead of 10.
Incorrect answer.
Option 3 is the correct choice. The code will not give any compile time or run time error. If the number starts with 0, it is considered to be an octal number in Java. 010 is 8 in octal, so when you run the code, it will print 8 instead of 10.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
public class Test{
static String name = "Test";
public Test(){
name = "TestObject";
}
public static void main(String[] args){
System.out.println("Name is " + name);
}
}
Correct answer.
Option 1 is the correct choice. The name String variable is declared as static and initialized with string “Test”. The value of the name variable is changed in the constructor.
However, the class constructor is called only when an object is created. The code does not create any objects of the class Test, and hence the constructor is never called. So the value of the name variable remains unchanged.
Incorrect answer.
Option 1 is the correct choice. The name String variable is declared as static and initialized with string “Test”. The value of the name variable is changed in the constructor.
However, the class constructor is called only when an object is created. The code does not create any objects of the class Test, and hence the constructor is never called. So the value of the name variable remains unchanged.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
float f = 10.2;
double d = 10.2;
if(f == d)
System.out.println("Same");
else
System.out.println("Not same");
}
}
Correct answer.
Option 3 is the correct choice. In Java, all the floating point literals like “10.2” are of type double by default. In order to assign this value to a float variable, the expression either needs an explicit cast or the literal needs to mention f or F at the end, for example, 10.2f.
The code will give compilation error at the line where the float variable is declared saying “Type mismatch: cannot convert from double to float”.
Incorrect answer.
Option 3 is the correct choice. In Java, all the floating point literals like “10.2” are of type double by default. In order to assign this value to a float variable, the expression either needs an explicit cast or the literal needs to mention f or F at the end, for example, 10.2f.
The code will give compilation error at the line where the float variable is declared saying “Type mismatch: cannot convert from double to float”.
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){
boolean b1 = false, b2 = null;
System.out.println( b1 && b2);
}
}
Correct answer.
Option 3 is the correct choice. The variables b1 and b2 are declared as boolean primitive values and cannot have null value, only object references can be assigned null value.
Hence the code will give compilation error “Type mismatch: cannot convert from null to boolean”.
Incorrect answer.
Option 3 is the correct choice. The variables b1 and b2 are declared as boolean primitive values and cannot have null value, only object references can be assigned null value.
Hence the code will give compilation error “Type mismatch: cannot convert from null to boolean”.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
import java.util.ArrayList;
import java.util.List;
public class Test{
public static void main(String[] args){
List listColors = new ArrayList<>();
listColors.add("Red");
listColors.add("Green");
listColors.add("Blue");
changeMe(listColors);
System.out.println(listColors);
}
private static void changeMe(final List listColors) {
listColors.add("Cyan");
listColors.remove("Blue");
}
}
Correct answer.
Option 2 is the correct choice. In case of a final primitive value, its value cannot be changed. In case of a final object reference, it cannot be pointed to another object. However, the object methods can be called which may change the object state or properties.
In the above code we cannot write listColors = new ArrayList(); in changeMe method, because listColors parameter is final and we cannot assign a new object to it. However, calling add or remove method on the same object is valid. There will be no compilation error.
Java passes method parameters as value. In this case, the object reference is copied. The copied reference still points to the same original object so any operation you do, apart from assigning the reference to a new object, will be reflected in the original object as well.
Incorrect answer.
Option 2 is the correct choice. In case of a final primitive value, its value cannot be changed. In case of a final object reference, it cannot be pointed to another object. However, the object methods can be called which may change the object state or properties.
In the above code we cannot write listColors = new ArrayList(); in changeMe method, because listColors parameter is final and we cannot assign a new object to it. However, calling add or remove method on the same object is valid. There will be no compilation error.
Java passes method parameters as value. In this case, the object reference is copied. The copied reference still points to the same original object so any operation you do, apart from assigning the reference to a new object, will be reflected in the original object as well.