Java object oriented programming quiz part 8 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 8 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?
class One{
public One(){
System.out.print("One");
}
}
class Two extends One{
public Two(){
System.out.print("Two");
}
}
class Three{
Two two = new Two();
public Three(){
System.out.print("Three");
}
}
public class Test{
public static void main(String[] args) {
Three three = new Three();
}
}
Correct answer.
Option 3 is the correct choice. When the class object is created, its constructor gets called. However, before the constructor body is executed, the static variables and initializer blocks are executed after which the super class constructor is called. Once that is done, all the instance variables and initializers are executed for that class. Once it is done, rest of the class constructor body is executed.
So, in our case, when the code creates an object of the class Three, its constructor is called. But before its body is executed, the object of the class Two is created which calls the constructor of the class Two. The constructor of the class Two calls the constructor of the class One first which will print “One” followed by “Two” by the constructor of the class Two. Once it is done, the code written in the constructor of the class Three will be executed and will print “Three”.
Option 3 is the correct choice. When the class object is created, its constructor gets called. However, before the constructor body is executed, the static variables and initializer blocks are executed after which the super class constructor is called. Once that is done, all the instance variables and initializers are executed for that class. Once it is done, rest of the class constructor body is executed.
So, in our case, when the code creates an object of the class Three, its constructor is called. But before its body is executed, the object of the class Two is created which calls the constructor of the class Two. The constructor of the class Two calls the constructor of the class One first which will print “One” followed by “Two” by the constructor of the class Two. Once it is done, the code written in the constructor of the class Three will be executed and will print “Three”.
Can you override public constructor declared in a base class to private in a child class?
Correct answer.
No is the correct choice. Constructor cannot be overridden at all since they are not inherited by the child class.
Incorrect answer.
No is the correct choice. Constructor cannot be overridden at all since they are not inherited by the child class.
Question 3 of 10
3. Question
A constructor can be final
Correct answer.
False is the correct choice. A constructor cannot be overridden by the child class and thus cannot be final.
Incorrect answer.
False is the correct choice. A constructor cannot be overridden by the child class and thus cannot be final.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
class TestInner{
public void print(){
System.out.println("Hi");
}
}
public class Test{
public static void main(String[] args) {
TestInner t = new TestInner(){
public void print(){
System.out.println("Hello");
}
};
t.print();
}
}
Correct answer.
Option 2 is the correct choice. The Test class has created an anonymous inner class which extends the TestInner class. The anonymous inner class overrides the print method from the TestInner class. So when the code invokes t.print(), the overridden print method gets called and it prints Hello.
Incorrect answer.
Option 2 is the correct choice. The Test class has created an anonymous inner class which extends the TestInner class. The anonymous inner class overrides the print method from the TestInner class. So when the code invokes t.print(), the overridden print method gets called and it prints Hello.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
class One{
String className = "One";
}
class Two extends One{
String className = "Two";
}
public class Test{
public static void main(String[] args) {
One one = new Two();
System.out.println(one.className);
}
}
Correct answer.
Option 1 is the correct choice. Unlike methods, the instance variables cannot be overridden by the subclass in Java. Since the reference is of type One, it always refer to the className instance variable defined by the class One. It does not matter which object the reference is pointing to.
So the code will print “One” when executed.
Incorrect answer.
Option 1 is the correct choice. Unlike methods, the instance variables cannot be overridden by the subclass in Java. Since the reference is of type One, it always refer to the className instance variable defined by the class One. It does not matter which object the reference is pointing to.
So the code will print “One” when executed.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
class One{
public One(){
System.out.print("One");
}
public One(String str){
System.out.print("OneStr");
}
}
class Two extends One{
public Two(String str){
System.out.print("TwoStr");
super(str);
}
}
public class Test{
public static void main(String[] args) {
Two two = new Two("Hello");
}
}
Correct answer.
Option 4 is the correct choice. The String argument constructor of the class Two tries to call the String argument constructor of the class One at second line of the code which is not allowed.
Any constructor call must be written before any other code in the constructor. So the code will give compilation error “Constructor call must be the first statement in a constructor”.
Incorrect answer.
Option 4 is the correct choice. The String argument constructor of the class Two tries to call the String argument constructor of the class One at second line of the code which is not allowed.
Any constructor call must be written before any other code in the constructor. So the code will give compilation error “Constructor call must be the first statement in a constructor”.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
public class Test{
public void doNothing(Object object){
System.out.println("Object");
}
public void doNothing(String string){
System.out.println("String");
}
public static void main(String[] args) {
Test test = new Test();
test.doNothing(args);
}
}
Correct answer.
Option 2 is the correct choice. Java array is an object. Since the command line arguments are passed to the Java program in form of a String array, the doNothing method with the Object will be called and “Object” will be printed.
If the doNothing method had String array argument instead of the String, the code would have printed String instead of Object.
Incorrect answer.
Option 2 is the correct choice. Java array is an object. Since the command line arguments are passed to the Java program in form of a String array, the doNothing method with the Object will be called and “Object” will be printed.
If the doNothing method had String array argument instead of the String, the code would have printed String instead of Object.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
public void doNothing(Object object){
System.out.println("Object");
}
public void doNothing(Integer i){
System.out.println("Integer");
}
public static void main(String[] args) {
Test test = new Test();
test.doNothing(null);
}
}
Correct answer.
Option 1 is the correct choice. During method invocation, Java always chooses the matching method with most specific parameters. Here, null can be accepted by both the overloaded methods. However, the Integer class is more specific than the Object class. So, the method with the Integer argument will be invoked.
Incorrect answer.
Option 1 is the correct choice. During method invocation, Java always chooses the matching method with most specific parameters. Here, null can be accepted by both the overloaded methods. However, the Integer class is more specific than the Object class. So, the method with the Integer argument will be invoked.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
public class Test{
public void doNothing(Object object){
System.out.println("Object");
}
public void doNothing(Integer i){
System.out.println("Integer");
}
public void doNothing(Double d){
System.out.println("Double");
}
public static void main(String[] args) {
Test test = new Test();
test.doNothing(null);
}
}
Correct answer.
Option 4 is the correct choice. During method invocation, Java always chooses the matching method with most specific parameters. Here, null can be accepted by all three overloaded methods. The Integer and Double classes are more specific than the Object class, however, none of them is more specific than the other. So the compiler cannot decide which version of the method (Integer or Double) to call.
The code will give compilation error “The method doNothing(Object) is ambiguous for the type Test”.
Incorrect answer.
Option 4 is the correct choice. During method invocation, Java always chooses the matching method with most specific parameters. Here, null can be accepted by all three overloaded methods. The Integer and Double classes are more specific than the Object class, however, none of them is more specific than the other. So the compiler cannot decide which version of the method (Integer or Double) to call.
The code will give compilation error “The method doNothing(Object) is ambiguous for the type Test”.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
public void doNothing(Object object){
System.out.println("Object");
}
public void doNothing(Integer i){
System.out.println("Integer");
}
public void doNothing(Double i){
System.out.println("Integer");
}
public static void main(String[] args) {
Test test = new Test();
test.doNothing(args);
}
}
Correct answer.
Option 1 is the correct choice. Since array is an Object in Java, the method with the Object parameter will be called and Object will be printed when the code is executed.
Incorrect answer.
Option 1 is the correct choice. Since array is an Object in Java, the method with the Object parameter will be called and Object will be printed when the code is executed.