GATE 2002
56 questions
MCQ 561-mark 56
Questions 31–35 of 56
The results returned by function under value-result and reference parameter passing conventions
Answer: (D)
In call by reference, the called function works on the memory locations of the passed variables, so every change takes effect at once.
In call by value-result, the called function works on copies. The final values are copied back to the original variables only when the function returns normally.
If an exception ends the function before it returns, call by reference has already changed the original variables, while call by value-result has not copied anything back. So the results may differ when an exception occurs. In an ordinary function that returns normally, with no aliasing, both give the same result, so (C) is wrong. A loop by itself does not create a difference, so (B) is wrong.
Answer: (D)
In call by value-result, the called function works on copies. The final values are copied back to the original variables only when the function returns normally.
If an exception ends the function before it returns, call by reference has already changed the original variables, while call by value-result has not copied anything back. So the results may differ when an exception occurs. In an ordinary function that returns normally, with no aliasing, both give the same result, so (C) is wrong. A loop by itself does not create a difference, so (B) is wrong.
Answer: (D)
Consider the following algorithm for searching for a given number x in an unsorted
array A[1.....n] having n distinct values :
(1) Choose an i uniformly at random from [1....n]
(2) If A[i] = x then stop else Goto 1;
Assuming that x is present A, What is the expected number of comparisons made by the algorithm before it terminates?
(1) Choose an i uniformly at random from [1....n]
(2) If A[i] = x then stop else Goto 1;
Assuming that x is present A, What is the expected number of comparisons made by the algorithm before it terminates?
Answer: (A)
Each try picks one of the positions at random, so it finds with probability , independent of earlier tries. The expected number of comparisons is the expected number of tries until the first success:
Multiply by :
Subtract the second line from the first:
The right side is a geometric series with first term and ratio , so it sums to . The left side is . So and .
This is the same as the mean of the geometric distribution.
Answer: (A)
Multiply by :
Subtract the second line from the first:
The right side is a geometric series with first term and ratio , so it sums to . The left side is . So and .
This is the same as the mean of the geometric distribution.
Answer: (A)
Consider the following declaration of a two-dimensional array in C :
Char a[100][100]
Assuming that the main memory is byte-addressable and that array is stored starting from memory address 0, the address of a [40] [50] is
Char a[100][100]
Assuming that the main memory is byte-addressable and that array is stored starting from memory address 0, the address of a [40] [50] is
Answer: (B)
C stores arrays in row-major order. The address of is
where BA is the base address, NC is the number of columns and is the size of one element. Here , , byte (char), and indices start from 0.
The value 5040 would come from column-major order (), which C does not use.
Answer: (B)
where BA is the base address, NC is the number of columns and is the size of one element. Here , , byte (char), and indices start from 0.
The value 5040 would come from column-major order (), which C does not use.
Answer: (B)
To evaluate an expression without any embedded function calls
Answer: (A)
An expression with no function calls, for example , can be evaluated in two steps that run one after the other:
Step 1: Convert infix to postfix using a stack. The stack holds the operators. At the end of this step the stack is empty.
Step 2: Evaluate the postfix expression using a stack. The stack holds the operands. This is the same stack, reused after step 1 has finished.
So one stack is enough. (B) is not needed, because the two steps are never running at the same time. (C) is wrong because the height of the expression tree does not decide how many stacks are needed. (D) is wrong because a stack is much less than a Turing machine and is already enough.
Answer: (A)
Step 1: Convert infix to postfix using a stack. The stack holds the operators. At the end of this step the stack is empty.
Step 2: Evaluate the postfix expression using a stack. The stack holds the operands. This is the same stack, reused after step 1 has finished.
So one stack is enough. (B) is not needed, because the two steps are never running at the same time. (C) is wrong because the height of the expression tree does not decide how many stacks are needed. (D) is wrong because a stack is much less than a Turing machine and is already enough.
Answer: (A)
In the worst case, the number of comparisons needed to search a single linked list of length n for a given element is
Answer: (D)
A & C are not correct. We cannot do binary search efficiently {it takes O(n)} in a linked list.
B looks like the average case, but the question asks for the worst case.
The worst case is when the element is not in the list. Then we may search the whole list and compare with every element. So the answer is (D).