GATE 2006
191 questions
MCQ 1911-mark 191
Questions 96–100 of 191
Consider the C code to swap two integers and these five statements: the code
S2 : may generate a segmentation fault at runtime depending on the arguments passed
S3 : correctly implements the swap procedure for all input pointers referreing to integers stored in memory locations accessible tot he process
S4 : implements the swap procedure correctly for some but not all valid input pointers
S5 : may add or subtract integers and pointers
void swap(int *px,int *py){
*px=*px-*py;
*py=*px+*py;
*px=*py-*px;
} S1 : will generate a compilation error S2 : may generate a segmentation fault at runtime depending on the arguments passed
S3 : correctly implements the swap procedure for all input pointers referreing to integers stored in memory locations accessible tot he process
S4 : implements the swap procedure correctly for some but not all valid input pointers
S5 : may add or subtract integers and pointers
Answer: (C)
Let the two locations hold (at px) and (at py), at different addresses.
So the values are swapped when px and py point to different locations (ignoring integer overflow).
S1 is false. Dereferencing pointers and doing integer arithmetic on the values is valid C, so the code compiles.
S2 is true. If px or py is NULL or points to memory the process cannot access, the dereference can cause a segmentation fault.
S3 is false. If px and py point to the same location, , then , then . The value is lost, but it should stay the same. Overflow in is another possible problem.
S4 is true. The code works for pointers to different locations but not for the same location, so it is correct for some valid inputs but not all.
S5 is false. The arithmetic is done on the integer values and , never on the pointers themselves.
So S2 and S4 are true.
Answer: (C)
So the values are swapped when px and py point to different locations (ignoring integer overflow).
S1 is false. Dereferencing pointers and doing integer arithmetic on the values is valid C, so the code compiles.
S2 is true. If px or py is NULL or points to memory the process cannot access, the dereference can cause a segmentation fault.
S3 is false. If px and py point to the same location, , then , then . The value is lost, but it should stay the same. Overflow in is another possible problem.
S4 is true. The code works for pointers to different locations but not for the same location, so it is correct for some valid inputs but not all.
S5 is false. The arithmetic is done on the integer values and , never on the pointers themselves.
So S2 and S4 are true.
Answer: (C)
Given two arrays of numbers and where each number is 0 or 1, the fastest algorithm to find the largest span(i,j) such that , or report that there is no such span,
Answer: (C)
Let with . The span (i, j) has equal sums exactly when . So the longest span comes from two equal values of d that are farthest apart.
Each is an integer from to , so there are only possible values. Keep a table first[v] of size that stores the first index k where . Scan k = 1 to n once: compute ; if first[] is empty store k, otherwise the span length is first[]. Keep the largest. If no value repeats, there is no span.
This takes steps of constant work, using a table of size (direct indexing, no real hashing needed): time and space. Any algorithm must read all numbers, since changing one can change the answer, so time is needed. So the time is .
(A) and (B) claim much slower running times, so they cannot describe the fastest algorithm. (D) claims , which is below the needed to read the input; the parity of the total sum has no effect.
Answer: (C)
Each is an integer from to , so there are only possible values. Keep a table first[v] of size that stores the first index k where . Scan k = 1 to n once: compute ; if first[] is empty store k, otherwise the span length is first[]. Keep the largest. If no value repeats, there is no span.
This takes steps of constant work, using a table of size (direct indexing, no real hashing needed): time and space. Any algorithm must read all numbers, since changing one can change the answer, so time is needed. So the time is .
(A) and (B) claim much slower running times, so they cannot describe the fastest algorithm. (D) claims , which is below the needed to read the input; the parity of the total sum has no effect.
Answer: (C)
An element in an array X is called a leader if it is greater than all elements to the right of it in X. The best algorithm to find all leaders in an array.
Answer: (B)
Option B. Go through the array from right to left. Keep track of the largest element seen so far (call it current_max).
- Begin at the rightmost element and store it in
current_max. The rightmost element is always a leader. - Now move left. If an element is bigger than
current_max, then is a leader too. Add it to the list of leaders (or just print it). Then setcurrent_maxto and keep going left.
The time complexity is .
Option A can also be done in linear time with a stack that pops smaller elements, but that needs extra space. The official key is B.
Answer: (B)
An implementation of a queue Q, using two stacks S1 and S2, is given below:
void insert(Q,x){
push (S1,x);
}
void delete(Q){
if (stack-empty (S2))then
if (stack-empty (S1))then{
print("Q is empty");
return;
}
else while (! (stack-empty)(S1)){
x=pop(S1);
push(S2,x);
}
x=pop(S2);
} Let n insert and m(n) delete operations be performed in an arbitrary on an empty queue Q, Let x and y be the number of push and pop operations performed respectively in the processes. Which one of the following is true for all m and n ?Answer: (A)
Let x be the number of pushes and y the number of pops. Assume every delete finds an element.
Each insert pushes once onto S1, giving n pushes. When S2 is empty, a delete moves all of S1 into S2: each moved element costs one pop from S1 and one push onto S2. Then the delete pops one element from S2. Let d be the number of elements moved to S2 over the whole run. Then
and (d pops from S1, and m pops from S2, one per delete).
Bounds on d: every deleted element must pass through S2, so . No more than n elements are ever inserted, so . So , which gives
and .
Both ends are reached. If inserts and deletes alternate, each delete moves just one element, so . If all n inserts come first, the first delete moves all n, so .
Option A gives exactly these bounds. The other options use looser limits, for example follows from because , and follows from .
Answer: (A)
Each insert pushes once onto S1, giving n pushes. When S2 is empty, a delete moves all of S1 into S2: each moved element costs one pop from S1 and one push onto S2. Then the delete pops one element from S2. Let d be the number of elements moved to S2 over the whole run. Then
and (d pops from S1, and m pops from S2, one per delete).
Bounds on d: every deleted element must pass through S2, so . No more than n elements are ever inserted, so . So , which gives
and .
Both ends are reached. If inserts and deletes alternate, each delete moves just one element, so . If all n inserts come first, the first delete moves all n, so .
Option A gives exactly these bounds. The other options use looser limits, for example follows from because , and follows from .
Answer: (A)
An array X of n distinct integers is interpreted as a complete binary tree. The index of the first element of the array is 0. If the root node is at level 0, the level of element , is
Answer: (C)
Note: the value written inside each node is the array index.
With the root at index 0, level holds the indices to . So for an element at level , , which gives . So (C) is correct.
The others fail on small cases:
(A) For , , but lies at level 1.
(B) For , , but lies at level 2.
(D) For , , but lies at level 1.
Answer: (C)