Programming and data structure miscellaneous
- In the following C program fragment j, k n and Two Log_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 2 *
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 numbers printed by this program fragment is
-
View Hint View Answer Discuss in Forum
If (!A[J]) condition was as follows
(!A[J]) = = 0)
Then, it prints a non zero value in the array
{m | m ≤ n, (Ei) [m = i2]}Correct Option: B
If (!A[J]) condition was as follows
(!A[J]) = = 0)
Then, it prints a non zero value in the array
{m | m ≤ n, (Ei) [m = i2]}
- Assume the following C variable declaration
int* A [10], B [10] [10];
Of the following expressions which will not give compile time errors if used as left hand sides of assignment statements in a C program ?
(i) A [2]
(ii) A [2] [3]
(iii) B [1]
(iv) B [2] [3]
-
View Hint View Answer Discuss in Forum
A is an array of pointers to int, and B is a 2D array
* A [2] = Can take a pointer
* A [2] [3] = Can take an Int
* B [1] = It is the base address of array and it cannot be changed, as array in C is a Constant pointer. We can not make B the point some other array. So this is false.
* B[2][3] = It can take an integer.
So, option (a) is correct.Correct Option: A
A is an array of pointers to int, and B is a 2D array
* A [2] = Can take a pointer
* A [2] [3] = Can take an Int
* B [1] = It is the base address of array and it cannot be changed, as array in C is a Constant pointer. We can not make B the point some other array. So this is false.
* B[2][3] = It can take an integer.
So, option (a) is correct.
- A single array A [1...MAXSIZE] is used to implement two stacks. The two stack graw from opposite ends of the array. Variable top 1 and top 2 (top 1 < top 2) point to the location of the topmost element in the each of the stacks. If the space is to be used efficiently, the condition for stack full is
-
View Hint View Answer Discuss in Forum
Let take MAXSIZE =10
Here the stack will be fuel if both top 1 & top 2 are at the adjacent index values i.e., their difference is 1.
So top 1 = top 2 – 1
Here (d) is correct option.Correct Option: D
Let take MAXSIZE =10
Here the stack will be fuel if both top 1 & top 2 are at the adjacent index values i.e., their difference is 1.
So top 1 = top 2 – 1
Here (d) is correct option.
- 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 ?
-
View Hint View Answer Discuss in Forum
We have to store frequencies of scores above 50. That is number of students having score 51, number of students having score 52 and so on. For that an array of size 50 is the best option.
Correct Option: A
We have to store frequencies of scores above 50. That is number of students having score 51, number of students having score 52 and so on. For that an array of size 50 is the best option.
- Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n + m] be another array. void xyz (int a [], int b [], int c [])
{
int i, j, k;
i = j = k = 0;
while ((i < n) && (j < m))
if (a [i] < b [j] c [k++] = a[i++];
else c[k++] = b[j++];
}
Which of the following conditions hold(s) after the termination of the while loop ?
(i) j < m, k = n + j – 1 and a [n – 1] < b[j], if i = n
(ii) i < n, k = m + i – 1 and b [m – 1] ≤ a[i], if j = m
-
View Hint View Answer Discuss in Forum
The condition (i) is true if the last inserted element in c[] is from a[] and condition (ii) is true if the last inserted element is from b[].
Correct Option: C
The condition (i) is true if the last inserted element in c[] is from a[] and condition (ii) is true if the last inserted element is from b[].