Spring 2020 21 ST1 Solutions
Spring 2020 21 ST1 Solutions
_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)
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)
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]
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 */
a = 0; NWays = 0; /* Initialize */
while (1) { /* Outer loop */
b = a;
while (1) { /* Inner loop */
s = a*a + b*b;
if (s < n) {
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. */
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]);
(B) if ( j < n )
A[i] = A[j];
}
}