DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Trending

  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Java Quiz 8: Upcasting and Downcasting Objects

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.

By 
Sar Maroof
·
Jan. 04, 18 · Survey/Contest
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
17.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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!

Java (programming language) Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook