Sample questions
Question 1Easy
Which of the following can distinguish two overloaded methods in Java?
- Only the access modifier
- The number or types of parameters
- Only the thrown exceptions
- Only the return type
Answer: B. The number or types of parameters
Explanation
Overloads must differ in parameter list. A difference only in return type, modifiers or throws clause is not enough.
Step-by-step solution
- Overloaded methods share a name but have different signatures.
- Java tells overloads apart by the number or types of their parameters.
- So the correct answer is option B.
Why the other options are wrong
- A. Access modifiers do not change the method signature.
- C. Thrown exceptions are not part of the overload signature.
- D. The return type alone cannot distinguish overloads.
Tip: Overloads differ in parameters, never by return type alone.
Question 2Easy
What does this Python code print?
class Account:
def fee(self):
return 10
class Premium(Account):
def fee(self):
return super().fee() * 2 - 5
accs = [Account(), Premium(), Premium()]
print(sum(a.fee() for a in accs))
- 30
- 60
- 50
- 40
Answer: D. 40
Explanation
Account gives 10 and each Premium gives 10 * 2 - 5 = 15. Total 10 + 15 + 15 = 40.
Step-by-step solution
- Account.fee() returns 10.
- Premium.fee() returns super().fee() × 2 − 5 = 10 × 2 − 5 = 15, for each of the two Premium objects.
- The total is 10 + 15 + 15 = 40; index 3 is correct.
Why the other options are wrong
- A. 30 would mean each Premium gives 10; the formula gives 15.
- B. 60 means each Premium gives 25, which adds 5 instead of subtracting it.
- C. 50 means each Premium gives 20, which skips the − 5.
Tip: When summing method results, work out each object's own override first, then add.
Question 3Easy
Method overloading is an example of:
- Compile-time (static) polymorphism
- Encapsulation
- Runtime (dynamic) polymorphism
- Multiple inheritance
Answer: A. Compile-time (static) polymorphism
Explanation
The compiler chooses among overloads by their parameter lists. Overriding is resolved at runtime.
Step-by-step solution
- Overloading means the same method name with different parameter lists.
- The compiler picks the version from the argument types before the program runs.
- That is compile-time, or static, polymorphism, so the answer is compile-time (static) polymorphism.
Why the other options are wrong
- B. Encapsulation is hiding data behind methods; overloading is not about hiding data.
- C. Runtime polymorphism is overriding, where the object's real class picks the method when it runs.
- D. Multiple inheritance means inheriting from several classes; overloading has nothing to do with it.
Tip: Overloading is decided at compile time; overriding is decided at runtime.
Question 4Easy
What is the output of this C++ program?
#include <iostream>
struct Vec {
int x, y;
Vec operator+(const Vec& o) const { return {x + o.x, y + o.y}; }
Vec operator*(int k) const { return {x * k, y * k}; }
};
int main() {
Vec a{1, 2}, b{3, 4};
Vec c = a + b * 2;
std::cout << c.x << "," << c.y << "\n";
}
- 14,20
- 8,12
- 4,6
- 7,10
Answer: D. 7,10
Explanation
Overloaded operators keep their normal precedence, so b * 2 = (6, 8) is computed first, then a + (6, 8) = (7, 10).
Step-by-step solution
- b * 2 is (3 × 2, 4 × 2) = (6, 8); multiplication comes before addition.
- a + (6, 8) is (1 + 6, 2 + 8) = (7, 10).
- The output is "7,10"; index 3 is correct.
Why the other options are wrong
- A. 14,20 doubles the sum again; the × 2 applies only to b, not to the total.
- B. 8,12 is (a + b) × 2, which ignores the precedence of multiplication.
- C. 4,6 is a + b with no multiplication at all.
Tip: Multiplication binds tighter than addition, as in arithmetic: work out b * 2 first.
Question 5Medium
What is the output of this Java program?
class Money {
int rupees;
Money(int r) { rupees = r; }
public String toString() { return "₹" + rupees; }
}
public class Main {
public static void main(String[] args) {
Money m = new Money(50);
System.out.println("Total: " + m + 1);
}
}
- Total: ₹50 1
- Total: ₹51
- Total: 51
- Total: ₹501
Answer: D. Total: ₹501
Explanation
Concatenation goes left to right: "Total: " + m calls the overridden toString() giving "Total: ₹50", and then 1 is appended as text.
Step-by-step solution
- m.toString() returns "₹" + rupees, which is ₹50.
- "Total: " + m joins the text, giving "Total: ₹50".
- Then + 1 adds the text 1, not a number, giving "Total: ₹501".
- Option D is "Total: ₹501", so the answer is D.
Why the other options are wrong
- A. The 1 is joined to the text, so there is no space and no separate 1; the output has 501 together.
- B. The 1 is joined to the text, not added to 50, so 51 is wrong.
- C. The output keeps the text 'Total:' and the rupee amount, so 51 alone is wrong.
Tip: In Java, string + object + number joins from the left, so the number becomes text.
Question 6Medium
What is a covariant return type?
- An overriding method returning a subtype of the overridden method's return type
- A generic method with a wildcard return
- A method that returns different, unrelated types depending on the values of its input arguments
- An overload that differs only in return type
Answer: A. An overriding method returning a subtype of the overridden method's return type
Explanation
Java and C++ allow an override to return a more specific type, for example Animal clone() overridden by Dog clone().
Step-by-step solution
- Covariant return type lets subclass methods return subtype of superclass method's return type.
- This allows more specific types in overriding, like
Dog instead of Animal. - Example:
Animal clone() overridden by Dog clone() shows covariant return type. - It improves type safety and code clarity by matching return types with actual objects.
- Correct answer is option A as it matches this definition exactly.
Why the other options are wrong
- B. A wildcard return is a generics feature, not a covariant return.
- C. Unrelated return types chosen by input values do not fit covariance, which needs a subtype.
- D. Overloads cannot differ only by return type in Java; covariance is about overriding a method.
Tip: Remember: covariant return types make overriding more precise by returning subtypes.
Question 7Medium
Python's 'duck typing' means:
- Methods are bound only once at import time
- An object's suitability is decided by the methods it supports, not its declared type
- All objects must inherit from a common Duck class
- Every type is checked strictly at compile time, so an object must declare which interfaces it implements
Answer: B. An object's suitability is decided by the methods it supports, not its declared type
Explanation
If an object has the needed methods ('walks and quacks like a duck'), it can be used, regardless of its class hierarchy.
Step-by-step solution
- Duck typing checks if an object has required methods.
- It does not care about the object's declared type.
- Object's suitability depends on methods it supports.
- So, an object's suitability is decided by the methods it supports, not its declared type.
- Final answer: An object's suitability is decided by the methods it supports, not its declared type.
Why the other options are wrong
- A. Method binding happens at call time in Python, not once at import.
- C. Python classes need not share a base; duck typing does not require a common Duck class.
- D. Duck typing is a runtime check of methods; Python does not check declared interfaces at compile time.
Tip: Check if an object can perform needed actions, not its exact type.
Question 8Medium
Python does not support classic method overloading by signature. What happens when a class defines two methods named f with different parameters?
- A SyntaxError is raised
- The first definition is kept
- The later definition replaces the earlier one
- Both are kept and chosen by argument count
Answer: C. The later definition replaces the earlier one
Explanation
A def statement simply binds a name in the class namespace, so a second def f rebinds it; only the last definition survives.
Step-by-step solution
- First method f is defined and bound to the class.
- Second method f redefines the same name, replacing the first one.
- Python allows redefining functions in a class, last definition wins.
- So the later definition replaces the earlier one.
- Final answer: The later definition replaces the earlier one.
Why the other options are wrong
- A. Python does not raise a SyntaxError for this; redefining a name is allowed.
- B. The first definition is not kept; the name is bound again to the later function.
- D. Both are not kept; Python has no overloading by argument count, so only one remains.
Tip: In Python, later method definitions in a class override earlier ones.
Question 9Hard
What is the output of this Java program?
public class Main {
static void f(int x) { System.out.print("int "); }
static void f(long x) { System.out.print("long "); }
static void f(double x) { System.out.print("double "); }
public static void main(String[] args) {
byte b = 1;
f(b);
f(5L);
f(2.0f);
}
}
- int int double
- Compilation error: no f(byte)
- long long double
- int long double
Answer: D. int long double
Explanation
Overload resolution picks the most specific widening conversion: byte widens to int first, 5L is a long, and float widens to double.
Step-by-step solution
- Byte b = 1; calls f(int) because byte widens to int.
- f(5L); calls f(long) because 5L is a long literal.
- f(2.0f); calls f(double) because float widens to double.
- Final output: int long double.
Why the other options are wrong
- A. f(5L) matches f(long) exactly, so the second word is long, and f(b) with a byte picks int, not a repeated int.
- B. There is no f(byte), but byte widens to int, so the call compiles and picks f(int) as the most specific match.
- C. f(b) with a byte widens to int and picks f(int), so the first word is int, not long.
Tip: Remember widening conversions for primitive types in method overloads.
Question 10Hard
How does a typical C++ implementation perform a virtual function call?
- By recompiling the call at runtime
- By searching the method name in a per-class hash table of strings on every single call
- By inlining every possible override
- Through a per-object pointer to the class's vtable, indexing the function's slot
Answer: D. Through a per-object pointer to the class's vtable, indexing the function's slot
Explanation
Each polymorphic object holds a vptr to its class's table of function pointers; a virtual call loads the vptr and jumps to the entry for that function.
Step-by-step solution
- A C++ object has a vptr pointing to its vtable.
- The vtable contains pointers to all virtual functions of the class.
- To call a virtual function, the program uses the vptr to locate the correct function in the vtable.
- It then jumps to the function's address from the vtable.
- Final answer: Through a per-object pointer to the class's vtable, indexing the function's slot.
Why the other options are wrong
- A. No recompiling happens at runtime; the call goes through the vtable pointer.
- B. Methods are not looked up by name in a hash table on each call in typical C++.
- C. Inlining every override is impossible when the type is only known at runtime.
Tip: Remember: vtable and vptr are key to virtual function calls in C++.
Practise all 34 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 →