GATE 2011
81 questions
MCQ 811-mark 40 · 2-mark 41
Questions 26–30 of 81
Consider the following recursive C function that takes two arguments
unsigned int foo(unsigned int n, unsigned int r) {
if (n > 0) return (n%r + foo (n/r, r ));
else return 0;
}What is the return value of the function foo when it is called as foo (513, 2)?Answer: (D)
When called as foo(n, 2), the function adds up the binary digits of n. So it counts the 1 bits.
513 = 512 + 1 = 1000000001 in binary it has 2 ones.
Answer: 2
513 = 512 + 1 = 1000000001 in binary it has 2 ones.
Answer: 2
Consider the following recursive C function that takes two arguments
What is the return value of the function foo when it is called as foo (345, 10) ?
unsigned int foo(unsigned int n, unsigned int r) {
if (n > 0) return (n%r + foo (n/r, r ));
else return 0;
} What is the return value of the function foo when it is called as foo (345, 10) ?
Answer: (B)
The function foo(n, r) finds the sum of the digits of n written in base r.
So foo(345, 10) = 5 + 4 + 3 = 12
Answer: 12
So foo(345, 10) = 5 + 4 + 3 = 12
Answer: 12
What does the following fragment of C-program print?
char c []="GATE2011";
char *p =c;
printf ("%s", p + p[3]- p[ 1 ]);
char c []="GATE2011";
char *p =c;
printf ("%s", p + p[3]- p[ 1 ]);
Answer: (C)
Here p[3] = 'E' (69) and p[1] = 'A' (65). So the pointer becomes p + 69 65 = p + 4.
The address p + 4 points to the string "2011".
Answer: 2011
The address p + 4 points to the string "2011".
Answer: 2011
We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?
Answer: (B)
A BST always gives its keys in sorted order when we do an in-order traversal. The shape of the tree already decides the order in which the positions are visited in in-order. So the sorted values can go into those positions in only one way.
Answer: 1
Answer: 1
A max-heap is a heap where the value of each parent is greater than or equal to the value of its children. Which of the following is a max-heap?
Answer: (B)
A heap must be a complete binary tree. Each parent must be its children.
- A: Not complete. Node 8 has only one child, but 5 has a child.
- B: Complete. 10 8, 6. 8 4, 5. 6 1, 2. So it is valid.
- C: Node 5 has child 8 not valid.
- D: Root 5 has child 8 not valid.
Answer: B
- A: Not complete. Node 8 has only one child, but 5 has a child.
- B: Complete. 10 8, 6. 8 4, 5. 6 1, 2. So it is valid.
- C: Node 5 has child 8 not valid.
- D: Root 5 has child 8 not valid.
Answer: B