Java and Spring Boot Coding MCQ
Questions
Java MCQ Questions (OOP & super keyword)
1. 1. class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
super.sound();
System.out.println("Dog barks");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
What is the output?
A. Dog barks
B. Animal makes a sound
C. Animal makes a sound followed by Dog barks
D. Compile-time error
2. 2. class A {
A() {
System.out.println("A's constructor");
}
}
class B extends A {
B() {
System.out.println("B's constructor");
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
}
}
What is the output?
A. B's constructor
B. A's constructor followed by B's constructor
C. B's constructor followed by A's constructor
D. Compile-time error
3. 3. class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void print() {
System.out.println(super.x);
}
}
public class Demo {
public static void main(String[] args) {
Child c = new Child();
c.print();
}
}
What is the output?
A. 10
B. 20
C. Compile-time error
D. 0
4. 4. Which is not an advantage of inheritance?
A. Code reusability
B. Runtime polymorphism
C. Reduces redundancy
D. Increases coupling between classes
5. 5. abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
What is the output?
A. Drawing Shape
B. Drawing Circle
C. Compile-time error
D. Runtime error