
These questions focus on object-oriented programming in Java: constructors, inheritance, method overriding, abstract classes, interfaces and access modifiers. Several questions show a short program - read the code carefully before picking an answer.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 16 questions.
Q1. What is printed by the following program?
class A {
A() { System.out.print("A"); }
}
class B extends A {
B() { System.out.print("B"); }
}
public class Main {
public static void main(String[] args) {
new B();
}
}
a) B
b) AB
c) BA
d) Compilation error
Q2. Which keyword completely prevents a class from being extended?
a) static
b) private
c) final
d) sealed
Q3. Method overriding in Java is an example of:
a) Compile-time polymorphism
b) Runtime polymorphism
c) Encapsulation
d) Multiple inheritance
Q4. What is printed?
class Animal { void sound() { System.out.print("..."); } }
class Dog extends Animal { void sound() { System.out.print("Woof"); } }
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
a) ...
b) Woof
c) ...Woof
d) Compilation error
Q5. Which statement about abstract classes is correct?
a) An abstract class cannot have constructors
b) An abstract class cannot be instantiated directly
c) All methods of an abstract class must be abstract
d) An abstract class cannot have fields
Q6. Since Java 8, an interface can contain which of these?
a) Instance fields
b) Constructors
c) default and static methods
d) protected abstract methods
Q7. A class can implement how many interfaces?
a) Only one
b) At most two
c) Any number
d) None if it already extends a class
Q8. Which access modifier makes a member visible in its own package and in subclasses in other packages?
a) private
b) default (no modifier)
c) protected
d) public
Q9. Which of these is method overloading?
a) Two methods with the same name and same parameters in parent and child
b) Two methods with the same name but different parameter lists in one class
c) A method with the same name as the class
d) A method declared with the final keyword
Q10. What happens when this code is compiled?
class Parent { final void show() { } }
class Child extends Parent { void show() { } }
a) It compiles and Child.show() overrides Parent.show()
b) Compilation error: show() in Child cannot override a final method
c) It compiles but throws an exception at run time
d) It compiles and Parent.show() is hidden
Q11. Static methods in Java are:
a) Overridden by subclasses
b) Hidden, not overridden, when redeclared in a subclass
c) Always abstract
d) Called only through objects
Q12. What is printed?
class Counter {
static int count = 0;
Counter() { count++; }
}
public class Main {
public static void main(String[] args) {
new Counter(); new Counter(); new Counter();
System.out.print(Counter.count);
}
}
a) 0
b) 1
c) 3
d) Compilation error
Q13. Which keyword is used to call the parent class's version of an overridden method?
a) this
b) super
c) parent
d) base
Q14. Encapsulation is best achieved by:
a) Making all fields public
b) Declaring fields private and exposing them through methods
c) Using only static methods
d) Avoiding constructors
Q15. Which statement is true about constructors?
a) A constructor must have a return type of void
b) A constructor can be inherited
c) If no constructor is written, the compiler adds a default no-argument constructor
d) A constructor can be declared abstract
Q16. The instanceof operator is used to:
a) Create a new instance of a class
b) Check whether an object is of a given type
c) Compare two objects for equality
d) Convert a primitive to an object
Recommended MCQs
Comments
Post a Comment