GATE 2014 (Session 3)
65 questions
MCQ 47 · NAT 181-mark 30 · 2-mark 35
Questions 51–55 of 65
Consider the pseudocode given below. The function Dosomething () takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of the tree is of type treeNode.
typedef struct treeNode* treeptr;
struct treeNode
{
treeptr leftMostChild, rightSibling;
};
int DoSomething (treeptr tree)
{
int value=0;
if (tree != NULL) {
if (tree->leftMostChild == NULL)
value = 1;
else
value = DoSomething(tree->leftMostChild);
value = value + DoSomething(tree->rightSibling);
}
return(value);
} When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned by the function corresponds to theAnswer: (D)
Look at what the function does at each node. If the node has no child, it gives 1. If the node has a child, the value comes from its children. After that, the count for the right siblings is added.
So the function counts only the nodes that have no children, which are the leaves.
Answer: number of leaf nodes in the tree
So the function counts only the nodes that have no children, which are the leaves.
Answer: number of leaf nodes in the tree
Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.
int ProcessArray(int *listA, int x, int n)
{
int i, j, k;
i = 0;
j = n-1;
do {
k = (i+j)/2;
if (x <= listA[k])
j = k-1;
if (listA[k] <= x)
i = k+1;
}while (i <= j);
if (listA[k] == x)
return(k);
else
return -1;
} Which one of the following statements about the function ProcessArray is CORRECT?Answer: (B)
Here k is the middle index of the range [i, j]. If x A[k], the search moves to the left half. If x A[k], the search moves to the right half. If x equals A[k], both updates run, so i becomes j, the loop stops, and A[k] = x.
This is binary search. It returns k when x is found and 1 when it is not found, and the loop always ends.
Answer: It is an implementation of binary search
This is binary search. It returns k when x is found and 1 when it is not found, and the loop always ends.
Answer: It is an implementation of binary search
An instruction pipeline has five stages, namely, instruction fetch (IF), instruction decode and register fetch (ID/RF), instruction execution (Ex), memory access (MEM), and register write back (WB) with stage latencies 1 ns, 2.2 ns, 2 ns, 1 ns, and 0.75 ns, respectively (ns stands for nanoseconds). To gain in terms of frequency, the designers have decided to split the ID/RF stage into three stages (ID, RF1, RF2) each of latency 2.2/3 ns. Also, the Ex stage is split into two stages (Ex1, Ex2) each of latency 1 ns. The new design has a total of eight pipeline stages. A program has 20% branch instructions which execute in the Ex stage and produce the next instruction pointer at the end of the Ex stage in the old design and at the end of the Ex2 stage in the new design. The IF stage stalls after fetching a branch instruction until the next instruction pointer is computed. All instructions other than the branch instruction have an average CPI of one in both the designs. The execution times of this program on the old and the new design are P and Q nanoseconds, respectively. The value of P/Q is __________
Answer: 1.5 to 1.6
Let the program have instructions. Then and the same for , so cancels in .
Old design: the clock is the slowest stage, 2.2 ns. A branch gives its next pointer at the end of EX, which is stage 3, so IF stalls for 2 cycles.
CPI = 0.8 1 + 0.2 3 = 1.4, so ns.
New design: the clock is 1 ns. A branch gives its next pointer at the end of Ex2, which is stage 6 (IF, ID, RF1, RF2, Ex1, Ex2), so IF stalls for 5 cycles.
CPI = 0.8 1 + 0.2 6 = 2, so ns.
Answer: 1.54
Old design: the clock is the slowest stage, 2.2 ns. A branch gives its next pointer at the end of EX, which is stage 3, so IF stalls for 2 cycles.
CPI = 0.8 1 + 0.2 3 = 1.4, so ns.
New design: the clock is 1 ns. A branch gives its next pointer at the end of Ex2, which is stage 6 (IF, ID, RF1, RF2, Ex1, Ex2), so IF stalls for 5 cycles.
CPI = 0.8 1 + 0.2 6 = 2, so ns.
Answer: 1.54
The memory access time is 1 nanosecond for a read operation with a hit in cache, 5 nanoseconds for a read operation with a miss in cache, 2 nanoseconds for a write operation with a hit in cache and 10 nanoseconds for a write operation with a miss in cache. Execution of a sequence of instructions involves 100 instruction fetch operations, 60 memory operand read operations and 40 memory operand write operations. The cache hit-ratio is 0.9. The average memory access time (in nanoseconds) in executing the sequence of instructions is __________.
Answer: 1.68
Total reads = 100 instruction fetches + 60 operand reads = 160. Total writes = 40.
- Average read time = 0.9 1 + 0.1 5 = 1.4 ns
- Average write time = 0.9 2 + 0.1 10 = 2.8 ns
Overall average:
Answer: 1.68
- Average read time = 0.9 1 + 0.1 5 = 1.4 ns
- Average write time = 0.9 2 + 0.1 10 = 2.8 ns
Overall average:
Answer: 1.68
The above synchronous sequential circuit built using JK flip-flops is initialized with = 000. The state sequence for this circuit for the next 3 clock cycles is
Answer: (C)
The circuit gives these inputs: ; ; .
- State 000: J2K2 = 10 (set), J1K1 = 01 (reset), J0K0 = 01 (reset). Next state 100
- State 100: J2K2 = 10, J1K1 = 10 (set), J0K0 = 01. Next state 110
- State 110: J2K2 = 00 (hold 1), J1K1 = 10, J0K0 = 11 (toggle). Next state 111
Answer: 100, 110, 111
- State 000: J2K2 = 10 (set), J1K1 = 01 (reset), J0K0 = 01 (reset). Next state 100
- State 100: J2K2 = 10, J1K1 = 10 (set), J0K0 = 01. Next state 110
- State 110: J2K2 = 00 (hold 1), J1K1 = 10, J0K0 = 11 (toggle). Next state 111
Answer: 100, 110, 111