Which class is normally used to read a text file in this course?
Q2
Which statement opens data.txt for reading?
Q3
What does the first option of the previous question actually do?
Q4
Which exception must be handled or declared when constructing a Scanner on a
File?
Q5
Which import is needed for the File class?
Q6
Which loop reads a file to the end, line by line?
Q7
This kind of loop is an example of:
Q8
What does new File("report.txt") do?
Q9
Which File method tells you whether a file or directory of that name exists?
Q10
Which statement creates a writer that overwrites any existing content?
Q11
Which statement creates a writer that appends?
Q12
What is the most likely consequence of forgetting out.close()?
Q13
Which PrintWriter method writes formatted output?
Q14
What is the advantage of try-with-resources?
Q15
A program cannot find marks.txt although the file is visible in the project folder.
The most likely reason is:
Q16
Which pair of parts must every recursive method have?
Q17
What happens when the base case is never reached?
Q18
What is the value of factorial(4) for the definition given in the review?
Q19
How many times is factorial called in total when factorial(4) is evaluated?
Q20
What does this method return for mystery(4)?
public static int mystery(int n) {
if (n == 0) return 0;
return n + mystery(n - 1);
}
Q21
What does fib(5) return, with fib(0) = 0 and fib(1) = 1?
Q22
Why is the naive recursive fib inefficient?
Q23
What is printed?
public static void count(int n) {
if (n == 0) return;
System.out.print(n);
count(n - 1);
}
// count(3);
Q24
And this one?
public static void count(int n) {
if (n == 0) return;
count(n - 1);
System.out.print(n);
}
// count(3);
Q25
Why do the two previous methods print in opposite orders?
Q26
What does each recursive call receive of its own?
Q27
What does this method compute?
public static int f(int[] a, int i) {
if (i == a.length) return 0;
return a[i] + f(a, i + 1);
}
Q28
What is the base case of the recursive binary search given in the review?
Q29
Which problem is most naturally solved with recursion?
Q30
Which statement is true of recursion and iteration?
Q31
What does reverse("abc") return for the recursive definition in the review?
Q32
Which change would turn a correct recursion into a StackOverflowError?
Q33
What is printed?
public static int power(int b, int e) {
if (e == 0) return 1;
return b * power(b, e - 1);
}
// System.out.print(power(2, 5));
Q34Short answer
Write, in five lines or fewer, the code that opens scores.txt,
sums all the integers it contains and prints the total, including the handling of the checked
exception.
Q35Short answer
Explain the difference between new PrintWriter("log.txt") and
new PrintWriter(new FileWriter("log.txt", true)), and give one situation where each is the
right choice.
Q36Short answer
Draw the sequence of calls and returns for factorial(4),
showing what each frame returns to the one below it.
Q37Programming
Write a program GradeReport that reads a file students.txt
whose lines have the form id,name,grade, builds a Student object for each line, and
writes report.txt containing one formatted line per student (name left-aligned in 15
columns, grade with two decimals) plus a final line with the class average. Handle a missing
input file gracefully and close both streams.
Q38Programming
Write recursive methods sumDigits(int n) (the sum of the digits
of n), isPalindrome(String s) (ignoring case) and gcd(int a, int b) (Euclid's
algorithm: gcd(a,0)=a and gcd(a,b)=gcd(b, a mod b)). For each one, state the base case in
a comment and test it in main.