GATE 2004
199 questions
MCQ 1991-mark 199
Questions 96–100 of 199
Two matrices M1 and M2 are to be stored in arrays A and B respectively. Each array can be stored either in row-major or column-major order in contiguous memory locations. The time complexity of an algorithm to compute M1 x M2 will be
Answer: (D)
The question asks about time complexity, which counts basic operations. Reading one array element takes constant time, , whether the array is stored in row-major or column-major order. The algorithm multiplies the same numbers in the same number of steps either way. So the time complexity does not depend on the storage scheme.
The storage order would change only the actual running time, through cache behaviour. For example, row-major A with column-major B reads both arrays one after another in memory, so option (A) could be faster in practice. But that is not a change in time complexity.
Answer: (D)
The storage order would change only the actual running time, through cache behaviour. For example, row-major A with column-major B reads both arrays one after another in memory, so option (A) could be faster in practice. But that is not a change in time complexity.
Answer: (D)
A program attempts to generate as many permutations as possible of the string, 'abcd' by pushing the characters a,b,c,d in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program?
Answer: (D)
Characters go onto the stack in the order a, b, c, d. A pop can happen at any time, so the output is the pop order.
1. abcd: push a, pop a; push b, pop b; push c, pop c; push d, pop d. This is possible.
2. dcba: push a, b, c, d, then pop all. The pops give d, c, b, a. This is possible.
3. cbad: push a, b, c, then pop three times to get c, b, a. Then push d and pop d. This is possible.
4. cabd: to pop c first, a and b must already be pushed, so the stack holds a, b, c with c on top and a at the bottom. After c is popped, the top is b, and a is below it. So b has to come out before a, and the order c, a, b is not possible.
Answer: (D)
1. abcd: push a, pop a; push b, pop b; push c, pop c; push d, pop d. This is possible.
2. dcba: push a, b, c, d, then pop all. The pops give d, c, b, a. This is possible.
3. cbad: push a, b, c, then pop three times to get c, b, a. Then push d and pop d. This is possible.
4. cabd: to pop c first, a and b must already be pushed, so the stack holds a, b, c with c on top and a at the bottom. After c is popped, the top is b, and a is below it. So b has to come out before a, and the order c, a, b is not possible.
Answer: (D)
Assume that the operators +, -, x , are left associative and ^ is right associative. the order of precedence (from highest to lowest) is ^, x , +, -. The postfix expression corresponding to the infix expression a + b x c-d^e^f is
Answer: (A)
Precedence from highest: , , , . The operator is right associative, the others are left associative. So the expression groups as , where .
Scan left to right with an operator stack. Operands go straight to the output.
a: output a
+: stack empty, push +
b: output b
: higher than +, push
c: output c. Output so far:
: lower than and than +, so pop then +. Output: . Push
d: output d
: higher than , push
e: output e
: same as the on top and right associative, so push (do not pop)
f: output f
End: pop everything, giving . Result: .
(B) writes , which is , but is right associative. (C) adds a and b first, which ignores that x binds tighter than +. (D) puts operators next to the first operands and ends with operands, so it is not a valid postfix form.
Answer: (A)
Scan left to right with an operator stack. Operands go straight to the output.
a: output a
+: stack empty, push +
b: output b
: higher than +, push
c: output c. Output so far:
: lower than and than +, so pop then +. Output: . Push
d: output d
: higher than , push
e: output e
: same as the on top and right associative, so push (do not pop)
f: output f
End: pop everything, giving . Result: .
(B) writes , which is , but is right associative. (C) adds a and b first, which ignores that x binds tighter than +. (D) puts operators next to the first operands and ends with operands, so it is not a valid postfix form.
Answer: (A)
The best data structure to check whether an arithmetic expression has balanced parentheses is a
Answer: (B)
Use a stack. Read the expression from left to right.
- On a left parenthesis, push it.
- On a right parenthesis, pop from the stack. If the stack is empty at that moment, there is a right parenthesis with no match (as in ")("), so the expression is not balanced.
- At the end of the expression, the stack must be empty. If it is not, some left parenthesis has no match.
A right parenthesis closes the most recently opened parenthesis that is still open. That is the last one pushed, and a stack gives the last item pushed first (LIFO). A queue would give the oldest one, which is wrong for nested parentheses such as ((a)(b)).
Answer: (B)
- On a left parenthesis, push it.
- On a right parenthesis, pop from the stack. If the stack is empty at that moment, there is a right parenthesis with no match (as in ")("), so the expression is not balanced.
- At the end of the expression, the stack must be empty. If it is not, some left parenthesis has no match.
A right parenthesis closes the most recently opened parenthesis that is still open. That is the last one pushed, and a stack gives the last item pushed first (LIFO). A queue would give the oldest one, which is wrong for nested parentheses such as ((a)(b)).
Answer: (B)
A single array A[1...MAXSIZE] is used to implement two stacks. The two stacks grow from opposite ends of the array. Variables top1 and top 2 (top1 top 2) point to the location of the topmost element in each of the stacks. If the space is to be used efficiently, the condition for "stack full" is
Answer: (D)
Stack 1 grows up from index 1, and stack 2 grows down from index MAXSIZE. When both are empty, and .
To use the space efficiently, either stack must be able to take any free cell, so one stack can grow large while the other stays small. The array is full only when there is no free cell between the two tops, which means the tops are next to each other: , that is .
(A) is true only when each stack holds exactly half, so an uneven split that fills the array is not detected. (B) says , which can hold while free cells remain between the tops. (C) calls the array full when stack 1 reaches half of it, even if stack 2 is empty, or when stack 2 has just one element, so it wastes space.
Answer: (D)
To use the space efficiently, either stack must be able to take any free cell, so one stack can grow large while the other stays small. The array is full only when there is no free cell between the two tops, which means the tops are next to each other: , that is .
(A) is true only when each stack holds exactly half, so an uneven split that fills the array is not detected. (B) says , which can hold while free cells remain between the tops. (C) calls the array full when stack 1 reaches half of it, even if stack 2 is empty, or when stack 2 has just one element, so it wastes space.
Answer: (D)