Question 1 |
Consider the following C code. Assume that unsigned long int type length is 64
bits.
unsigned long int fun(unsigned long int n){
unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return(sum);
}
The value returned when we call fun with the input 2^{40} is4 | |
5 | |
6 | |
40 |
Question 1 Explanation:
Question 2 |
Consider the C program fragment below which is meant to divide x by y using repeated
subtractions. The variables x, y, q and r are all unsigned int.
while (r >= y) {
r = r - y;
q = q +1;
}
Which of the following conditions on the variables x, y, q and r before the execution of the
fragment will ensure that the loop terminates in a state satisfying the condition x==(y*q+r)?(q==r) && (r==0) | |
(x \gt 0) && (r==x) && (y \gt 0) | |
(q==0) && (r==x) && (y \gt 0) | |
(q==0) && (y \gt 0) |
Question 2 Explanation:
Question 3 |
The following function computes X^{Y} for positive integers X and Y.
int exp(int X,int Y){
int res=1, a=X, b=Y;
while (b!=0){
if(b%2==0){a=a*a; b=b/2;}
else {res=res*a; b=b-1;}
}
return res;
}
Which one of the following conditions is TRUE before every iteration of the loop?X^{Y}=a^{b} | |
(res*a)^{Y}=(res*X)^{b} | |
X^{Y}=res*a^{b} | |
X^{Y}=(res*a)^{b} |
Question 3 Explanation:
Question 4 |
The following function computes the maximum value contained in an integer array p[] of size n (n>=1).
int max(int*p, int n){
int a=0,b=n-1;
while (__________){
if (p[a]<=p[b]){a=a+1;}
else {b=b-1;}
}
return p[a];
}
The missing loop condition isa !=n | |
b !=0 | |
b \gt (a+1) | |
b !=a |
Question 4 Explanation:
Question 5 |
Consider the following pseudo code, where x and y are positive integers.
begin
q := 0
r := x
while r \geq y do
begin
r := r - y
q := q + 1
end
end
The post condition that needs to be satisfied after the program terminates is{r=qx+y \; \wedge \; r \lt y} | |
{x=qy+r \; \wedge \; r \lt y} | |
{y=qx+r \; \wedge \; 0 \lt r \lt y} | |
{ q+1 \lt r-y \; \wedge \; y \gt 0} |
Question 5 Explanation:
Question 6 |
Consider the following pseudo code. What is the total number of multiplications to be performed?
D= 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3
Half of the product of the 3 consecutive integers | |
One-third of the product of the 3 consecutive integers. | |
One-sixth of the product of the 3 consecutive integers | |
None of the above. |
Question 6 Explanation:
Question 7 |
Consider the following C function in which size is the number of elements in the array E:
int MyX(int *E, unsigned int size)
{
int Y = 0;
int Z;
int i, j, k;
for(i = 0; i < size; i++)
Y = Y + E[i];
for(i = 0; i < size; i++)
for(j = i; j < size; j++)
{
Z = 0;
for(k = i; k <= j; k++)
Z= Z + E[k];
if (Z > Y)
Y = Z;
}
return Y;
}
The value returned by the function MyX is themaximum possible sum of elements in any sub-array of array E. | |
maximum element in any sub-array of array E. | |
sum of the maximum elements in all possible sub-arrays of array E. | |
the sum of all the elements in the array E. |
Question 7 Explanation:
Question 8 |
Consider the following segment of C-code:
int j, n;
j = 1;
while (j <=n)
j=j*2;
The number of comparisons made in the execution of the loop for any n \gt 0 is:\left \lceil log_{2}n \right \rceil +1
| |
n | |
\left \lceil log_{2}n \right \rceil | |
\left \lfloor log_{2}n \right \rfloor +1 |
Question 8 Explanation:
Question 9 |
Consider the following C-function in which a[n] and b[m] are two sorted
integer arrays and c[n+m] be another integer array.
I. j\ltm, k=n+j-1, and a [n-1]\ltb[j] if i=n
II. i\ltn, k=m+i-1, and b[m-1]\leqa[i] if j=m
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 condition(s) hold(s) after the termination of the while loop ?I. j\ltm, k=n+j-1, and a [n-1]\ltb[j] if i=n
II. i\ltn, k=m+i-1, and b[m-1]\leqa[i] if j=m
only (I) | |
only (II) | |
either (I) or (II) but not both | |
neither (I) nor (II) |
Question 9 Explanation:
Question 10 |
Consider line number 3 of the following C-program.
int min ( ) { /* Line 1 */
int I, N; /* Line 2 */
fro (I =0, I < N, I++); /* Line 3 */
}
Identify the compiler's response about this line while creating the object-module:No compilation error | |
Only a lexical error | |
Only syntactic errors | |
Both lexical and syntactic errors |
Question 10 Explanation:
There are 10 questions to complete.
It should be b[m] in question number 9 of loops. Please correct it.
Thank You PRAFUL SAMBHAJI RANE,
We have updated the question.
Confusion between option c and d in question number 10 of loops of c programming.
Answer is option A
C language allows only certain words in it- these are called tokens. If we input any invalid tokens it causes lexical error.
eg:
44a44
causes lexical error as in C as an alphabet cannot come in between digits.
Syntactic error is caused by bad combination of tokens. For example, we cannot have a constant on the left hand side of an assignment statement, a for loop must have two expressions inside () separated by semi colon etc.
In the given question, line 3 won’t cause a lexical error or syntactic error. The statement will be treated as a function call with three arguments. Function definition being absent will cause link time error, but the question asks only for compile-time errors. So, (a) must be the answer.
question number 5, options are:-
A. {r=qx+y∧r<y}
B. {x=qy+r∧r<y}
C. {y=qx+r∧0<r<y}
D. {q+10}
please update it. 😉
Thank You Bikash Dutta,
We have updated the option.