0% found this document useful (0 votes)
110 views6 pages

CS140 40 20 F Sol

The document contains instructions for a final exam for a Computer Programming 1 course. It provides 4 questions to answer in 2 hours, with a total of 40 marks. Question 1 asks the student to identify the output of a code sample and explain why a while loop may iterate infinitely. Question 2 identifies 5 errors in a code sample and asks the student to provide the line numbers and corrections. Question 3 asks the student to write a method to check if every character in a string appears at least twice. Question 4 has two parts, first to write a method to replace all occurrences of an integer in an array with 0, and second to write a method to calculate the centered average of an array by excluding the largest and smallest values.

Uploaded by

kiledcomw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views6 pages

CS140 40 20 F Sol

The document contains instructions for a final exam for a Computer Programming 1 course. It provides 4 questions to answer in 2 hours, with a total of 40 marks. Question 1 asks the student to identify the output of a code sample and explain why a while loop may iterate infinitely. Question 2 identifies 5 errors in a code sample and asks the student to provide the line numbers and corrections. Question 3 asks the student to write a method to check if every character in a string appears at least twice. Question 4 has two parts, first to write a method to replace all occurrences of an integer in an array with 0, and second to write a method to calculate the centered average of an array by excluding the largest and smallest values.

Uploaded by

kiledcomw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Al Imam Mohammad Ibn Saud Islamic University

College of Computer and Information Sciences


Computer Science Department
Course Title: Computer Programming 1
Course Code: CS140
Course Dr Alabbad, Dr Alaql, Dr Alghamdi, Dr
Instructors: Almohimeed, Dr Sriti, Ms Alaloula, Ms Alaql, Ms
Albusayli, Ms Almutairi, Ms Alosaimi
Exam: Final Exam
Semester: Spring 2019
Date: 16/08/1440 - 21/04/2019
Duration: 2 hours
Marks: 40
Privileges: ☐ Open Book ☐ Open Notes
☐ Calculator Permitted ☐ Laptop Permitted

Student Name: MODEL ANSWER

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.

Official Use Only


Question Student Marks Question Marks
1 10
2 10
3 8
4 12
Total 40

Page 1 of 6

Imam University | CCIS | Doc. No. 006-02-20170316


Question 1: To be answered in (25) Minutes [ ] / 10 Marks

Consider the following numbers generated randomly by calling sr.nextInt(1000) several times,
where sr is a variable of type SecureRandom:

1 0 2 3 0 2 10 6 20 15 14 250 570 999 0 23 ...

a) What is the output of the following code? (8 marks)

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:

1 2 3 10 20 250 570 999 (8 marks: 1 mark for each number)

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

Imam University | CCIS | Doc. No. 006-02-20170316


Question 2: To be answered in (25) Minutes [ ] /10 Marks

The following Java program reads weather.txt file weather.txt averages.txt


containing daily temperatures, then computes their 03/04/2019 17.0 22.5 29.5 27.0 03/04/2019 24.00
averages and writes them in averages.txt for every 04/04/2019 24.0 25.5 32.5 27.5 04/04/2019 27.38
05/04/2019 22.5 24.5 30.5 29.0 05/04/2019 26.63
day. These are examples of the two files:
There are 5 errors inside the main method.
Find/fix the errors by filling the erroneous instruction line number and its correction in the table below.

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)

Line # Error Correction

7 new Scanner(Paths.get("weather.txt"));

8 new Formatter("averages.txt");

10 while(input.hasNext()) {

14 output.format("%s %.2f\n", day, sum / 4);

17 input.close(); output.close();

Page 3 of 6

Imam University | CCIS | Doc. No. 006-02-20170316


Question 3: To be answered in (25) Minutes [ ] / 8 Marks

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("avabvmbm<< ")); // will return true

charTwice("aabbvvmm<< ")); // will return false because the space found once

You are not asked to write a main method.

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

int firstIndex = s.indexOf(cur); // 1 mark


int lastIndex = s.lastIndexOf(cur); // 1 mark

if (firstIndex == lastIndex) // 1 mark


return false; // 1 mark
}
return true; // 1 mark
}

// second approach using nested loops


public static boolean charTwice2(String s) { // 1 mark
for (int i = 0; i < s.length(); i++) { // 1 mark
char cur = s.charAt(i); // 1 mark

int count = 0; // 0.5 mark


for (int j = 0; j < s.length(); j++)// 0.5 mark
if (cur == s.charAt(j)) // 0.5 mark
count++; // 0.5 mark

if (count < 2) // 1 mark


return false; // 1 mark
}
return true; // 1 mark
}

Page 4 of 6

Imam University | CCIS | Doc. No. 006-02-20170316


Question 4: To be answered in (45) Minutes [ ] / 12 Marks

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:

int[] a = {0, 2, 5, 2, 3, 4};

System.out.println(remove(a, 2));

System.out.println(Arrays.toString(a));

Will output:

3
[0, 0, 5, 0, 3, 4]

Answer (6 marks):

public static int remove(int[] a, int v) { // 1 mark


int count = 0; // 0.5 mark
for(int i = 0; i < a.length; i++) { // 1 mark
if (a[i] == v) // 1 mark
a[i] = 0; // 0.5 mark
if(a[i] == 0) // 1 mark
count++; // 0.5 mark
}
return count; // 0.5 mark
}

Page 5 of 6

Imam University | CCIS | Doc. No. 006-02-20170316


b) Write a Java method called centeredAverage that takes an array of integers and will return the
"centered" average of its values.
The centered average is the average of the values except the largest and smallest values in the array.
If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest
value. Hint: Use only one loop to compute the largest, smallest, and sum of the values at once.
Examples:
centeredAverage([1, 2, 3, 4, 100]) → 3

centeredAverage([1, 1, 5, 5, 10, 8, 7]) → 5

centeredAverage([-10, -4, -2, -4, -2, 0]) → -3

Answer (6 marks):

public int centeredAverage(int[] nums) { // 1 mark


int max = nums[0], min = nums[0], sum = nums[0]; // 0.75 mark
for(int i = 1; i < nums.length; i++) { // 0.25 mark
sum += nums[i]; // 0.50 mark
max = max < nums[i] ? nums[i] : max; // 0.75 mark
min = min > nums[i] ? nums[i] : min; // 0.75 mark
}

sum -= (min + max); // 1 mark

return sum / (nums.length-2); // 1 mark


}

Page 6 of 6

Imam University | CCIS | Doc. No. 006-02-20170316

You might also like