Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Quiz 9: Demonstrating Multilevel Inheritance

DZone's Guide to

Java Quiz 9: Demonstrating Multilevel Inheritance

Check out the answer to a quiz about upcasting and downcasting objects in Java and try out your knowledge when it comes to multilevel inheritance.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Before we start with this week's quiz, here is the answer to Java Quiz 8: Upcasting and Downcasting Objects.

By upcasting objects, the overridden variable depends on the type of the object reference vc, but the overridden methods depend on the type of the object that was created. By downcasting objects, both variables and methods depend on the type of the object reference car.

The correct answer is: e.

Here is the quiz for today!

What happens when the following program is compiled and run?

Note: The classes Animal, WildAnimal, and BigCat are three separate files in one package.

Animal.java:

public class Animal {
    Animal() {
        System.out.print("Tiger" + " ");
    }
}


WildAnimal.java:

class WildAnimal extends Animal {
    WildAnimal(String s) {
        System.out.print(s + " ");
    }
}


BigCat.java:

public class BigCat extends WildAnimal {
    BigCat() {
        this("Jaguar");
    }
    BigCat(String s) {
        super(s);
        System.out.print(s + " ");
    }
    public static void main(String[] args) {
        new WildAnimal("Leopard");
        new BigCat();
    }
}


  • A. This program writes "Tiger Leopard Jaguar Jaguar" to the standard output.

  • B. This program writes "Tiger Leopard Tiger Jaguar Jaguar" to the standard output.

  • C. This program writes "Tiger Leopard Jaguar" to the standard output.

  • D. This program writes "Tiger Leopard Tiger Jaguar" to the standard output.

  • E. This program writes "Tiger Leopard" to the standard output.

  • F. This program does not compile.

The correct answer and its explanation will be included in the next quiz in two weeks! For more Java quizzes, puzzles, and assignments, take a look at my site!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
java ,quiz

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}