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

Java Quiz 10: Demonstrating Anonymous Class

DZone's Guide to

Java Quiz 10: Demonstrating Anonymous Class

Check out the answer to our last quiz about multilevel inheritance and try your hand at guessing the output of an anonymous class.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Before we start with this week's quiz, here is the answer to Java Quiz 9: Demonstrating Multilevel Inheritance

  1. The statement new WildAnimal("Leopard"); calls the one-argument constructor of the class WildAnimal. The class WildAnimal is a subclass of the class Animal. Therefore, it calls the no-argument constructor of its superclass. The statement System.out.print("Tiger" + " "); writes Tiger to the standard output. The WildAnimal constructor writes the argument to the standard output. So, it writes Leopard.

  2. The statement new BigCat(); calls its one-argument constructor using this("Jaguar");. The statement super(s); calls the one-argument constructor of its superclass WildAnimal, which also calls the constructor of its superclass Animal. The statement System.out.print("Tiger" + " "); writes Tiger to the standard output. The statement System.out.print(s + " "); writes the argument Jaguar to the standard output. The statement System.out.print(s + " "); inside the one-argument constructor of the BigCat also writes Jaguar to the standard output. Therefore, the code writes: Tiger Leopard Tiger Jaguar Jaguar to the standard output

    The correct answer is: b.

Here is the quiz for today!

What happens when the following program is compiled and run?

Note: The interface Height.java and the class AnonymousClass.java are two separate files in one package.

Height.java

public interface Height
{
    int i = 3;

    void print();
    void write();
}


AnonymousClass.java

public class AnonymousClass
{
    int i = 5;

    public static void main(String[] args)
    {
        AnonymousClass ac = new AnonymousClass();
        System.out.print(ac.i);
        Height height = new Height()
        {
            public void print()
            {
                System.out.print(i);
            }
            public void write()
            {
                System.out.print("x");
            }
        };
        height.print();
    }
}


  • A. This program writes "53" to the standard output.

  • B. This program writes "53x" to the standard output.

  • C. This program writes "5x" to the standard output.

  • D. This program writes "5" to the standard output.

  • E. This program writes "35" to the standard output.

  • F. Runtime error.

  • G. Compilation error.

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!

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,quiz ,anonymous class ,inheritance

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}