Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 55

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;}

static { count = 0; }

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("Lars", "Svenson");

c4 = null;

c3 = c2;

System.out.println (Customer.getCount());

}

}

What is the result?

    Correct Answer: D

    The static variable 'count' is initialized to 0. Each time a Customer object is instantiated, the constructor increments the static 'count' variable by 1. Four Customer objects are instantiated (c1, c2, c3, and c4), so 'count' is incremented four times, resulting in a final count of 4. Nullifying 'c4' and setting 'c3' to 'c2' does not affect the 'count' because it tracks the number of Customer objects created, not their current references.

Discussion
thetechOption: D

Answer is D=4. Tested

asdfjhfgjuaDCVOption: D

Tested: 4 is the correct answer

steefaandOption: D

D is correct as count is increased only when new object is created. Since it is static variable it is shared among all instances.

GaelBernardOption: D

Each call to constructor increases count by 1. count starts at 0 and there are 4 calls to constructor. It makes count = 4 at the end when printing.

WilsonKKerllOption: D

Answer is D.