Identify the line of code to be placed in Line 27 of the code snippet given below to display the zipCode of the customer.

class Address {
	private int zipCode;

    public Address(int zipCode) {
		this.zipCode= zipCode;
	}

	public int getZipCode() {
		return zipCode;
	}
}

public class Customer {
	public Address address;
	public String name;

	public Customer(String name, int zipCode) {
		this.name = name;
		address = new Address(zipCode);
	}
}

public class Tester{

	public static void main(String args[]) {
		Customer customer = new Customer("Sam",100001);
		// Line 27
	}
}

System.out.println(this.address.getZipCode());

System.out.println(customer.address.zipCode);

System.out.println(customer.address.getZipCode());

Compilation error - not possible to access the zipCode of Address class

Verified Answer
Correct Option - c
Anush said

The correct line of code to place in Line 27 to display the zipCode of the customer is: System.out.println(customer.address.getZipCode());. This works because the getZipCode() method is a public method that allows access to the private zipCode field of the Address class. Accessing customer.address.zipCode directly would cause a compilation error since zipCode is private. The this keyword is not applicable here since it is used within instance methods, not in static contexts like the main method.

To get all Self Assessment - Programming using Java Exam questions Join Telegram Group https://rebrand.ly/lex-telegram-236dee

Telegram