Scanner Basics
Reading From Files
Common Errors & Bugs
Loops & File Processing
Output & File Writing
100

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

100

What does this code do?:

Scanner sc = new Scanner(new File("data.txt"));

 Opens data.txt so it can be read using a Scanner.

100

What error occurs if the file does not exist?

FileNotFoundException

100

What loop is commonly used to read a file until the end?

while loop

100

What class is used to write to files in Java?

PrintWriter

200

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

200

What loop condition is best for reading all tokens in a file?

while (sc.hasNext())

200

What is wrong with this code?:

while (sc.hasNext()) {

    System.out.println(sc.nextLine());

}

Mixing hasNext() with nextLine() can cause issues; should use hasNextLine().

200

What does this code count?:

int ronak = 0;

while (sc.hasNext()) {

    sc.next();

    ronak++;

}

Number of tokens (words/items)

200

What does this code do?:

PrintWriter pw = new PrintWriter("out.txt");

pw.println("Hello");

pw.close();

Writes "Hello" to out.txt

300

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

300

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

300

What happens here if the file contains letters instead of numbers? (What error/exception is the result?):

int x = sc.nextInt();

InputMismatchException

300

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

300

What happens if close() is not called?

Data may not be written properly (buffer not flushed).

400

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

400

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

400

Why is this an infinite loop?:

while (sc.hasNextInt()) {

    System.out.println("Number found");

}

It never calls nextInt(), so the Scanner never advances.

400

What does this code count?:

int blindemann = 0;

while (sc.hasNextLine()) {

    sc.nextLine();

    blindemann++;

}

Number of lines in the file

400

What does this code produce in the file?:

PrintWriter pw = new PrintWriter("out.txt");

pw.print(5);

pw.print(10);

pw.close();

510

500

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.

500

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.

500

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.

500

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

500

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

M
e
n
u