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.
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.
A. 0
B. 1
C. 2
D. 3
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
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.
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.
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.
END of PAPER
8