You are on page 1of 11

CS1010E

National University of Singapore


SCHOOL OF COMPUTING
MID SEMESTER TEST
Semester 1 AY2014/2015
CS1010E PROGRAMMING METHODOLOGY
4 October 2014

Time Allowed: 60 minutes

INSTRUCTIONS TO CANDIDATES
1. This test paper consists of TEN (10) questions and comprises ELEVEN (11)
printed pages, including this page.
2. This is an OPEN BOOK test. The maximum mark is 20.
3. Answer all questions by shading the letter corresponding to the most appropriate
answer on the OCR form provided. 2 marks for each correct answer and no penalty
for wrong answer.
4. Calculators are allowed, but not electronic dictionaries, notebooks, tablets, or other
computing devices.
5. Do not look at the questions until you are told to do so.
6. Please write your student card number as well as shade your student card number
clearly on the OCR form.

CS1010E

10 Multiple Choice Questions: 20 Marks


Each question has one correct answer. Shade the letter corresponding to the correct
answer on the OCR form provided.
1. Study the following truth table.
A
true
true
true
true
false
false
false
false

B
true
true
false
false
true
true
false
false

C
true
false
true
false
true
false
true
false

Q
true
false
true
true
true
false
true
true

Which of the following isQ function is a correct implementation for the above truth
table?
A. bool isQ(bool A, bool B, bool C)
{
return B && !C;
}
B. bool isQ(bool A, bool B, bool C)
{
return !B || C;
}
C. bool isQ(bool A, bool B, bool C)
{
return A && (!B || C);
}
D. bool isQ(bool A, bool B, bool C)
{
return A || (B && !C);
}
E. None of the above.

CS1010E

2. Study the following three functions. What type of error does each function exhibit?
i. int f(int n)
{
int i, s = 0;
for (i = 1; i <= n; i++)
s = s * i;
return s;
}
ii. void g(int *x)
{
x = 0;
*x = 1;
return;
}
iii. int h(int n)
{
int r;
if (n == 0)
{
r = 1;
}
else
{
if (n > 0)
{
r = 2;
}
}
else
{
r = 3;
}
return r;
}
A. i. logical error; ii. runtime error; iii. syntax error
B. i. logical error; ii. syntax error; iii. runtime error
C. i. runtime error; ii. logical error; iii. syntax error
D. i. runtime error; ii. syntax error; iii. logical error
E. i. syntax error; ii. logical error; iii. runtime error

CS1010E

3. The game of Pick Up Sticks is played between two players with an initial pile of
n sticks. Each player takes turns in picking out 1 to k (< n) sticks from the pile.
Whoever takes the last stick loses the game.
Suppose k = 2 and it is your turn. If there are 2 sticks in the pile, you will take 1 stick
and guarantee a win (one stick left for the opponent). Likewise, if there are 3 sticks
in the pile, you will take 2 sticks and guarantee a win. However, if you need to pick
from a pile of 4 sticks with k = 2, then you could lose because one of two situations
may occur:
you take 1 stick, opponent takes 2 sticks, and you are left with one (lose);
you take 2 sticks, opponent takes 1 stick, and you are left with one (lose).
So for k = 2, n = 4 is a losing state. Likewise, for k = 2, n = 7 is also a losing
state since
you take 1 stick, opponent takes 2 sticks, and you are left with 4 sticks;
you take 2 sticks, opponent takes 1 stick, and you are left with 4 sticks.
It has been shown earlier that n = 4 is a losing state.
Which of the following boolean expression (condition) determines a losing state (i.e.
evaluates to true) for any values of n and k with n > k?
A. n-k == 2
B. n%k == 0
C. n-(n-k) == k
D. n%(k+1) == 1
E. None of the above.

CS1010E

4. Study the following program.


#include <stdio.h>
int main(void)
{
int count = 0, i, n;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (i%4 == 0)
{
count = count + 1;
if (i%100 == 0)
{
count = count - 1;
if (i%400 == 0)
{
count = count + 1;
}
}
}
}
printf("%d\n", count);
return 0;
}
What is the output of the program when the user input is 2000?
A. 480
B. 485
C. 490
D. 500
E. None of the above.

CS1010E

5. Study the following program.


#include <stdio.h>
int main(void)
{
int n, k, i, j, count = 0;
scanf("%d", &n);
scanf("%d", &k);
for (i = 1; i <=
for (j = 1; j
if (i+k <=
count =

n; i++)
<= n; j++)
j)
count + (i*j);

printf("%d\n", count);
return 0;
}
What is the output of the program when the user input is 5 followed by 1?
A. 45
B. 85
C. 140
D. 180
E. None of the above.

CS1010E

6. The program below computes the elapsed time (in seconds) between t1 and t2 . Assume
that time t1 is earlier than time t2 , and both time values are within the same day.
#include <stdio.h>
int readTime(int t);
int timeElapsed(int t1, int t2);
int readTime(int t)
{
int h, m, s;
printf("Enter time t_%d (hh mm ss): ", t);
scanf("%d%d%d", &h, &m, &s);
return 3600*h + 60*m + s;
}
int timeElapsed(int t1, int t2)
{
return t2 - t1;
}
Which of the following is the most reliable implementation for the main function?
A. int main(void)
{
int t1, t2, elapsed;
t1 = readTime(1); t2 = readTime(2);
elapsed = timeElapsed(t1,t2);
printf("Time elapsed: %d\n", elapsed);
return 0;
}
B. int main(void)
{
int elapsed;
elapsed = timeElapsed(readTime(1),readTime(2));
printf("Time elapsed: %d\n", elapsed);
return 0;
}
C. int main(void)
{
printf("Time elapsed: %d\n", timeElapsed(readTime(1),readTime(2)));
return 0;
}
D. All the above are reliable.
E. None of the above are reliable.
7

CS1010E

7. What is the output of the following program?


#include <stdio.h>
void swap1(int *a, int *b);
void swap2(int *x, int *y);
void print(int q, int r);
int main(void)
{
int a = 2, b = 3;
swap1(&a,&b);
print(a,b);
return 0;
}
void swap1(int *a, int *b)
{
int x, y;
x = *a;
y = *b;
swap2(&x, &y);
print(x,y);
return;
}
void swap2(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
void print(int q, int r)
{
printf("%d %d\n", q, r);
return;
}
A. 2 3
2 3

B. 2 3
3 2

C. 3 2
2 3

D. 3 2
3 2

E. None of the above.

CS1010E

8. Which of the following function is not equivalent to the rest (i.e. returns a different
value for the same parameter values n and k)? Assume that n > 0 and k > 0.
A. int foo(int n, int k)
{
int t = 0;
while (n >= k)
{
n = n - k;
n = n + 1;
t = t + 1;
}
return t;
}
B. int foo(int n,
{
int t = 0;
while (n >=
{
if (n >=
{
n = n
n = n
t = t
}
}
return t;
}

int k)

C. int foo(int n, int k)


{
int t = 0;
while (n >= k)
{
n = n - k;
if (n >= 0)
{
n = n + 1;
t = t + 1;
}
}
return t;
}

k)
k)
- k;
+ 1;
+ 1;

D. int foo(int n, int k)


{
int t = 0;
while (n >= k)
{
t = t + n/k;
n = n%k + n/k;
}
return t;
}

E. All the functions are equivalent.

CS1010E

9. The least common multiple of two numbers a and b, abbreviated lcm(a, b), is defined as
the smallest positive integer that is a multiple of both a and b. For example, lcm(4, 5)
is 20 and lcm(10, 12) is 60. Which of the following is a correct implementation of the
lcm function?
i. int lcm(int a, int b)
{
int r = a;
while (r%b != 0)
r = r + a;
return r;
}
ii. int lcm(int a, int b)
{
int r = a;
while (r%a != 0 || r%b != 0)
r = r + a;
return r;
}
iii. int lcm(int a, int b)
{
int r;
if (a < b)
{
r = a;
while (r%a
r = r +
}
else
{
r = b;
while (r%a
r = r +
}
return r;

!= 0 || r%b != 0)
a;

!= 0 || r%b != 0)
b;

}
A. i only.
B. iii only.
C. i and ii only.
D. i and iii only.
E. All i, ii and iii.

10

CS1010E

10. The following program can be used to accurately add up the cost of grocery items by
representing dollars and cents as separate integer values.
#include <stdio.h>
int main(void)
{
int d,c,dt=0,ct=0;
printf("$");
while(scanf("%d.%d",&d,&c) != EOF) /* reads two integers separated by . */
{
/* Missing code here. */
printf("$");
}
printf("\nTotal $%d.%d\n", dt,ct);
return 0;
}
A sample run of the program is as follows. User input is underlined. User input is
terminated using the EOF marker.
$1.23
$9.99
$^D
Total $11.22
Which of the following three sets of statements are correct?
i. ct = ((d+dt)*100) + (c+ct);
dt = ct/100;
ct = ct%100;
ii. dt = (d + dt) + ((c + ct)/100);
ct = (c + ct)%100;
iii. ct = (d + dt) + ((c + ct)%100);
dt = ct/100;
A. i only.
B. ii only.
C. i and ii only.
D. i and iii only.
E. All i, ii and iii.

END OF PAPER

11

You might also like