Java object oriented programming quiz part 6 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 6 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
Will this code compile?
public class Test{
public int count(int i){
return 0;
}
}
class TestChild extends Test{
public int count(long i) throws Exception{
return 0;
}
}
Correct answer.
Yes is the correct choice. The Test class defines a method named count. The TestChild class defines a method with the same name but with different type of parameter. It also throws an Exception.
Defining methods with same name but different parameters is called method overloading. It is valid to throw different exceptions in case of the method overloading. So the code will compile fine without any errors.
Incorrect answer.
Yes is the correct choice. The Test class defines a method named count. The TestChild class defines a method with the same name but with different type of parameter. It also throws an Exception.
Defining methods with same name but different parameters is called method overloading. It is valid to throw different exceptions in case of the method overloading. So the code will compile fine without any errors.
Question 2 of 10
2. Question
Will this code compile without error?
interface Test {
abstract public void someMethod() throws Exception;
}
Correct answer.
Yes is the correct choice. All the methods defined in an interface are implicitly abstract. Specifying the abstract keyword in the method declaration is allowed in the interface but does not make any difference.
Hence, it is a valid method declaration and valid interface. The code will compile without any errors.
Incorrect answer.
Yes is the correct choice. All the methods defined in an interface are implicitly abstract. Specifying the abstract keyword in the method declaration is allowed in the interface but does not make any difference.
Hence, it is a valid method declaration and valid interface. The code will compile without any errors.
Question 3 of 10
3. Question
Will this code compile?
public class Test{
public int count(int i){
return 0;
}
public int count(int j){
return 1;
}
}
Correct answer.
No is the correct choice. The class Test defines two methods with exactly same signature. Just the returned values are different which is not a valid qualifier for the method overloading.
A class cannot have more than one method with same signatures so the code will give compilation error “Duplicate method count(int) in type Test”.
Incorrect answer.
No is the correct choice. The class Test defines two methods with exactly same signature. Just the returned values are different which is not a valid qualifier for the method overloading.
A class cannot have more than one method with same signatures so the code will give compilation error “Duplicate method count(int) in type Test”.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
class One{
private void count(int i){
System.out.println(0);
}
}
public class Test extends One{
public void count(int j){
System.out.println(1);
}
public static void main(String[] args) {
One one = new Test();
one.count(0);
}
}
Correct answer.
Option 3 is the correct choice. In case of overridden methods, which method to call is decided at runtime depending on which object is being referenced.
Here, the One class has declared a method which is private. The Test class extends the One class and overrides the count method with public access. Then reference of One class is assigned to the object of Test child class.
At compile time, the compiler checks if the method defined by the class of the reference (not actual object) is accessible or not. The count method in the class One is declared as private, so it cannot be accessed outside the One class. Hence, the compile will give error “The method count(int) from the type One is not visible”.
If the count method was declared as protected or no access modifiers at all, the code would have compiled without any error and printed 1 because reference of One is pointing to the object of Test class. This decision is taken at runtime.
Incorrect answer.
Option 3 is the correct choice. In case of overridden methods, which method to call is decided at runtime depending on which object is being referenced.
Here, the One class has declared a method which is private. The Test class extends the One class and overrides the count method with public access. Then reference of One class is assigned to the object of Test child class.
At compile time, the compiler checks if the method defined by the class of the reference (not actual object) is accessible or not. The count method in the class One is declared as private, so it cannot be accessed outside the One class. Hence, the compile will give error “The method count(int) from the type One is not visible”.
If the count method was declared as protected or no access modifiers at all, the code would have compiled without any error and printed 1 because reference of One is pointing to the object of Test class. This decision is taken at runtime.
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){
One o = new One();
o.sayHello();
}
}
final abstract class One{
public final void sayHello(){
System.out.println("Hello");
}
public abstract void sayHi();
}
Correct answer.
Option 2 is the correct choice. The final and abstract keywords are opposite of each other.
Marking a class abstract means that the implementation is not complete, subclass has to provide the concrete implementation. While marking a class final means that the implementation is final and the behavior cannot be altered by extending it. Therefore, a class cannot be both, final and abstract at the same time.
The code will give compilation error “The class One can be either abstract or final, not both”.
Incorrect answer.
Option 2 is the correct choice. The final and abstract keywords are opposite of each other.
Marking a class abstract means that the implementation is not complete, subclass has to provide the concrete implementation. While marking a class final means that the implementation is final and the behavior cannot be altered by extending it. Therefore, a class cannot be both, final and abstract at the same time.
The code will give compilation error “The class One can be either abstract or final, not both”.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test{
class TestInner{
void sayHi(){
System.out.println("Hi");
}
}
public static void main(String[] args) {
TestInner inner = new TestInner();
inner.sayHi();
}
}
Correct answer.
Option 2 is the correct choice. Object of an Inner class cannot be created directly without the object of the outer class. The object of an inner class should be created like below.
Test t = new Test();
TestInner inner = t.new TestInner();
The code will give compilation error “No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).”.
Incorrect answer.
Option 2 is the correct choice. Object of an Inner class cannot be created directly without the object of the outer class. The object of an inner class should be created like below.
Test t = new Test();
TestInner inner = t.new TestInner();
The code will give compilation error “No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).”.
Question 7 of 10
7. Question
Which class has defined the equals(Object o) method?
Correct answer.
Option 2 is the correct choice. The equals(Object o) method is defined by the Object class.
Incorrect answer.
Option 2 is the correct choice. The equals(Object o) method is defined by the Object class.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
public static void compute(int i, int j){
System.out.println("int version");
}
public void compute(int i, int j){
System.out.println("long version");
}
public static void main(String args[]){
Test t = new Test();
compute(10, 5);
}
}
Correct answer.
Option 3 is the correct choice. In Java, a method cannot be overloaded if the only difference between them is one being static and other non-static.
Here, compute method is defined twice with the exact same parameters and return type. The first method being static and second one is non-static. Hence, compiler will give error “Duplicate method compute(int, int) in type Test”.
Incorrect answer.
Option 3 is the correct choice. In Java, a method cannot be overloaded if the only difference between them is one being static and other non-static.
Here, compute method is defined twice with the exact same parameters and return type. The first method being static and second one is non-static. Hence, compiler will give error “Duplicate method compute(int, int) in type Test”.
Question 9 of 10
9. Question
Which of the following variables are accessible at line 13?
public class Test{
private int i = 0;
int j = 1;
public int k = 2;
class TestInner{
private int l = 3;
int m = 4;
public int n = 5;
void sayHi(){
//access variable here
}
}
}
Correct answer.
All the options are correct. The member inner class can access all the variables declared by its outer class including the private variables. So i, j and k are accessible inside the inner class. The l, m and n variables are local to the inner class, so they are also accessible inside the method.
Incorrect answer.
All the options are correct. The member inner class can access all the variables declared by its outer class including the private variables. So i, j and k are accessible inside the inner class. The l, m and n variables are local to the inner class, so they are also accessible inside the method.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
class One{
public static void process(){
System.out.print("Parent");
}
}
class Two extends One{
public static void process(){
super.process();
System.out.print("Child");
}
}
public class Test{
public static void main(String args[]){
One one = new Two();
one.process();
}
}
Correct answer.
Option 4 is the correct choice. Static method in Java cannot access super or this keywords as static methods are defined at the class level and not associated with the object of the class. The code will give compilation error “Cannot use super in a static context”.
Incorrect answer.
Option 4 is the correct choice. Static method in Java cannot access super or this keywords as static methods are defined at the class level and not associated with the object of the class. The code will give compilation error “Cannot use super in a static context”.