Вы находитесь на странице: 1из 7

SECTION A : Multiple-choice [10 Questions, 15 Marks (1.

5 mrk each)] Choose the best answer and write down in the answer booklet. Answer all the questions. 1. If a is an integer variable, a = 5/2; will return a value (a) (b) (c) (d) 2.5 3 2 0

2. Which of the following are invalid identifiers? i. R3D3 ii. per-capita iv. ice_cream v. 92_aardvarks (a) (b) (c) (d) i, ii, iv, v i, iv ii, iii,v ii, iv, v

iii. phone#

3. If the input to the program segment at the right is 85, what is its output? (a) A (b) B (c) C (d) D scanf("%d", &s); if (s >= 90) printf("A\n"); else if (s >= 70) printf("C\n"); else if (s >= 80) printf("B\n"); else printf("D\n");

4. What will be the value of i after the C statements at the right have been executed? (a) (b) (c) (d) 5 6 8 10 i = 3; j = 10; if ((3 * i) < j) i = i + 2; i = i + 3;

5. How many lines of output will be displayed by the following program fragment? i = 0; do { for (j = 0; j < 4; j = j + 1) printf("%d\n", i + j); i = i + 1; } while (i < 5); (a) (b) (c) (d) 0 7 9 20

6. Text enclosed in /* */ in a C program _________. (a) (b) (c) (d) gives instructions to the processor declares memory requirements is ignored by the C compiler causes a syntax error

7. What is displayed by the C statements that follow if the value input is 2? scanf("%d", &ctl); switch (ctl) { case 0: case 1: printf("red "); case 2: printf("blue "); case 3: printf("green "); case 4: printf("yellow"); } printf("\n"); (a) (b) (c) (d) blue green yellow blue green yellow

8. A special value that marks the end of a list of a loop is called a __________. (a) (b) (c) (d) terminal value sentinel value loop control value input value

9. Which of the following statements concerning the while statement is correct? (a) (b) (c) (d) It is used to define loops The loop body is executed if the associated condition is false When the condition becomes true, the control drops out of the loop All the above

10. A pseudocode can be used in (a) (b) (c) (d) Designing algorithms Implementing algorithms as programs Debugging logic errors in programs All of the above

SECTION B Short questions [50 Marks]

Answer all the questions. Question 1 (a) Write a single C statement corresponding to each of the following tasks: (i) Declare the variables weight and height as floating-point variables. (ii) Declare the variable counter as an integer variable with an initial value of 0. (iii) Compute the value of new_weight as the difference of the value of weight and the value of weight_loss. (iv) Add 1 to the value of counter and store the result in counter. (v) Print the following message using ONLY 1 printf() statement. Principles of C Programming [5marks] (b) Identify whether the following identifiers are valid or not. (i) Mark9 _________ _________ _________ _________ _________ [5 marks] Question 2 Identify and correct the error(s) in the program given below: #include <stdio.h> int main() { double number; printf("Enter a number :") scanf("%lf",number); inverse =1.0/number; print("Inverse of %d is %f",number,inverse);

(ii) TesT (iii) Res#ult (iv) __Average (v) 5Ptr

[6marks]

Question 3 Rewrite the following if statements as an equivalent switch statement. The variable digit is of type int. if (digit == 0) value = 3; else if (digit = = 1) value = 3; else if (digit = = 2) value = 6; else if (digit = = 3) value = 9; [5marks] Question 4 Rewrite the following code segment as an equivalent segment that uses a for statement. product = 1; next = 1; while (next <= m) { product = product * next; next = next + 1; } [5 marks] Question 5. Trace and write the output for the program given below. #include <stdio.h> int main() { int x=1,total=0,y; while (x<=10){ y=x * x; printf("%d\n",y); total += y; ++x; } printf("Total is %d\n",total); return 0; } [6 marks]

Question 6 Using nested loop, produce a code fragment to produce a diagram below:

* *** ***** ******* [6 marks] Question 7 Write the following statements in C: (i) If (x + y) is not equal to 100 and b is greater than or equal to 200 (ii) If (a + b) is less than or equal to 1200 or (a b) * 4 is equal to 600 [6 marks] Question 8 Based on variable declarations and program fragment below, trace and write the output . int a =3, b=5, c= 4, d=1; printf(%d %d \n, --a, ++b); a = ++c; b += a++; c *= --b; d= c + d++; printf(%d %d %d %d \n, a, b, c,d); [6 marks]

Section C: Design and writing program.[35marks] Answer all the questions Question 1 (a) Write a pseudocode that reads a positive integer n as input and computes and outputs the product of all integers from 1 to n, including n. This product is called the factorial of n. [10 marks] (b) Based on the question 1(a), draw a flowchart for the problem stated. [10 marks] Question 2 Write a program that reads the number of letter grades A, B, C, D and F for a student; computes and prints the students grade point average; and determines and prints the students academic standing, which can be honors, satisfactory and probation according to the following table: Grade point average 3.51 4.00 3.00 3.50 2.00 2.99 Less than 2.00 Academic Standing High Honors Honors Satisfactory Probation

In computing grade point average, assume that the weight of letter grade A is 4, B is 3, C is 2, D is 1 and F is 0. [15 marks]

Вам также может понравиться