Sample questions
Question 1Easy
What happens when this code is compiled?
class A {
A(int x) { }
}
class B extends A {
B() { }
}
public class Main {
public static void main(String[] args) {
new B();
}
}
- Runtime error: NoSuchMethodError
- It compiles and runs with no output
- It compiles; x defaults to 0
- Compilation error: A has no no-argument constructor
Answer: D. Compilation error: A has no no-argument constructor
Explanation
B() implicitly calls super(), but A only declares A(int). The compiler reports an error because no matching constructor exists.
Step-by-step solution
- B() has no explicit super call, so Java inserts super() on its own.
- Class A has only A(int x) and no no-argument constructor.
- The compiler cannot find a matching A() constructor and reports an error.
- So the code does not compile; option D is the correct answer.
Why the other options are wrong
- A. This is a compile-time error, not a runtime NoSuchMethodError.
- B. The code does not compile, so nothing runs.
- C. The code does not compile; x is only a constructor parameter in A, not a field set to 0.
Tip: If a superclass has only constructors with parameters, every subclass constructor must call super(...) with matching arguments.
Question 2Easy
Which access modifier makes a member visible within its own package and to subclasses in other packages?
- private
- public only through getters
- protected
- default (no modifier)
Answer: C. protected
Explanation
protected = package access plus access from subclasses anywhere. Default (package-private) access does not extend to subclasses in other packages.
Step-by-step solution
- Private: visible only inside the same class.
- default (no modifier): visible only inside the same package, so subclasses in other packages cannot see it.
- Protected: visible inside the same package and to subclasses anywhere.
- So the answer is protected, option C.
Why the other options are wrong
- A. Private hides the member from the same package and from subclasses too.
- B. Getters are a design pattern, not an access modifier, so they do not make a member visible to subclasses by themselves.
- D. Default access stops at the package boundary, so subclasses in other packages cannot see the member.
Tip: Public means everywhere, protected means package plus subclasses, default means package only, private means the class only.
Question 3Easy
What is the output?
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
}
public class Main {
public static void main(String[] args) {
Point a = new Point(1, 2);
Point b = new Point(1, 2);
System.out.println((a == b) + " " + a.equals(b));
}
}
- true true
- false true
- true false
- false false
Answer: D. false false
Explanation
a and b are different objects, so == is false. Point does not override equals, so Object.equals also compares references.
Step-by-step solution
- A and b are two separate Point objects, each created with new.
- a == b compares references, and the two references differ, so it is false.
- Point does not override equals, so a.equals(b) uses Object.equals, which also compares references and gives false.
- The output is false false, option D.
Why the other options are wrong
- A. Two separate objects are never == each other, so the first value is not true.
- B. Equals is not overridden, so it compares references and gives false, not true.
- C. The == check is false because the objects differ, so the first value cannot be true.
Tip: Two new objects are never == each other. To compare contents, override equals and hashCode.
Question 4Easy
Which statement about method overloading in Java is correct?
- Overloaded methods must differ in return type only
- Overloading is resolved at runtime using the object type
- Overloaded methods must be in different classes
- Overloaded methods must differ in their parameter lists
Answer: D. Overloaded methods must differ in their parameter lists
Explanation
Overloads share a name but need different parameter types, count or order. They are resolved at compile time; a different return type alone is not enough.
Step-by-step solution
- Overloads share the name.
- They must differ in parameter list.
- They are resolved at compile time.
Why the other options are wrong
- A. Return type alone is not enough.
- B. Overloading is compile-time; overriding is runtime.
- C. Overloads can be in the same class.
Tip: Parameters decide the overload.
Question 5Medium
What is the output?
class Parent { }
class Child extends Parent { }
public class Main {
public static void main(String[] args) {
Parent p = new Parent();
Child c = (Child) p;
System.out.println("done");
}
}
- done
- null
- ClassCastException at runtime
- Compilation error
Answer: C. ClassCastException at runtime
Explanation
A downcast compiles because Child is a subtype of Parent, but the object is a plain Parent, so the cast fails at runtime.
Step-by-step solution
new Parent() creates a plain Parent object.- Casting to
Child compiles since Child is a subtype. - At runtime the object is not a
Child, so the cast fails. ClassCastException.
Why the other options are wrong
- A. The cast fails before 'done' is reached.
- B. No null is printed.
- D. Downcasts between related classes compile.
Tip: Compile-time looks at the reference, runtime at the object.
Question 6Medium
What is the output?
class Parent { }
class Child extends Parent { }
public class Main {
public static void main(String[] args) {
Object o = new Child();
System.out.println((o instanceof Parent) + " " + (o instanceof Runnable));
}
}
- false false
- true true
- Compilation error
- true false
Answer: D. true false
Explanation
A Child is-a Parent, so the first test is true. Child does not implement Runnable, so the second is false; the check compiles because o is of type Object.
Step-by-step solution
- Object o is assigned a Child instance, which is a Parent subclass.
- O instanceof Parent checks if o is a Parent or subclass, which it is. So result is true.
- O instanceof Runnable checks if o implements Runnable, but Child does not. So result is false.
- The code compiles because Object can be checked against any interface.
- Final output is 'true false'.
Why the other options are wrong
- A. The first check is true, since a Child object is a Parent, so the result cannot be false false.
- B. Child does not implement Runnable, so the second check is false and the output is not true true.
- C. instanceof with an interface on an Object reference compiles, so the code does not fail to compile.
Tip: Remember: instanceof works for classes and interfaces. If not implemented, it returns false.
Question 7Medium
What is the output?
class Parent {
String name = "P";
String getName() { return name; }
}
class Child extends Parent {
String name = "C";
String getName() { return name; }
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name + " " + p.getName());
}
}
- P P
- C P
- C C
- P C
Answer: D. P C
Explanation
Fields are not polymorphic: p.name uses the declared type Parent. getName() is overridden, so Child.getName() returns Child's field "C".
Step-by-step solution
- Fields are not overridden: p.name uses the declared type Parent, so it is P.
- getName() is overridden in Child, so p.getName() runs Child's version.
- Child's getName() returns Child's own name field, which is C.
- The output is P C, which is option D, the correct answer.
Why the other options are wrong
- A. p.name is P, but getName() returns C from Child.
- B. p.name uses the declared type Parent, so it is P, not C.
- C. p.name uses the declared type Parent, so it is P, not C.
Tip: Fields follow the declared type at compile time, but overridden methods follow the actual object at run time.
Question 8Medium
What is the output?
class Counter {
private int count;
Counter increment() { count++; return this; }
int get() { return count; }
}
public class Main {
public static void main(String[] args) {
Counter c = new Counter();
System.out.println(c.increment().increment().increment().get());
}
}
- 3
- Compilation error
- 1
- 0
Answer: A. 3
Explanation
increment() returns this, so the calls are chained on the same object and count is incremented three times.
Step-by-step solution
c.increment() increments count to 1.c.increment().increment() increments count to 2.c.increment().increment().increment() increments count to 3.get() returns count which is 3.- Final answer: 3.
Why the other options are wrong
- B. The code compiles, since each method returns this and the chained calls are valid.
- C. 1 would need only one increment, but three increment calls run in the chain.
- D. 0 would be right only if no increment ran, but each call adds one to count.
Tip: Use method chaining for concise code when methods return the object itself.
Question 9Hard
What is the output?
class A {
void m(int x) { System.out.print("A.int "); }
void m(long x) { System.out.print("A.long "); }
}
class B extends A {
void m(long x) { System.out.print("B.long "); }
}
public class Main {
public static void main(String[] args) {
A a = new B();
a.m(1);
}
}
- B.long
- Compilation error
- A.long
- A.int
Answer: D. A.int
Explanation
At compile time m(int) is the best overload for argument 1. B only overrides m(long), so at runtime the inherited A.m(int) runs.
Step-by-step solution
- At compile time a has type A, so the compiler looks only at A's methods.
- The argument 1 is an int, so m(int) is the exact match and is chosen.
- B overrides only m(long) and does not override m(int), so A's m(int) is inherited unchanged.
- At runtime the inherited A.m(int) runs and prints A.int, option D.
Why the other options are wrong
- A. B's m(long) is never called, because the compiler chose m(int).
- B. The code compiles: a.m(1) matches A's m(int), and B inherits it.
- C. A's m(long) would only be picked for a long argument; the int argument picks m(int).
Tip: The compile-time type chooses the overload. The runtime object only changes which body runs for that exact signature.
Question 10Hard
What is the output?
public class Main {
static void f(Object o) { System.out.print("Object "); }
static void f(String s) { System.out.print("String "); }
public static void main(String[] args) {
f(null);
Object x = "hi";
f(x);
}
}
- String Object
- Object Object
- String String
- Compilation error
Answer: A. String Object
Explanation
For null, the most specific applicable overload (String) wins. For x, overloads are chosen by the declared type Object, not the runtime type.
Step-by-step solution
- f(null): both f(Object) and f(String) accept null, and String is more specific, so f(String) runs and prints String.
- Object x = "hi": overloads are chosen by the declared type Object, not by the runtime value.
- f(x) therefore picks f(Object), which prints Object.
- The output is String Object, option A.
Why the other options are wrong
- B. The null call goes to f(String) because it is more specific, so the first word is not Object.
- C. The second call uses the declared type Object, so it cannot pick f(String).
- D. The code compiles, because null matches both overloads and String is more specific, so there is no ambiguity.
Tip: For null, the most specific overload wins. For a variable, Java uses its compile-time type.
Practise all 23 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 →