Anda di halaman 1dari 8

CS1101C

National University of Singapore


School of Computing
MID-SEMESTER TEST FOR Semester 2 AY2009/2010
CS1101C Programming Methodology in C
13 March 2010

Time Allowed: 60 Minutes

INSTRUCTIONS TO CANDIDATES

1. This test paper contains TWENTY (20) questions and comprises EIGHT (8) printed
pages, including this page.
2. Each question is worth one mark. The maximum possible mark is 20.
3. Answer ALL questions by shading the letter corresponding to the most appropriate
answer on the OCR form provided.
4. This is an OPEN BOOK test.
5. Do not look at the questions until you are told to do so.
6. There is no negative marking, so please attempt every question.
7. You may keep the question paper after the test is over.

CS1101C
For all the questions, assume that the relevant #include pre-processor statements have
been included in the program where necessary. Choose the most appropriate answer for
each question.
1. What is the output of the following code segment?
int main() {
int x = i = 0, z = 5;
for(; i < 5; i*=2)
x += i;
printf("%d\n", x);
}
A. 6
B. 7
C. An infinite loop occurs.
D. A compilation error occurs.
E. A run-time error occurs.

2. What is printed out by the following C code fragment?


int x = 9, y = 12;
if(x < y)
if(y > 5)
y++;
else
x++;
y++;
else
y += 2;
x += 2;
printf("%d %d\n", x, y);
A. 11 14
B. 13 14
C. 9 13
D. 9 14
E. A compilation error occurs.

CS1101C
3. What is printed by the following C code fragment? Assume the user always enters
five integers between 1 to 100 inclusive.
int i, n, x = 100;
for (i = 0; i < 5; i++) {
printf("Enter an integer (1-100): ");
scanf("%d", &n);
if(n < x)
x = n;
}
printf("%i\n", x);
A. Prints out the average of the five integers.
B. Prints out the median of the five integers.
C. Prints out the largest of the five integers.
D. Prints out the smallest of the five integers.
E. None of the above.

4. Suppose a and b are integer variables with values of 4 and 6 respectively. What is
the value of the following expression?
++(a + b);
A. 10
B. 11
C. 12
D. A compilation error occurs.
E. A run-time error occurs.

5. What is NOT the equivalent for the code given below, assuming all variables shown
are integer variables?
if(a < b)
if(b < c)
x = b;
else
x = c;
A. x = (a < b) ? (b < c) ? b : c : x;
B. x = (a < b && b < c) ? b : c;
C. x = (a < b && b < c) ? b : (a < b) ? c : x;
D. x = (a >= b) ? x : (b < c) ? b : c;
E. x = (a >= b) ? x : (a >= c) ? c : b;

CS1101C
6. What is printed out by the following C program fragment?
int i = 29, j = 1;
switch (i/15) {
case 0: j += 5;
case 1: j *= 4;
case 2: j /= 5;
default: j -= 2;
}
printf("%i\n", j);
A. -2
B. -1
C. 1
D. 4
E. A compilation error will occur.

7. What is printed out by the following C program fragment?


int a = -1, b = 0;
if(++a && ++b)
b++;
printf("%d\n", b);

A. 0
B. 1
C. 2
D. 3
E. A compilation error will occur.

8. What is printed out by the following C program fragment?


int i = 0;
for(; i < 5; ++i)
printf("%i ", i);
A. 0 1 2 3 4
B. 1 2 3 4
C. 0 1 2 3 4 5
D. 1 2 3 4 5
E. A compilation error will occur.

CS1101C
9. What is printed out by the following C code fragment?
int a = 0, b = 1, c = 0, d = 0;
if(a++ || a++)
if(b++ && c++)
d++;
else
d+=2;
else
d+=4;
printf("%d %d %d %d\n", a, b, c, d);

A. 0 1 0 4
B. 1 1 0 4
C. 1 1 0 0
D. 2 2 1 1
E. 2 2 1 2

10. In general, which of the following expressions is the correct way to test whether two
floating-point variables, x and y, are approximately equal in value? The constant
EPSILON is assumed to be defined as follows:
#define EPSILON 0.000001;
A. x == y;
B. x-y <= EPSILON
C. x <= y+EPSILON || x >= y-EPSILON
D. -EPSILON <= x-y && x-y <= EPSILON
E. -EPSILON <= x-y || x-y <= EPSILON

11. What is printed out by the following C code fragment?


int a = 3, b = 2, c = 5, t = 0;
int x = ( (t=(a<b)? a:b) < c )? t:c;
printf("%d\n", x);
A. 0
B. 2
C. 3
D. 5
E. A compilation error will occur.

CS1101C
12. Assuming that all variables that appear in the following have not been declared
before. Which of the following will generate a compile-time error?
A. double x = (int) 20.5;
B. int a = 1, b;
C. double y = 0; z = 1.0;
D. int C = 12 * 34 - 99;
E. None of the above.

13. Which of the following expressions generates a random odd integer in the range [7,
21] (inclusive of 7 and 21)? The rand float function has been defined below.
float rand_float()
{
((double)rand()/RAND_MAX);
}
A. (int)( rand float()*(21-7) ) + 7
B. (int)( rand float()*(10-3) ) * 2 + 7
C. (int)rand float()*(10-3) * 2 + 7
D. (int)( rand float()* 2 ) * (10-3+1) + 7
E. None of the above.

14. What is printed out by the following C code fragment?


int i, j, k = 0;
for (i = 10; i > 0; i /= 2) {
for (j = 0; j < i; j++)
k++;
}
printf("%d\n", k);
A. 0
B. 8
C. 18
D. An infinite loop occurs.
E. None of the above.

CS1101C
15. What is printed out by the following C code fragment?
double d = 777.77777;
printf("%5f\n", d);
A. 777.77
B. 777.78
C. 777.77777
D. 777.777770
E. None of the above.

16. What is printed out by the following C code fragment?


int i = 100;
do {
--i;
} while (i--);
printf("%i\n", i);
A. -2
B. -1
C. 0
D. 1
E. None of the above.

17. Which of the following macros are equivalent?


.
I #define MAX(a, b) (((a) > (b)) ? (a) : (b))
II #define MAX(a, b) a > b ? a : b
III #define MAX(a, b) (a > b ? a : b)

A. I and II
B. I and III
C. II and III
D. All of them are equivalent.
E. None of them are equivalent to one another.

CS1101C
18. What is printed by the following C code fragment?
int main()
{
int i = 0;
increase(i);
printf("%d", increase(i));
}
int increase(int i)
{
return i+=2;
}
A. 0
B. 2
C. 4
D. 6
E. None of the above.

19. What is printed by the following C code fragment?


int i, j, k = 0;
for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
k++;
printf("%d %d %d", i, j, k);
A. 9 9 20
B. 10 10 20
C. 9 9 100
D. 10 10 100
E. None of the above.

20. Which of the following statements is false?


A. A run-time error is caused by mistakes in program logic.
B. A run-time error will occur if srand is not called before rand.
C. We can always rewrite any for loop with a while loop by introducing
new variables.
D. A function does not always require a return statement.
E. We cannot use a reserved keyword as a function name.

END of PAPER
8

Anda mungkin juga menyukai