Java Declaration Initialization and Access Control Quiz 4
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 4 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%
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{
public static void main(String[] args) {
int i = 10 + 011 + 12;
System.out.println(i);
}
}
Correct answer.
Option 1 is the correct choice. The numbers starting with 0 are considered octal numbers in Java. So the number 011 is considered as octal which equals to 9 in decimal. So the total becomes 10 + 9 + 12 = 31.
Incorrect answer.
Option 1 is the correct choice. The numbers starting with 0 are considered octal numbers in Java. So the number 011 is considered as octal which equals to 9 in decimal. So the total becomes 10 + 9 + 12 = 31.
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 i = 10+0x12;
System.out.println(i);
}
}
Correct answer.
Option 4 is the correct choice. The numbers starting with 0x is considered as hexadecimal numbers in Java. The number 0x12 is considered as hexadecimal which equals to 18 in decimal. So the total will be 10 + 18 = 28.
Incorrect answer.
Option 4 is the correct choice. The numbers starting with 0x is considered as hexadecimal numbers in Java. The number 0x12 is considered as hexadecimal which equals to 18 in decimal. So the total will be 10 + 18 = 28.
Question 3 of 10
3. Question
A constructor cannot throw any exception. It must handle all the exception itself using try catch block.
Correct answer.
False is the correct choice. A constructor can throw exceptions just like the methods in Java.
Incorrect answer.
False is the correct choice. A constructor can throw exceptions just like the methods in Java.
Question 4 of 10
4. Question
A constructor can be synchronized.
Correct answer.
False is the correct choice. Only public, protected or private modifier is allowed for a class constructor. The code will give compilation error “Illegal modifier for the constructor in type ; only public, protected & private are permitted”.
Incorrect answer.
False is the correct choice. Only public, protected or private modifier is allowed for a class constructor. The code will give compilation error “Illegal modifier for the constructor in type ; only public, protected & private are permitted”.
Question 5 of 10
5. Question
An interface cannot define an inner class.
Correct answer.
False is the correct choice. An interface can define an inner class in Java.
Incorrect answer.
False is the correct choice. An interface can define an inner class in Java.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test{
static String time = getTime();
static String currentTime = "5:30";
private static String getTime() {
return currentTime;
}
public static void main(String[] args) {
System.out.println(time);
}
}
Correct answer.
Option 1 is the correct choice. Instance variables of a class are initialized in the textual order in which they appear in the code. Since the String variable time is declared before the currentTime variable, it will be initialized first.
So getTime method is called which prints the value of currentTime variable. However, the currentTime variable is not initialized yet, so it will print default value. For object reference, the default value is null. So null will be printed when code is run.
Incorrect answer.
Option 1 is the correct choice. Instance variables of a class are initialized in the textual order in which they appear in the code. Since the String variable time is declared before the currentTime variable, it will be initialized first.
So getTime method is called which prints the value of currentTime variable. However, the currentTime variable is not initialized yet, so it will print default value. For object reference, the default value is null. So null will be printed when code is run.
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){
int[] intarr = new int[2]{4, 5, 6};
for(int i = 0; i < intarr.length; i++)
System.out.print(intarr[i]);
}
}
Correct answer.
Option 3 is the correct choice. We cannot specify the size of an array along with array initialization values using curly braces.
Correct way to declare an array along with the initialization values would be int[] intarr = new int[]{4, 5, 6} without the size 2.
Incorrect answer.
Option 3 is the correct choice. We cannot specify the size of an array along with array initialization values using curly braces.
Correct way to declare an array along with the initialization values would be int[] intarr = new int[]{4, 5, 6} without the size 2.
Question 8 of 10
8. Question
Will this code compile?
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
final ArrayList aList =
new ArrayList();
aList.add("Hello");
}
}
Correct answer.
True is the correct choice. Making object reference final does not allow us to change the object it refers to later on. However, we can modify the object itself by invoking its methods.
The statement “aList = new ArrayList();” anywhere after line 6 will give compilation error.
Incorrect answer.
True is the correct choice. Making object reference final does not allow us to change the object it refers to later on. However, we can modify the object itself by invoking its methods.
The statement “aList = new ArrayList();” anywhere after line 6 will give compilation error.
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 = 1, j = 2;
swap(i, j);
System.out.println(i + " " + j);
}
private static void swap(int i, int j) {
int x = i;
i = j;
j = x;
}
}
Correct answer.
Option 1 is the correct choice. Java is a “pass by value” language. It means it copies value during the method calls. Here, code swaps the numbers in swap method. However, the original variables declared in main method still retain their value as is. So the code will print “12” when executed.
Incorrect answer.
Option 1 is the correct choice. Java is a “pass by value” language. It means it copies value during the method calls. Here, code swaps the numbers in swap method. However, the original variables declared in main method still retain their value as is. So the code will print “12” when executed.
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){
double d = 10.0/0;
System.out.println(d);
}
}
Correct answer.
Option 4 is the correct choice. The code will not throw any exception and it will print “Infinity” when run.
Java float and double types can represent invalid values like positive infinity, negative infinity and NaN (not a number) value while int type cannot. When we run 10/0, it throws ArithmeticException as it is an integer operation but when we run 10.0/0, the operation is of floating point type which has infinity constant defined.
Incorrect answer.
Option 4 is the correct choice. The code will not throw any exception and it will print “Infinity” when run.
Java float and double types can represent invalid values like positive infinity, negative infinity and NaN (not a number) value while int type cannot. When we run 10/0, it throws ArithmeticException as it is an integer operation but when we run 10.0/0, the operation is of floating point type which has infinity constant defined.