Java object oriented programming quiz part 1 contains 10 single and multiple choice questions. Java object oriented programming questions are designed in such a way that it will help you understand 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 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{
int i = 1;
public int getInt(){
return i;
}
}
class Two extends One{
int i = 2;
public int getInt(){
return i;
}
}
public class Test{
public static void main(String[] args) {
One one = new One();
Two two = (Two)one;
System.out.println( two.getInt() );
}
}
Correct answer.
Option 4 is the correct choice. The code will give Runtime error “Exception in thread “main” java.lang.ClassCastException: One cannot be cast to Two”.
In Java, an object of a parent class cannot be cast down to object of the child class. However, if you have created an object like this,
One one = new Two();
Two two = (Two) one;
it will not give an error, because the object being referenced by “one” reference is of class Two. Even though the reference is of type parent class One, the object it is pointing to is of child class i.e. Two.
Incorrect answer.
Option 4 is the correct choice. The code will give Runtime error “Exception in thread “main” java.lang.ClassCastException: One cannot be cast to Two”.
In Java, an object of a parent class cannot be cast down to object of the child class. However, if you have created an object like this,
One one = new Two();
Two two = (Two) one;
it will not give an error, because the object being referenced by “one” reference is of class Two. Even though the reference is of type parent class One, the object it is pointing to is of child class i.e. Two.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
class One{
public static void print(){
System.out.println("1");
}
}
class Two extends One{
public static void print(){
System.out.println("2");
}
}
public class Test{
public static void main(String args[]){
One one = new Two();
one.print();
}
}
Correct answer.
Option 1 is the correct choice. The code will output 1 when run.
The reference of a parent class can refer to the object of the child class. However, reverse is not true, i.e. reference of a child class cannot point to the object of a parent class (i.e. Two two = new One(); will give compilation error). So there will be no compilation or runtime error.
Static methods are defined at the class level. Even if the parent reference is pointing the child object, calling static method on it will always call the method defined by the parent class because the reference “one” is of type One.
If the print methods in both the classes were defined as instance methods, the output would have been 2 instead of 1.
Incorrect answer.
Option 1 is the correct choice. The code will output 1 when run.
The reference of a parent class can refer to the object of the child class. However, reverse is not true, i.e. reference of a child class cannot point to the object of a parent class (i.e. Two two = new One(); will give compilation error). So there will be no compilation or runtime error.
Static methods are defined at the class level. Even if the parent reference is pointing the child object, calling static method on it will always call the method defined by the parent class because the reference “one” is of type One.
If the print methods in both the classes were defined as instance methods, the output would have been 2 instead of 1.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
class One{
public static void print(int i){
System.out.println("Parent");
}
}
class Two extends One{
public static void print(byte b){
System.out.println("Child");
}
}
public class Test{
public static void main(String args[]){
One one = new Two();
one.print(10);
}
}
Correct answer.
Option 1 is the correct choice. Polymorphism for static methods does not work in the same way as instance methods.
Static methods are defined at the class level. So if the reference is of type parent, static method defined by parent class will be called regardless of whether the reference is pointing to the object of a parent class or a child class.
Incorrect answer.
Option 1 is the correct choice. Polymorphism for static methods does not work in the same way as instance methods.
Static methods are defined at the class level. So if the reference is of type parent, static method defined by parent class will be called regardless of whether the reference is pointing to the object of a parent class or a child class.
Question 4 of 10
4. Question
Will this code compile successfully?
class One{
public void process(){
System.out.println("Parent");
}
}
public abstract class Test extends One{
public abstract void process();
}
Correct answer.
Yes is the correct choice. In Java, a child class can make the method abstract which is inherited from the parent class.
Incorrect answer.
Yes is the correct choice. In Java, a child class can make the method abstract which is inherited from the parent class.
Question 5 of 10
5. Question
What option if written at the line 12 will compile and run without any errors?
interface Inter{}
class One{}
class Two extends One{}
class Three extends One{}
class Four extends Two implements Inter{}
class Five extends Four{}
class Six extends Three{}
public class Test{
public static void main(String[] args) {
//your code here
}
}
Correct answer.
Options 2 and 3 are the correct choices.
The Six class extends Three class which extends One class. The Inter interface is not a direct or indirect parent of class Six, hence the reference of the Inter interface cannot point to the object of class Six.
The One class is indirect parent class of the class Five (Five => Four => Two => One). Hence, the reference of the One class can point to the reference of the Five class.
Four class extends the Two class which implements the Inter interface. So the reference of the Inter interface can point to the object of the Four class.
The Three class is not a direct or indirect parent of the Four class. Hence, the reference of the Three class cannot point to the object of the Four class.
Incorrect answer.
Options 2 and 3 are the correct choices.
The Six class extends Three class which extends One class. The Inter interface is not a direct or indirect parent of class Six, hence the reference of the Inter interface cannot point to the object of class Six.
The One class is indirect parent class of the class Five (Five => Four => Two => One). Hence, the reference of the One class can point to the reference of the Five class.
Four class extends the Two class which implements the Inter interface. So the reference of the Inter interface can point to the object of the Four class.
The Three class is not a direct or indirect parent of the Four class. Hence, the reference of the Three class cannot point to the object of the Four class.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
class One{
}
class Two extends One{
public void print(){
System.out.println("Two");
}
}
public class Test{
public static void main(String[] args) {
One one = new Two();
one.print();
}
}
Correct answer.
Option 3 is the correct choice. The code will give compilation error “The method print() is undefined for the type One”.
If a reference of a parent class is pointing to the object of a child class, it can only call methods defined by the parent class. Which method needs to be called will be decided at runtime depending upon the object the reference is pointing to.
Incorrect answer.
Option 3 is the correct choice. The code will give compilation error “The method print() is undefined for the type One”.
If a reference of a parent class is pointing to the object of a child class, it can only call methods defined by the parent class. Which method needs to be called will be decided at runtime depending upon the object the reference is pointing to.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
class One{
int x;
public One(int x){
this.x = x;
}
}
public class Test{
public static void main(String[] args){
One one = new One();
System.out.println(one.x);
}
}
Correct answer.
Option 3 is the correct choice. The code will give compilation error.
Java compiler provides default no argument constructor if and only if no other constructors are defined for the class. In the code, the class One has defined a constructor with int argument. Hence, the default constructor is not provided automatically by the compiler.
The code tries to create an object of class One using default no argument constructor which is not available. Hence, the code will give compilation error “The constructor One() is undefined”.
Incorrect answer.
Option 3 is the correct choice. The code will give compilation error.
Java compiler provides default no argument constructor if and only if no other constructors are defined for the class. In the code, the class One has defined a constructor with int argument. Hence, the default constructor is not provided automatically by the compiler.
The code tries to create an object of class One using default no argument constructor which is not available. Hence, the code will give compilation error “The constructor One() is undefined”.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
class One{
public One(int x){
System.out.print("int constructor");
}
public One(long l){
System.out.print("long constructor");
}
}
public class Test{
public static void main(String[] args){
long l = 20l;
One one = new One(l);
}
}
Correct answer.
Option 2 is the correct choice. Just like the methods, constructors can also be overloaded for different type or number of parameters.
The code creates an object with long parameter, so constructor with the long argument will be used to create an object of class One.
Incorrect answer.
Option 2 is the correct choice. Just like the methods, constructors can also be overloaded for different type or number of parameters.
The code creates an object with long parameter, so constructor with the long argument will be used to create an object of class One.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
class One{
public One(){}
public One(long l){
System.out.print("long constructor");
}
}
class Two extends One{
public One(int i){
System.out.print("int constructor");
}
}
public class Test{
public static void main(String[] args){
long l = 20l;
One one = new Two(l);
}
}
Correct answer.
Option 3 is the correct choice. Unlike methods, constructors are not inherited by the child class and hence they cannot be overridden.
The code tries to override a constructor of the parent class One in the class Two. Since the constructors are not inherited in the child class, the Java compiler will consider it as a method. However, method needs a return type, so the code will give compilation error “Return type for the method is missing”.
Additionally, there is no constructor for class Two with the long argument, hence compiler will also give error “The constructor Two(long) is undefined”.
Incorrect answer.
Option 3 is the correct choice. Unlike methods, constructors are not inherited by the child class and hence they cannot be overridden.
The code tries to override a constructor of the parent class One in the class Two. Since the constructors are not inherited in the child class, the Java compiler will consider it as a method. However, method needs a return type, so the code will give compilation error “Return type for the method is missing”.
Additionally, there is no constructor for class Two with the long argument, hence compiler will also give error “The constructor Two(long) is undefined”.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
class One{
public One(int x){
System.out.print("int constructor");
}
public One(char c){
System.out.print("char constructor");
}
void One(String str){
System.out.print("String constructor");
}
}
public class Test{
public static void main(String[] args){
String ch = "c";
One one = new One(ch);
}
}
Correct answer.
Option 4 is the correct choice. Java constructors do not have return type. If you specify the return type, it automatically becomes a method instead of constructor.
The code will give compilation error “The constructor One(String) is undefined”.
Incorrect answer.
Option 4 is the correct choice. Java constructors do not have return type. If you specify the return type, it automatically becomes a method instead of constructor.
The code will give compilation error “The constructor One(String) is undefined”.