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

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

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.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

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