nerdexam
Oracle

1Z0-809 · Question #105

Given: ``java public class Customer { private String fName; private String lName; private static int count; public Customer (String first, String last) {fName = first; lName = last; count++;} public…

The correct answer is D. 4. count is a static variable, meaning it belongs to the Customer class itself and is incremented inside the constructor. Four Customer objects are constructed (c1, c2, c3, c4), so the constructor runs four times, making count = 4 - and getCount() returns that value regardless of…

Question

Given:
public class Customer {
 private String fName;
 private String lName;
 private static int count;
 public Customer (String first, String last) {fName = first; lName = last; count++;}
 public static int getCount() {return count;}
}

public class App {
 public static void main (String [] args) {
 Customer c1 = new Customer("Larry", "Smith");
 Customer c2 = new Customer("Pedro", "Gonzales");
 Customer c3 = new Customer("Penny", "Jones");
 Customer c4 = new Customer("Lara", "Svenson");
 c3 = c2;
 c4 = null;
 System.out.println (Customer.getCount());
 }
}
What is the result?

Options

  • A0
  • B2
  • C3
  • D4
  • E5

How the community answered

(62 responses)
  • A
    8% (5)
  • B
    16% (10)
  • C
    3% (2)
  • D
    71% (44)
  • E
    2% (1)

Explanation

count is a static variable, meaning it belongs to the Customer class itself and is incremented inside the constructor. Four Customer objects are constructed (c1, c2, c3, c4), so the constructor runs four times, making count = 4 - and getCount() returns that value regardless of what happens to the references afterward.

Why the distractors fail:

  • A (0) - count starts at 0 but is incremented every time the constructor is called; it never stays 0.
  • B (2) - There is no mechanism in this code that decrements count; 2 might seem appealing if you mistakenly count only "reachable unique objects" after c3 = c2.
  • C (3) - A common trap: assuming c4 = null somehow "removes" an object from count. It doesn't - nulling a reference has no effect on a static counter.
  • E (5) - No fifth constructor call ever happens; the line c3 = c2 just redirects a reference, it does not create a new object.

Memory tip: Think of static as a scoreboard on the wall of the class - every time a new object is born (constructor called), a tally is added, but erasing a player's name from the roster (null or reassignment) never removes a tally from the scoreboard.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice