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

Given the code fragment:

public static void main (String [ ] args) throws IOException {

BufferedReader br = new BufferedReader (new InputStremReader (System.in));

System.out.print ("Enter GDP: ");

//line 1

}

Which code fragment, when inserted at line 1, enables the code to read the GDP from the user?

    Correct Answer: A

    To enable the code to read an integer value such as GDP from the user input, using BufferedReader, you need to read the input as a string and then convert it to an integer. The correct method to read a line of text is br.readLine(), and you can parse this string to an integer using Integer.parseInt(). Hence, the correct code fragment is int GDP = Integer.parseInt(br.readLine());. The other options are incorrect because br.read() reads a single character, and br.nextInt() and br.next() methods do not exist for BufferedReader.

Discussion
Ritesh_Option: A

Answer is A

Sofia_ShireenOption: A

int GDP = Integer.parseInt(br.readLine());

InnovationOption: A

A is correct, tested

steefaandOption: A

A is correct. C and D don't compile and B reads single char and not whole value of input.

duydnOption: A

readLine() not readline()

wk8bOption: B

Answer is B. A is incorrect, in code was used br.readline() not br.readLine so it is incorrect.

r1muka5Option: A

Correct answer is A. Tested.