Programming and data structure miscellaneous
- Consider the following C function :
int f (int n)
{ static int i = 1;
if (n > = 5) return n;
n = n + 1;
i ++;
return f(n);
}
The value returned by f (1) is
-
View Hint View Answer Discuss in Forum
The iterations that the given code will undergo are: From the given conditions we does not take into account when n = 1
Iteration 1
N = 1 + 1 = 2 therefore, i = 2
Iteration 2
N = 2 + 2 = 4 therefore, i = 3
Iteration 3
N = 4 + 3 = 2 = 4 therefore, i = 4
Hence, the value returned after three iterations is 7
When, i = 4 and it also fulfill the condition of n>=5Correct Option: C
The iterations that the given code will undergo are: From the given conditions we does not take into account when n = 1
Iteration 1
N = 1 + 1 = 2 therefore, i = 2
Iteration 2
N = 2 + 2 = 4 therefore, i = 3
Iteration 3
N = 4 + 3 = 2 = 4 therefore, i = 4
Hence, the value returned after three iterations is 7
When, i = 4 and it also fulfill the condition of n>=5
- What does the following algorithm approximate?
(Assume m > 1, ∈ > 0).
x = m; y = 1; while (x– y > ∈ )
{ x = (x + y)/ 2;
y = m / x;
}
print (x);
-
View Hint View Answer Discuss in Forum
Here we take let x = 16
Loop will stop when x – y = 0 or > 0Iteration X Y 1 (16 + 1)/ 2 = 8 16/ 8 = 2 2 (8 + 2)/ 2 = 5 16/ 5 = 3 3 (5 + 3)/ 2 = 4 16/ 4 = 4
Here X = Y
Then take X which is 4.
(m)1 / 2 = 4 = (16)1 / 2
Hence (c) is correct option.Correct Option: C
Here we take let x = 16
Loop will stop when x – y = 0 or > 0Iteration X Y 1 (16 + 1)/ 2 = 8 16/ 8 = 2 2 (8 + 2)/ 2 = 5 16/ 5 = 3 3 (5 + 3)/ 2 = 4 16/ 4 = 4
Here X = Y
Then take X which is 4.
(m)1 / 2 = 4 = (16)1 / 2
Hence (c) is correct option.
- 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
n = n – m;
}
printf (“% d”, n);
}
The program computes
-
View Hint View Answer Discuss in Forum
This is an implementation of Euclid's algorithm to find GCD
Here if m > n, then m = m – n
m < n, then n = n – m
Let take X = 24, Y = 9
Then m = 24, n = 9iteration m n 1 24 – 9 = 15 9 2 15 – 9 = 6 9 3 6 9 – 6 = 3 4 6 – 3 = 3 3
Here m = n, so n returned
Which is GCD (Greatest common divisor) of X&Y
Hence (c) is correct option.
Correct Option: C
This is an implementation of Euclid's algorithm to find GCD
Here if m > n, then m = m – n
m < n, then n = n – m
Let take X = 24, Y = 9
Then m = 24, n = 9iteration m n 1 24 – 9 = 15 9 2 15 – 9 = 6 9 3 6 9 – 6 = 3 4 6 – 3 = 3 3
Here m = n, so n returned
Which is GCD (Greatest common divisor) of X&Y
Hence (c) is correct option.
- Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = d1 d2... 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
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
NA
- Consider the following C-program
double foo (double); /* Line 1 */
int main () {
double da, db;
// input da db = foo (da);
}
double foo (double a) {
return a;
The above code compiled without any error or warning. If Line 1 is deleted, the above code will show
-
View Hint View Answer Discuss in Forum
When a function is called without being defined, C. Compiler assumes if to reterm “ Int” but here (800) is returning “ double” and hence the compiler will throw type mis-match error. So it is a compiler errors.
Correct Option: D
When a function is called without being defined, C. Compiler assumes if to reterm “ Int” but here (800) is returning “ double” and hence the compiler will throw type mis-match error. So it is a compiler errors.