17_autumn
17_autumn
INSTRUCTIONS
1. Answer ALL questions
2. Please write the answers either within the boxes provided or on the blank lines to be filled up. Any
answer written elsewhere will not be evaluated.
3. You may use the last two blank pages for your rough works.
a) What will get displayed when the following program is executed? [1]
#include <stdio.h>
int main() { result=5
int x = 2, y = 17, result = 5;
result -= x/5 ∗ 13 ∗ y/3 ∗ x;
printf(“result=%d\n”, result);
return 0;
}
b) What will get displayed when the following program is executed? [1]
#include <stdio.h>
int main() {
int x = -5, y = 10;
x=-10
if (x > y) x = 1;
else if (y < 0) x = x ∗ (-1);
else x = 2 ∗ x;
printf(“x=%d\n”, x);
return 0;
}
c) What is the 8-bit two’s complement representation of the decimal number ̶ 37? [2]
11011011
1 of 8
d) What will get displayed when the following program is executed? [1]
#include <stdio.h>
int main() {
char a = 'a';
while ((a > 'a') && (a <= 'c')) a++;
a
printf(“%c\n”, a);
return 0;
}
e) What will get displayed when the following program is executed? [2]
#include <stdio.h>
int main() {
int sum = 1, index = 9;
sum=2, index=8
do {
index = index - 1;
sum = 2 ∗ sum;
} while (index > 9);
printf(“sum=%d, index=%d\n”, sum, index);
return 0;
}
f) What value will the following function return when called as recur(3)? [2]
g) What value will the following function return when called as g(1024)? [1]
int g(int n) {
if (n < 2) return n; 1
return g(n/2);
}
h) What is the binary number corresponding to the hexadecimal number C5.75? [2]
11000101.01110101
2 of 8
i) Consider the program segment given below to read a letter from a..z and A..Z from the keyboard
and convert it to uppercase if not already so. It is assumed that the user will only input a character
from a..z and A..Z. Fill up the missing line with a single C expression so that the variable ch will
contain the character in uppercase. Do not use any library functions. [2]
char ch;
ch = getchar();
ch = (ch>=’A’)&&(ch<=’Z’)?ch:’A’+ch-‘a’;
j) The following program segment is supposed to check whether the values stored by three integer
variables a, b, and c are in ascending order. However, it contains an error. Encircle the part of the
program that contains the error and write only that part corrected. [1]
if (a < b < c)
printf(“Numbers in ascending order \n”);
((a<b) && (b<c))
else printf(“Not in ascending order\n”);
a) What will get displayed when the following program is executed? [2]
#include <stdio.h>
int main() {
int i;
for (i = 1; i = -1; i++) -1
if (i < 5) break;
printf(“%d\n”, i);
return 0;
}
b) What will get displayed when the following program is executed? [2]
#include <stdio.h>
void increment(int i) {
i++;
} i=11, j=0
int main() {
int i = 0, j = 0;
while (i++ < 10) increment(j);
printf(“i=%d, j=%d\n”, i, j);
return 0; 3 of 8
}
c) What will get displayed when the following program is executed? [2]
#include <stdio.h>
int main() {
float j = 1.0, i = 2.0;
n=6, j=64.000000
int n = 0;
while (i/j > 0.05) {
j = j + j;
n++;
}
printf(“n=%d, j=%f\n”, n, j);
return 0;
}
d) What will get displayed when the following function is called as f(2, 8)? [2]
e) What will get displayed when the following program is executed? [2]
#include <stdio.h>
int main() {
sum=1683, i=102
int sum = 0, i = 3;
while (i < 100) {
sum = sum + i;
i = i + 3;
}
printf(“sum=%d, i=%d\n”, sum, i);
return 0;
}
4 of 8
Q.3.
a) A number is said to be perfect if it is the sum of all its factors (except itself). For example, 6 has
factors 1, 2, 3 and 1+2+3 = 6, hence it is perfect. Also, 28 = 1+2+4+7+14 is perfect. In the
following function checkPerfect fill up the missing lines so that it returns 1 if n is a perfect
number and 0 if n is not a perfect number. [2 + 2]
int checkPerfect(int n) {
int i, sum = 0;
for (i = 1; i < n; i++) {
if (n % i == 0 )
sum += i;
}
return (sum == n );
b) The following function strEqual takes two strings S1 and S2 as parameters. Fill up the missing
lines in the function so that it returns 1 if the two strings are the same, 0 otherwise.
[1 + 2 + 2]
5 of 8
Q.4.
n
a) The following recursive function find_power should return x when called as find_power(x,n),
n
n being a non-negative integer. Fill up the missing lines in the function so that it returns x .
[1 + 1 + 2]
if (n == 0 )
return 1;
else
return x*find_power(x,n-1);
}
b) Fill up the missing lines in the following program so that it will display the sum of the elements
of the array A when executed. [2 + 2]
#include <stdio.h>
int main() {
int i, n, k = 0, A[10], lim;
printf("Enter number of elements ");
scanf("%d", &n);
printf("Enter the elements ");
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
for (i = 0, lim = n/2; i < lim; i++) {
/∗ Accumulate Sum ∗/
k = k + A[i]+A[n-i-1];
}
if (lim <(n-lim)) /∗ if middle element left out ∗/
k = k + A[i];
printf("%d\n", k);
return 0;
}
6 of 8
Q.5.
a) The following program is supposed to insert a new integer value x into an already sorted (in
ascending order) array A containing n distinct integers. You can assume that x does not already
exist in A, and there is space available to insert x in A. For example, assume that n is 10, and A
has the elements 10, 20, 30, 40, 60, 70, 80, 90, 100, 110, and x is 56. After insertion of x, the
array would become 10, 20, 30, 40, 56, 60, 70, 80, 90, 100, 110, and n would be 11. Fill up the
missing lines in the program so that the program inserts x in the sorted array A.
[2 + 2 + 2 + 2]
#include <stdio.h>
int main(){
int x, i = 0, j, n, A[100];
scanf("%d%d", &n, &x);
for (j = 0; j < n; j++) scanf("%d", &A[j]);
while (x > A[i] && i < n) i++; /∗ find position after which to insert ∗/
A[j] = A[j-1];
n++;
if ((A[i]-val)<(val-A[i-1]))
}
return index;
}
8 of 8