Java SE 8 Programmer II — Question 13

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?

Answer options

Correct answer: D

Explanation

The correct answer is 4 because four instances of the Customer class are created, which increments the static count variable. The subsequent assignments of c4 to null and c3 to c2 do not affect the count, as they do not remove the Customer instances that were created.