Java OCA OCP Practice Question 301

Question

Given:

 3. import java.util.*;
 4. public class Main {
 5.   public static void main(String[] args) {
 6.     String input = "1 2 a 3 45 6";
 7.     Scanner sc = new Scanner(input);
 8.     int x = 0;
 9.     do {//from www  . jav a  2 s . c o  m
10.       x = sc.nextInt();
11.       System.out.print(x + " ");
12.     } while (x!=0);
13.   }
14. }

What is the result?

  • A. 1 2
  • B. 1 2 3 45 6
  • C. 1 2 3 4 5 6
  • D. 1 2 a 3 45 6
  • E. Compilation fails
  • F. 1 2 followed by an exception


F is correct.

Note

The nextXxx() methods are typically invoked after a call to a hasNextXxx(), which determines whether the next token is of the correct type.




PreviousNext

Related