0% found this document useful (0 votes)
26 views3 pages

Spring 2020 21 ST1 Solutions

Uploaded by

Madhur Sharma
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)
26 views3 pages

Spring 2020 21 ST1 Solutions

Uploaded by

Madhur Sharma
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/ 3

Short Test 1

Programming and Data Structures, Spring 2021


28-Apr-2021

1. [Variable Naming] Which of the following is NOT a valid variable name in C?

_count
🗸 count-a
if_else
🗸 10digits

2. [Expression] Suppose a, b, c are int type variables, with a = 13, b = 5, c = 7. What would be the value of the expression 4*a/b +
3*b/c/a? (WRITE ONLY THE VALUE, NOTHING ELSE)

Ans: 10

3. [If-Else] Consider the following program fragment, where a and b are int type variables. What will be printed if a = 0, b = 10?
(WRITE ONLY THE VALUE, NOTHING ELSE)

if ((a > 0 && b > 0) || (a < 0 && b < 0))


printf("%d", a + b);
else if ((a > 0 && b < 0) || (a < 0 && b > 0))
printf("%d", a - b);
else
printf("%d", b - a);

Ans: 10

4. [Characters] Consider the following program fragment, where ch is a char type variable. What will be printed if the character D
is entered from the keyboard?

scanf("%c", &ch);
printf("%c", 'Z' - ch + 'A');

🗸 W
X
V
87

5. [While Loop] Consider the following while loop. How many times will the loop be entered? (WRITE ONLY THE VALUE,
NOTHING ELSE)

i = 200; j = 50;
while(i > 0 && j > 0) {
i = i - j;
j = j - 5;
}

Ans: 5

6. [For Loop] How many times will the line "I am here" get printed if the following program fragment is run with n = 100?
(WRITE ONLY THE VALUE, NOTHING ELSE)

for (i = 0; i < n; i++)


for (j = n-i; j > 0; j--)
printf("I am here\n");

Ans: 5050
7. [Switch] Consider the switch statement below, where a is an int type variable. What will be printed if a = 10 initially? (WRITE
ONLY THE VALUE, NOTHING ELSE)

switch(a) {
case 10: a = a + 10;
case 20: a = a + 20;
case 30: a = a + 30;
case 40: a = a + 40;
break;
case 50: a = a + 50;
default: a = a + 100;
}
printf("%d", a);

Ans: 110

8. [Array] What will be the content of the array A after the following program fragment is executed? Assume that i is an int type
variable already declared.
[1 mark]

float A[5] = {0.5, 1.5, 2.5, 3.5, 4.5};


i = 4;
do {
A[i-1] = A[i] + A[i-1];
i--;
} while (i > 0);

Ans: {12.5, 12.0, 10.5, 8.0, 4.5}

9. [Strings] Consider the following program fragment, where i, j, count are int type variables, and A and B are 50 element character
arrays. What will be printed if we enter the strings "program" and "Process" from the keyboard (in that order)? (WRITE ONLY THE
VALUE, NOTHING ELSE)

scanf("%s%s", A, B);
i = 0; count = 0;
while (A[i] != '\0') {
j = 0;
while (B[j] != '\0') {
if (A[i] != B[j]) {
j++;
continue;
} else {
count++;
break;
}
}
i++;
}
printf ("%d", count);

Ans: 3

10. [SumOfSquares] Consider the following program. Fill in the blank lines marked (A), (B), (C) so that the program prints (in the
printf statement at the end) the number of ways the positive integer n (read in scanf) can be represented as a sum of two squares (For
example, if n =8, there is only one way to represent it (2^2 + 2^2); if n = 7, there is no way (so 0 ways) to represent it; and if n = 25,
there are two ways to represent it (4^2 + 3^2, and 0^2 + 5^2)). For your answer, just write what is to be filled in the blank lines
marked (A), (B), and (C). Note that you may add more than one statement in a single line if needed.
[3 marks]

int main ()
{
int n, NWays, a, b, s;

scanf("%d", &n);
/* Assume that n is positive */

/* Search for all a,b with 0 ⩽ a ⩽ b


* and a² + b² ⩽ n. Nways stores the
* number of ways in which n can be
* expressed as a sum of squares. */

a = 0; NWays = 0; /* Initialize */
while (1) { /* Outer loop */
b = a;
while (1) { /* Inner loop */
s = a*a + b*b;
if (s < n) {

(A) ++b; continue;


}

if (s == n) {

(B) ++nways;
}

break;
}

++a;
/* If a is too large for a²+b² = n,
* break the outer loop. Do not use
* math library calls. */

(C) if ( a * a > n / 2 ) break;


}
printf("Number of ways = %d\n", NWays);
}

11. [Array] Consider the following program that first reads in an array A of n integers. Fill in the blanks marked (A) and (B) so that
the program replaces each element A[i] with the first integer following it in A that is greater than it, if any. If there are no integers
following A[i] that is greater than A[i], then A[i] is unchanged. For example, if A = [10, 1, 20, 30, 5, 7, 4], then on execution of the
program, A = [20, 20, 30, 30, 7, 7, 4]. For your answer, just write what is to be filled in the blank lines marked (A) and (B).
[2 marks]

int main()
{
int A[20];
int n, i, j;

scanf("%d", &n);
for(i=0; i<n; i++) scanf("%d", &A[i]);

for(i=0; i<n-1; i++) {

(A) for ( j = i+1; j < n; j++ ) {


if (A[j] > A[i]) break;
}

(B) if ( j < n )
A[i] = A[j];
}
}

You might also like