Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peer Instruction 9 File Input/Output.

Similar presentations


Presentation on theme: "Peer Instruction 9 File Input/Output."— Presentation transcript:

1 Peer Instruction 9 File Input/Output

2 cs163/164: Peer 9 - File Input/Output - Fall Semester 2016
Which line of code creates a Scanner to read a file named by the String inputFile? Scanner in = new Scanner(inputFile); Scanner in = new Scanner("inputFile"); Scanner in = new Scanner (System.in); Scanner in = new Scanner(new File(inputFile)); Scanner in = new Scanner(new File("inputFile")); makes a Scanner for the filename. makes a Scanner for the literal. makes a Scanner for the keyboard. D) correct answer, makes a File object using the file name. E) only works if the file name is “inputFile” File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

3 cs163/164: Peer 9 - File Input/Output - Fall Semester 2016
Which line of code creates a PrintWriter to write a file named "output.txt"? PrintWriter out = new PrintWriter (output.txt); PrintWriter out = new PrintWriter ("output.txt"); PrintWriter out = new PrintWriter (System.out); PrintWriter out = new PrintWriter (new File(output.txt)); PrintWriter out = new PrintWriter (new File("output.txt")); Answer is B) or E), need a File object and literal was specified, overloaded constructor! File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

4 cs163/164: Peer 9 - File Input/Output - Fall Semester 2016
Given the file contents in red and a Scanner in, which values are read to d0/d1/d2? double d0 = in.nextDouble(); double d1 = in.nextDouble(); double d2 = in.nextDouble(); File contents: 11.1, 22.2, 33.3 11, 83, 22 11.1, 83.0, 22.2 11.1, exception! Answer is C), integers in the input will be promoted to doubles. File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

5 cs163/164: Peer 9 - File Input/Output - Fall Semester 2016
Given the code shown, how many lines in the output file? Assume out is a PrintWriter. out.print("123\thello\n"); out.println("there\t&&\n"); out.println("\ngoodbye"); 3 lines 4 lines 5 lines 6 lines 7 lines Answer is C), two println statements and three newlines. File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

6 Which of the following lines are incorrect in the code shown below?
1 File in = new File(inputFile); 2 try { 3 Scanner s = new Scanner(inputFile); 4 while (s.hasNextLine()) 5 System.out.println(s.next()); 6 } catch (IOException) { s.close(); 8 } Line 3 Line 4 and 5 Line 6 Line 7 What a mess! Answer is E), line 3 has inputFile instead of in, 4 and 5 are dangerous, 6 is missing object, 7 has close in catch! File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

7 cs163/164: Peer 9 - File Input/Output - Fall Semester 2016
What are the values of the four variables after executing the code below? int i = keyboard.nextInt(); double d = keyboard.nextDouble(); String s0 = keyboard.nextLine(); String s1 = keyboard.next(); File contents: 12 5.4 Hello There 12, 5.4, "", "Hello" 12, 5.4, "Hello There", "" 12, 5.4, "", "Hello There" 12, 5.4, "\n", "Hello" 12, 5.4, "\n", "Hello\n" Answer is A), the nextLine() calls reads from end of previous token to end of line, discards newline. File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

8 How many bytes are written to the file by the code shown below:
PrintWriter out = null; try { out = new PrintWriter("out.txt"); out.println("Hello World!"); } catch (IOException e) { out.close(); } 11 12 13 No file written! Answer is A) or D), file is never closed. File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

9 How many bytes are written to the file by the code shown below:
try { PrintWriter out = new PrintWriter(new File("out.txt")); out.println("Hello World!"); out.close(); } catch (IOException e) { // Error message } 11 12 13 No file written! Answer is D), length of string is 12 plus newline = 13 File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

10 Which definition of checked or unchecked exception is correct?
Unchecked: the program must catch/throw the exception. Unchecked: the program cannot catch/throw the exception. Checked: the program must catch/throw the exception. Checked: the program cannot catch/throw the exception. None of the above Answer is C), unchecked means catch/throw is optional, checked means must catch/throw. A method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow. Java Exceptions cs163/164: Peer 9 - File Input/Output - Fall Semester 2016


Download ppt "Peer Instruction 9 File Input/Output."

Similar presentations


Ads by Google