Question 2631
apple banana cherry. What is printed?
String s = sc.next();
sc.close();
System.out.println(s);Answer images
Work through AP Computer Science A multiple-choice questions unit by unit, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
apple banana cherry. What is printed?
String s = sc.next();
sc.close();
System.out.println(s);
3.5. What is printed?
double d = sc.nextDouble();
sc.close();
System.out.println(d * 2 + 1);
true. What is printed?
boolean b = sc.nextBoolean();
sc.close();
if (b) {
System.out.println("YES");
} else {
System.out.println("NO");
}
7 3. What is printed?
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
System.out.println(a / b);
hello worldgoodbyeString s = sc.next();
String t = sc.nextLine();
sc.close();
System.out.println(s + t);
25. What is printed?
String s = sc.next();
sc.close();
int x = Integer.parseInt(s) + 5;
System.out.println(x);
2.5. What is printed?
String s = sc.next();
sc.close();
double d = Double.parseDouble(s);
System.out.println(d + d);
hello world (with extra spaces). What is printed?
String s = sc.next();
String t = sc.next();
sc.close();
System.out.println(s.length() + " " + t.length());
boolean result = sc.hasNext();
sc.close();
System.out.println(result);The correct answer is C: false.
hasNext() returns true only if there is another token available in the input. Since the file is completely empty, there are no tokens, so it cannot return true.hasNext() does not throw an exception when the file is empty — it simply returns false. No runtime error occurs.Scanner is connected to a completely empty file, hasNext() checks whether there is any token remaining. Since the file has no data at all, there are no tokens, so hasNext() returns false. This boolean value is stored in result and printed.hasNext() returns a primitive boolean, not a reference type, so it can never be null. Printing a boolean primitive always prints either true or false.This question tests understanding of the Scanner class and its hasNext() method in Java.
Key concept: Scanner.hasNext() is a non-destructive method that returns a primitive boolean — true if there is another token in the input, and false if there is not. It does not advance the scanner or throw an exception when the input is exhausted.
Why false? An empty file contains zero bytes of data. When the scanner attempts to find the next token, it finds nothing, so hasNext() returns false. This is the standard, safe way to check for end-of-file in Java.
Primitive vs. reference types: Because result is declared as a primitive boolean (not Boolean), it cannot hold null. System.out.println(boolean) will always print either true or false.
Study tip: Memorize the Scanner "check before you read" pattern: always call hasNext() (or hasNextInt(), hasNextLine(), etc.) before calling the corresponding read method. hasNext() is always safe to call and returns false at end-of-file — it never throws an exception. Also practice distinguishing primitive types (boolean, int, double) from their wrapper classes (Boolean, Integer, Double): primitives cannot be null. These two concepts appear frequently on the AP CS A exam.
Students commonly fall into these traps on this question:
hasNext() with next(), or assume the scanner always starts as "ready" and returns true by default. Remember: hasNext() reflects the actual state of the input stream.NoSuchElementException thrown by next() on an empty scanner may assume hasNext() also throws an exception. It does not — hasNext() is specifically designed to be called safely to prevent that exception.null is only a valid value for reference types (like Boolean, String, etc.). A primitive boolean can never be null. Always check the declared type of a variable.
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.