nerdexam
Oracle

1Z0-808 · Question #36

class Product { double price; } public class Test { public static void updatePrice (Product product, double price) { product.price = price; price = 2; product.price = product.price + price; } public…

The correct answer is C. 200.0 : 100.0. After call to updatePrice prt.price change its value to 400 (prt is passed by reference) variable newPrice never changes its value from 100 (newPrice is passed by value)

Java Class Design

Question

class Product { double price; } public class Test { public static void updatePrice (Product product, double price) { product.price = price; price = 2; product.price = product.price + price; } public static void main(String[] args) { Product prt = new Product(); prt.price = 200; double newPrice = 100; Test t = new Test(); t.updatePrice (prt, newPrice); System.out.println (prt.price + ":" + newPrice); } } What is the result?

Options

  • A200.0 : 100.0
  • B400.0 : 200.0
  • C200.0 : 100.0
  • DCompilation fails.

How the community answered

(33 responses)
  • A
    6% (2)
  • B
    3% (1)
  • C
    79% (26)
  • D
    12% (4)

Explanation

After call to updatePrice prt.price change its value to 400 (prt is passed by reference) variable newPrice never changes its value from 100 (newPrice is passed by value)

Topics

#pass by value#object references#primitive types#method parameters

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice