GATE 2003
107 questions
MCQ 1071-mark 107
Questions 56–60 of 107
In the following C program fragment, j, k, n and TwoLog_n are integer variables, and A is an array of integers. The variable n is initialized to an integer 3, and TwoLog_n is initialized to the value of
for (k = 3; k <= n; k++)
A[k] = 0;
for (k = 2; k <= TwoLog_n; k++)
for (j = k + 1; j <= n; j++)
A[j] = A[j] || (j % k);
for (j = 3; j <= n; j++)
if (!A[j]) printf("%d", j); The set of number printed by this program fragment isAnswer: (D)
A[j] becomes 1 as soon as j % k is non-zero for some k that the loops use, that is, for some k with (the inner loop starts at j = k + 1). A[j] stays 0 only if j is divisible by every such k. The program prints those j, for .
Let .
Case : k = j - 1 is used, and j % (j-1) = 1, which is non-zero (j - 1 ≥ 2). So A[j] = 1.
Case : j would have to be a multiple of lcm(2, 3, ..., T). That lcm is at least , and , so it is at least , which is more than for . So no j ≤ n qualifies, and A[j] = 1.
So A[j] = 1 for every j from 3 to n, and nothing is printed. The set is empty.
Answer: (D)
Let .
Case : k = j - 1 is used, and j % (j-1) = 1, which is non-zero (j - 1 ≥ 2). So A[j] = 1.
Case : j would have to be a multiple of lcm(2, 3, ..., T). That lcm is at least , and , so it is at least , which is more than for . So no j ≤ n qualifies, and A[j] = 1.
So A[j] = 1 for every j from 3 to n, and nothing is printed. The set is empty.
Answer: (D)
Assume the following C variable declaration
int *A [10], B[10][10];
Of the following expressions
I A[2]
II A[2][3]
III B[1]
IV B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
int *A [10], B[10][10];
Of the following expressions
I A[2]
II A[2][3]
III B[1]
IV B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
Answer: (A)
is an array of 10 pointers to . is a array of . A left side of an assignment must be a modifiable lvalue.
I. is an variable, so it can be assigned a pointer. Valid.
II. is , an variable. Valid.
III. is a whole row, with array type . An array cannot be assigned to, so this is a compile-time error.
IV. is an element. Valid.
So I, II and IV only.
Answer: (A)
I. is an variable, so it can be assigned a pointer. Valid.
II. is , an variable. Valid.
III. is a whole row, with array type . An array cannot be assigned to, so this is a compile-time error.
IV. is an element. Valid.
So I, II and IV only.
Answer: (A)
Let S be a stack of size n 1. Starting with the empty stack, suppose we push the first n natural numbers in sequence, and then perform n pop operations. Assume that Push and Pop operation take X seconds each , and Y seconds elapse between the end of the one such stack operation and the start of the next operation. For m 1, define the stack-life of m as the time elapsed from the end or Push (m) to the start of the pop operation that removes m from S . The average stack-life of an element of this stack is
Answer: (C)
Let us write the stack-life of the element as . The element stays in the stack until elements are pushed and popped. We add one more for the gap between the push of the element and the push of the element. So,
where
The average stack-life is then,
Correct Answer:
where
The average stack-life is then,
Correct Answer:
Consider the function f defined below.
struct item
{
int data;
struct item * next;
};
int f(struct item *p)
{
return ((p == NULL) || (p->next == NULL) ||
((p->data <= p->next->data) &&
f(p->next)));
} For a given linked list p, the function f returns 1 if and only if Answer: (B)
The function returns the value of this expression, which is an OR of three parts:
1. : the empty list gives 1.
2. : a list with one node gives 1.
3. AND : the first node is not bigger than the second, and the same check holds for the rest of the list.
So the function walks down the list and compares every node with the next one. It returns 1 only if every pair satisfies until the list ends. If any pair is out of order, the AND is false and the result is 0. This means the list is sorted in non-decreasing order, which is option (B).
Why the others are wrong:
(A) Empty and one-node lists do return 1, but so does a longer sorted list such as . So (A) is not "if and only if".
(C) For , the check fails, so the result is 0, though the list is non-increasing.
(D) For , the function returns 1, but all the values are the same.
Answer: (B)
1. : the empty list gives 1.
2. : a list with one node gives 1.
3. AND : the first node is not bigger than the second, and the same check holds for the rest of the list.
So the function walks down the list and compares every node with the next one. It returns 1 only if every pair satisfies until the list ends. If any pair is out of order, the AND is false and the result is 0. This means the list is sorted in non-decreasing order, which is option (B).
Why the others are wrong:
(A) Empty and one-node lists do return 1, but so does a longer sorted list such as . So (A) is not "if and only if".
(C) For , the check fails, so the result is 0, though the list is non-increasing.
(D) For , the function returns 1, but all the values are the same.
Answer: (B)
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the inorder transversal sequence of the resultant tree ?
Answer: (C)
An in-order traversal of a binary search tree lists the elements from smallest to largest.
So the correct option is C