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

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

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.

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. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

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