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:
There are 5 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.