GATE 2001
60 questions
MCQ 601-mark 60
Questions 31–35 of 60
Program P1()
{
x = 10;
y = 3;
func1(y,x,x);
print x;
print y;
}
func1(x,y,z)
{
y = y+4;
z = x+y+z;
}
Answer: (B)
- formal x is the same location as P1's y (value )
- formal y and formal z are both the same location as P1's x (value )
: the location of P1's x becomes .
: formal x is , and formal y and z are both , so the result is . This is stored in the location of P1's x.
Nothing is copied back later, since y and z share P1's x directly. P1's y is never changed.
So P1 prints x = 31 and y = 3.
Answer: (B)
[P1]
int * g (void)
{
int x= 10;
return (&x);
} [P2] int * g (void)
{
int * px;
*px= 10;
return px;
} [P3] int *g (void)
{
int *px;
px = (int *) malloc (sizeof(int));
*px= 10;
return px;
} Which of the above three functions are likely to cause problems with pointers ?Answer: (C)
may cause an error, because the function returns the address of a variable declared locally.
will cause a problem, because px is an int pointer that has no address assigned to it, and still we dereference it.
will work, because memory of the size of an int, in bytes, is reserved and its address is stored in px, so it can be used later. Once the function finishes, this memory still stays in the Heap until we free it with the function.
So, the answer is (C).
Answer: (B)
Two stacks are enough to build a queue. One stack is not enough, because a stack is last-in first-out: to remove the oldest element (at the bottom) you must take out everything above it, and one stack has nowhere to hold those elements while you do it. A second stack gives that place.
Let the queue be and let the two stacks be "stack1" and "stack2". The queue never holds more than elements, so neither stack needs more than places. There are two ways to build :
Method 1 (enQueue is costly)
The newly added element always goes to the bottom of stack1, so deQueue only pops from stack1. Stack2 is used to place the new element at the bottom of stack1.
- While stack1 is not empty, move every element from stack1 to stack2.
- Push onto stack1.
- Move everything back from stack2 to stack1.
- If stack1 is empty, report an error.
- Pop one item from stack1 and return it.
Method 2 (deQueue is costly)
In enQueue, the new element goes on top of stack1. In deQueue, if stack2 is empty, all elements are moved to stack2, which reverses their order, and then the top of stack2 is returned.
- Push onto stack1.
- If both stacks are empty, report an error.
- If stack2 is empty, while stack1 is not empty, move every element from stack1 to stack2.
- Pop the element from stack2 and return it.
Answer: (B)
Answer: (B)
Go back from a child to its parent: and . Both children give the parent , so the parent of the node at index is at .
The other options fail: is not a whole number for odd (for it is 1.5), and is wrong, since the parent of index 4 is 2, not 3. also gives fractions (for it is 1.5).
Answer: (B)
Answer: (C)
- If the random pivot is the smallest or largest element every time, each partition splits n elements into 0 and n 1. Then , which is . The chance of this is . It is very small but not zero.
- If all elements are equal and the usual partition is used, every split is also 0 and n 1, whatever pivot is chosen.
So the worst case is . O(n!) is also a valid upper bound, since for , but it is not tight. O(n) and O(n log n) are too small.
Answer: (C)