GATE 2007
184 questions
MCQ 1841-mark 184
Questions 76–80 of 184
An array of n numbers is given, where n is an even number. The maximum as well as the minimum of these n numbers needs to be determined. Which of the following is TRUE about the number of comparisons needed?
Answer: (B)
Pair up the numbers and compare each pair: comparisons. Call the larger of each pair a winner and the smaller a loser.
The maximum is a winner, since it cannot be the smaller one of its pair. The minimum is a loser for the same reason.
Find the maximum among the winners: comparisons. Find the minimum among the losers: comparisons.
Total: comparisons, so at most are needed.
(A) is false: is already less than for large and any constant c. (C) is false: is far more than . (D) is false since (B) is true.
Answer: (B)
The maximum is a winner, since it cannot be the smaller one of its pair. The minimum is a loser for the same reason.
Find the maximum among the winners: comparisons. Find the minimum among the losers: comparisons.
Total: comparisons, so at most are needed.
(A) is false: is already less than for large and any constant c. (C) is false: is far more than . (D) is false since (B) is true.
Answer: (B)
Consider the following C program:
What is the output of the program for the following input?
5 2 * 3 3 2 + * +
#include < stdio.h >
#define EOF -1
void push (int); /* push the argument on the stack */
int pop (void); /* pop the top of the stack */
void flagError ();
int main ()
{ int c, m, n, r;
while ((c = getchar ()) != EOF)
{ if (isdigit (c) )
push (c);
else if ((c == '+') || (c == '*'))
{ m = pop ();
n = pop ();
r = (c == '+') ? n + m : n*m;
push (r);
}
else if (c != ' ')
flagError ();
}
printf("% c", pop ());
} What is the output of the program for the following input?
5 2 * 3 3 2 + * +
Answer: (B)
The program reads a postfix expression. A digit is pushed (as the number it shows). An operator pops two items, first and then , and pushes . At the end it prints the top of the stack.
Trace for 5 2 * 3 3 2 + * +:
- push 5, push 2: stack is 5, 2
- : pop 2 and 5, push . Stack: 10
- push 3, push 3, push 2. Stack: 10, 3, 3, 2
- : pop 2 and 3, push . Stack: 10, 3, 5
- : pop 5 and 3, push . Stack: 10, 15
- : pop 15 and 10, push . Stack: 25
The value left on the stack is 25.
Note: the code is printed this way in the paper. Read literally, push(c) pushes the character code of each digit (for example 53 for '5', not 5), and printf("% c", ...) prints a character, not a number. The intended reading, used by the key, takes each digit as its value and prints the result as an integer, which gives 25.
Answer: (B)
Trace for 5 2 * 3 3 2 + * +:
- push 5, push 2: stack is 5, 2
- : pop 2 and 5, push . Stack: 10
- push 3, push 3, push 2. Stack: 10, 3, 3, 2
- : pop 2 and 3, push . Stack: 10, 3, 5
- : pop 5 and 3, push . Stack: 10, 15
- : pop 15 and 10, push . Stack: 25
The value left on the stack is 25.
Note: the code is printed this way in the paper. Read literally, push(c) pushes the character code of each digit (for example 53 for '5', not 5), and printf("% c", ...) prints a character, not a number. The intended reading, used by the key, takes each digit as its value and prints the result as an integer, which gives 25.
Answer: (B)
The following postfix expression with single digit operands is evaluated using a stack:
8 2 3 ^ / 2 3 * + 5 1 * - Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is evaluated are:Answer: (A)
Step by step:
- Push 8, 2, 3, then apply ^ the stack holds 8, 8 (2^3 = 8)
- Next is /, so 8 / 8 = 1
- Push 2 and 3, then apply * 2 3 = 6 (this is the first *)
The stack now holds 1, 6 (top = 6).
Answer: 6, 1
- Push 8, 2, 3, then apply ^ the stack holds 8, 8 (2^3 = 8)
- Next is /, so 8 / 8 = 1
- Push 2 and 3, then apply * 2 3 = 6 (this is the first *)
The stack now holds 1, 6 (top = 6).
Answer: 6, 1
Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are:
i. isEmpty(Q) returns true if the queue is empty, false otherwise.
ii. delete(Q) deletes the element at the front of the queue and returns its value.
iii. insert(Q,i) inserts the integer i at the rear of the queue.
Consider the following function
What operation is performed by the above function f ?
i. isEmpty(Q) returns true if the queue is empty, false otherwise.
ii. delete(Q) deletes the element at the front of the queue and returns its value.
iii. insert(Q,i) inserts the integer i at the rear of the queue.
Consider the following function
void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
i = delete(Q);
f(Q);
insert(Q, i);
}
} What operation is performed by the above function f ?
Answer: (B)
Each call of deletes the front element into its own local and calls again. For , the calls hold in turn, and the queue becomes empty. The last call finds an empty queue and returns.
Now the calls return, innermost first. The call holding inserts 5 first, then the one holding inserts 4, and so on, and the outermost call inserts 1 last. The queue is now from front to rear, which is the reverse of the original order.
Answer: (B)
Now the calls return, innermost first. The call holding inserts 5 first, then the one holding inserts 4, and so on, and the outermost call inserts 1 last. The queue is now from front to rear, which is the reverse of the original order.
Answer: (B)
Consider the following C program segment where CellNode represents a node in a binary tree:
struct CellNode
{
struct CellNode *leftChild;
int element;
struct CellNode *rightChild;
};
int GetValue(struct CellNode *ptr)
{
int value = 0;
if (ptr != NULL)
{
if ((ptr->leftChild == NULL) &&
(ptr->rightChild == NULL))
value = 1;
else
value = value + GetValue(ptr->leftChild)
+ GetValue(ptr->rightChild);
}
return(value);
} The value returned by GetValue when a pointer to the root of a binary tree is passed as its argument is:Answer: (C)
The function gives back only when a node has both its left and right child equal to . Such a node is a leaf node. So the value goes up by one at every leaf node.
Answer: (C)