Java object oriented programming quiz part 7 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 7 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
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{
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 2 of 10
2. Question
What will happen when you compile and run the following code?
class One{
public final One(){
System.out.print(" One ");
}
}
class Two extends One{
public Two(){
System.out.print(" Two ");
}
}
public class Test{
public static void main(String[] args) {
One one = new One();
Two two = new Two();
}
}
Correct answer.
Option 3 is the correct choice. A class constructor cannot be final. Additionally, the constructor cannot be abstract or native either.
The code will give compilation error “Illegal modifier for the constructor in type One; only public, protected & private are permitted”.
Incorrect answer.
Option 3 is the correct choice. A class constructor cannot be final. Additionally, the constructor cannot be abstract or native either.
The code will give compilation error “Illegal modifier for the constructor in type One; only public, protected & private are permitted”.
Question 3 of 10
3. Question
Will this code compile?
class Test{
interface TestInterface{
}
}
Correct answer.
Yes is the correct choice. Defining an interface inside a class is valid in Java.
Incorrect answer.
Yes is the correct choice. Defining an interface inside a class is valid in Java.
Question 4 of 10
4. Question
Will this code compile?
interface TestInterface{
class Test{
}
}
Correct answer.
Yes is the correct choice. Defining an inner class inside an interface is valid in Java.
Incorrect answer.
Yes is the correct choice. Defining an inner class inside an interface is valid in Java.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
class One{
public abstract One();
}
class Two extends One{
public Two(){
System.out.println("Two");
}
}
public class Test{
public static void main(String[] args) {
Two two = new Two();
}
}
Correct answer.
Option 2 is the correct choice. In Java, constructor cannot be abstract, final or native. The code will give compilation error.
Incorrect answer.
Option 2 is the correct choice. In Java, constructor cannot be abstract, final or native. The code will give compilation error.
Question 6 of 10
6. Question
Will this code compile?
interface TestInterface{
interface Test{
}
}
Correct answer.
Yes is the correct choice. Defining an interface inside another interface is valid in Java.
Incorrect answer.
Yes is the correct choice. Defining an interface inside another interface is valid in Java.
Question 7 of 10
7. Question
What will be the generated class file name when you compile the following class?
public class Test{
class TestInner{
}
}
Correct answer.
Option 3 is the correct choice. The format for the inner class file name is OuterClass$InnerClass. The generated class file name for the TestInner class will be Test$TestInner.class
Incorrect answer.
Option 3 is the correct choice. The format for the inner class file name is OuterClass$InnerClass. The generated class file name for the TestInner class will be Test$TestInner.class
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
class One{
int i = 1;
public One(){
System.out.println(i);
}
}
class Two extends One{
int i = 10;
public One(){
System.out.println(i);
}
}
public class Test{
public static void main(String[] args) {
One one = new Two();
}
}
Correct answer.
Option 3 is the correct choice. Constructors cannot be overridden, as it is not inherited in the child class.
The code tries to override the constructor of the parent class One, but since it is not inherited in class Two, Java treats it as a method. However, a method needs a return type which is missing. So, the code will give compilation error “Return type for the method is missing”.
Incorrect answer.
Option 3 is the correct choice. Constructors cannot be overridden, as it is not inherited in the child class.
The code tries to override the constructor of the parent class One, but since it is not inherited in class Two, Java treats it as a method. However, a method needs a return type which is missing. So, the code will give compilation error “Return type for the method is missing”.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
class One{}
class Two extends One{}
public class Test extends One{
public static void main(String[] args) {
One one = new One();
Two two = new Two();
System.out.println(one instanceOf Two);
}
}
Correct answer.
Option 4 is the correct choice. The correct syntax of the operator is instanceof with small o. The code will give compilation error “Syntax error on token “instanceOf”, instanceof expected”.
If it was instanceof instead of instanceOf, the output would have been false. That is because the class one is not direct or indirect child of the class Two.
Incorrect answer.
Option 4 is the correct choice. The correct syntax of the operator is instanceof with small o. The code will give compilation error “Syntax error on token “instanceOf”, instanceof expected”.
If it was instanceof instead of instanceOf, the output would have been false. That is because the class one is not direct or indirect child of the class Two.
Question 10 of 10
10. Question
Is it mandatory to give inner class a name?
Correct answer.
No is the correct choice. An inner class can be created without giving any name. Such inner class is called an anonymous inner class.
Incorrect answer.
No is the correct choice. An inner class can be created without giving any name. Such inner class is called an anonymous inner class.