0% found this document useful (0 votes)
9 views4 pages

CT1_22_spring

The document is a class test for CS10003 Programming and Data Structures, focusing on the C programming language. It includes various questions on variable naming, declarations, expressions, conditional statements, loops, and outputs of code snippets. The test is structured with specific tasks requiring students to fill in answers directly on the question paper.

Uploaded by

Arya Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

CT1_22_spring

The document is a class test for CS10003 Programming and Data Structures, focusing on the C programming language. It includes various questions on variable naming, declarations, expressions, conditional statements, loops, and outputs of code snippets. The test is structured with specific tasks requiring students to fill in answers directly on the question paper.

Uploaded by

Arya Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS10003 Programming and Data Structures, Spring 2022–2023

Class Test 1
30–March–2023 06:30pm–07:30pm Maximum marks: 30

Roll no: Name:


h i
Write your answers in the question paper. Answer all questions. All questions use the programming language C.

1. (a) Which of the following are valid variable names? Fill in the blanks with only Yes/No answers. No explanations
are needed. (3)

_rs579t Yes -rs579t No r_s579t Yes

r-s579t No 5_79rst No _579rst Yes

(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)

if (!(( marks < 80 ) || ( marks >= 90 )))


printf("A grade");
else
printf("Not A grade");

(b) Consider the following code snippet.


int x = 5;
if (x = 0) printf("rhino");
else if (x > 0) printf("giraffe");
else printf("zebra");

What will be printed by this snippet? zebra (2)

(c) Consider the following program.


#include <stdio.h>
int main ()
{
if (-5) printf("W"); if (’5’) printf("X");
else printf("Y"); printf("Z");
return 0;
}

What will be the output of this program? WXZ (2)

(d) Consider the following two code snippets. Write the respective outputs of the snippets in the spaces provided. (2)

int x = 10; int x = 10;


if ((x == 10) || (x = 100)); if ((x == 10) && (x = 100));
printf("x = %d", x); printf("x = %d", x);

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");
}

What will be printed by this code snippet? CDEFGHI (2)

— 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;

printf("Enter n: "); scanf( "%d", &n );


s = 0; /* Initialize the sum s */

while ( n > 0 ) { /* Loop on n */

s += (double)(2*n - 1) / (double)n ; /* Update the sum s */


--n;
}

printf("The desired sum is %lf \n", 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

(c) Consider the following code snippet.


int x, y = 0, counter = 0;
for (x = 1; x <= 5; x++) {
counter++;
y -= 2;
if (y % 6 == 0) {
x += y;
y = x;
}
}
printf("counter = %d", counter);

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 —

You might also like