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

Java Quiz: Using Inheritance and Static Methods

DZone's Guide to

Java Quiz: Using Inheritance and Static Methods

A new quiz for advanced Java programmers on the behavior of Java inheritance and static methods.

· 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!

Last Week's Answer

ClassA classA = new ClassA(new ClassB(new ClassB().method(2)));

//1)
  new ClassB().method(2)
   //c)
  System.out.print(new ClassA().method()); //the method returns y=0
     //b)
  System.out.print(new ClassB(4).method(3)); //the method returns x+i=3+3=6
       //a)
System.out.print(i); i=4 ;
//2)
   new ClassB(new ClassB().method(2));
   new ClassB().method(2) returns x+i = 3+2=5
   new ClassB(new ClassB().method(2)) = new ClassB(5);
   System.out.print(i); i=5
//3)
   new ClassA(new ClassB(new ClassB().method(2))) = new ClassA(new ClassB);
     System.out.print(classB.method(1));
     classB.method(1); returns x + i = 3+1 = 4

The result is 46054.

This Week's Quiz 

Purpose

  1. Introduce some tricks and behaviors of Java inheritance and overwriting methods.

  2. Compare an instance variable with a static variable.

  3. Mind training for programmers to improve their imaginations and expectations. 

What is written to the standard output as the result of executing the following code?

public class MySuper {

    int x = 2;
    static int s = 3;

    MySuper() {
        x += 4;
    }
    MySuper(int x) {
        this();
        x += x + 20;
    }
    public MySuper method(){
        x += 35;
        return new MyClass();
    }
}
----------------

public class MyClass extends MySuper {

    public MyClass() {
        super(3);
        x -= 7;
        s += x;
    }
    public MySuper method(){
        x += 7;
        return super.method();
    }
    public static void main(String[] args){
        MyClass mc = new MyClass();
        mc.method();
        System.out.println(s);
    }
}


Let us know if you have any questions or comments on our quiz series, and as always, you can find Sar here

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. 

Topics:
java ,quiz ,inheritance ,static methods

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}