Java object oriented programming quiz part 4 contains 10 single and 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 4 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
Exception Handling0%
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{
void Two(){
System.out.print("Two,");
}
}
class Three extends Two{
public Three(){
System.out.print("Three,");
}
}
public class Test{
public static void main(String[] args){
Three three = new Three();
}
}
Correct answer.
Option 4 is the correct choice. Constructor of a class having a parent class always makes an implicit call to the parent class constructor using super(). The call to super is done before any code of the child constructor is executed.
The class Two has “void Two()”. Since it has a return type, it is considered as a method not a constructor.
The code creates an object of class Three which calls its constructor. However, class Three is extended from Two, so constructor of Three will call constructor of class Two before executing any of its code. The class Two does not have any constructor so default constructor will be provided by the compiler and will be executed.
However, class Two is extended from the First, so the constructor of the First will be called which will print “One,” followed by default constructor of the class Two having no code followed by remaining code of the Three constructor which will print “Three,”. The output of the code will be “One,Three,”.
Incorrect answer.
Option 4 is the correct choice. Constructor of a class having a parent class always makes an implicit call to the parent class constructor using super(). The call to super is done before any code of the child constructor is executed.
The class Two has “void Two()”. Since it has a return type, it is considered as a method not a constructor.
The code creates an object of class Three which calls its constructor. However, class Three is extended from Two, so constructor of Three will call constructor of class Two before executing any of its code. The class Two does not have any constructor so default constructor will be provided by the compiler and will be executed.
However, class Two is extended from the First, so the constructor of the First will be called which will print “One,” followed by default constructor of the class Two having no code followed by remaining code of the Three constructor which will print “Three,”. The output of the code will be “One,Three,”.
Question 2 of 10
2. Question
What all method1 method declarations are valid for TestChild class at the line number 11?
class TestException extends Exception{}
class TestChild1Exception extends TestException{}
class TestChild2Exception extends TestChild1Exception{}
public class Test{
public void method1() throws TestChild1Exception{
}
}
class TestChild extends Test{
//method declaration here
}
Correct answer.
Option 1, 2 and 3 are the correct choices. Overriding methods in a subclass can either throw the same exceptions as the method in the parent class, child exceptions of the exceptions thrown by the method in the parent class, or throw no exceptions at all. It cannot throw different exceptions or parent class of the exceptions thrown in the parent class method.
Option 1 is correct because it is not throwing any exception at all. Option 2 is correct because it is throwing same exception as those of the parent method. Option 3 is also correct because it is throwing child exception (i.e. TestChild2Exception) of the exception thrown by the method in the parent class (i.e. TestChild1Exception).
Option 4 is incorrect as it is throwing Exception which is indirect parent class of the TestChild1Exception class. Option 5 is also incorrect for the same reason, TestException being the parent class of the TestChild1Exception class.
Incorrect answer.
Option 1, 2 and 3 are the correct choices. Overriding methods in a subclass can either throw the same exceptions as the method in the parent class, child exceptions of the exceptions thrown by the method in the parent class, or throw no exceptions at all. It cannot throw different exceptions or parent class of the exceptions thrown in the parent class method.
Option 1 is correct because it is not throwing any exception at all. Option 2 is correct because it is throwing same exception as those of the parent method. Option 3 is also correct because it is throwing child exception (i.e. TestChild2Exception) of the exception thrown by the method in the parent class (i.e. TestChild1Exception).
Option 4 is incorrect as it is throwing Exception which is indirect parent class of the TestChild1Exception class. Option 5 is also incorrect for the same reason, TestException being the parent class of the TestChild1Exception class.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
public class Test{
final int i;
public static void main(String[] args){
System.out.println(i);
}
}
Correct answer.
Option 3 is the correct choice. The final variables need to be initialized before they can be accessed.
The final instance variable can be initialized either at the time of the declaration or in the constructor of the class. The final static variable can be initialized either at the time of the declaration or in the static initializer block. The local final variables can be initialized either at the time of declaration or assigned the value only once after declaration before it can be accessed.
The code will give compilation error “The blank final field i may not have been initialized”.
Incorrect answer.
Option 3 is the correct choice. The final variables need to be initialized before they can be accessed.
The final instance variable can be initialized either at the time of the declaration or in the constructor of the class. The final static variable can be initialized either at the time of the declaration or in the static initializer block. The local final variables can be initialized either at the time of declaration or assigned the value only once after declaration before it can be accessed.
The code will give compilation error “The blank final field i may not have been initialized”.
Question 4 of 10
4. Question
Will this code compile?
abstract class Test{
public void something(){
System.out.println("Something");
}
abstract public void nothing();
}
Correct answer.
Yes is the correct choice. If the class has one or more abstract methods, we have to declare the class as an abstract class. The abstract class can have non abstract methods. Since the Test class is declared as abstract, the code will compile without any errors.
Incorrect answer.
Yes is the correct choice. If the class has one or more abstract methods, we have to declare the class as an abstract class. The abstract class can have non abstract methods. Since the Test class is declared as abstract, the code will compile without any errors.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
boolean b1 = true;
boolean b2 = true;
if(b1 == b2){
System.out.print("==");
}
if(b1.equals(b2)){
System.out.print("equals");
}
}
}
Correct answer.
Option 4 is the correct choice. This is a trick question which makes you think of == vs equals method.
The boolean (with small b) data type is a primitive type which does not have equals method and hence the code will not compile. So options 1, 2 and 3 are incorrect. Boolean (with capital B) is a wrapper class which has equals method.
Incorrect answer.
Option 4 is the correct choice. This is a trick question which makes you think of == vs equals method.
The boolean (with small b) data type is a primitive type which does not have equals method and hence the code will not compile. So options 1, 2 and 3 are incorrect. Boolean (with capital B) is a wrapper class which has equals method.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test{
static String name = "Test";
public Test(){
name = "TestObject";
}
public static void main(String[] args){
System.out.println("Name is " + name);
}
}
Correct answer.
Option 1 is the correct choice. The name String variable is declared as static and initialized with string “Test”. The value of the name variable is changed in the constructor.
However, the class constructor is called only when an object is created. The code does not create any objects of the class Test, and hence the constructor is never called. So the value of the name variable remains unchanged.
Incorrect answer.
Option 1 is the correct choice. The name String variable is declared as static and initialized with string “Test”. The value of the name variable is changed in the constructor.
However, the class constructor is called only when an object is created. The code does not create any objects of the class Test, and hence the constructor is never called. So the value of the name variable remains unchanged.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
class One{
public One(){
System.out.println("One");
}
abstract void nothing();
}
public class Test{
public static void main(String[] args) {
One one = new One();
}
}
Correct answer.
Option 3 is the correct choice. If a class has one or more abstract methods, it must also be declared as an abstract class.
The One class has an abstract method nothing() but it is not declared as an abstract class. The code will give compilation error “The type One must be an abstract class to define abstract methods”.
Additionally, even after declaring the class as an abstract, we cannot create an object of the abstract class.
Incorrect answer.
Option 3 is the correct choice. If a class has one or more abstract methods, it must also be declared as an abstract class.
The One class has an abstract method nothing() but it is not declared as an abstract class. The code will give compilation error “The type One must be an abstract class to define abstract methods”.
Additionally, even after declaring the class as an abstract, we cannot create an object of the abstract class.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
abstract class One{
public One(){
System.out.println("One");
}
abstract void nothing();
}
public class Test{
public static void main(String[] args) {
One one = new One();
}
}
Correct answer.
Option 3 is the correct choice. The One class is declared as an abstract class. The object of an abstract class cannot be created.
The code will give compilation error “Cannot instantiate the type One”.
Incorrect answer.
Option 3 is the correct choice. The One class is declared as an abstract class. The object of an abstract class cannot be created.
The code will give compilation error “Cannot instantiate the type One”.
Question 9 of 10
9. 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(){
System.out.print("Hi,");
}
}
public class Test{
public static void main(String[] args) {
Two two = new Two();
}
}
Correct answer.
Option 3 is the correct choice. The One class has defined a constructor with one argument hence the compiler will not automatically provide the default constructor with no argument.
The class Two extends from the class One and has defined no argument constructor. The constructor of the child class implicitly calls the no argument constructor of the parent class as a first line of the code. Since the class One does not have constructor with no argument, the code will give compilation error “Implicit super constructor One() is undefined. Must explicitly invoke another constructor”.
To compile the code successfully, we have to either write no argument constructor in class One, or write super(“StringParameter”); as a first line in the constructor of the class Two.
Incorrect answer.
Option 3 is the correct choice. The One class has defined a constructor with one argument hence the compiler will not automatically provide the default constructor with no argument.
The class Two extends from the class One and has defined no argument constructor. The constructor of the child class implicitly calls the no argument constructor of the parent class as a first line of the code. Since the class One does not have constructor with no argument, the code will give compilation error “Implicit super constructor One() is undefined. Must explicitly invoke another constructor”.
To compile the code successfully, we have to either write no argument constructor in class One, or write super(“StringParameter”); as a first line in the constructor of the class Two.
Question 10 of 10
10. 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(){
System.out.print("Hi,");
super("Two");
}
}
public class Test{
public static void main(String[] args) {
Two two = new Two();
}
}
Correct answer.
Option 4 is the correct choice. The call to super must be the first line of the constructor.
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 call to super must be the first line of the constructor.
The code will give compilation error “Constructor call must be the first statement in a constructor”.