GATE 2019
65 questions
MCQ 41 · NAT 241-mark 30 · 2-mark 35
Questions 46–50 of 65
Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let be the placeholders for the non-terminals D, T, L or in the following table:
Which one of the following are the appropriate choices for ?
| Production rule | Semantic action |
| D → T L | X1.type = X2.type |
| T → int | T.type = int |
| T → float | T.type = float |
| L → L1 , id | X3.type = X4.type addType(id.entry, X5.type) |
| L → id | addType(id.entry, X6.type) |
Which one of the following are the appropriate choices for ?
Answer: (A)
Here the type is passed from T to the list L, so it is an inherited attribute.
- In the rule D T L, the list L takes the type of T , so .
- In the rule L , id, the type is handed down to the smaller list , so (and id gets L.type).
Answer:
- In the rule D T L, the list L takes the type of T , so .
- In the rule L , id, the type is handed down to the smaller list , so (and id gets L.type).
Answer:
There are n unsorted arrays: . Assume that n is odd. Each of contains n distinct elements. There are no common elements between any two arrays. The worst-case time complexity of computing the median of the medians of is ________ .
Answer: (C)
Let us go through each part:
- Find the median of one unsorted array of n elements using linear-time selection. It takes per array. For n arrays this gives .
- Now find the median of those n medians: .
The total is . It cannot be lower, since all elements have to be examined.
Answer:
- Find the median of one unsorted array of n elements using linear-time selection. It takes per array. For n arrays this gives .
- Now find the median of those n medians: .
The total is . It cannot be lower, since all elements have to be examined.
Answer:
Let G be any connection, weighted, undirected graph:
I. G has a unique minimum spanning tree if no two edges of G have the same weight.
II. G has a unique minimum spanning tree if, for every cut of G, there is a unique minimum weight edge crossing the cut.
Which of the above two statements is/are TRUE?
I. G has a unique minimum spanning tree if no two edges of G have the same weight.
II. G has a unique minimum spanning tree if, for every cut of G, there is a unique minimum weight edge crossing the cut.
Which of the above two statements is/are TRUE?
Answer: (C)
II is true. Let T be any MST and e an edge of T. Removing e from T splits the vertices into two sets, which is a cut. Let g be the unique lightest edge crossing this cut. If , then and is a lighter spanning tree, which is impossible. So e is the unique lightest edge across this cut.
Now let be two MSTs and e an edge of . Suppose e is not in . Adding e to makes a cycle, and this cycle crosses the cut above at another edge g. Then and is lighter than , which is impossible. So every edge of is in , and .
I is true. If no two edges have the same weight, then every cut has a unique lightest crossing edge. So the condition in II holds, and the MST is unique.
Answer: (C)
Now let be two MSTs and e an edge of . Suppose e is not in . Adding e to makes a cycle, and this cycle crosses the cut above at another edge g. Then and is lighter than , which is impossible. So every edge of is in , and .
I is true. If no two edges have the same weight, then every cut has a unique lightest crossing edge. So the condition in II holds, and the MST is unique.
Answer: (C)
Consider the following snapshot of a system running n concurrent processes. Process is holding instances of a resource R, . Assume that all instances of R are currently in use. Further, for all , process can place a request for at most additional instances of R while holding the instances it already has. Of the n processes, there are exactly two processes p and q such that . Which one of the following conditions guarantees that no other process apart from p and q can complete execution?
Answer: (A)
Processes p and q ask for nothing more, so both can finish. They then give back instances. No instance is free before this, because all are in use.
Any other process k can complete only when it receives more instances. Now suppose . Then the freed instances are too few for any other process, so none of them can complete.
Answer:
Any other process k can complete only when it receives more instances. Now suppose . Then the freed instances are too few for any other process, so none of them can complete.
Answer:
Consider the following statements:
I. The smallest element in a max-heap is always at a leaf node.
II. The second largest element in a max-heap is always a child of the root node.
III. A max-heap can be constructed from a binary search tree in time.
IV. A binary search tree can be constructed from a max-heap in time.
Which of the above statements is/are TRUE?
I. The smallest element in a max-heap is always at a leaf node.
II. The second largest element in a max-heap is always a child of the root node.
III. A max-heap can be constructed from a binary search tree in time.
IV. A binary search tree can be constructed from a max-heap in time.
Which of the above statements is/are TRUE?
Answer: (A)
Let us check each statement one by one:
- I: Any node that has a child is larger than that child. So the smallest element cannot have a child so it is a leaf TRUE.
- II: The 2nd largest element can only be smaller than the root. So its parent has to be the root TRUE.
- III: Build-heap works on any array (for example, the elements of the BST) and takes TRUE.
- IV: Building a BST from a heap in would give a comparison sort (by inorder traversal) FALSE.
Answer: I, II and III
- I: Any node that has a child is larger than that child. So the smallest element cannot have a child so it is a leaf TRUE.
- II: The 2nd largest element can only be smaller than the root. So its parent has to be the root TRUE.
- III: Build-heap works on any array (for example, the elements of the BST) and takes TRUE.
- IV: Building a BST from a heap in would give a comparison sort (by inorder traversal) FALSE.
Answer: I, II and III