You are on page 1of 11

CS1100 Quiz-I - Nov. 2022 Sem.

Student’s Name and Roll Number: KEY

CS 1100: Introduction to Programming


Quiz I: Dec. 2, 2022, CRC, 8:00am - 8:50am
Drs. C. Chandra Sekhar, Manikandan Narayanan, N. S. Narayanaswamy, Krishna Sivalingam
Closed Book, Notes and Neighbours; No Cell Phones; NO Scientific Calculator
Any form of academic dishonesty during the Quiz will be duly reported to relevant IIT Madras
Committee(s) for disciplinary action. The typical corrective action for such violations is ’U’ grade
in this course and ONE letter grade less in all other courses this semester.

INSTRUCTIONS

A. Write your name and Roll Number on the top of each page.

B. Make sure that the question paper has 11 pages, including this Cover page.

C. Include all calculations and mathematical derivations in your answer sheets, for complete credit.

D. You can write on BOTH sides of the given question paper. Please write legibly. ANSWER TO THE
POINT.

SIGNATURES

NAME: KEY

ROLL NUMBER: KE22K001

“I have read all the information specified above. I certify that all my answers written in this exami-
nation are entirely from my efforts.”

(Student’s Signature):

Student’s IIT Madras ID has been shown and verified: YES / NO

TA’s Name: TA’s Signature:

MARKS (Not for Student Use)

1. (7 marks)

2. (9 marks)

3. (9 marks)

4. (7 marks)

5. (8 marks)

Total: (40 marks)

Page 1 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

Q1. Graders’ name(s): Verifiers’ Names: *


Q2. Graders’ name(s): Verifiers’ Names: *
Q3. Graders’ name(s): Verifiers’ Names: *
Q4. Graders’ name(s): Verifiers’ Names: *
Q5. Graders’ name(s): Verifiers’ Names: *

Page 2 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

1. (7 marks) Answer the following questions:

(a) (5: 1 mark each) For each statement below, state whether it is True or False. If the answer is
False, write the correction required to make the statement True, in order to receive complete
credit.
(i) The range of a 32-bit unsigned int in C is 0 to 231 .
(ii) A variable X of type float can be used to store floating point numbers that are larger than
1050 .
(iii) After execution of this statement, "int a = 1.0/3.0;", the value of variable a will be greater
than 0.
(iv) The C statement “int a = INT_MIN - 1;” will lead to an overflow. (INT_MIN and
INT_MAX are respectively the minimum and maximum representable integer (int) value
in C.)
(v) For the scanf format specifier of %u, an input value of −24 will result in a correct value
being stored.
Write your answers below.

Answer: For writing ONLY False, 0 mark; writing False and giving CORRECT reason: 1
Mark.

(i) False. The range is 0 to 232 − 1.

(ii) False. float can store only numbers up to 1038 .

(iii) False. Since the result is 0.3333 (OR) due to integer division, the value stored in a
will be 0.

(iv) False. This will lead to an underflow.

(v) False. A large positive number will be stored (OR) %u is used for unsigned numbers.

Page 3 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

(b) (2: 1 mark each) Given the equation y = a + x(b + x(c + dx)), consider the following C state-
ments that intend to assign y as per this equation.
(i) y = a + x * b + x * c + d * x;
(ii) y = a + x * b + x * (c + d * x);
For each statement above, specify if it will correctly assign the value of y. If the statement is
incorrect, state the reason why.

Answer:
1 Mark per question. The correct reason should be given to get 1 mark. Stating only Incor-
rect, without given reason will be given 0 marks.

(i) y = a + x * b + x * c + d * x;
Incorrect statement.
There should be an open parenthesis before b (and closing parenthesis) and likewise
before c. (OR)
The above computes y = a + xb + xc + dx (OR)
Any other valid reason.

(ii) y = a + x * b + x * (c + d * x);
Incorrect statement. There should be a parenthesis before b. (OR) Any other valid
reason.

Page 4 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

2. (9 marks) Answer the following questions:

(a) (5 marks) Circle five possible errors (syntax, semantics, logic or unintended by the programmer)
in the following C program. The program might have more than five errors. Some of these
errors could generate compiler warnings, compiler errors or even be ignored by the compiler.
For each error that you identify, write the brief reason on the right side of that statement.

1 Mark per error correctly identified for a maximum of 5 marks.

If a statement is circled WITHOUT reason, then 0 marks to be given for that stmt.
If a statement is circled but reason is wrong, then 0 marks to be given for that stmt.
If a statement is circled AND correct reason given, then 1 mark to be given.
If a statement is NOT circled but correct reason given, then 1 mark to be given.

1 #include<studio.h> // should be stdio


2

3 int Main(){ // should be main()


4 integer x, y; // should be int
5 unsigned int z;
6

7 z = (unsigned int) -30.1;


8 // Neg. number assigned to unsigned; prog. error
9

10 if (y < z) { // y not initialized; prog. error


11 unsigned int r;
12 r = y;
13 printf(’r = %d\n’, r); // Single quote is wrong
14 }
15

16 if (y = z) // Probably meant ==; prog. error


17 print("r = %d", r); // r is out of scope
18

19 doubles p, q; // should be double


20 p = 2 * q; // q not initialized; prog. error
21 q = sin(3.1415) + p; // math.h must be included
22 if (p == q) // Not advisable to compare double value
23 printf("y = %f\n", y); // %f not applicable to int
24 return -1;
25 }

Page 5 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

(b) (4: 2 marks each) Let a and b be two int variables in C, whose values need to be exchanged
(e.g., if a = 4 and b = 3 before the exchange, it is required that a = 3 and b = 4 after the
exchange). Assuming that the arithmetic operations below do not lead to overflow/underflow,
determine whether each of these statement blocks will perform the exchange. Use one example
value, per statement block, for a and b (where a != b) to illustrate your answer.
(i) { int tmp; tmp = a; a = b; b = tmp; }
(ii) { a = a + b; a = a - b; b = a - b; }

Answer:

(i) { int tmp; tmp = a; a = b; b = tmp; }


Say a = 6; b = 9.

tmp = 6; (0.5)
a = 9; (0.5)
b = 6 (0.5).
This exchanges values correctly. (0.5 Marks)

(ii) { a = a + b; a = a - b; b = a - b; }
Say a = 6; b = 9.

a = 6 + 9 = 15 (0.5).
a = 15 - 9 = 6 (0.5).
b = 6 - 9 = -3 (0.5).
This does not exchange values correctly. (0.5 Marks)

Page 6 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

3. (9 marks) Answer the following questions.

(a) (5 marks) Consider the following C program. As per C-language’s precedence rules, write down
the order of operator evaluation for the two assignment statements in the program. Assume that
all variables are declared and initialized.
1 {
2 ....
3 x = a - b / c * d + e % f; // Expression 1
4 y = a * b % (c + d) - e / f; // Expression 2
5 ....
6 }

Expression 1: 2.5 Marks for correct operator order as below; No partial marks.
Operator Order Operator (+, -, etc.)
1 /
2 ∗
3 %
4 −
5 +

Expression 2: 2.5 Marks for correct operator order as below; No partial marks.
Operator Order Operator (+, -, etc.)
1 +
2 ∗
3 %
4 /
5 −

Page 7 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

(b) (4 marks) Consider the following C program.


1 #include <stdio.h>
2 int main(){
3 unsigned int x, y;
4 scanf("%u", &x);
5 scanf("%u", &y);
6 if (x < 15)
7 if (y >= 25)
8 printf("aaa\n");
9 else
10 printf("bbb\n");
11

12 printf("ccc\n");
13 return 0;
14 }

(i) (1) What is the output for x = 19, y = 26?

Answer: ccc

(ii) (1) What is the output for x = 6, y = 29?

Answer:
aaa
ccc

(iii) (2) What is the range of x and y values for which the output will be as given below:

bbb
ccc

Answer: (x < 15), (y < 25)


1 mark for each correct sub-clause above.

Page 8 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

4. (7) Answer the following questions:

(a) (3) What is the output of the following program? Show all calculations.
1 #include<stdio.h>
2 int main()
3 {
4 float u = 3.5;
5 int v, w, x, y;
6 v = (u + 0.5);
7 w = (int)u + 0.5;
8 x = (u + (int)0.5);
9 printf("v = %d, w = %d, x = %d\n", v, w, x);
10 return 0;
11 }

Answer:
v = 4, w = 3, x = 3
1 Mark per statement below
v = (3.5 + 0.5) = 4; (If written as 4.0, give only 0.5 mark)
w = (int) 3.5 + 0.5 = 3 + 0.5 = 3.5 = 3; (If written as 3.0, give only 0.5 mark)
x = (3.5 + 0) = 3.5 = 3; (If written as 3.0, give only 0.5 mark)

(b) (4) Let i = 3; j = 8; k = -2; m = 9 be int variables in C. What is the value of


the following logical expression? Show the values of each sub-expression.

(i != j) && ((j < k) && (m > i)) || (!(m > 1) && (i == k))

Answer: For stmts 1–7, 0.5 Mark each; For stmts 8–9, 0.25 Mark Each
1: (i != j): 1
2: (j < k): 0
3: (m > i): 1
4: ((j < k) && (m > i)): 0
5: (m > 1): 1; !(m > 1): 0
6: (i == k): 0
7: (!(m > 1) && (i == k)): 0
8: 1 && 0: 0
9: 0 || 0: 0
Note: 0 can be written as False; and 1 as True.

Page 9 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

5. (8 marks) Write a C program that takes as input a two-digit unsigned integer, X, in decimal repre-
sentation and prints out the sum of the two digits. That is, the input value for X is required to be
from 10 to 99. Your program MUST validate the user input, i.e., check if X meets this requirement,
before proceeding with the rest of the program. If X does not meet the requirement, no output should
be printed.
For example, if the user inputs 67, the output should be 13; if the user inputs 99, the output should
be 18.

1 #include<stdio.h>
2 int main()
3 {
4 unsigned int X; // 0.5 marks
5 unsigned int a, b; // Temp. variables not needed
6

7 // printf with prompt is optional


8 scanf("%u", &X); // %d: 0.5; &X: 0.5 mark
9

10 if ((X >= 10) && (X <= 99))


11 // 2 marks for correct condition
12 {
13 a = X / 10;
14 b = X % 10;
15 sum = a + b; // Correct Logic for sum: 4 marks
16 printf("%u", sum); // Fully correct: 0.5 marks
17 }
18 }

Multiple variations are possible for the if-else condition and the computing logic ; you have to verify
that the logic to compute the sum is correct.
A few correct alternatives:

printf("%d", X / 10 + X % 10);
a = X / 10; b = X - 10*a; sum = a + b;
b = X % 10; a = (X - b) / 10; sum = a + b;

In case int variable is used consistently, full marks can be given.

Page 10 of 11
CS1100 Quiz-I - Nov. 2022 Sem.Student’s Name and Roll Number: KEY

EMPTY PAGE – FOR ANSWERS OR ROUGH WORK

Page 11 of 11

You might also like