Java Declaration Initialization and Access Control Quiz 2
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 2 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 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?
import java.io.FileNotFoundException;
interface TestInterface{
void sayHello();
}
public class Test implements TestInterface{
public void sayHello() throws FileNotFoundException{
System.out.println("File Not Found");
}
public static void main(String[] args) throws FileNotFoundException {
TestInterface t = new Test();
t.sayHello();
}
}
Correct answer.
Option 3 is the correct choice. When a class defines a method declared in an interface, it can
1) Throw the same exception declared in the interface
2) Throw no checked exception at all
3) Throw exceptions which are subclasses of the exception classes declared in an interface method
The sayHello method declared in TestInterface does not throw any exception but the method defined in the Test class throws FileNotFoundException which is checked exception. So the code will give compilation error “Exception FileNotFoundException is not compatible with throws clause in TestInterface.sayHello()”.
Incorrect answer.
Option 3 is the correct choice. When a class defines a method declared in an interface, it can
1) Throw the same exception declared in the interface
2) Throw no checked exception at all
3) Throw exceptions which are subclasses of the exception classes declared in an interface method
The sayHello method declared in TestInterface does not throw any exception but the method defined in the Test class throws FileNotFoundException which is checked exception. So the code will give compilation error “Exception FileNotFoundException is not compatible with throws clause in TestInterface.sayHello()”.
Question 2 of 10
2. Question
What will happen when you compile and run the following code with below given command?
java Test 1 2
interface ITestInterface{
int status = 0;
}
public class Test implements ITestInterface{
public static void main(String[] args){
if(args[0] == "1")
status = 0;
else if(args[0] == "2")
status = 1;
System.out.print(status);
}
}
Correct answer.
Option 4 is the correct choice. All the variables declared in an interface are implicitly final even if you do not declare them using final keyword.
Since the final variables cannot be changed once they are initialized, the code will give compilation error “The final field ITestInterface.status cannot be assigned”.
Incorrect answer.
Option 4 is the correct choice. All the variables declared in an interface are implicitly final even if you do not declare them using final keyword.
Since the final variables cannot be changed once they are initialized, the code will give compilation error “The final field ITestInterface.status cannot be assigned”.
Question 3 of 10
3. Question
Will this code compile without any errors?
import java.io.FileNotFoundException;
import java.io.IOException;
interface TestInterface{
void sayHello() throws IOException;
}
public class Test implements TestInterface{
public void sayHello() throws FileNotFoundException{
}
public static void main(String[] args) throws FileNotFoundException {
Test t = new Test();
t.sayHello();
}
}
Correct answer.
Yes is the correct choice. Since FileNotFoundException is a subclass of IOException, the method sayHello defined in class Test can throw it without causing any compilation error.
Incorrect answer.
Yes is the correct choice. Since FileNotFoundException is a subclass of IOException, the method sayHello defined in class Test can throw it without causing any compilation error.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
char charArray[] = new char[2];
System.out.println(charArray[1]);
}
}
Correct answer.
Option 2 is the correct choice. There is no compilation error in the code. Since the array reference is initialized with the new array object of size of 2, there will be no ArrayIndexOutOfBoundsException when you run the code.
The default value of char in Java is null character ‘\u0000’ not 0. So when you execute the code, output will be blank instead of 0.
Incorrect answer.
Option 2 is the correct choice. There is no compilation error in the code. Since the array reference is initialized with the new array object of size of 2, there will be no ArrayIndexOutOfBoundsException when you run the code.
The default value of char in Java is null character ‘\u0000’ not 0. So when you execute the code, output will be blank instead of 0.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test{
int i;
void print(){
System.out.println(i);
}
public static void main(String[] args) {
new Test().print();
}
}
Correct answer.
Option 2 is the correct choice. The main method of the class Test creates anonymous object of the Test class and invokes the print method of it in a single statement.
Since the variable i is an instance variable, it will be initialized with the default value 0. When you run the code, the print method will be invoked and will print value of i which is 0.
Incorrect answer.
Option 2 is the correct choice. The main method of the class Test creates anonymous object of the Test class and invokes the print method of it in a single statement.
Since the variable i is an instance variable, it will be initialized with the default value 0. When you run the code, the print method will be invoked and will print value of i which is 0.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
abstract class TestParent{
abstract int i;
}
public class Test extends TestParent{
int i = 0;
public static void main(String[] args) {
System.out.println(new Test().i);
}
}
Correct answer.
Option 4 is the correct choice. The abstract modifier is not a valid modifier for a variable. Only methods or classes can be abstract, not variables.
The code will give compilation error “Illegal modifier for the field i; only public, protected, private, static, final, transient & volatile are permitted”.
Incorrect answer.
Option 4 is the correct choice. The abstract modifier is not a valid modifier for a variable. Only methods or classes can be abstract, not variables.
The code will give compilation error “Illegal modifier for the field i; only public, protected, private, static, final, transient & volatile are permitted”.
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) {
byte b1 = 10, b2 = 5;
byte b3 = b1 / b2;
System.out.println(b3);
}
}
Correct answer.
Option 3 is the correct choice. Arithmetic operations involving byte, short and char data types are automatically promoted to int type in Java.
Since the result is automatically promoted to int value, it cannot be assigned back to the byte variable (without explicit cast). The code will give compilation error “Type mismatch: cannot convert from int to byte”.
Incorrect answer.
Option 3 is the correct choice. Arithmetic operations involving byte, short and char data types are automatically promoted to int type in Java.
Since the result is automatically promoted to int value, it cannot be assigned back to the byte variable (without explicit cast). The code will give compilation error “Type mismatch: cannot convert from int to byte”.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
abstract class TestParent{
abstract void main(String[] args);
static int i = 0;
}
public class Test extends TestParent{
static int i = 1;
public static void main(String[] args) {
System.out.println(i);
}
}
Correct answer.
Option 3 is the correct choice. The TestParent abstract class declares an abstract method named main. Since main is not a keyword, it is a valid method name.
The Test subclass has defined main method with static keyword. A subclass cannot define a static method with the same signature as the non-static method defined in the parent class. Here, the Test class has defined a main method with static keyword which is declared as non-static in the parent class TestParent.
The code will compilation error “This static method cannot hide the instance method from TestParent”.
Incorrect answer.
Option 3 is the correct choice. The TestParent abstract class declares an abstract method named main. Since main is not a keyword, it is a valid method name.
The Test subclass has defined main method with static keyword. A subclass cannot define a static method with the same signature as the non-static method defined in the parent class. Here, the Test class has defined a main method with static keyword which is declared as non-static in the parent class TestParent.
The code will compilation error “This static method cannot hide the instance method from TestParent”.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
public class Test{
private int i = 0;
class TestInner{
public int i = 1;
void sayHi(){
System.out.println(i);
}
}
public static void main(String[] args) {
TestInner inner = new Test().new TestInner();
inner.sayHi();
}
}
Correct answer.
Option 3 is the correct choice. There are no compilation errors in the code.
The class Test declares a private int variable i. The inner class TestInner also declares the int variable named i. Since both the variables have same name, the variable local to the inner class takes precedence. So the code will print 1 when run.
Incorrect answer.
Option 3 is the correct choice. There are no compilation errors in the code.
The class Test declares a private int variable i. The inner class TestInner also declares the int variable named i. Since both the variables have same name, the variable local to the inner class takes precedence. So the code will print 1 when run.
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){
int i = 10;
if( i < 10){
String str = "Less";
}else{
String str = "Not Less";
}
System.out.println(str);
}
}
Correct answer.
Option 3 is the correct choice. The String str variables are declared in the local blocks of if and else respectively. They are local to the block in which they are defined and hence they are not visible outside of their respective blocks of the code. The code tries to print the str variable outside the block in which they are defined so it will give compilation error “str cannot be resolved to a variable”.
Incorrect answer.
Option 3 is the correct choice. The String str variables are declared in the local blocks of if and else respectively. They are local to the block in which they are defined and hence they are not visible outside of their respective blocks of the code. The code tries to print the str variable outside the block in which they are defined so it will give compilation error “str cannot be resolved to a variable”.