GATE 2005
203 questions
MCQ 2031-mark 203
Questions 96–100 of 203
The following C function takes two ASCII strings and determines whether one is an anagram of the other. An anagram of a string s is a string obtained by permuting the letters in s.
Choose the correct alternative for statements A and B.
int anagram (char *a, char *b) {
int count [128], j;
for (j = 0; j < 128; j++) count[j] = 0;
j = 0;
while (a[j] && b[j]) {
A;
B;
}
for (j = 0; j < 128; j++) if (count [j]) return 0;
return 1;
} Choose the correct alternative for statements A and B.
Answer: (D)
The loop must walk both strings together, so j has to increase once per pass, and the counts must cancel for an anagram. So A adds 1 for the letter in a, and B subtracts 1 for the letter in b. If every count is 0 at the end, each letter appears equally often in both.
Statement A uses j and statement B must do the j++ after it uses b[j]. This is option (D): count[a[j]]++ and count[b[j++]]--.
(A) never changes j, so the while loop does not end. (B) adds 1 for both strings, so the counts never cancel, and j also never moves. (C) does j++ inside A, so B then uses the next index of b and compares a[j] with b[j+1].
Example a = ISS, b = SIS: count[I] +1, count[S] -1; then count[S] +1, count[I] -1; then count[S] +1 and count[S] -1. All counts are 0, so it returns 1.
Answer: (D)
Statement A uses j and statement B must do the j++ after it uses b[j]. This is option (D): count[a[j]]++ and count[b[j++]]--.
(A) never changes j, so the while loop does not end. (B) adds 1 for both strings, so the counts never cancel, and j also never moves. (C) does j++ inside A, so B then uses the next index of b and compares a[j] with b[j+1].
Example a = ISS, b = SIS: count[I] +1, count[S] -1; then count[S] +1, count[I] -1; then count[S] +1 and count[S] -1. All counts are 0, so it returns 1.
Answer: (D)
Let a and b be two sorted arrays containing n integers each, in non-decreasing order. Let c be a sorted array containing 2n integers obtained by merging the two arrays a and b. Assuming the arrays are indexed starting from 0, consider the following four statements
I.
II.
III.
IV.
Which of the following is TRUE?
I.
II.
III.
IV.
Which of the following is TRUE?
Answer: (C)
Count positions in the merged array c (indices from 0). Given .
III is true. The elements are all at most , and so are (each is at most ). That is elements that are at most . So the th smallest element, , is at most .
II is true. An element smaller than is either in (at most elements) or in . An element smaller than is also smaller than , so (at most elements). So at most elements are smaller than . Then , the th smallest, cannot be smaller than .
I is false. For and : , but .
IV is false. For and : , but gives .
Only II and III are true.
Answer: (C)
III is true. The elements are all at most , and so are (each is at most ). That is elements that are at most . So the th smallest element, , is at most .
II is true. An element smaller than is either in (at most elements) or in . An element smaller than is also smaller than , so (at most elements). So at most elements are smaller than . Then , the th smallest, cannot be smaller than .
I is false. For and : , but .
IV is false. For and : , but gives .
Only II and III are true.
Answer: (C)
A program P reads in 500 integers in the range [0,100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies?
Answer: (A)
Only the scores above 50 are needed, that is 51, 52, ..., 100. That is different scores, so one counter for each is enough: an array of 50 numbers.
Use as the counter, so counts the score 51 and counts the score 100. After reading each score, if it is above 50, add 1 to its counter. Then print the 50 counters.
An array of 100 or 500 numbers wastes space, and the count of students (500) does not decide the number of counters.
Answer: (A)
Use as the counter, so counts the score 51 and counts the score 100. After reading each score, if it is above 50, add 1 to its counter. Then print the 50 counters.
An array of 100 or 500 numbers wastes space, and the count of students (500) does not decide the number of counters.
Answer: (A)
A function defined on stacks of integers satisfies the following properties. and for all stacks and integers .
If a stack contains the integers 2, -3, 2, -1, 2 in order from bottom to top, what is ?
Answer: (C)
Each push gives , where i is the pushed integer and f(old) is the value for the stack before the push. Start with and push 2, -3, 2, -1, 2 in that order (bottom to top).
push 2:
push -3:
push 2:
push -1:
push 2:
So .
Answer: (C)
push 2:
push -3:
push 2:
push -1:
push 2:
So .
Answer: (C)
The following C function takes a singly-linked list of integers as a parameter and rearranges the elements of the list. The list is represented as pointer to a structure. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given order. What will be the contents of the list after the function completes execution?
struct node {int value; struct node *next;};
void rearrange (struct node *list) {
struct node *p, *q;
int temp;
if (!list || !list -> next) return;
p = list; q = list -> next;
while (q) {
temp = p -> value;
p -> value = q -> value;
q -> value = temp;
p = q -> next;
q = p ? p -> next : 0;
}
} Answer: (B)
Each pass of the loop swaps the values of the nodes p and q, where . Then p jumps two nodes ahead () and q becomes the node after it (). So the values are swapped in adjacent pairs.
Start: 1, 2, 3, 4, 5, 6, 7
Pass 1: swap nodes 1 and 2: 2, 1, 3, 4, 5, 6, 7
Pass 2: p at the 3rd node, q at the 4th: 2, 1, 4, 3, 5, 6, 7
Pass 3: p at the 5th node, q at the 6th: 2, 1, 4, 3, 6, 5, 7
Then p is the 7th node and , so the loop stops. The last value 7 has no partner, so it stays.
Result: 2, 1, 4, 3, 6, 5, 7
Answer: (B)
Start: 1, 2, 3, 4, 5, 6, 7
Pass 1: swap nodes 1 and 2: 2, 1, 3, 4, 5, 6, 7
Pass 2: p at the 3rd node, q at the 4th: 2, 1, 4, 3, 5, 6, 7
Pass 3: p at the 5th node, q at the 6th: 2, 1, 4, 3, 6, 5, 7
Then p is the 7th node and , so the loop stops. The last value 7 has no partner, so it stays.
Result: 2, 1, 4, 3, 6, 5, 7
Answer: (B)