Java puzzle 27: Implementing interfaces and casting

<< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
 P25   P26   P27   P28   P29   P30 
 By title 

The output of this program is 3200.0, 1199.0. Write only one statement at line 8.
As a result of your statement, the output of this code will be 3100.0, 1350.0
What is that statement?

Java puzzle 27: Implementing interfaces and casting

Level: Hard

public class Test
  public static void main(String[] args)
    Payable pay = new Freelancer(3400);
    Freelancer fr = new Freelancer(3500);
    // add your statement here!
    System.out.print(fr.wage + ", ");
class Freelancer implements Payable
  public double wage;
  public double tax = 1234;
  public Freelancer(double wage)
    this.wage = wage - 300; -= wage / 100;
  public void setTax()
  { += 150;
interface Payable
  public void setTax();
Author: Sar Maroof

Answer explanation

  1. The statement should assign both values 3100.0 and 1350.0 respectively to th variables wage and tax.
  2. The reference pay = new Freelancer(3400); assigns the value 3400 – 300 = 3100.0 to the variable wage, and assigns the value (tax = 1234 – 3400/100 = 1200) 1200 to the variable tax. By invoking the method pay.setTax(); the value ( += 150 = 1200 + 150 = 1350) 1350  is assigned to the variable tax.
  3. So, the statement is fr = (Freelancer) pay;

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below! offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.

Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof


 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!


Add a Comment
  1. Your “Answer explanation” line #3 is not correct, it should be:

    fr = (Freelancer) pay;

    1. Thanks Greg, you’r right the “freelance” object doesn’t exist it is “fr” object. I have fixed it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017