CS140 40 20 F Sol
CS140 40 20 F Sol
Student ID:
Section No.:
Instructions:
1. Answer 4 questions; there are 4 questions in 6 pages.
2. Write your name on each page of the exam paper.
3. Write your answers directly on the question sheets. Use the ends of the question pages for
rough work or if you need extra space for your answer.
4. If information appears to be missing from a question, make a reasonable assumption, state
your assumption, and proceed.
5. No questions will be answered by the invigilator(s) during the exam period.
Page 1 of 6
Consider the following numbers generated randomly by calling sr.nextInt(1000) several times,
where sr is a variable of type SecureRandom:
1 import java.security.SecureRandom;
2
3 public class RandomNumbers {
4 public static void main(String[] args) {
5 SecureRandom sr = new SecureRandom();
6 int currentRandomNumber = sr.nextInt(1000);
7 int previousRandomNumber = currentRandomNumber;
8 System.out.print(currentRandomNumber + " ");
9
10 int count = 0;
11 while (count < 8) {
12 if (currentRandomNumber > previousRandomNumber) {
13 System.out.print(currentRandomNumber + " ");
14 count++;
15 previousRandomNumber = currentRandomNumber;
16 }
17 currentRandomNumber = sr.nextInt(1000);
18 }
19 }
20 }
Output:
b) The while loop in this program may iterate infinitely, why? (2 marks)
If the number 999 is generated randomly before the last one. (2 marks)
Page 2 of 6
1 import java.util.Scanner;
2 import java.util.Formatter;
3 import java.nio.file.Paths;
4
5 public class Temperature {
6 public static void main(String[] args) throws Exception {
7 Scanner input = new Scanner(Paths.get());
8 Formatter output = new Formatter();
9
10 while(output.hasNext()) {
11 String day = input.next();
12 double sum = input.nextDouble() + input.nextDouble();
13 sum += input.nextDouble() + input.nextDouble();
14 input.format("%s %.2f\n", day, sum / 4);
15 }
16
17 input.close; output.close;
18 }
19 }
Answer: (for each correct line 0.5, and for each correction 1.5)
7 new Scanner(Paths.get("weather.txt"));
8 new Formatter("averages.txt");
10 while(input.hasNext()) {
17 input.close(); output.close();
Page 3 of 6
Write a Java method called charTwice that takes a non-empty string and returns true if every characters
of the string is found at least twice in the same string or returns false otherwise.
Examples:
charTwice("aabbvvmm<< ")); // will return false because the space found once
Answer:
// first approach using strings methods only
public static boolean charTwice(String s) { // 1 mark
for (int i = 0; i < s.length(); i++) { // 1 mark
char cur = s.charAt(i); // 1 mark
Page 4 of 6
a) Write a Java method called remove that takes 2 parameters: an array of integers and a another integer,
then replaces all occurrences of this integer in the array by 0. The method will also return the number
of elements that are 0 in the array.
Example:
System.out.println(remove(a, 2));
System.out.println(Arrays.toString(a));
Will output:
3
[0, 0, 5, 0, 3, 4]
Answer (6 marks):
Page 5 of 6
Answer (6 marks):
Page 6 of 6