GATE 2026 (Session 1)
65 questions
MCQ 28 · MSQ 24 · NAT 131-mark 30 · 2-mark 35
Questions 61–65 of 65
Consider the recursive functions represented by the following code segment:
Note: Ignore syntax errors (if any) in the function.
int bar(int n) {
if (n == 1) return 0;
else return 1 + bar(n/2);
}
int foo(int n) {
if (n == 1) return 1;
else return 1 + foo(bar(n));
} The smallest positive integer n for which returns is ________. (answer in integer) Note: Ignore syntax errors (if any) in the function.
Answer: 65536
Note that halves again and again until it reaches . It counts the halvings, which is .
for , and . So is plus the number of times is applied (taking ) to until we get .
Find the smallest for each value of :
- : smallest .
- : need , that is : smallest .
- : need : smallest .
- : need : smallest .
- : need : smallest .
Answer: 65536
for , and . So is plus the number of times is applied (taking ) to until we get .
Find the smallest for each value of :
- : smallest .
- : need , that is : smallest .
- : need : smallest .
- : need : smallest .
- : need : smallest .
Answer: 65536
The following sequence corresponds to the preorder traversal of a binary search tree :
The position of the element in the postorder traversal of is ________ . (answer in integer)
Note: The position begins with .
The position of the element in the postorder traversal of is ________ . (answer in integer)
Note: The position begins with .
Answer: 7
Build the BST from the preorder . In preorder the root comes first. Smaller values go to the left subtree and larger values go to the right subtree.
The root is . The left subtree has values : . The right subtree has values : .
Left subtree: the root is . Its left child is , a leaf. Its right subtree is , with root , left child and right child .
Right subtree: the root is . Its left subtree has root with only a right child (no left child). Its right subtree has root with only a left child (no right child).
Now take the postorder (left, right, root) of the whole tree:
The -subtree gives:
The -subtree gives:
Last comes the root .
Full postorder:
Count positions from . Then is element number .
Answer: 7
The root is . The left subtree has values : . The right subtree has values : .
Left subtree: the root is . Its left child is , a leaf. Its right subtree is , with root , left child and right child .
Right subtree: the root is . Its left subtree has root with only a right child (no left child). Its right subtree has root with only a left child (no right child).
Now take the postorder (left, right, root) of the whole tree:
The -subtree gives:
The -subtree gives:
Last comes the root .
Full postorder:
Count positions from . Then is element number .
Answer: 7
Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always successful in creating a process.
int main () {
int i;
for (i = 0; i < 3; i++) {
if (fork() == 0) {
continue;
}
break;
}
printf("Hello!");
return 0;
} The total number of times that the printf statement gets executed is ________. (answer in integer)Answer: 4
Trace the loop. Remember that after , the child gets the return value and runs , so it goes to the next loop round. The parent gets a nonzero value and runs , so it leaves the loop at once.
- : The original process forks. The parent breaks at once, so 1 process reaches printf with . The child continues the loop.
- (in that child): It forks again. This process, now the parent of its own fork, breaks, so the 2nd process reaches printf. Its child continues.
- (in that grandchild): It forks again. This one breaks, so the 3rd process reaches printf. Its child continues.
- (in the great-grandchild): The loop condition fails, so the loop ends by itself with no more forks. This is the 4th process to reach printf.
So exactly different processes each run the statement once.
Answer: 4
- : The original process forks. The parent breaks at once, so 1 process reaches printf with . The child continues the loop.
- (in that child): It forks again. This process, now the parent of its own fork, breaks, so the 2nd process reaches printf. Its child continues.
- (in that grandchild): It forks again. This one breaks, so the 3rd process reaches printf. Its child continues.
- (in the great-grandchild): The loop condition fails, so the loop ends by itself with no more forks. This is the 4th process to reach printf.
So exactly different processes each run the statement once.
Answer: 4
Consider a CPU that has to execute two types of processes. The first type, Actuators (A), requires a CPU burst of seconds. The second type, Controllers (C), requires a CPU burst of seconds. A new process of type A arrives at time , , and (in seconds). Similarly, a new process of type C arrives at time , and (in seconds). The CPU scheduling policy is First Come First Serve (FCFS). The first process of type A starts running at seconds. The average waiting time (in seconds) for the processes is ________. (rounded off to one decimal place)
Answer: 9.5
Write down all processes in the order they arrive. Then run FCFS on them. The CPU serves processes strictly in arrival order and does not preempt.
Add up the waiting times: .
The average waiting time seconds.
Answer: 9.5
Add up the waiting times: .
The average waiting time seconds.
Answer: 9.5
Consider a relational database schema with a relation . If and are the only two candidate keys of the relation , then the number of superkeys of relation is ________. (answer in integer)
Answer: 6
A superkey is any set of attributes that has at least one candidate key inside it. The relation has attributes, which are . The candidate keys are and .
Take the subsets of that contain . Choose the other attributes in any way → subsets.
Take the subsets that contain . Choose the other attributes in any way → subsets.
Some subsets are counted two times. These contain both and , which means they contain . Choose the last attribute in any way → subsets.
Using inclusion-exclusion, the total number of superkeys .
Answer: 6
Take the subsets of that contain . Choose the other attributes in any way → subsets.
Take the subsets that contain . Choose the other attributes in any way → subsets.
Some subsets are counted two times. These contain both and , which means they contain . Choose the last attribute in any way → subsets.
Using inclusion-exclusion, the total number of superkeys .
Answer: 6