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

Java Quiz 8: Upcasting and Downcasting Objects

DZone's Guide to

Java Quiz 8: Upcasting and Downcasting Objects

Check in on the results from a previous quiz about unary operators in Java and try out your knowledge up upcasting and downcasting objects.

· Java Zone ·
Free Resource

Learn how to build stream processing applications in Java-includes reference application. Brought to you in partnership with Hazelcast.

Before we start with this week's quiz, here is the answer to Java Quiz 7: Using Unary Operators.

x++ and ++y are very similar, but not exactly the same. Both increment the value of the variable by one, but ++y increments the value by one before the current expression is evaluated, while x++ increments the value of the variable by one after the expression is evaluated. The statement MyClass mc = new MyClass(3, 3); creates the object mc. By using the statement this.x = x++; the value of x remains x, while the statement this.y = ++y; increments the value of y by one. So, y = 4. The statement System.out.println(mc.method(mc)); invokes the method. The statement mc.x += 9; increments the value of x by 9. So, x = 3 + 9 = 12. The statement mc.y += 2; increments the value of y by 2. So, y = 4 + 2 = 6. The method returns mc.x + mc.y = 12 + 6 = 18.

The correct answer is c.

Here is the quiz for today!

What happens when the following program is compiled and run?

Note: The classes Vehicle and Car are in two separate files, namely Vehicle.java and Car.java:

Vehicle.java:

public class Vehicle
{
    int id = 120;

    public int getId()
    {
        return id;
    }
}


Car.java:

public class Car extends Vehicle
{
    int id = 100;

    public int getId()
    {
        return(id - 20);
    }
    public static void main(String[] args)
    {
        Vehicle vc = new Car();
        Car car = (Car) vc;
        System.out.print(vc.id + ", " + vc.getId() + ", ");
        System.out.print(car.id + ", " + car.getId());
    }
}


  • A. The program writes "120, 100, 120, 100" to the standard output.
  • B. The program writes "120, 120, 120, 120" to the standard output.
  • C. The program writes "100, 100, 100, 100" to the standard output.
  • D. The program writes "120, 80, 120, 80" to the standard output.
  • E. The program writes "120, 80, 100, 80" to the standard output.
  • F. The program writes "100, 80, 120, 80" 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!

Learn how to build distributed stream processing applications in Java that elastically scale to meet demand- includes reference application.  Brought to you in partnership with Hazelcast.

Topics:
java ,quiz ,upcasting ,downcasting ,unary operators

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}