Java object oriented programming quiz part 9 contains 10 single choice questions. The Java OOPs questions will help you understand the OOPs concepts of the Java language. At the end of the quiz, result will be displayed along with your score and OOPs quiz answers online.
There is no time limit to complete the quiz. Click Start Quiz button to start the Java object oriented programming quiz 9 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
OOPs0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
What all variables you can access at line 12?
public class Test{
private int i = 1;
int j = 2;
public void TestMethod(){
class TestInner{
int k = 3;
final int l = 4;
public void InnerMethod(int m){
System.out.println(//variable name here);
}
}
}
}
Correct answer.
All the options are correct. A local inner class, a class declared inside a method, can access all the variables defined in the outer class. So variables i and j are accessible.
Till Java 1.7, only variables which are declared final are accessible inside the local inner class. However, from Java 1.8 onward, even non final variables which are effectively final can also be accessed inside the local inner class. Effective final variables means the non-final variables which are not changed after they are first assigned.
So if you use Java 1.7 or earlier versions, only variable l will be accessible at line 12. But for Java 1.8 onward, variables k, l and m will also be accessible.
Incorrect answer.
All the options are correct. A local inner class, a class declared inside a method, can access all the variables defined in the outer class. So variables i and j are accessible.
Till Java 1.7, only variables which are declared final are accessible inside the local inner class. However, from Java 1.8 onward, even non final variables which are effectively final can also be accessed inside the local inner class. Effective final variables means the non-final variables which are not changed after they are first assigned.
So if you use Java 1.7 or earlier versions, only variable l will be accessible at line 12. But for Java 1.8 onward, variables k, l and m will also be accessible.
Question 2 of 10
2. Question
What all variables you can access at line 9?
public class Test{
int x = 0;
static int y = 1;
static class InnerClass{
int z = 2;
public void getValue(){
//access variable here
}
}
}
Correct answer.
Option 2 and 3 are correct choices. A static inner class can only access static variables declared in the outer class. It cannot access non-static instance variables of the outer class. Hence, variable y is accessible inside the static inner class while variable x is not. The variable z is local to the inner class, so it is also accessible.
Incorrect answer.
Option 2 and 3 are correct choices. A static inner class can only access static variables declared in the outer class. It cannot access non-static instance variables of the outer class. Hence, variable y is accessible inside the static inner class while variable x is not. The variable z is local to the inner class, so it is also accessible.
Question 3 of 10
3. Question
An inner class can be declared private.
Correct answer.
True is the correct choice. An inner class can be declared as private.
Incorrect answer.
True is the correct choice. An inner class can be declared as private.
Question 4 of 10
4. Question
Will this code compile without any errors?
public class Main{
public void TestMethod(){
int a = 0;
class TestInner{
public void InnerMethod(final int x, int y){
System.out.println(a);
}
}
}
}
Correct answer.
Yes is the correct choice. From Java 1.8 onward, any final or effectively final variables can be accessed inside the local inner class. Here the variable a is not final, but it is not being changed once it is initialized with 0. That means it is effectively final and hence it can be accessed inside the inner class.
However, if you try to change the value of variable a inside the inner class, for example replacing the System.out.println(a) statement with a = y statement, it will report compilation error saying “local variables referenced from an inner class must be final or effectively final”. Because in this case variable a is no more effectively final, its being changed after it has been assigned.
Please note that for Java 1.7 or earlier version, the code will give compilation error.
Incorrect answer.
Yes is the correct choice. From Java 1.8 onward, any final or effectively final variables can be accessed inside the local inner class. Here the variable a is not final, but it is not being changed once it is initialized with 0. That means it is effectively final and hence it can be accessed inside the inner class.
However, if you try to change the value of variable a inside the inner class, for example replacing the System.out.println(a) statement with a = y statement, it will report compilation error saying “local variables referenced from an inner class must be final or effectively final”. Because in this case variable a is no more effectively final, its being changed after it has been assigned.
Please note that for Java 1.7 or earlier version, the code will give compilation error.
Question 5 of 10
5. Question
Will this code compile without any errors?
public class Test{
class InnerClass{
static int z = 1;
}
}
Correct answer.
No is the correct choice. A non-static inner class cannot have a static variable unless they are constant variables. Since variable z is not declared final along with the static, compiler will give error “The field z cannot be declared static in a non-static inner type, unless initialized with a constant expression”.
If you replace static int z = 1; statement with static final int z = 1; the code will compile without any errors.
Incorrect answer.
No is the correct choice. A non-static inner class cannot have a static variable unless they are constant variables. Since variable z is not declared final along with the static, compiler will give error “The field z cannot be declared static in a non-static inner type, unless initialized with a constant expression”.
If you replace static int z = 1; statement with static final int z = 1; the code will compile without any errors.
Question 6 of 10
6. Question
Will this code compile without any errors?
public class Test{
public void TestMethod(){
static class TestInner{
int x = 0;
}
}
}
Correct answer.
No is the correct choice. A class defined inside a method (also known as local inner class) cannot be declared static. The compiler will give error “Illegal modifier for the local class TestInner; only abstract or final is permitted”.
Incorrect answer.
No is the correct choice. A class defined inside a method (also known as local inner class) cannot be declared static. The compiler will give error “Illegal modifier for the local class TestInner; only abstract or final is permitted”.
Question 7 of 10
7. Question
Will this code compile without any errors?
public class Test{
public void method1(){
class TestInner{
public int x = 0;
}
}
public void method2(){
TestInner inner = new TestInner();
System.out.println(inner.x);
}
}
Correct answer.
No is the correct choice. A local inner class which is defined inside a method can only be instantiated inside that method. The code creates a class inside method1, but trying to create an object of it inside method2 which is not valid. The compiler will give error “TestInner cannot be resolved to a type”.
Incorrect answer.
No is the correct choice. A local inner class which is defined inside a method can only be instantiated inside that method. The code creates a class inside method1, but trying to create an object of it inside method2 which is not valid. The compiler will give error “TestInner cannot be resolved to a type”.
Question 8 of 10
8. Question
Will this code compile without any errors?
public class Test{
static int i = 0;
int j = 1;
public void TestMethod(){
class TestInner{
public void InnerMethod(){
System.out.println(i);
}
}
}
}
Correct answer.
Yes is the correct choice. A local inner class can access all the variables declared by outer class, static as well as non-static.
Incorrect answer.
Yes is the correct choice. A local inner class can access all the variables declared by outer class, static as well as non-static.
Question 9 of 10
9. Question
Will this code compile without any errors?
public class Test{
public class TestInner{
public void print(){
System.out.println("Hi");
}
}
public static void main(String[] args) {
TestInner testInner = new TestInner();
testInner.print();
}
}
Correct answer.
No is the correct choice. An object of a member inner class cannot be created without the object of the outer class. Valid syntax to create an object of the inner class is,
TestInner testInner = new Test().new TestInner();
Incorrect answer.
No is the correct choice. An object of a member inner class cannot be created without the object of the outer class. Valid syntax to create an object of the inner class is,
TestInner testInner = new Test().new TestInner();
Question 10 of 10
10. Question
A top level class cannot be static but inner class can be.
Correct answer.
True is the correct choice. A top level class cannot be declared static. However, you can have static inner class.
Incorrect answer.
True is the correct choice. A top level class cannot be declared static. However, you can have static inner class.