Sample questions
Question 1Medium
Which of these is supported by method overriding in Java?
- Abstraction
- Encapsulation
- Polymorphism
- None of the mentioned
Answer: C. Polymorphism
Explanation
Method overriding in Java is supported by polymorphism, which allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
Step-by-step solution
- Method overriding lets subclasses change method behavior.
- This enables different classes to act differently when same method is called.
- It allows objects to be treated as instances of their parent class.
- This is a key feature of polymorphism in Java.
- So, method overriding supports polymorphism directly.
Why the other options are wrong
- A. Overriding changes behaviour in subclasses; it does not provide abstraction, which hides implementation details.
- B. Encapsulation means hiding data behind access controls, which overriding does not provide by itself.
- D. None is wrong, because overriding directly supports polymorphism, as the answer says.
Tip: Remember: Method overriding is all about polymorphism in Java.
Question 2Medium
What is the return type of Constructors?
- int
- float
- void
- None of the mentioned
Answer: D. None of the mentioned
Explanation
Constructors do not have a return type. They are special methods used to initialize objects and cannot return any value, including void.
Step-by-step solution
- Constructors are used to initialize objects and do not return any value.
- They cannot have a return type like int or float.
- The return type of a constructor is always None.
- So the correct answer is None of the mentioned.
Why the other options are wrong
- A. Constructors do not return an int; they have no return type at all.
- B. Constructors do not return a float; they have no return type at all.
- C. void is not the answer; a constructor has no declared return type, not void.
Tip: Remember: Constructors have no return type, so always choose 'None of the mentioned'.
Question 3Medium
What is the output of this program?
class recursion {
int fact(int n) {
int result;
if (n == 1)
return 1;
result = fact(n - 1) * n;
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.fact(5));
}
}
- 24
- 30
- 120
- 720
Answer: C. 120
Explanation
fact() method recursively calculates factorial of a number, when value of n reaches 1, base case is excuted and 1 is returned.
Output:
$ javac Output.javac
$ java Output
120
Step-by-step solution
- fact(n) returns 1 when n is 1, and otherwise returns fact(n - 1) multiplied by n.
- fact(5) = fact(4) × 5, fact(4) = fact(3) × 4, fact(3) = fact(2) × 3 and fact(2) = fact(1) × 2.
- fact(1) = 1, so fact(2) = 2, fact(3) = 6, fact(4) = 24 and fact(5) = 120.
- The program prints 120, which is the correct answer, option C.
Why the other options are wrong
- A. 24 is 4 factorial; the call for 5 multiplies one more step, giving 120.
- B. 30 does not come from multiplying 1 to 5, so the function never gives it.
- D. 720 is 6 factorial; the function stops at 5, not 6.
Tip: N factorial is n × (n − 1) × ... × 1, so 4! = 24, 5! = 120 and 6! = 720.
Question 4Medium
Which of these keyword can be used in subclass to call the constructor of superclass?
- super
- this
- extent
- extends
Answer: A. super
Explanation
The super keyword is associated with Inheritance. Inheritance is a concept of Object Oriented Programming where in the sub class inherits characteristics and properties of the super class. The base class is also called as super class.
The Super keyword is used for two different purposes:
1. To call the Super Class Constructor
2. To execute the super class methods
Step-by-step solution
- Super calls superclass constructor.
- This refers to current object.
- Extent is not a valid keyword.
- Extends is used for inheritance.
- Final answer: super.
Why the other options are wrong
- B. this refers to the current object, not to the superclass constructor, so it cannot call it.
- C. extent is not a Java keyword, so it cannot be used to call a superclass constructor.
- D. extends sets up inheritance in the class header; it does not call any constructor.
Tip: Use 'super' to call superclass constructor in subclass.
Question 5Medium
What is the output of this program?
class A {
public void display() {
System.out.println("A");
}
}
class B extends A {
public void display() {
System.out.println("B");
}
}
class Dynamic_dispatch {
public static void main(String args[])
{
A obj1 = new A();
B obj2 = new B();
A r;
r = obj1;
r.display();
r = obj2;
r.display();
}
}
- A B
- B A
- Runtime Error
- Compilation Error
Answer: A. A B
Explanation
r first holds an A object, so the first call prints A. Then r holds a B object, and the overridden display() of B prints B. The output is A B.
Step-by-step solution
- Obj1 is an A, so r.display() runs A's version and prints A.
- Obj2 is a B, and r is assigned obj2, so r.display() runs B's overriding version at runtime and prints B.
- This is dynamic method dispatch, which picks the method from the actual object type.
- The output is A then B, which is the correct answer, option A.
Why the other options are wrong
- B. The first call uses A's version, since obj1 is an A, so the order is A then B.
- C. No runtime error occurs, because both calls are valid.
- D. The code compiles, since B's display has the same signature as A's.
Tip: Which version of an overridden method runs depends on the object's actual type, not the variable type.
Question 6Medium
What is the output of this program?
class box {
int width;
int height;
int length;
int volume;
box() {
width = 5;
height = 5;
length = 6;
}
void volume() {
volume = width*height*length;
}
}
class constructor_output {
public static void main(String args[])
{
box obj = new box();
obj.volume();
System.out.println(obj.volume); }
}
- 100
- 150
- 200
- 250
Answer: B. 150
Explanation
The program defines a box class with properties for width, height, length, and volume. The constructor initializes these values, and the volume() method calculates the volume by multiplying the width, height, and length. In the main method, an instance of box is created, the volume is calculated, and then printed. The correct output is 150 because 5 * 5 * 6 = 150.
Step-by-step solution
- width = 5.
- height = 5.
- length = 6.
- volume = 5 * 5 * 6 = 150.
- obj.volume is printed.
- Final answer: 150.
Why the other options are wrong
- A. 100 would come from 5 times 5 times 4; the length is 6, so the product is 150.
- C. 200 is not the product; with width 5, height 5 and length 6 the volume is 150.
- D. 250 would need length 10; the constructor sets length to 6, giving 150.
Tip: Multiply width, height, and length to find volume.
Question 7Medium
What is the process of defining more than one method in a class differentiated by method signature?
- Function overriding
- Function overloading
- Function doubling
- None of the mentioned
Answer: B. Function overloading
Explanation
Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example int volume(int length, int width) & int volume(int length, int width, int height) can be used to calculate volume.
Step-by-step solution
- Function overloading means several methods in one class share a name but have different parameter lists.
- The methods must differ in parameter types or number, not only in return type.
- Defining several such methods is function overloading, so the correct answer is option B.
Why the other options are wrong
- A. Overriding is when a subclass replaces an inherited method with the same signature, not several methods in one class.
- C. Function doubling is not a Java term for this idea.
- D. Option B is correct, so 'none of the mentioned' is wrong.
Tip: Overloading means same name, different parameters; a return type alone cannot tell two overloads apart.
Question 8Medium
Which of these is not a correct statement?
- A recursive method must have a base case.
- Recursion always uses stack.
- Recursive methods are faster that programmers written loop to call the function repeatedly using a stack
- Recursion is managed by Java's Run-Time environment.
Answer: C. Recursive methods are faster that programmers written loop to call the function repeatedly using a stack
Explanation
recursion is not faster than a loop; the JVM does manage the call stack
Step-by-step solution
- A recursive method must have a base case to stop, so that statement is true.
- Recursion uses the call stack, and the JVM manages it, so those statements are true.
- Recursion is not faster than a loop; it adds call overhead.
- So option C is the incorrect statement.
Why the other options are wrong
- A. This is correct: a base case is needed to stop the recursion.
- B. This is correct: recursive calls use the call stack.
- D. This is correct: the JVM manages the run time stack for calls.
Tip: Recursion is elegant but usually not faster than a loop.
Question 9Medium
What is the process of defining a method in terms of itself, that is a method that calls itself?
- Polymorphism
- Abstraction
- Encapsulation
- Recursion
Answer: D. Recursion
Explanation
Recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function.
Step-by-step solution
- A recursive method is one that calls itself.
- Defining something in terms of itself is the process of recursion.
- Polymorphism, abstraction and encapsulation are different object-oriented ideas.
- So the answer is Recursion, option D.
Why the other options are wrong
- A. Polymorphism means one interface used in many forms, not self-calls.
- B. Abstraction hides detail and shows only the essentials.
- C. Encapsulation groups data and methods together and hides the internals.
Tip: A recursive method needs a base case to stop; without one it never ends.
Question 10Medium
Which of the following is a method having same name as that of it's class?
- finalize
- delete
- class
- constructor
Answer: D. constructor
Explanation
A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
Step-by-step solution
- Constructor is a special method used to initialize objects.
- It has the same name as the class it belongs to.
- Other methods like finalize, delete, and class do not have this property.
- Therefore, constructor is the correct answer.
- Final answer: D (constructor).
Why the other options are wrong
- A. finalize is a method that runs before garbage collection and it does not share the class name.
- B. delete is not a Java method name for this purpose and it does not match the class name.
- C. class is a keyword that declares a class, not a method that has the class name.
Tip: Remember: Constructor's name matches the class name.
Practise all 37 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →