Java object oriented programming quiz part 3 contains 10 single multiple 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 3 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 will happen when you compile and run the following code?
class One{
private void className(){
System.out.println("Parent");
}
}
class Two extends One{
public void className(){
System.out.println("Child");
}
}
public class Test{
public static void main(String[] args){
One one = new Two();
one.className();
}
}
Correct answer.
Option 3 is the correct choice. Private methods cannot be seen from any other classes, hence they cannot be overridden in the child class.
Here the code declares reference of class One and assigns the object of class Two to it. In case of the method overriding, which method to call is decided at runtime depending upon the actual object the reference is pointing to. However, whether the method is visible or not is checked at the compile time. So, compiler will check whether the className method declared in class One is visible in the Test class, which is not because of the private declaration.
Hence the code will give compilation error “The method className() from the type One is not visible”.
Incorrect answer.
Option 3 is the correct choice. Private methods cannot be seen from any other classes, hence they cannot be overridden in the child class.
Here the code declares reference of class One and assigns the object of class Two to it. In case of the method overriding, which method to call is decided at runtime depending upon the actual object the reference is pointing to. However, whether the method is visible or not is checked at the compile time. So, compiler will check whether the className method declared in class One is visible in the Test class, which is not because of the private declaration.
Hence the code will give compilation error “The method className() from the type One is not visible”.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
class One{
private void callMe(){
System.out.println("Parent");
}
public void caller(){
callMe();
}
}
class Two extends One{
public void callMe(){
System.out.println("Child");
}
}
public class Test{
public static void main(String[] args){
Two two = new Two();
two.caller();
}
}
Correct answer.
Option 1 is the correct choice. Private methods cannot be seen from any other classes, hence they cannot be overridden in the child class.
The code tries to override the private method declared in the parent class. However since the method is declared private, the method in child class cannot override it. Instead it will be considered as a separate method with no relation to the parent class method.
The Two class has the caller method inherited from the parent class One which calls the callMe method. However, in absence of the overridden method of the child class, local private callMe method will be called which will print “Parent”.
Incorrect answer.
Option 1 is the correct choice. Private methods cannot be seen from any other classes, hence they cannot be overridden in the child class.
The code tries to override the private method declared in the parent class. However since the method is declared private, the method in child class cannot override it. Instead it will be considered as a separate method with no relation to the parent class method.
The Two class has the caller method inherited from the parent class One which calls the callMe method. However, in absence of the overridden method of the child class, local private callMe method will be called which will print “Parent”.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
class One{
public void callMe(){
System.out.println("Parent");
}
public void caller(){
callMe();
}
}
class Two extends One{
public void callMe(){
System.out.println("Child");
}
}
public class Test{
public static void main(String[] args){
Two two = new Two();
two.caller();
}
}
Correct answer.
Option 2 is the correct choice. The class Two overrides the callMe method of the parent class One.
The code creates an object of the Two class and calls the inherited method caller. However, since Two class has overridden callMe method of the One class, the overridden method will be called which will print “Child” when executed.
Incorrect answer.
Option 2 is the correct choice. The class Two overrides the callMe method of the parent class One.
The code creates an object of the Two class and calls the inherited method caller. However, since Two class has overridden callMe method of the One class, the overridden method will be called which will print “Child” when executed.
Question 4 of 10
4. Question
A private method defined in the parent class cannot be overridden by child class.
Correct answer.
True is the correct choice. A private method is not visible by any other class and hence it cannot be overridden in the child class. If you define a method with the same signature in the child class, there will not be any compilation error but the method declared in the child class will have no relation with the parent class method.
Incorrect answer.
True is the correct choice. A private method is not visible by any other class and hence it cannot be overridden in the child class. If you define a method with the same signature in the child class, there will not be any compilation error but the method declared in the child class will have no relation with the parent class method.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
class One{
public int method(){
System.out.println("int");
return 0;
}
}
class Two extends One{
public short method(){
System.out.println("short");
return 1;
}
}
public class Test{
public static void main(String[] args){
One one = new Two();
one.method();
}
}
Correct answer.
Option 3 is the correct choice. The overridden method in the child class can only return same or subclass of the return type declared in the method of the parent class. It cannot have any other return type.
The code will give compilation error “The return type is incompatible with One.method()”.
Incorrect answer.
Option 3 is the correct choice. The overridden method in the child class can only return same or subclass of the return type declared in the method of the parent class. It cannot have any other return type.
The code will give compilation error “The return type is incompatible with One.method()”.
Question 6 of 10
6. Question
Will the following code compile?
class One{
public void someMethod() throws Exception{
}
}
class Two extends One{
public void someMethod(){
}
}
Correct answer.
Yes is the correct choice. The overridden method in the child class can throw same exception, child exception of the exception thrown by the parent method or no exception at all.
The overridden method in the class Two does not throw any exception which is valid so the code will compile without any errors.
Incorrect answer.
Yes is the correct choice. The overridden method in the child class can throw same exception, child exception of the exception thrown by the parent method or no exception at all.
The overridden method in the class Two does not throw any exception which is valid so the code will compile without any errors.
Question 7 of 10
7. Question
Will the following code compile?
import java.io.IOException;
class One{
public void someMethod() throws IOException{
}
}
class Two extends One{
public void someMethod() throws Exception{
}
}
Correct answer.
No is the correct choice. The overridden method in the child class can throw same exception, child exception of the exception thrown by the parent method or no exception at all.
The overridden method in the class Two throws Exception which is not same or the child class of the IOException class thrown in the method of the parent class. So, the code will give compilation error “Exception Throwable is not compatible with throws clause in One.someMethod()”.
Incorrect answer.
No is the correct choice. The overridden method in the child class can throw same exception, child exception of the exception thrown by the parent method or no exception at all.
The overridden method in the class Two throws Exception which is not same or the child class of the IOException class thrown in the method of the parent class. So, the code will give compilation error “Exception Throwable is not compatible with throws clause in One.someMethod()”.
Question 8 of 10
8. Question
Will the following code compile?
import java.io.FileNotFoundException;
import java.io.IOException;
class One{
public void someMethod() throws IOException{
throw new IOException();
}
}
class Two extends One{
public void someMethod() throws FileNotFoundException{
throw new FileNotFoundException();
}
}
Correct answer.
Yes is the correct choice. The overridden method in the child class can throw same exception, child exception of the exception thrown by the parent method or no exception at all.
The overridden method in the class Two throws FileNotFoundException which is a child class of the IOException class thrown in the method of the parent class. So, the code will compile without any errors.
Incorrect answer.
Yes is the correct choice. The overridden method in the child class can throw same exception, child exception of the exception thrown by the parent method or no exception at all.
The overridden method in the class Two throws FileNotFoundException which is a child class of the IOException class thrown in the method of the parent class. So, the code will compile without any errors.
Question 9 of 10
9. Question
Does the class Two override the someMethod() correctly?
class One{
public void someMethod(int i){
}
}
class Two extends One{
public void someMethod(short s){
}
}
Correct answer.
No is the correct choice. The overridden method in the child class cannot have different parameters. The class Two declares someMethod with short parameter while someMethod has int parameter int the parent class.
Since the method in the child class has different parameter than that of the parent class it is not method overriding but method overloading.
Incorrect answer.
No is the correct choice. The overridden method in the child class cannot have different parameters. The class Two declares someMethod with short parameter while someMethod has int parameter int the parent class.
Since the method in the child class has different parameter than that of the parent class it is not method overriding but method overloading.
Question 10 of 10
10. Question
Select all method declarations that successfully override the someMethod() defined in the class One?
class One{
public void someMethod(int i) throws Exception{
}
}
class Two extends One{
//method declaration here
{
}
}
Correct answer.
Option 3 and 4 are the correct choice.
Option 1 is incorrect because of the syntax, void cannot come before the public. Option 2 is incorrect because it is reducing the visibility of the overridden method from public to default, which is not allowed.
Option 3 is correct. Overridden method in a child class can throw subclass of the exception thrown by the method of the parent class. Option 4 is also correct. Overridden method in a child class may not throw any exception at all.
Option 5 declares same method with different type of parameter in a child class which is method overloading not method overriding. It will not cause any compilation error, but it is still incorrect because questions is about overriding.
Incorrect answer.
Option 3 and 4 are the correct choice.
Option 1 is incorrect because of the syntax, void cannot come before the public. Option 2 is incorrect because it is reducing the visibility of the overridden method from public to default, which is not allowed.
Option 3 is correct. Overridden method in a child class can throw subclass of the exception thrown by the method of the parent class. Option 4 is also correct. Overridden method in a child class may not throw any exception at all.
Option 5 declares same method with different type of parameter in a child class which is method overloading not method overriding. It will not cause any compilation error, but it is still incorrect because questions is about overriding.