What does this code print if the user enters 42?
Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println(x);
42
What does this code do?:
Scanner sc = new Scanner(new File("data.txt"));
Opens data.txt so it can be read using a Scanner.
What error occurs if the file does not exist?
FileNotFoundException
What loop is commonly used to read a file until the end?
while loop
What class is used to write to files in Java?
PrintWriter
What is the output when the user enters the string "hello world"?
Scanner sc = new Scanner(System.in); String word = sc.next(); System.out.println(word);
hello
What loop condition is best for reading all tokens in a file?
while (sc.hasNext())
What is wrong with this code?:
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
Mixing hasNext() with nextLine() can cause issues; should use hasNextLine().
What does this code count?:
int ronak = 0;
while (sc.hasNext()) {
sc.next();
ronak++;
}
Number of tokens (words/items)
What does this code do?:
PrintWriter pw = new PrintWriter("out.txt");
pw.println("Hello");
pw.close();
Writes "Hello" to out.txt
What does the code print if the user enters 5 10? (5, a space, and then 10?)
Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a + b);
15
What does the code below print if data.txt contains the following?:
1 2 3
Scanner sc = new Scanner(new File("data.txt")); int sum = 0; while (sc.hasNextInt()) {sum += sc.nextInt();} System.out.println(sum);
6
What happens here if the file contains letters instead of numbers? (What error/exception is the result?):
int x = sc.nextInt();
InputMismatchException
What does this print if file contains:
10 20 30
Code:
int max = Integer.MIN_VALUE;
while (sc.hasNextInt()) {
int n = sc.nextInt();
if (n > max) {
max = n;
}
}
System.out.println(max);
30
What happens if close() is not called?
Data may not be written properly (buffer not flushed).
What is printed if the user enters the following?:
Hello
World
Scanner sc = new Scanner(System.in); String a = sc.nextLine(); String b = sc.nextLine(); System.out.println(a + " " + b);
Hello World
What does this code print if data.txt contains the following?:
apple
banana
Code:
Scanner sc = new Scanner(new File("data.txt")); while (sc.hasNextLine()) {System.out.println(sc.nextLine());}
apple
banana
Why is this an infinite loop?:
while (sc.hasNextInt()) {
System.out.println("Number found");
}
It never calls nextInt(), so the Scanner never advances.
What does this code count?:
int blindemann = 0;
while (sc.hasNextLine()) {
sc.nextLine();
blindemann++;
}
Number of lines in the file
What does this code produce in the file?:
PrintWriter pw = new PrintWriter("out.txt");
pw.print(5);
pw.print(10);
pw.close();
510
Why does this code sometimes skip input?:
Scanner sc = new Scanner(System.in); int num = sc.nextInt(); String line = sc.nextLine(); System.out.println(line);
nextInt() leaves a newline in the buffer, so nextLine() reads that leftover newline instead of actual input.
What must be added to this code to avoid a compile-time error?:
Scanner sc = new Scanner(new File("data.txt"));
throws FileNotFoundException or a try-catch block.
What should be done to fix the bug in the code?:
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextLine();
Change int y to String y OR consume newline before reading line.
What does this print if file contains:
a b c d
Code:
int count = 0;
while (sc.hasNext()) {
String s = sc.next();
if (s.equals("c")) {
break;
}
count++;
}
System.out.println(count);
2
What does this write to the file?:
PrintWriter pw = new PrintWriter("out.txt");
for (int i = 1; i <= 3; i++) {
pw.println(i * 2);
}
pw.close();
2
4
6