Welcome to the Java Inheritance Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of Java Inheritance. Each question has a correct and brief explanation.
1. What is the output of the following Java code snippet?
class Animal {
String sound() {
return "Generic Sound";
}
}
class Dog extends Animal {
String sound() {
return "Bark";
}
}
public class Test {
public static void main(String[] args) {
Animal myAnimal = new Dog();
System.out.println(myAnimal.sound());
}
}
Answer:
Explanation:
This is an example of method overriding in Java. The Dog class overrides the sound method of the Animal class. Even though the object is referred to by a variable of type Animal, the Dog's sound method is called due to polymorphism.
2. What does this Java code snippet output?
class A {
int i = 10;
}
class B extends A {
int i = 20;
}
public class Main {
public static void main(String args[]) {
A a = new B();
System.out.println(a.i);
}
}
Answer:
Explanation:
Variable shadowing occurs here. The variable i in class B shadows the variable i in class A. Since the reference type of a is A, it accesses A's i, which is 10.
3. Identify the output of the following code:
class Vehicle {
String type = "4W";
int maxSpeed = 100;
Vehicle(String type, int maxSpeed) {
this.type = type;
this.maxSpeed = maxSpeed;
}
}
class Car extends Vehicle {
String trans;
Car(String trans) {
super("4W", 150);
this.trans = trans;
}
}
public class Test {
public static void main(String[] args) {
Car c = new Car("Auto");
System.out.println(c.type + " " + c.maxSpeed + " " + c.trans);
}
}
Answer:
Explanation:
The Car constructor calls the superclass constructor Vehicle with arguments, initializing type and maxSpeed. Then it initializes its own field trans.
4. What will be printed by this Java code?
class Parent {
void show() {
System.out.println("Parent's show()");
}
}
class Child extends Parent {
void show() {
System.out.println("Child's show()");
}
}
public class Main {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
obj1.show();
obj2.show();
}
}
Parent's show()
Child's show()
Answer:
Child's show()
Explanation:
obj1 is an instance of Parent and calls Parent's show(). obj2 is an instance of Child referred by a Parent reference and calls the overridden show() in Child.
5. What does this code snippet output?
class Base {
static void display() {
System.out.println("Base display()");
}
}
class Derived extends Base {
static void display() {
System.out.println("Derived display()");
}
}
public class Test {
public static void main(String args[]) {
Base obj = new Derived();
obj.display();
}
}
Answer:
Explanation:
Static methods are not overridden. They are hidden. obj.display() calls the display method of the Base class because the reference type of obj is Base.
6. What is the result of executing this code?
class Animal {
Animal() {
System.out.println("Animal is created");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog is created");
}
}
public class Test {
public static void main(String args[]) {
Dog d = new Dog();
}
}
Dog is created
Animal is created
Answer:
Dog is created
Explanation:
When an instance of a subclass is created, the constructor of the superclass is called first, followed by the constructor of the subclass.
7. What will the following Java code snippet output?
class Animal {
void eat() throws Exception {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void eat() {
System.out.println("Dog eats");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.eat();
}
}
Answer:
Explanation:
The eat method in Dog class cannot reduce the exception declared in the Animal class. Overridden methods must not throw checked exceptions that are new or broader than those declared by their superclass.
8. What does the following code snippet print?
class A {
String s = "Class A";
}
class B extends A {
String s = "Class B";
void display() {
System.out.println(s);
System.out.println(super.s);
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
b.display();
}
}
Class A
Class A
Class B
Answer:
Class A
Explanation:
The display method in class B prints its own field s and then the field s of its superclass A.
9. Determine the output of this Java code:
interface I {
default void display() {
System.out.println("Interface I");
}
}
class C implements I {
public void display() {
System.out.println("Class C");
}
}
public class Test {
public static void main(String[] args) {
I obj = new C();
obj.display();
}
}
Answer:
Explanation:
Class C provides its own implementation of the display method, which overrides the default method in the interface I.
10. What is the result of the following code snippet?
abstract class AbstractClass {
abstract void abstractMethod();
void concreteMethod() {
System.out.println("This is a concrete method.");
}
}
class SubClass extends AbstractClass {
void abstractMethod() {
System.out.println("Abstract method implementation.");
}
}
public class Test {
public static void main(String[] args) {
AbstractClass obj = new SubClass();
obj.abstractMethod();
obj.concreteMethod();
}
}
This is a concrete method.
Answer:
This is a concrete method.
Explanation:
The SubClass provides an implementation for the abstract method of AbstractClass. Both the implemented abstract method and the concrete method are called on the SubClass instance.
11. What is the result of the following code snippet?
class ClassOne {
void method(String s1) {
method(s1, s1 + s1);
}
void method(String s1, String s2) {
method(s1, s2, s1 + s2); //
}
void method(String s1, String s2, String s3) {
System.out.println(s1 + s2 + s3);
}
}
public class Main {
public static void main(String[] args) {
ClassOne one = new ClassOne();
one.method("A");
}
}
Answer:
Explanation:
The program demonstrates method overloading in Java within the ClassOne. The class defines three overloaded versions of the method function, each accepting a different number of string parameters. In the main method, an instance of ClassOne is created, and the overloaded method is invoked with a single string argument "A," showcasing the flexibility of method overloading in handling different parameter combinations. The output is "AAAAAA," resulting from the concatenation of the provided strings in the invoked methods.
Comments
Post a Comment
Leave Comment