Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 56

Given:

What is the result?

    Correct Answer: A

    The code attempts to convert the string 'a' to an integer using Integer.parseInt, which throws a NumberFormatException since 'a' is not a valid integer. This exception is caught, and an empty Optional is returned. When the main method calls convert("a").get(), it attempts to retrieve the value from this empty Optional, which results in a NoSuchElementException being thrown at runtime.

Discussion
Ankit1010Option: A

Since Integer.parseInt("a") will throw NumberFormatException. Empty Optional will be returned. and if we try to get() from Empty Optional it results into NoSuchElementException. So, correct answer is A

aruni_mishraOption: A

.get() : If a value is present, returns the value, otherwise throws NoSuchElementException.

d7bb0b2Option: A

public static void main(String[] args) { System.out.println("Ans:" + convert("a").get()); } private static Optional<Integer> convert(String s){ try{ return Optional.of(Integer.parseInt(s)); }catch (Exception e){ return Optional.empty(); } } A is correct

OmnisumemOption: A

Tested: A.

tmuralimanoharOption: A

Answer: A

StavokOption: A

A is correct

RP384Option: A

NoSuchElementException at runtime