Java object oriented programming quiz part 2 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 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 with below given command?
java Test 1 2
interface ITestInterface{
int status = 0;
}
public class Test implements ITestInterface{
public static void main(String[] args){
if(args[0] == "1")
status = 0;
else if(args[0] == "2")
status = 1;
System.out.print(status);
}
}
Correct answer.
Option 4 is the correct choice. All the variables declared in an interface are implicitly final even if you do not declare them using final keyword.
Since the final variables cannot be changed once they are initialized, the code will give compilation error “The final field ITestInterface.status cannot be assigned”.
Incorrect answer.
Option 4 is the correct choice. All the variables declared in an interface are implicitly final even if you do not declare them using final keyword.
Since the final variables cannot be changed once they are initialized, the code will give compilation error “The final field ITestInterface.status cannot be assigned”.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
interface ITestInterface{
int i = 0;
public ITestInterface(){
System.out.print(" Interface Constructor ");
}
}
public class Test implements ITestInterface{
public Test(){
System.out.print(" sub-class Constructor ");
}
public static void main(String[] args){
Test t = new Test();
}
}
Correct answer.
Option 4 is the correct choice. Java interface cannot have constructors because object of an interface cannot be created like class.
The code will give compilation error “Interfaces cannot have constructors”.
Incorrect answer.
Option 4 is the correct choice. Java interface cannot have constructors because object of an interface cannot be created like class.
The code will give compilation error “Interfaces cannot have constructors”.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
class One{
String sayHello(String name){
return "Hello " + name;
}
char[] sayHello(String name){
return ("Hello " + name).toCharArray();
}
}
public class Test{
public static void main(String[] args){
One one = new One();
Object object = one.sayHello("OCAJP");
if(object instanceof String)
System.out.println("String");
else
System.out.println("Array");
}
}
Correct answer.
Option 4 is the correct choice. The class One has defined two overloaded methods sayHello. However, method overloading is not valid if the only difference between them is the return type.
In the code, the only difference between two sayHello methods is the return type, one returns String while another char array, the compiler will give compilation error “Duplicate method sayHello(String) in type One”.
Incorrect answer.
Option 4 is the correct choice. The class One has defined two overloaded methods sayHello. However, method overloading is not valid if the only difference between them is the return type.
In the code, the only difference between two sayHello methods is the return type, one returns String while another char array, the compiler will give compilation error “Duplicate method sayHello(String) in type One”.
Question 4 of 10
4. Question
Java compiler always provides the default constructor.
Correct answer.
False is the correct choice. The default constructor is provided only if the class does not define any constructor(s).
Incorrect answer.
False is the correct choice. The default constructor is provided only if the class does not define any constructor(s).
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
class One{
public static void greetings(){
System.out.println("Hello");
}
}
class Two extends One{
public void greetings(){
System.out.println("Hi");
}
}
public class Test{
public static void main(String[] args){
One one = new Two();
one.greetings();
}
}
Correct answer.
Option 3 is the correct choice. The instance method in the child class cannot override the static method defined in the parent class with the same signature. Likewise, the static method defined in the child class cannot override the instance method with the same signature defined in the parent class.
The class Two tries to override the greetings static method defined in the parent class One with the instance method. Hence, the code will give compilation error “This instance method cannot override the static method from One”.
Incorrect answer.
Option 3 is the correct choice. The instance method in the child class cannot override the static method defined in the parent class with the same signature. Likewise, the static method defined in the child class cannot override the instance method with the same signature defined in the parent class.
The class Two tries to override the greetings static method defined in the parent class One with the instance method. Hence, the code will give compilation error “This instance method cannot override the static method from One”.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
class One{
public void greetings(){
System.out.println("Hello");
}
}
class Two extends One{
public static void greetings(){
System.out.println("Hi");
}
}
public class Test{
public static void main(String[] args){
One one = new Two();
one.greetings();
}
}
Correct answer.
Option 3 is the correct choice. The instance method in the child class cannot override the static method defined in the parent class with the same signature. Likewise, the static method defined in the child class cannot override the instance method with the same signature defined in the parent class.
The class Two tries to override the greetings method defined in the parent class One with the static method. Hence, the code will give compilation error “This static method cannot hide the instance method from One”.
Incorrect answer.
Option 3 is the correct choice. The instance method in the child class cannot override the static method defined in the parent class with the same signature. Likewise, the static method defined in the child class cannot override the instance method with the same signature defined in the parent class.
The class Two tries to override the greetings method defined in the parent class One with the static method. Hence, the code will give compilation error “This static method cannot hide the instance method from One”.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
class One{
public static void greetings(){
System.out.println("Hello");
}
}
class Two extends One{
public static void greetings(){
System.out.println("Hi");
}
}
public class Test{
public static void main(String[] args){
One one = new Two();
one.greetings();
}
}
Correct answer.
Option 1 is the correct choice. The code will print “Hello” when executed.
Static methods are defined at the class level. In case of the static methods, regardless of which object the reference is pointing to, it always calls the static method defined by the reference class. Here, the reference is of type One, so the static method of class One will be called.
Incorrect answer.
Option 1 is the correct choice. The code will print “Hello” when executed.
Static methods are defined at the class level. In case of the static methods, regardless of which object the reference is pointing to, it always calls the static method defined by the reference class. Here, the reference is of type One, so the static method of class One will be called.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
class One{
void One(){
System.out.println("Object created");
}
}
public class Test{
public static void main(String[] args){
One one = new One();
}
}
Correct answer.
Option 2 is the correct choice. The code will not output anything when executed.
Java constructors do not have return type. If you mention the return type, it automatically becomes a method instead of the constructor. Hence, the void One() becomes a method of the class One and thus it will not be called when an object of the class One is created.
Incorrect answer.
Option 2 is the correct choice. The code will not output anything when executed.
Java constructors do not have return type. If you mention the return type, it automatically becomes a method instead of the constructor. Hence, the void One() becomes a method of the class One and thus it will not be called when an object of the class One is created.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
class One{
public One(int i){
System.out.println("int");
}
public void One(short s){
System.out.println("short");
}
}
public class Test{
public static void main(String[] args){
short s = 10;
One one = new One(s);
}
}
Correct answer.
Option 1 is the correct choice. Java constructor does not have return types. The code tries to declare a constructor with short parameter with return type. However, since it has return type, it automatically becomes a method instead of a constructor.
So effectively, the class One has one constructor i.e. with int argument. The short value is automatically promoted to int value during object creation so the constructor with the int argument will be called and it will print “int”.
Incorrect answer.
Option 1 is the correct choice. Java constructor does not have return types. The code tries to declare a constructor with short parameter with return type. However, since it has return type, it automatically becomes a method instead of a constructor.
So effectively, the class One has one constructor i.e. with int argument. The short value is automatically promoted to int value during object creation so the constructor with the int argument will be called and it will print “int”.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
class One{
public void className(){
System.out.println("Parent");
}
}
class Two extends One{
void className(){
System.out.println("Child");
}
}
public class Test{
public static void main(String[] args){
One one = new Two();
one.className();
}
}
Correct answer.
Option 4 is the correct choice. When overriding a parent class method in a child class, we cannot reduce the visibility of the method. For example, if the method is defined as public in parent class, a child class cannot override it with protected.
The code will give compilation error “Cannot reduce the visibility of the inherited method from One”.
Incorrect answer.
Option 4 is the correct choice. When overriding a parent class method in a child class, we cannot reduce the visibility of the method. For example, if the method is defined as public in parent class, a child class cannot override it with protected.
The code will give compilation error “Cannot reduce the visibility of the inherited method from One”.