0% found this document useful (0 votes)
51 views17 pages

CS1010 Midterm-2425-S1-With-Comments

NUS CS1010 Computer Science Programming Methodology Midterm Paper
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)
51 views17 pages

CS1010 Midterm-2425-S1-With-Comments

NUS CS1010 Computer Science Programming Methodology Midterm Paper
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/ 17

Midterm Assessment CS1010 AY24/25 Sem 1

NATIONAL UNIVERSITY OF SINGAPORE

SCHOOL OF COMPUTING
MIDTERM ASSESSMENT FOR
Semester 1 AY2024/2025

CS1010 Programming Methodology

October 2024 Time Allowed 80 Minutes

INSTRUCTIONS TO CANDIDATES
1. This assessment paper contains 12 questions and comprises 8 printed pages, including this page.

2. Write all your answers in the answer sheet provided.

3. Please write and shade your student number in the corresponding box in the answer sheet.

4. The total marks for this assessment is 60. Answer ALL questions.

5. This is an OPEN BOOK assessment.

6. You can assume that in all the code given: (i) no overflow nor underflow will occur during ex-
ecution; (ii) all given inputs are valid and fits within the specified variable type; (iii) compile
without syntax errors; and (iv) all the required header files are already included.
Midterm Assessment CS1010 AY24/25 Sem 1

Part I
Multiple Choice Questions (24 points)
• For each of the questions below, shade your answer in the corresponding answer bubble on
the answer sheet. Each question is worth 3 points.

• If multiple answers are equally appropriate for a question, pick one and shade the chosen answer
on the answer sheet. Do NOT shade more than one bubble per question.

• If none of the answers is appropriate for a question, shade X on the answer sheet for that ques-
tion.

1. (3 points) Which of the following expressions evaluates to true ?

(i) 5 / 2 == 2.5
(ii) 5 / 2 == 2 1/2
(iii) 5 / 2 == (long) 2.5
(iv) fabs((5 / 2) - 2.5) < 0.000001
A. (i) only
B. (i) and (ii) only
C. (iii) only
D. (iii) and (iv) only
E. (iv) only
Shade X on the answer sheet if none of the choices above is correct.

Solution: C. (iii) only.


Since 5 and 2 are integers, the result of the division is an integer, which is 2. Since casting 2.5
to a long results in 2, the expression evaluates to true.
About 60% of students got this correct, with Option (iv) being deemed correct by about 25%
of students. Note that 5 / 2 would evaluate to 2, and fabs(2 - 2.5) < 0.000001 would
evaluate to false.

2. (3 points) Consider the following functions:


long foo(long n) {
return ((n / 10) % 10) + ((n % 10) * 10);
}

long bar(long n) {
if (n < 100) {
return foo(n);
}
return bar(n / 100) * 100 + foo(n);
}

What does bar(123456) return?

Page 2
Midterm Assessment CS1010 AY24/25 Sem 1

A. 123456
B. 132546
C. 214365
D. 563412
E. 654321
Shade X on the answer sheet if none of the choices above is correct.

Solution: C. 214365.
The function foo extracts the last two digits and swaps their position. For example, foo(123)
returns 32.
The function bar recursively extracts the last two digits and swaps their position, and then
concatenates the result. So, bar(123456) returns 214365.
84% of students got this correct. Well done!

Page 3
Midterm Assessment CS1010 AY24/25 Sem 1

3. (3 points) Consider the following four functions. Among guk , ham and ice , which functions
are equivalent to fin ? Two functions are equivalent if they return the same value when given
the same inputs, for all possible inputs.
bool fin(bool a, bool b) {
if (a) {
if (!b) {
return false;
}
}
return true;
}

bool guk(bool a, bool b) {


if (!a) {
return true;
}
if (b) {
return true;
}
return false;
}

bool ham(bool a, bool b) {


if (!a) {
return true;
}
if (!b) {
return false;
}
return true;
}

bool ice(bool a, bool b) {


if (!b) {
return !a;
}
return true;
}
A. guk only
B. ham only
C. ice only
D. ham and ice only
E. guk , ham , and ice
Shade X on the answer sheet if none of the choices above is correct.

Solution: The function fin returns false only if a is true and b is false . It returns
true otherwise. In other words, it returns !a || b .

Now consider the other three functions and what conditions would make them return false .
All three functions return false only if a is true and b is false .
So all three guk , ham , and ice are equivalent to fin . The answer is E.

Page 4
Midterm Assessment CS1010 AY24/25 Sem 1

You can also verify this by considering the truth table for the four functions. Fill in the table
below as you trace through the code.
a b fin guk ham ice
false false true
false true true
true false false
true true true
60% of students got this correct. With the most chosen wrong answer being A and X .

Page 5
Midterm Assessment CS1010 AY24/25 Sem 1

4. (3 points) Consider a function that determines the time to set an alarm based on three factors:
whether it is a weekday, whether the user is on vacation, and whether the user is on call. The
alarm rules are as follows: (i) If the user is on vacation, then there is no need to set an alarm
regardless of the day and whether the user is on call; (ii) If it is a weekday, the user is not on
vacation and is on call, the alarm should be set for 7:00 AM; (iii) If it is a weekday, the user is not
on vacation, but is not on call, there is no need to set an alarm; (iv) If it is a not a weekday and
the user is not on vacation, the alarm should be set for 10:00 AM, regardless of whether he is on
call or not.
We have three bool variables, is_weekday (true for weekday, false otherwise), is_on_vacation
(true if on vacation, false otherwise), and is_on_call (true if on call, false otherwise). Which of
the following functions correctly return the time to set the alarm? The function returns 0, 700,
and 1000, corresponding to the cases of no alarm, 7:00 AM alarm, and 10:00 AM alarm, respectively.
long ang(bool is_weekday, bool is_on_vacation, bool is_on_call) {
if (is_on_vacation || (is_weekday && !is_on_call)) {
return 0;
}
if (is_weekday && is_on_call) {
return 700;
}
return 1000;
}

long bee(bool is_weekday, bool is_on_vacation, bool is_on_call) {


if (is_on_vacation) {
return 0;
}
if (is_weekday && is_on_call) {
return 700;
}
if (is_on_call) {
return 1000;
}
return 0;
}

long choo(bool is_weekday, bool is_on_vacation, bool is_on_call) {


if (!is_weekday && is_on_call) {
return 1000;
}
if (is_on_vacation) {
return 0;
}
return 700;
}

A. ang only
B. bee only
C. choo only
D. ang and bee only
E. bee and choo only
Shade X on the answer sheet if none of the choices above is correct.

Page 6
Midterm Assessment CS1010 AY24/25 Sem 1

Solution: A. ang only.


The requirement can be translated to the table below:

is_on_vacation is_weekday is_on_call alarm


1 true true true 0
2 true true false 0
3 true false true 0
4 true false false 0
5 false true true 700
6 false true false 0
7 false false true 1000
8 false false false 1000

For ang , the condition is_on_vacation || (is_weekday && !is_on_call)) handles Con-
ditions 1, 2, 3, 4, and 6 correctly. The next condition is_weekday && is_on_call handles
Condition 5. So the last return statement corresponds to Condition 7 and 8. The function
ang is correct.

For bee , the condition is_on_vacation handles Conditions 1, 2, 3, and 4 correctly. The
next condition is_weekday && is_on_call handles Condition 5 correctly. Next, the condi-
tion is_on_call corresponds to Condition 7, and correctly returns 1000. The last return
statement thus corresponds to Conditions 6 and 8. But Condition 8 should return 1000 instead
of 0. So bee is not correct.
For choo , the condition !is_weekday && is_on_call corresponds to Conditions 3 and 7.
Thus, it incorrectly returns 1000 for Condition 3. The function choo is not correct.
227 (79%) students got this correct. Well done!

Page 7
Midterm Assessment CS1010 AY24/25 Sem 1

5. (3 points) Consider the code snippet below. Which comparison always evaluates to true ?
void classify(double bmi) {
if (bmi >= 25 && bmi < 30) {
cs1010_println_string("Overweight");
return;
}
if (bmi < 25 && bmi >= 18.5) { // Line K
cs1010_println_string("Normal");
} else if (bmi < 18.5) { // Line L
cs1010_println_string("Underweight");
} else if (bmi >= 30) { // Line M
cs1010_println_string("Obese");
}
}

(i) bmi < 25 on Line K


(ii) bmi < 18.5 on Line L
(iii) bmi >= 30 on Line M
A. (i) only
B. (ii) only
C. (i) and (iii) only
D. (ii) and (iii) only
E. (i), (ii), and (iii)
Shade X on the answer sheet if none of the choices above is correct.

Solution: X.
If the execution reaches Line K, we know that bmi < 25 or bmi >= 30 . So we cannot be
sure that bmi < 25 is true at Line K.
If the execution reaches Line L, we can further narrow down the range of bmi to bmi < 18.5
or bmi >= 30 . We cannot be sure that bmi < 18.5 is true at Line L.
If the execution reaches Line M, we can further narrow down the range of bmi to bmi >= 30 .
So bmi >= 30 is always true at Line M and this comparison is redundant.
The answer is (iii) only.
Only 55% of students got this correct. 38% of students think that (ii) always evaluates to true.
(ii) is always evaluated to true if Line K compares only bmi >= 18.5 .

6. (3 points) Consider the code snippet below:


if (a > 2 * b) {
long temp = a;
a = b;
b = temp;
}
// Line T

Page 8
Midterm Assessment CS1010 AY24/25 Sem 1

What can we assert at Line T?


A. b > a
B. a > b
C. 2b > a
D. 2a > b
E. a > 2b
Shade X on the answer sheet if none of the choices above is correct.

Solution: X.
Consider two cases, either we enter the if block, or we do not.
If we do not enter the if block, then a ≤ 2b, or b ≥ a/2.
If we enter the if block, then a > 2b at the beginning of the true block, and after swapping
a and b, we have b > 2a.
So we have either b ≥ a/2 or b > 2a at Line T.
Can we derive another expression that must be true at Line T? If b > 2a, then we know that
b > a/2. So we can assert b ≥ a/2, or 2b ≥ a, at Line T.
But this does not correspond to any of the choices above. So the answer is X.
A quick check with a = 0 and b = 0 also reveals that we cannot assert any of the choices
given.
Only 42% of students got this correct. With A (22%) and C (29%) being the most chosen wrong
answers. C is very close but it does not account for the case where 2b == a .

Page 9
Midterm Assessment CS1010 AY24/25 Sem 1

7. (3 points) Consider the function below:


long dim(long n) {
long accum = 1;

for (long i = 1; i < n; i *= 2) {


accum *= i;
}

return accum;
}

Which of the following contains only integers that are among the possible return values of dim ?
A. 2, 4, 8
B. 2, 6, 24
C. 2, 8, 64
D. 3, 7, 15
E. 4, 9, 16
Shade X on the answer sheet if none of the choices above is correct.

Solution: C.
i in this case iterates through 1, 2, 4, 8, . . .. The accum is the product of the numbers from
1 to i . So the possible return values of dim are 1, 2, 8, 64, . . ..
88% of students got this correct. Well done!

8. (3 points) Consider the code snippet below:


void elf(double n) {
if (n <= 0) {
return;
}
double x = 0;
while (x < n) {
x += 0.5;
}
// Line S
}

Which of the following can be asserted at Line S?

(i) x < n
(ii) x > n
(iii) x < n + 0.5
(iv) n < x + 0.5
A. (i) and (iii) only
B. (i) and (iv) only

Page 10
Midterm Assessment CS1010 AY24/25 Sem 1

C. (ii) and (iii) only


D. (ii) and (iv) only
E. (iii) and (iv) only
Shade X on the answer sheet if none of the choices above is correct.

Solution: When we reach the loop, we can assert that n > 0. So, we always enter the loop.

while (x < n) {
// Line A
x += 0.5;
// Line B
}

At Line A, we can assert x < n. At Line B, we increment x by 0.5. So we can assert x − 0.5 < n,
or x < n + 0.5. This holds after we exit the loop.
When we exit the loop, we can assert x ≥ n.
So at Line S, we can assert n ≤ x < n + 0.5. Since n ≤ x imples that n − 0.5 < x, we have
the assertions n − 0.5 < x < n + 0.5. So both (iii) and (iv) are true. So the answer is E.
42% of students got this correct. With C, D, and X being the most commonly chosen wrong
answer.

Page 11
Midterm Assessment CS1010 AY24/25 Sem 1

Part II
Short Questions (36 points)
Answer all questions in the space provided on the answer sheet. Be succinct and write neatly.

9. (6 points) Consider the code below.


long n = 5040;
long i = 0;
do {
cs1010_print_long(n);
cs1010_print_string(" ");
/* Statement P (updates n) */
/* Statement Q (updates i) */
} while ( /* Condition R */ );
cs1010_println_string("");

On the answer sheet, fill in the missing statements and condition so that it prints the following
exactly:

5040 5040 2520 840 210 42 7 1

Note that 7 × 6 = 42, 42 × 5 = 210, 210 × 4 = 840, 840 × 3 = 2520, and 2520 × 2 = 5040.

Solution:

long n = 5040;
long i = 0;
do {
cs1010_print_long(n);
cs1010_print_string(" ");
n /= (i + 1);
i += 1;
} while ( n > 0 );
cs1010_println_string("");

Statement P (2 pt)

• Correct (2 pt): n /= (i+1); , n = n/(i+1); , etc.

• Incorrect (0 pt): n /= i , etc.

Statement Q (2 pt)

• Correct (2 pt): i += 1; , i = i + 1;

• Incorrect (0 pt): otherwise

Condition R (2 pt)

• Correct (2pt): n > 0 , n >= 1 , n != 0 , i <= 7 , i < 8 , n/i > 0 , and other tech-
nically correct conditions

Page 12
Midterm Assessment CS1010 AY24/25 Sem 1

• Partial (1pt); n > 1 , n >= i

• Incorrect (0 pt): otherwise

Terminating the when is n is 1, causing only part of the list to be printed, is a common error.
72% of students received 5 marks or above. Thumbs up!

10. (8 points) Mr. Nodle had n coffee pods numbered from 0 to n − 1. He chose one coffee pod to
drink each day, using the following algorithm: On Day i, he skipped i pods, and drank the next
one in the sequence. Every time he reached the end of the sequence, he wrapped around to the
beginning of the sequence. Each pod can be drank only once. He started his algorithm from Pod 0
on Day 1.
For example, if Mr. Nodle had 5 coffee pods, then:
• On Day 1, he skipped one pod (Pod 0) and drank Pod 1.
• On Day 2, he skipped two pods (Pods 2 and 3) and drank Pod 4. He had reached the end of the
sequence.
• On Day 3, he wrapped around and skipped 3 pods (Pods 0, 2, and 3). He reached the end of
the sequence again, so he wrapped around and drank Pod 0.
• On Day 4, he skipped 4 pods (Pods 2, 3, wrapped around, 2, 3, wrapped around) and drank
Pod 2.
• Finally, on Day 5, only Pod 3 remained. He drank Pod 3.
We are interested in finding out, given the number of pods n on Day 1, which pod did Mr. Nodle
drink on Day n? Solve this recursively by completing the function pod below. The function takes
in two non-negative integers n and i as parameters, where n is the number of coffee pods that
Mr. Nodle had on Day i . When i is 1, the function returns the pod number that Mr. Nodle drank
on Day n . For example, consider the case where n is 5 at Day 1. Calling pod(5, 1) returns 3.
long pod(long n, long i) {
if (<base case>) {
return <base case value>;
}
return <recursive case>;
}

Solution: The simplest case is when there is only one pod (n is 1). In this case, Mr. Nodle
drank Pod 0.
To derive the recursive case, consider what happened after Day 1. Taking n = 5 as an example,
on Day 2, he has four pods left, and his algorithm starts from Pod 2 and considers the sequence
from Pod 2, 3, 4, 0. If we shift the numbering by 2 (modulo 5), we can renumber the Pods as 0,
1, 2, 3. So we have a simpler version of the same problem (with one less pod!) to solve. The
only difference for this simpler problem is that we start with skipping 2 pods.
To generalize this observation, we can recursively solve pod(n, i) by solving pod(n − 1, i + 1),
and then shifting the numbering of the returned pod by i + 1 (modulo n).

Page 13
Midterm Assessment CS1010 AY24/25 Sem 1

#include <stdio.h>

long pod(int n, int i) {


if (n == 1) {
return 0;
}
return (pod(n - 1, i + 1) + (i + 1)) % n;
}

We give 2 marks if the base case condition and return value is correct. The only correct answer
if to return 0 (the only pod) and n is 1 (Mr. Nodle has one pod).
Other common (wrong) answers are: checking if n is 0 (then there is no pod to return), check-
ing if i is 0 or 1 (On Day 0 or 1, there might be more than one pods left), checking if n == i
(we would have about half of the coffee pods left).
We give 6 marks if the recursive case is correct. 2 marks is given if students are able to identify
the correct recursion pod(n - 1, i + 1) . The other 4 marks are given if students are able
to construct the solution for pod(n, i) from pod(n - 1, i + 1) – by shifting the num-
bering by i + 1 (2 marks) and then (modulo n ) (2 marks). If students realized that they
need to shift the pod numbering by shift it by i or i - 1 , we give 2 marks. If students are
able to shift the numbering by i or i - 1 , we give 1 partial marks.
This is a challenging question, with 16 students scoring 4 marks or above. Well done to them!

Page 14
Midterm Assessment CS1010 AY24/25 Sem 1

(n ) n!
11. (12 points) The number of ways we can choose k items out of n items is = k!(n−k)! . The function
( ) k
below computes nk , given two positive integers n and k, n ≥ k.
long choose(long n, long k) {
if (k == 0 || n == k) {
return 1;
}
long a = 1;
long i = 1;
// Line U
while (i <= k) {
// Line V
a *= n - (k - i);
// Line W
a /= i;
// Line X
i += 1;
// Line Y
}
// Line Z
return a;
}

In this question, your task is to show that the while loop in the function has the invariant

(n − k + i − 1)!
a ==
(i − 1)!(n − k)!
(n)
and therefore correctly computes k .
(n−k+i−1)!
(a) (2 points) Argue that the assertion a == (i−1)!(n−k)! holds at Line U.
(n−k+i−1)!
(b) (2 points) Argue that the assertion a == (i−1)!(n−k)! holds at Line Y after the first iteration.
(n−k+i−1)!
(c) (6 points) Now, assuming that the assertion a == (i−1)!(n−k)! holds at Line V. By deriving the
relationships between a, n, k, and i at Line W and X, show that the assertion holds at Line Y.
n!
(d) (2 points) Finally, show that the assertion a == k!(n−k)! holds at Line Z.

Solution:
(n−k+i−1)! (n−k)!
(a) When i is 1, the expression (i−1)!(n−k)! is 0!(n−k)! , which is 1. This is the value of a at Line
U.
(n−k+1)!
(b) After the first iteration, i is now 2, a is n − k + 1, this is the same as 1!(n−k)! . The
statement holds.
(n−k+i)!
(c) At Line W, since a is multiplied by n − k + i, we can assert a to be (i−1)!(n−k)! . At Line X,
(n−k+i)!
since a is divided by i, we can assert a to be i!(n−k)! . At Line Y, since i increases by 1, we
(n−k+i−1)!
can assert a to be (i−1)!(n−k)! again.
(n−k+k)! n!
(d) When the loop exits, we can assert i is k + 1, so a is k!(n−k)! , which is k!(n−k)! .

Page 15
Midterm Assessment CS1010 AY24/25 Sem 1

2 marks each for Part (a), (b), and (d), if students are able to demonstrate an understanding of
the code and the assertion.
For Part (c), we give 6 marks for a complete answer that clearly explains the behavior of asser-
tions at Line W and Line X and provides a valid final proof at Line Y with correct expressions.
For partial credits, we give up to 2 marks for each of Line W, X, and Y, based on the depth and
accuracy of the explanation provided.
36% scored more than 10 marks for this question.

12. (10 points) The reverse of a list of integers L = ⟨l0 , l1 , . . . lk−1 ⟩ is ⟨lk−1 , . . . l1 , l0 ⟩. Given a list
L with k ≥ 1 elements, draw the flowchart for an algorithm that checks if L is the same as its
reverse, i.e., lk−1 is the same as l0 , lk−2 is the same as l1 , and so on. The algorithm should output
true if L is the same as its reverse, and false otherwise.
Do not use recursion for this question. To obtain full marks for this question, you should not make
any unnecessary comparison between the elements of L.

Solution: Have two variables i and j, i starts from 0, j starts from k − 1. In a loop, compare
li and lj . If different, exits. If the same, continue until i > j.
Note that to avoid unnecessary comparisons, the loop should exit as soon as a negative com-
parison is found. Furthermore, the loop should exit as soon as i ≥ j (i.e., i and j ”cross each
other”).

input k and
⟨l0, . . . lk−1⟩

set i to 0 i=i+1
i<j? li == lj ?
set j to k - 1 YES YES
j=j-1
NO NO

output true output false

Marking scheme:

• 1 marks – correctly indicate the input of the algorithm.

• 5 marks - correctly set the loop:

– 1 mark - correctly initializing loop index.


– 2 mark - correctly advancing the loop index.
– 1 mark - correctly terminating the loop.
– 1 mark - correctly labeling the yes and no branch of the termination condition of
loop.

Page 16
Midterm Assessment CS1010 AY24/25 Sem 1

• 4 marks - correct logic:

– Fully correct: 4 marks.


– Partially correct with at most 2 logical mistake: 2 marks.
– More than 2 logical mistakes: 0 marks.

• 1 mark is deducted for unnecessary comparisons (not terminating as soon as a negative


comparison is found or comparing the element with itself.)

• At most 2 marks are deducted for missing shapes that distinguish the different compo-
nents of a flowchart

A common mistake is having terminating condition similar to i == j . This will cause infinite
loop if k is even. Another common mistake is to redundantly compare the middle element
with itself when k is odd.
65% receive 8 marks or above for this question.

END OF PAPER

Page 17

You might also like