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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Creating a Deep vs. Shallow Copy of an Object in Java
  • Java String: A Complete Guide With Examples
  • Java: A Time-Tested Programming Language Still Going Strong
  • Generics in Java and Their Implementation

Trending

  • How To Handle Technical Debt in Scrum
  • Extracting Maximum Value From Logs
  • SAP Business One vs. NetSuite: Comparison and Contrast of ERP Platforms
  • Understanding Europe's Cyber Resilience Act and What It Means for You
  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.

Sar Maroof user avatar by
Sar Maroof
·
Jan. 04, 18 · Survey/Contest
Like (4)
Save
Tweet
Share
16.24K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Creating a Deep vs. Shallow Copy of an Object in Java
  • Java String: A Complete Guide With Examples
  • Java: A Time-Tested Programming Language Still Going Strong
  • Generics in Java and Their Implementation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: