GATE 2004
199 questions
MCQ 1991-mark 199
Questions 86–90 of 199
What does the following algorithm approximate? (Assume ).
x = m;
y = 1;
while (x - y > e)
{
x = (x + y)/2;
y = m/x;
}
print(x); Answer: (C)
Before the loop, and . After every pass, , so .
The loop stops when , that is, when and are almost equal. At that point the update no longer changes , so putting gives .
Check with : ; then ; then ; then . The values move towards . This is Newton's method for the square root.
Answer: (C)
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 computesAnswer: (C)
The loop repeats until . In each pass, the smaller number is subtracted from the larger one. This is the subtraction form of Euclid's algorithm.
Why the gcd does not change: any common divisor of and also divides , and any common divisor of and also divides . So , and the same for . The values stay positive and keep getting smaller, so the loop must stop, and it stops at . Then , which is the gcd of the starting and , and it is printed.
Example with : . It prints 6, which is . The other options give different values for this input: sum , , lcm .
Answer: (C)
Why the gcd does not change: any common divisor of and also divides , and any common divisor of and also divides . So , and the same for . The values stay positive and keep getting smaller, so the loop must stop, and it stops at . Then , which is the gcd of the starting and , and it is printed.
Example with : . It prints 6, which is . The other options give different values for this input: sum , , lcm .
Answer: (C)
Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let .
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 iteration is: Answer: (A)
Take , for example with . Each pass moves the last digit of to the end of and removes it from .
After iteration 1: and .
After iteration 2: and .
In general, after iterations, and . For this gives the starting values and (no digits yet), and for it gives and . So this condition holds before the loop, after every iteration and at the end.
Why the others fail:
(B) puts the digits of in the wrong order: holds first, not , and its first part does not say what is.
(D) is true only at the start ( is the full number) or at the end ( is the full reversal); after iteration 1 of , and , so neither part holds.
(C) says nothing about the digits, and it fails too: for , after iteration 1, .
Answer: (A)
After iteration 1: and .
After iteration 2: and .
In general, after iterations, and . For this gives the starting values and (no digits yet), and for it gives and . So this condition holds before the loop, after every iteration and at the end.
Why the others fail:
(B) puts the digits of in the wrong order: holds first, not , and its first part does not say what is.
(D) is true only at the start ( is the full number) or at the end ( is the full reversal); after iteration 1 of , and , so neither part holds.
(C) says nothing about the digits, and it fails too: for , after iteration 1, .
Answer: (A)
Consider the following C program:
#include <stdio.h>
typedef struct {
char *a;
char *b;
} t;
void f1 (t s);
void f2 (t *p);
main()
{
static t s = {"A", "B"};
printf ("%s %s\n", s.a, s.b);
f1(s);
printf ("%s %s\n", s.a, s.b);
f2(&s);
}
void f1 (t s)
{
s.a = "U";
s.b = "V";
printf ("%s %s\n", s.a, s.b);
return;
}
void f2(t *p)
{
p -> a = "V";
p -> b = "W";
printf("%s %s\n", p -> a, p -> b);
return;
} What is the output generated by the program ?Answer: (B)
The first line printed is
The function uses call by value, so its changes stay local only.
From is printed.
Back in main, is printed.
Then, in is printed.
So, the answer is (B).
Choose the correct option to fill the ?1 and ?2 so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.
#include <stdio.h>
void wrt_it (void);
int main (void)
{
printf("Enter Text");
printf ("\n");
wrt_it();
printf ("\n");
return 0;
}
void wrt_it (void)
{
int c;
if (?1)
wrt_it();
?2
} Answer: (D)
Each call of
So ?1 must read a character into and compare it with the newline:
Trace for the input
Why the others fail:
(A)
(B) The
(C) The test uses before any character is read, so it holds garbage, and
Answer: (D)
wrt_it has its own local variable . A call reads one character into , calls itself for the rest of the line, and only after that returns does it print its own . The printing happens on the way back, so the characters come out in reverse order.So ?1 must read a character into and compare it with the newline:
(c=getchar()) != '\n'. And ?2 must print it: putchar(c);.Trace for the input
abc followed by a newline: the calls read a, b, c. The fourth call reads the newline, the test fails, so it prints the newline and returns. The earlier calls then print c, b, a. The output is cba.Why the others fail:
(A)
getchar takes no argument, so getchar(c); does not compile. Even if it did, nothing is printed.(B) The
; after (c=getchar()) ends the condition early, so != '\n' is a syntax error.(C) The test uses before any character is read, so it holds garbage, and
putchar(c) prints garbage.Answer: (D)