Sample questions
Question 1Easy
A TV remote lets you change channels without knowing the circuitry inside. This illustrates:
- Abstraction
- Polymorphism
- Inheritance
- Aggregation
Answer: A. Abstraction
Explanation
Abstraction exposes only what users need (buttons) and hides implementation details.
Step-by-step solution
- The remote uses buttons to change channels without showing internal circuitry.
- This hides complex details from the user.
- Abstraction allows using simple interfaces for complex systems.
- Users don't need to know how channels are changed internally.
- This matches the definition of abstraction.
Why the other options are wrong
- B. Polymorphism is about one interface with many forms, not hiding the circuit.
- C. Inheritance is about a subtype reusing a parent's code, which the remote example does not show.
- D. Aggregation means one object holding others as parts, not hiding how a device works.
Tip: Think about what you don't need to know when using a tool or system.
Question 2Easy
Which keyword declares an interface implementation in Java?
- inherits
- implements
- extends
- uses
Answer: B. implements
Explanation
A class implements interfaces and extends a class; an interface extends other interfaces.
Step-by-step solution
- In Java, a class uses the keyword 'implements' to adopt an interface.
- 'Implements' is used to define how a class will fulfill the contract of an interface.
- Other keywords like 'extends' are for inheritance from classes, not interfaces.
- The correct keyword for interface implementation is 'implements'.
- Final answer: implements.
Why the other options are wrong
- A. inherits is not a Java keyword; a class inherits from another class with extends and implements an interface with implements.
- C. extends is used to inherit from a class, or one interface from another, but not to implement an interface.
- D. uses is not a Java keyword, so it cannot declare an interface implementation.
Tip: Remember: 'Implements' connects classes with interfaces; 'extends' is for classes only.
Question 3Easy
What happens when this Java code is compiled?
abstract class Shape {
abstract double area();
}
public class Main {
public static void main(String[] args) {
Shape s = new Shape();
System.out.println(s.area());
}
}
- Compilation error: an abstract class cannot be instantiated
- Throws an exception at runtime
- Compiles, then throws an
AbstractMethodError when area() is called - Prints
0.0
Answer: A. Compilation error: an abstract class cannot be instantiated
Explanation
Abstract classes can be used only as types or superclasses. A concrete subclass must implement area() before objects can be created.
Step-by-step solution
- Shape is an abstract class, so it cannot be instantiated.
- Trying to create a Shape object with new Shape() causes a compilation error.
- The code fails to compile because of the invalid constructor call.
- Compilation error: an abstract class cannot be instantiated.
- Final answer: Compilation error: an abstract class cannot be instantiated.
Why the other options are wrong
- B. The compiler rejects new Shape() on an abstract class, so the program fails at compile time, not at runtime.
- C. No AbstractMethodError happens, because the code never compiles and area() is never called.
- D. Nothing is printed, because the code does not compile, so the value 0.0 is never produced.
Tip: Remember: you can't create objects of abstract classes directly.
Question 4Easy
Abstraction in OOP means:
- Copying code from one class to another
- Creating many objects from one class
- Storing all data in public fields so every class can read it directly
- Exposing essential behaviour while hiding the details
Answer: D. Exposing essential behaviour while hiding the details
Explanation
Users of a List call add() and get() without knowing whether it is backed by an array or linked nodes.
Step-by-step solution
- Abstraction hides complex details behind simple interfaces.
- Users interact with behaviour, not implementation specifics.
- Exposing only what's needed makes systems easier to use.
- This matches option D exactly.
- Final answer: Exposing essential behaviour while hiding the details.
Why the other options are wrong
- A. Copying code between classes is code duplication, which abstraction does not mean; abstraction hides details.
- B. Creating many objects from one class is instantiation, which is a different idea from abstraction.
- C. Public fields expose data directly, the opposite of abstraction, which hides details behind behaviour.
Tip: Think of abstraction as a black box — you use it but don't need to know how it works inside.
Question 5Medium
Fields declared in a Java interface are implicitly:
- public and mutable per object
privatepublic static finalprotected
Answer: C. public static final
Explanation
Interface fields are constants: public, static and final, whether or not the modifiers are written.
Step-by-step solution
- Fields in Java interfaces are automatically public.
- They are also automatically static as they cannot be instantiated.
- They are final by default since they must have a fixed value.
- So, interface fields are implicitly public static final.
- The correct answer is
public static final.
Why the other options are wrong
- A. Interface fields are not mutable per object; they are static constants shared by all.
- B. Interface fields are not private; they are public by rule.
- D. Interface fields are not protected; they are public by rule, which is wider access.
Tip: Remember: Interface fields are always public, static, and final.
Question 6Medium
What is the output of this Java program?
interface Config { int LIMIT = 10; }
class Service implements Config {
int twice() { return LIMIT * 2; }
}
public class Main {
public static void main(String[] args) {
System.out.println(new Service().twice() + Service.LIMIT);
}
}
- 20
- 2010
- 30
- Compilation error
Answer: C. 30
Explanation
LIMIT is an implicitly public static final constant inherited by Service. Both operands of + are ints (20 and 10), so they are added numerically, not concatenated.
Step-by-step solution
- Interface fields are implicitly public static final, so Service inherits LIMIT.
- twice() returns LIMIT times 2, which is 10 times 2, so 20.
- Service.LIMIT is 10, and 20 plus 10 is 30; the correct answer is option C.
Why the other options are wrong
- A. This adds LIMIT again only once; the full sum is 30.
- B. The values are added as numbers, not joined as text.
- D. The code compiles, because the field is inherited from Config.
Tip: Interface constants are static, so they can be read through any implementing class.
Question 7Medium
What is the output of this Java program?
abstract class Shape {
Shape() { System.out.print("Shape "); }
abstract double area();
}
class Sq extends Shape {
double s;
Sq(double s) { this.s = s; System.out.print("Sq "); }
double area() { return s * s; }
}
public class Main {
public static void main(String[] args) {
Shape sh = new Sq(3);
System.out.println(sh.area());
}
}
- Sq 9.0
- Shape Sq 9
- Compilation error: an abstract class cannot have a constructor
- Shape Sq 9.0
Answer: D. Shape Sq 9.0
Explanation
Abstract classes can have constructors; they run when a concrete subclass is created. area() returns a double, so 9.0 is printed.
Step-by-step solution
new Sq(3) first implicitly calls Shape()'s constructor, printing "Shape ".- Then
Sq's own constructor body runs, printing "Sq ". area() returns s * s = 3.0 * 3.0 = 9.0, a double value.System.out.println on a double always shows the decimal point, so it prints "9.0", giving the full output "Shape Sq 9.0".
Why the other options are wrong
- A. This misses the Shape() constructor call, which does run (and print) first, before Sq's own constructor body.
- B. println on a double literal always prints with a trailing ".0"; it never prints a bare "9".
- C. Abstract classes CAN have constructors - they just can't be invoked directly with 'new Shape()'; a subclass's constructor still calls them implicitly.
Tip: println on a double ALWAYS shows the trailing .0 - a classic distinguisher between trick options.
Question 8Medium
A class extends an abstract class but does not implement all of its abstract methods. What must be true?
- The class can be instantiated normally
- The compiler adds empty implementations automatically
- The missing methods throw exceptions when called
- The class must itself be declared abstract
Answer: D. The class must itself be declared abstract
Explanation
A concrete class must provide implementations for every inherited abstract method; otherwise it is a compile-time error unless the class is abstract.
Step-by-step solution
- A concrete class must implement all abstract methods from its parent.
- If it doesn't, it cannot be instantiated normally.
- The missing methods will cause a compile-time error.
- To avoid this, the class must itself be declared abstract.
- The class must itself be declared abstract.
Why the other options are wrong
- A. A class that leaves abstract methods unimplemented cannot be instantiated, so it cannot be used as a normal class.
- B. Java does not write method bodies for you; the compiler reports an error for the missing method.
- C. Missing methods do not become throwing stubs; the class must be declared abstract or the code will not compile.
Question 9Hard
What happens when this Java program is compiled and run?
interface Util {
static int twice(int x) { return 2 * x; }
}
class Impl implements Util { }
public class Main {
public static void main(String[] args) {
System.out.println(Impl.twice(4));
}
}
- 8
- 4
- Runtime error
- Compilation error
Answer: D. Compilation error
Explanation
Static interface methods are not inherited by implementing classes; they must be called as Util.twice(4).
Step-by-step solution
- Static methods in an interface belong to that interface only.
- They are not inherited by implementing classes.
- So Impl.twice(4) does not compile; the correct answer is option D, a compilation error.
Why the other options are wrong
- A. The call does not compile, so it never runs and prints nothing.
- B. The value 4 would not be printed; the code is rejected.
- C. The problem is caught at compile time, not at runtime.
Tip: Call interface static methods as Util.twice, not through a class.
Question 10Hard
An abstract class has no abstract methods at all. Which statement is true in Java?
- It is automatically treated as concrete
- It must then be declared final
- It is legal; it still cannot be instantiated directly
- It fails to compile
Answer: C. It is legal; it still cannot be instantiated directly
Explanation
abstract is a class modifier; it prevents instantiation even when every method has a body, which is useful for base classes meant only to be extended.
Step-by-step solution
- An abstract class is declared with the abstract keyword.
- It can have zero abstract methods and still compile.
- It cannot be created with new, so it is legal but not instantiable; the correct answer is option C.
Why the other options are wrong
- A. Being declared abstract is not the same as being concrete.
- B. An abstract class does not need to be final, and abstract with final is not allowed.
- D. The class compiles fine.
Tip: Abstract blocks new, not compilation; a class with no abstract methods can still be abstract.
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 →