330x Filetype PDF File size 0.24 MB Source: cse.iitkgp.ac.in
Department of Computer Science & Engineering
Indian Institute of Technology Kharagpur
Practice Sheet #03
Topic: C-Programming Constructs Date: 17-01-2017
1. Consider the following C function.
float f(float x, int y)
{
float p, s; int i;
for (s=1, p=1, i=1; i < y; i ++)
{
p*= x/i;
s+=p;
}
return s;
}
For large values of y, the return value of the function f best approximates
(a) x^y
(b) e^x
(c) ln(1 + x)
(d) x^x
2. Consider the following program fragment for reversing the digits in a given integer to obtain a
new integer. Let n = D1D2…Dm
int n, rev;
rev = 0;
while (n > 0)
{
rev = rev*10 + n%10;
n = n/10;
}
The loop invariant condition at the end of the ith iteration is:
(a) n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1
(b) n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1
(c) n != rev
(d) n = D1D2….Dm and rev = DmDm-1…D2D1
3. Consider the following C program
main()
{
int x, y, m, n;
scanf ("%d %d", &x, &y);
/* Assume x > 0 and y > 0 */
m = x;
n = y;
while (m! = n)
{
if (m > n)
m = m - n;
else
Page | 1
n = n - m;
}
print f ("% d", n);
}
The program computes:
(a) x ÷ y using repeated subtraction
(b) x mod y using repeated subtraction
(c) the greatest common divisor of x and y
(d) the least common multiple of x and y
4. What does the following algorithm approximate?
x = m;
y = 1;
while (x - y > e)
{
x = (x + y)/2;
y = m/m;
}
print(x);
(Assume m > 1, e > 0).
(a) Log m
(b) m2
(c) m1/2
(d) m1/3
5. Consider the following C-program:
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf ("%d\n", sum);
getchar();
}
What does the above program print?
(a) 8, 4, 0, 2, 14
(b) 8, 4, 0, 2, 0
(c) 2, 0, 4, 8, 14
(d) 2, 0, 4, 8, 0
6. Consider line number 3 of the following C- program.
Page | 2
int main ( ) { /* Line 1 */
int I, N; /* Line 2 */
fro (I = 0, I < N, I++); /* Line 3 */
}
Identify the compiler's response about this line while creating the object-module
(a) No compilation error
(b) Only a lexical error
(c) Only syntactic errors
(d) Both lexical and syntactic errors
7. Consider the following code fragment:
if (fork() == 0)
{ a = a + 5; printf(“%d,%d\n”, a, &a); }
else { a = a –5; printf(“%d, %d\n”, a, &a); }
Let u, v be the values printed by the parent process, and x, y be the values printed by the child
process. Which one of the following is TRUE?
(a) u = x + 10 and v = y
(b) u = x + 10 and v != y
(c) u + 10 = x and v = y
(b) u + 10 = x and v != y
8. Consider the following C code segment.
for (i = 0, i
int main( )
{
int i, j, k = 0;
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
k -= --j;
for (i = 0; i < 5; i++)
{
switch(i + k)
{
case 1:
case 2: printf("\n%d", i + k);
case 3: printf("\n%d", i + k);
default: printf("\n%d", i + k);
}
}
return 0;
}
The number of times printf statement is executed is __________.
(a) 8
(b) 9
(c) 10
(d) 11
11. Determine what the following program prints. (10)
#include
main ()
int r, t, m;
scanf("%d", &r); /* Enter the last four digits of your roll number as r */
printf("r = %d\n", r);
m = 0;
while (r > 0){
t = r % 10;
if (t > m) m = t;
r = r / 10;
printf("m = %d\n", m);
}
12. How many times is the body of the following loop executed?
int i;
for (i=0; i<100; i=i+3) printf("i = %d\n", i);
(a) 100
(b) 97
(c) 34
(d) 33
Page | 4
no reviews yet
Please Login to review.