Java Declaration Initialization and Access Control Quiz 9
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 9 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%
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{
public static void main(String[] args) {
int[] intArray;
System.out.println(intArray[0]);
}
}
Correct answer.
Option 3 is the correct choice. Since the array is declared as local to the method, it must be initialized before it can be used. The compiler will give error “The local variable intArray may not have been initialized”.
Incorrect answer.
Option 3 is the correct choice. Since the array is declared as local to the method, it must be initialized before it can be used. The compiler will give error “The local variable intArray may not have been initialized”.
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) {
int[] intArray = new int[2];
System.out.println(intArray[0]);
}
}
Correct answer.
Option 1 is the correct choice. Since arrays are Objects in java, its elements are assigned a default value when the array is initialized which is 0 for type int. The code will print 0 when run.
Incorrect answer.
Option 1 is the correct choice. Since arrays are Objects in java, its elements are assigned a default value when the array is initialized which is 0 for type int. The code will print 0 when run.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
public class Test{
int i;
Test(int i){
this.i = i;
}
System.out.print(this.i);
public static void main(String[] args) {
Test t1 = new Test(1);
Test t2 = new Test(2);
}
}
Correct answer.
Option 3 is the correct choice. We cannot write statements directly inside the class. The code has System.out.println statement which is not inside a method, constructor, static block or instance initializer block which will give compilation error.
Incorrect answer.
Option 3 is the correct choice. We cannot write statements directly inside the class. The code has System.out.println statement which is not inside a method, constructor, static block or instance initializer block which will give compilation error.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test{
boolean b;
public static void main(String[] args) {
System.out.print(b);
}
}
Correct answer.
Option 4 is the correct choice. Class level variables are assigned a default value which is false for boolean, so variable b will be initialized as false. However, since it is not declared as static, the static main method cannot access it directly without object reference. The code will give compilation error “Cannot make a static reference to the non-static field b”.
Incorrect answer.
Option 4 is the correct choice. Class level variables are assigned a default value which is false for boolean, so variable b will be initialized as false. However, since it is not declared as static, the static main method cannot access it directly without object reference. The code will give compilation error “Cannot make a static reference to the non-static field b”.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test{
static int[] intarray = new int[5];
public static void main(String[] args) {
for(int i = 0; i < intarray.size(); i++)
System.out.print(intarray[i]);
}
}
Correct answer.
Option 4 is the correct choice. All the elements of the array intArray will be initialized with default value 0. Size of an array is determined by its length property. Arrays do not have method named size(). Correct way to get size of an array is intArray.length. The code will give compilation error “Cannot invoke size() on the array type int[]”
Incorrect answer.
Option 4 is the correct choice. All the elements of the array intArray will be initialized with default value 0. Size of an array is determined by its length property. Arrays do not have method named size(). Correct way to get size of an array is intArray.length. The code will give compilation error “Cannot invoke size() on the array type int[]”
Question 6 of 10
6. Question
Will this code compile?
abstract class Test{
public Test(){
System.out.println("Constructor");
}
}
Correct answer.
Yes is the correct choice. An abstract class may not have any abstract method. The code will compile without any error. Remember that you still cannot create an object of such class even if it does not have any abstract method.
Incorrect answer.
Yes is the correct choice. An abstract class may not have any abstract method. The code will compile without any error. Remember that you still cannot create an object of such class even if it does not have any abstract method.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
public class Test {
static int i = 5;
public static void main(String[] args){
int i = 6, j = 5;
System.out.println(i*j+5);
}
}
Correct answer.
Option 3 is the correct choice. Variable i is defined twice, one at class level as static and another inside the main method as local variable. Local variable takes precedence over the class level variable. Plus, multiplication is done before addition, so expression will be evaluated like (6*5)+5, i.e. 35.
Incorrect answer.
Option 3 is the correct choice. Variable i is defined twice, one at class level as static and another inside the main method as local variable. Local variable takes precedence over the class level variable. Plus, multiplication is done before addition, so expression will be evaluated like (6*5)+5, i.e. 35.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
import java.util.ArrayList;
public class Test {
public static void main(String[] args){
ArrayList aListColors = new ArrayList<>();
aListColors.add("Red");
aListColors.add("Blue");
aListColors.add(5*5);
System.out.println(aListColors);
}
}
Correct answer.
Option 2 is the correct choice. We declared ArrayList without any type, so any object can be added to it. Java automatically autobox 5*5 i.e. 25 to Integer object and will add to the ArrayList object. So when we run the code [Red, Blue, 25] will be printed.
Incorrect answer.
Option 2 is the correct choice. We declared ArrayList without any type, so any object can be added to it. Java automatically autobox 5*5 i.e. 25 to Integer object and will add to the ArrayList object. So when we run the code [Red, Blue, 25] will be printed.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
import java.util.ArrayList;
public class Test {
public static void main(String[] args){
ArrayList aListNumbers = new ArrayList<>();
aListNumbers.add(1);
aListNumbers.add(2);
System.out.println( aListNumbers.get(0)+aListNumbers.get(1) );
}
}
Correct answer.
Option 2 is the correct choice. The ArrayList object is created without any type so any object can be added to it. However, in that case get method also returns Object. The + operator cannot be applied to Object operands so compiler will give error “The operator + is undefined for the argument type(s) java.lang.Object, java.lang.Object”.
For this code to work, either explicit cast needs to be done or ArrayList should be declared with numeric type for e.g.
ArrayList<Integer> aListNumbers = new ArrayList<>();
Incorrect answer.
Option 2 is the correct choice. The ArrayList object is created without any type so any object can be added to it. However, in that case get method also returns Object. The + operator cannot be applied to Object operands so compiler will give error “The operator + is undefined for the argument type(s) java.lang.Object, java.lang.Object”.
For this code to work, either explicit cast needs to be done or ArrayList should be declared with numeric type for e.g.
ArrayList<Integer> aListNumbers = new ArrayList<>();
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
static boolean b;
public static void main(String[] args) {
if(b = false)
System.out.println(false);
else
System.out.println(true);
}
}
Correct answer.
Option 1 is the correct choice. The if condition statement assigns false to the boolean variable b, which returns false. So the control will go to else block and true will be printed.
Incorrect answer.
Option 1 is the correct choice. The if condition statement assigns false to the boolean variable b, which returns false. So the control will go to else block and true will be printed.