Java object oriented programming quiz part 5 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 5 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(String name){
System.out.print("Hello " + name + ",");
}
}
class Two extends One{
public Two(){
this("Two");
System.out.print("Hi,");
}
}
public class Test{
public static void main(String[] args) {
Two two = new Two();
}
}
Correct answer.
Option 4 is the correct choice. The parent class constructors do not get inherited by the child class. So the class Two does not have the constructor with the String argument.
The constructor of the class Two calls the constructor with the String parameter of the same class using this(“Two”) which is not defined. So the code will give compilation error “The constructor Two(String) is undefined”.
Incorrect answer.
Option 4 is the correct choice. The parent class constructors do not get inherited by the child class. So the class Two does not have the constructor with the String argument.
The constructor of the class Two calls the constructor with the String parameter of the same class using this(“Two”) which is not defined. So the code will give compilation error “The constructor Two(String) is undefined”.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
class One{
public One(String name){
System.out.print("Hello");
}
}
class Two extends One{
}
public class Test{
public static void main(String[] args) {
Two two = new Two();
}
}
Correct answer.
Option 3 is the correct choice. The class One has defined a constructor with String argument, hence the compiler will not provide the default no argument constructor for class One.
Since the class Two does not have any constructors defined, the compiler will provide the default no argument constructor. However, the default constructor of the class Two implicitly calls the no argument constructor of the class One which is not available.
The code will give compilation error “Implicit super constructor One() is undefined for default constructor. Must define an explicit constructor”.
Incorrect answer.
Option 3 is the correct choice. The class One has defined a constructor with String argument, hence the compiler will not provide the default no argument constructor for class One.
Since the class Two does not have any constructors defined, the compiler will provide the default no argument constructor. However, the default constructor of the class Two implicitly calls the no argument constructor of the class One which is not available.
The code will give compilation error “Implicit super constructor One() is undefined for default constructor. Must define an explicit constructor”.
Question 3 of 10
3. Question
Which of the following classes define the toString() method?
Correct answer.
Option 4 is the correct choice. The toString method is defined by Object, String and StringBuffer classes.
Incorrect answer.
Option 4 is the correct choice. The toString method is defined by Object, String and StringBuffer classes.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
class One{
private One(){
System.out.print("Parent");
}
}
class Two extends One{
public Two(){
System.out.print("Child");
}
}
public class Test{
public static void main(String[] args) {
Two two = new Two();
}
}
Correct answer.
Option 4 is the correct choice. The class One has a constructor which is private. The class Two extends class One and defines its own constructor.
When an object of the class Two is created, the constructor of the class Two implicitly calls the parent class constructor using super(). Since the constructor of the class One is declared as private, it cannot be invoked from any other class. So the code will give compilation error “Implicit super constructor One() is not visible. Must explicitly invoke another constructor”.
Incorrect answer.
Option 4 is the correct choice. The class One has a constructor which is private. The class Two extends class One and defines its own constructor.
When an object of the class Two is created, the constructor of the class Two implicitly calls the parent class constructor using super(). Since the constructor of the class One is declared as private, it cannot be invoked from any other class. So the code will give compilation error “Implicit super constructor One() is not visible. Must explicitly invoke another constructor”.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
class TestChild extends Test{
public TestChild(){
System.out.println("TestChild1");
}
}
public class Test{
void TestChild(){
System.out.println("TestChild2");
}
public static void main(String[] args) {
new Test().TestChild();
}
}
Correct answer.
Option 3 is the correct choice. The class TestChild extends the Test class. It is given only to create confusion.
The class Test has defined a method called TestChild. It is a method because it has a return type, not a constructor. The main method creates a new object of the Test class and invokes the TestChild method in a single statement. The code will output TestChild2 when executed.
Incorrect answer.
Option 3 is the correct choice. The class TestChild extends the Test class. It is given only to create confusion.
The class Test has defined a method called TestChild. It is a method because it has a return type, not a constructor. The main method creates a new object of the Test class and invokes the TestChild method in a single statement. The code will output TestChild2 when executed.
Question 6 of 10
6. 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 7 of 10
7. Question
Will this code compile?
public class Test{
public void count(){
}
}
class TestChild extends Test{
public void count(int i) throws Exception{
}
}
Correct answer.
Yes is the correct choice. The Test class defines a method count with no parameters. The TestChild class extends the Test class and defines a count method with one parameter. It is a valid method overloading and hence the code will compile without any errors.
Please make a note that since there is a difference in parameters, it is method overloading not overriding. The overridden method cannot throw different or super class of the exception thrown by the parent method. There is no such restrictions for the overloaded methods.
Incorrect answer.
Yes is the correct choice. The Test class defines a method count with no parameters. The TestChild class extends the Test class and defines a count method with one parameter. It is a valid method overloading and hence the code will compile without any errors.
Please make a note that since there is a difference in parameters, it is method overloading not overriding. The overridden method cannot throw different or super class of the exception thrown by the parent method. There is no such restrictions for the overloaded methods.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
One o = new Two();
o.sayHello();
}
}
final class One{
public void sayHello(){
System.out.println("Parent Hello");
}
}
class Two extends One{
public void sayHello(){
System.out.println("Child Hello");
}
}
Correct answer.
Option 3 is the correct choice. Since the class One is declared as final, it cannot be extended by any other class.
Code will give compilation error “The type Two cannot subclass the final class One”.
Incorrect answer.
Option 3 is the correct choice. Since the class One is declared as final, it cannot be extended by any other class.
Code will give compilation error “The type Two cannot subclass the final class One”.
Question 9 of 10
9. Question
Will this code compile?
public class Test{
public int count(int i){
return 0;
}
public long count(int i){
return 0;
}
}
Correct answer.
No is the correct choice. The overloaded methods cannot just differ in the return types. The Test class defines count method twice with only difference in the return types. So the code will give compilation error “Duplicate method count(int) in type Test”.
Incorrect answer.
No is the correct choice. The overloaded methods cannot just differ in the return types. The Test class defines count method twice with only difference in the return types. So the code will give compilation error “Duplicate method count(int) in type Test”.
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){
One o = new Two();
o.sayHello();
}
}
class One{
public final void sayHello(){
System.out.println("Parent Hello");
}
}
class Two extends One{
public void sayHello(){
System.out.println("Child Hello");
}
}
Correct answer.
Option 3 is the correct choice. Since the sayHello method in the One class is declared as final, it cannot be overridden in the child class Two.
The code will give compilation error “Cannot override the final method from One”.
Incorrect answer.
Option 3 is the correct choice. Since the sayHello method in the One class is declared as final, it cannot be overridden in the child class Two.
The code will give compilation error “Cannot override the final method from One”.