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

Java Quiz 1: Overriding Methods

DZone's Guide to

Java Quiz 1: Overriding Methods

As this series of Java quizzes starts back up, test your strength against this sample code demonstrating method overriding.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Welcome back to the second series of Java quizzes by me, Sar Maroof! If you haven't seen my first series of quizzes, take a look here. So, let's take a look at our kick-off quiz for this series.

What happens when you try to compile and run the following program?

public class MySuper
{
    String str1 = "x";

    public MySuper()
    {
        myMethod();
    }
    void myMethod()
    {
        System.out.print(str1);
    }
}
public class MySub extends MySuper
{
    String str2 = "y";

    void myMethod()
    {
        System.out.print(str2);
    }
    public static void main(String[] args)
    {
        MySub mySub = new MySub();
   }
}


Select the correct answer:

  • A: This code writes "x" to the standard output.

  • B: This code writes "y" to the standard output.

  • C: This code writes "xy" to the standard output.

  • D: This code writes "yx" to the standard output.

  • E: This code writes "null" to the standard output.

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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,quiz ,inheritance ,object oriented programming ,override

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}