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

Given the code fragments:

class Employee {

Optional

address;

Employee (Optional

address) {

this.address = address;

}

public Optional

getAddress() { return address; }

}

class Address {

String city = "New York";

public String getCity { return city: }

public String toString() {

return city;

}

}

and

Address address = new Address;

Optional

addrs1 = Optional.ofNullable (address);

Employee e1 = new Employee (addrs1);

String eAddress = (addrs1.isPresent()) ? addrs1.get().getCity() : "City Not available";

System.out.println(eAddress);

What is the result?

    Correct Answer: A

    The given code creates an Address object which is then wrapped in an Optional using Optional.ofNullable() and assigned to addrs1. Since the address object is not null, addrs1.isPresent() will return true. Therefore, the expression addrs1.get().getCity() will be evaluated, returning the city of the Address object, which is 'New York'. Hence, the result printed is 'New York'.

Discussion
Svetleto13Option: A

Ok.Lets resolve it.Answer is A,tested.If you check with Address address = null; answer is B.Happy days for everyone.

maslacOption: A

Answer is A. Notice creation of Optional with ofNullable(). This is another version of this question, in first version null is passed to ofNullable() method when creating Optional so that version returns City Not Available.

steefaandOption: A

A is correct.

iSnoverOption: A

The answer is A, he gets access to New York.

jduarteOption: B

Answer B. tested City Not available

maslac

wrong!