GATE 2016 (Session 1)
65 questions
MCQ 39 · NAT 261-mark 30 · 2-mark 35
Questions 46–50 of 65
What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?
a=3;
void n(x){x=x*a; print(x);}
void m(y){a=1;a=y-a;n(a);print(a);}
void main(){m(a);}Answer: (D)
Treat "a = 1" in m as a local a that belongs to m. The name y points to the global a, which is 3.
- In m: local a is 1. Then a = y a = 3 1 = 2.
- In n(a): x points to the local a of m. Under dynamic scoping, the a used inside n is also the a of m, because it is the latest active a. So x = x a = 2 2 = 4. Since x is the same variable as a, a becomes 4. It prints 4.
- Back in m: print(a) prints 4.
Note: if a = 1 is read as an assignment to the global a (which y aliases), the output would be 0, 0, which is not an option. The official key D assumes the local reading above.
Answer: 4,4
- In m: local a is 1. Then a = y a = 3 1 = 2.
- In n(a): x points to the local a of m. Under dynamic scoping, the a used inside n is also the a of m, because it is the latest active a. So x = x a = 2 2 = 4. Since x is the same variable as a, a becomes 4. It prints 4.
- Back in m: print(a) prints 4.
Note: if a = 1 is read as an assignment to the global a (which y aliases), the output would be 0, 0, which is not an option. The official key D assumes the local reading above.
Answer: 4,4
An operator delete(i) for a binary heap data structure is to be designed to delete the item in the i-th node.Assume that the heap is implemented in an array and i refers to the i-th index of thearray.If the heap tree has depth d (number of edges on the path from the root to the farthest leaf),the n what is the time complexity to re-fix the heap efficiently after the removal of the element?
Answer: (B)
To delete node i, put the last element in position i. Then sift it either up or down. In both cases it moves along a single path from the root to a leaf, so it goes at most d levels . It can need all d levels, so the cost is not .
Answer: O(d) but not O(1)
Answer: O(d) but not O(1)
Consider the weighted undirected graph with 4 vertices,where the weigh to edge {i, j} is given by the entry Wij in the matrix W.
The largest possible integer value of x, for which at least one shortest path between some pair of vertices will contain the edge with weight x is ____.
The largest possible integer value of x, for which at least one shortest path between some pair of vertices will contain the edge with weight x is ____.
Answer: 12
Weights: .
Pair (3, 4). The routes that avoid the edge are 3-2-1-4 with weight , 3-1-4 with , and 3-2-4 with . (3-1-2-4 is longer.) The shortest is 12, so the edge of weight lies on a shortest 3-4 path (a tie counts) only if .
Other pairs. For (1, 3) the route through is 1-4-3 with weight , and the route 1-2-3 weighs 7, so this needs . For (2, 4) the route 2-3-4 weighs and 2-1-4 weighs 7, so again . For (1, 4), (2, 3) and (1, 2) a route through is never shorter than the best other route. So these pairs give smaller bounds.
So the largest integer is 12.
Answer: 12
Pair (3, 4). The routes that avoid the edge are 3-2-1-4 with weight , 3-1-4 with , and 3-2-4 with . (3-1-2-4 is longer.) The shortest is 12, so the edge of weight lies on a shortest 3-4 path (a tie counts) only if .
Other pairs. For (1, 3) the route through is 1-4-3 with weight , and the route 1-2-3 weighs 7, so this needs . For (2, 4) the route 2-3-4 weighs and 2-1-4 weighs 7, so again . For (1, 4), (2, 3) and (1, 2) a route through is never shorter than the best other route. So these pairs give smaller bounds.
So the largest integer is 12.
Answer: 12
Let G be a complete undirected graph on 4 vertices, having 6 edges with weights being 1,2,3,4,5, and 6. The maximum possible weight that a minimum weight spanning tree of G can haveis .
Answer: 7
A spanning tree on 4 vertices has 3 edges. Kruskal takes the edges 1 and 2 first, because two edges cannot form a cycle.
The third edge is at most 4. If edge 3 closes a cycle with 1 and 2, then edges 1, 2, 3 form a triangle on three vertices. Edge 4 cannot join two of these three vertices (all three possible edges there are already 1, 2, 3), so it has an endpoint at the fourth vertex and is taken. If edge 3 does not close a cycle, it is taken. So the third MST edge is 3 or 4, never more.
4 is possible. Place the edges 1, 2, 3 as a triangle. Then the MST is 1, 2 and 4.
Maximum MST weight .
Answer: 7
The third edge is at most 4. If edge 3 closes a cycle with 1 and 2, then edges 1, 2, 3 form a triangle on three vertices. Edge 4 cannot join two of these three vertices (all three possible edges there are already 1, 2, 3), so it has an endpoint at the fourth vertex and is taken. If edge 3 does not close a cycle, it is taken. So the third MST edge is 3 or 4, never more.
4 is possible. Place the edges 1, 2, 3 as a triangle. Then the MST is 1, 2 and 4.
Maximum MST weight .
Answer: 7
G = (V,E) is an undirected simple graph in which each edge has a distinct weight,and e is a particular edgeof G. Which of the following statements about the minimum spanning trees (MSTs) of G is/are TRUE?
I. If e is the lightest edge of some cycle in G, then every MST of G includes e
II. If e is the heaviest edge of some cycle in G, then every MST of G excludes e
I. If e is the lightest edge of some cycle in G, then every MST of G includes e
II. If e is the heaviest edge of some cycle in G, then every MST of G excludes e
Answer: (B)
- I is false. Take vertices a, b, c, d with edges ab = 5, bc = 6, ca = 7, ad = 1, bd = 2. The edge ab is the lightest edge of the cycle a-b-c. But Kruskal takes ad (1) and bd (2), then rejects ab (5) because it closes the cycle a-d-b, and then takes bc (6). The MST is {ad, bd, bc} and it does not include ab.
- II is true. Let e be the heaviest edge of a cycle C, and suppose an MST T contains e. Removing e from T splits it into two parts. The cycle C must cross between the two parts at another edge e'. Since the weights are distinct, , so is a lighter spanning tree. This is impossible, so no MST contains e.
Answer: (B)
- II is true. Let e be the heaviest edge of a cycle C, and suppose an MST T contains e. Removing e from T splits it into two parts. The cycle C must cross between the two parts at another edge e'. Since the weights are distinct, , so is a lighter spanning tree. This is impossible, so no MST contains e.
Answer: (B)