GATE 2004
199 questions
MCQ 1991-mark 199
Questions 91–95 of 199
What is the output of the following program?
#include <stdio.h>
int funcf (int x);
int funcg (int y);
main ()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count) {
y += funcf(x) + funcg(x);
printf ("%d", y);
}
}
funcf (int x) {
int y;
y = funcg(x);
return (y);
}
funcg (int x) {
static int y = 10;
y += 1;
return (y + x);
}
Answer: (A)
The variable inside
Each loop pass calls
Pass 1: static becomes 11, returns . Then it becomes 12, returns . Sum , so . Prints 43.
Pass 2: static becomes 13, returns 18. Then 14, returns 19. Sum , so . Prints 80.
The two printed values are 43 and 80.
Answer: (A)
funcg starts at 10 once and keeps its value between calls. It is a different variable from the in main.Each loop pass calls
funcg(5) twice: once directly and once through funcf. C does not fix which operand of runs first, so either call can get the smaller return value. But the two return values always add up to the same total, so the order does not change the result.Pass 1: static becomes 11, returns . Then it becomes 12, returns . Sum , so . Prints 43.
Pass 2: static becomes 13, returns 18. Then 14, returns 19. Sum , so . Prints 80.
The two printed values are 43 and 80.
Answer: (A)
Consider the following C function:
int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
} The value returned by f(1) isAnswer: (C)
The static variable i is created once, with value , and keeps its value across the recursive calls. Each call returns the value of the next call, so follow the calls until .
: , then
: , then
: , then
: , so it returns
So .
Answer: (C)
: , then
: , then
: , then
: , so it returns
So .
Answer: (C)
Consider the following C function
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
} In order to exchange the values of two variables x and y.Answer: (D)
The function swap gets only copies of x and y, because the call is by value. It swaps the copies, so x and y in the caller stay the same. Passing x and y would not match the int parameters either.
Answer: swap(x,y) cannot be used as the parameters are passed by value
Answer: swap(x,y) cannot be used as the parameters are passed by value
Consider the following C program which is supposed to compute the transpose of a given matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program.
#include <stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
} Answer: (C)
A swap needs a temporary: save one value in , copy the other over it, then put in the other place. So the body must be . This rules out (B) and (D): they write first, before holds anything useful, so the old value of is lost and the matrix is destroyed.
Now the start of . If starts at (option A), the pair and is swapped when , and swapped back when . Every off-diagonal pair is swapped twice, so is unchanged and not transposed.
If starts at (option C), each pair above the diagonal is swapped exactly once, and the diagonal entry swaps with itself, which does nothing. This gives the transpose.
Answer: (C)
Now the start of . If starts at (option A), the pair and is swapped when , and swapped back when . Every off-diagonal pair is swapped twice, so is unchanged and not transposed.
If starts at (option C), each pair above the diagonal is swapped exactly once, and the diagonal entry swaps with itself, which does nothing. This gives the transpose.
Answer: (C)
Consider the following C program segment:
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s",p); The output of the program isAnswer: (D)
length = strlen("string") = 6. In the loop, p[i] = s[length - i] (not s[length - 1 - i]). For i = 0 this is p[0] = s[6], which is the '\0' that ends the string literal.
printf with %s stops at the first '\0'. Here that is p[0], so it prints an empty string, which is no output.
The other copies (p[1] = 'g', ..., p[5] = 't') come after the '\0' and are never printed. s[0] = 's' is never copied at all.
(A) "gnirts" would need s[length - 1 - i] and a '\0' stored at the end. (C) "gnirt" is what p[1] to p[5] hold, but printf stops at p[0] before reaching them. (B) is the original string and the loop does not copy in forward order.
Answer: (D)
printf with %s stops at the first '\0'. Here that is p[0], so it prints an empty string, which is no output.
The other copies (p[1] = 'g', ..., p[5] = 't') come after the '\0' and are never printed. s[0] = 's' is never copied at all.
(A) "gnirts" would need s[length - 1 - i] and a '\0' stored at the end. (C) "gnirt" is what p[1] to p[5] hold, but printf stops at p[0] before reaching them. (B) is the original string and the loop does not copy in forward order.
Answer: (D)