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

Delivering modern software? Atomist automates your software delivery experience.

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.

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.

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 }}