CT1_22_spring
CT1_22_spring
Class Test 1
30–March–2023 06:30pm–07:30pm Maximum marks: 30
1. (a) Which of the following are valid variable names? Fill in the blanks with only Yes/No answers. No explanations
are needed. (3)
(b) You want to declare and initialize the following variables: an integer variable z1 initialized to 0, another integer
variable z2 left uninitialized, a character variable mychar initialized to the new-line character, and a constant double-
precision floating-point variable me initialized to 9.11 × 10−31 (the mass of an electron in kg). Write the variable
declarations in the box provided below. All initializations mentioned above must be during the declaration. (3)
int z1 = 0, z2;
char mychar = ’\n’;
const double me = 9.11e-31;
(c) Let a1, a2, a3, and a4 be four int variables storing the values 1, 2, 3, 4, respectively. What are the values of
the following four expressions for these values of the variables? Fill in the blank beside each expression. Write only
the final answers. No need to show your calculations. (2)
a1 + a4 / a3 * a2 3
a1 + a4 * a3 % a2 1
a1 + a4 % a3 * a2 3
a1 + a4 % a3 % a2 2
(d) Let a, b, c, and d be int variables storing the values 10, 20, 30, 40, respectively. At this point, you execute the
following two statements. What will be the values of the four variables immediately after these statements? Write
only the final answers. No need to show your calculations. (2)
b = a++;
d += (c -= b);
Fill in the following blanks with the correct answers.
a = 11 b = 10 c = 20 d = 60
— Page 1 of 4 —
2. (a) Suppose that marks is a variable of data type int. The following code snippet is supposed to print A grade
if marks is at least 80 and at most 89; otherwise it should print Not A grade. Fill in the blanks below so that the
program behaves correctly. (2)
(d) Consider the following two code snippets. Write the respective outputs of the snippets in the spaces provided. (2)
What will be printed by this snippet? What will be printed by this snippet?
x = 10 x = 100
(e) Suppose that the integer variable j holds the value 5. Then, the following code snippet is executed.
switch (j + 2) {
case 9: printf("A"); break;
case 8: printf("B");
case 7: printf("C");
case 6: printf("D");
case 5: printf("E");
case 4: printf("F");
case 3: printf("G");
case 2: printf("H");
case 1: printf("I"); break;
case 0: printf("JKLM");
default: printf("NOPQ");
}
— Page 2 of 4 —
3. (a) Suppose that you want to compute the sum
1 3 5 7 2n − 1
s(n) = + + + +···+
1 2 3 4 n
as a floating-point (double) value. The user supplies n as an integer. You then write a loop that computes s(n) for the
given n. After the loop, you print the value of s(n) computed by the loop. Fill in the blanks to complete the program
below that computes s(n). Assume that the user enters a positive integer as n. Use no variables other than n and s. (5)
int main ()
{
int n;
double s;
(b) Consider the following two code snippets. Write the respective outputs of the snippets in the spaces provided.
Assume that p and q are int variables. Write only the outputs. No explanations are needed. (2)
p = 50; p = 50;
q = 0; q = 0;
while (p % 10) { do {
--p; --p;
++q; ++q;
} } while (p % 10);
printf("q = %d", q); printf("q = %d", q);
What will be printed by this snippet? What will be printed by this snippet?
q = 0 q = 10
Write (only) the output of this code snippet in the blank. counter = 11 (3)
— Page 3 of 4 —
For rough work only
— Page 4 of 4 —