GATE 2015 (Session 2)
64 questions
MCQ 42 · NAT 221-mark 29 · 2-mark 35
Questions 21–25 of 64
Consider the following function written in the C programming language.
void foo(char *a){
if ( *a && *a != ' '){
foo(a+1);
putchar(*a);
}
} The output of the above function on input isAnswer: (D)
The function calls itself on the next character first, and only then prints the current character. So the characters come out in reverse order. The calls stop when the string ends or when a space is found.
Only "ABCD" gets processed. It is printed backwards: DCBA
Answer: DCBA
Only "ABCD" gets processed. It is printed backwards: DCBA
Answer: DCBA
Consider a complete binary tree where the left and the right subtrees of the root are max-heaps. The lower bound for the number of operations to convert the tree to a heap is
Answer: (A)
Both subtrees of the root are already max-heaps, so only the root can be out of place. Sift-down repairs it: compare the root with its larger child, swap if the child is bigger, and repeat one level lower. Each step moves down one level, and the height is about , so operations are enough.
Worst case: the root holds the smallest key. It must travel down to a leaf, so about swaps and comparisons are needed, and no method can do with fewer. So the lower bound is .
A bound like or cannot be right, because the work above is only .
Answer: (A)
Worst case: the root holds the smallest key. It must travel down to a leaf, so about swaps and comparisons are needed, and no method can do with fewer. So the lower bound is .
A bound like or cannot be right, because the work above is only .
Answer: (A)
A binary tree T has 20 leaves. The number of nodes in T having two children is _______.
Answer: 19
Let be the numbers of nodes with 0, 1 and 2 children. The tree has nodes, so it has edges (every node except the root has one edge to its parent).
Counting the same edges from the parents' side, there are edges. So , which gives .
Here (the leaves), so .
Answer: 19
Counting the same edges from the parents' side, there are edges. So , which gives .
Here (the leaves), so .
Answer: 19
Consider the following C function.
int fun(int n){
int x=1,k;
if (n==1) return x;
for (k=1; k < n; ++k)
x = x + fun(k) * fun(n-k);
return x;
} The return value of fun(5) is ________.Answer: 51
Recurrence used: , with base case fun(1) = 1. Now find each value in turn:
- fun(2) = 1 + 1 = 2
- fun(3) = 1 + 2 + 2 = 5
- fun(4) = 1 + 5 + 4 + 5 = 15
- fun(5) = 1 + 15 + 10 + 10 + 15 = 51
Answer: 51
- fun(2) = 1 + 1 = 2
- fun(3) = 1 + 2 + 2 = 5
- fun(4) = 1 + 5 + 4 + 5 = 15
- fun(5) = 1 + 15 + 10 + 10 + 15 = 51
Answer: 51
A software requirements specification (SRS) document should avoid discussing which one of the following?
Answer: (C)
An SRS says what the system must do, not how it will be built. So it lists the functional and non-functional requirements and the interfaces the system must offer, including the user interface and the links to third party software.
How the software is organised inside (modules, data structures, algorithms) is design. It belongs in the design document, written after the SRS. So the SRS should avoid the design specification.
Answer: (C)
How the software is organised inside (modules, data structures, algorithms) is design. It belongs in the design document, written after the SRS. So the SRS should avoid the design specification.
Answer: (C)