CDS S5
CDS S5
Solutions
1. The minimum number of temporary variables needed to swap the contents of two variables is:
(a) 1 (b) 2
(c) 3 (d) 0
Explanation:
Suppose there are two variables a and b whose values needs to be swapped. Follwing the below
procedure we can swap the values in two variables.
a=a+b
b=a - b
a=a - b
Let a, b be two non-negative integers. The call find(a, find(a, b)) can be used to find the:
(a) maximum of a, b (b) positive difference of a, b
(c) sum of a, b (d) minimum of a, b
3. The following:
printf (“%f”, 9/5);
prints:
(a) 1.8 (b) 1.0
(c) 2.0 (d) none of the above
1
Solution: Option (d)
Explanations:
9/5 yeilds integer 1. Printing 1 as a floating point number prints garbage.
4. If an integer needs two bytes of storage then maximum value of unsigned integer is:
5. If an integer needs two bytes of storage then maximum value of a signed integer is:
Explanation:
In signed magnitude form, one bit is dedicated to store the sign. (e.g., 1 for negative and 0,
otherwise). Only the remaining 15 bits are available to store the magnitude. Hence the answer.
6. printf(“%d”, printf(“tim”));
Explanation:
Any function (including main( )), returns a value to the calling environment. In case of the printf,
it is the characters it printed. So, the output will be tim3 (since it printed the three characters a, b,
c).
results in:
(a) a syntax error (b) a fatal error
(c) segmentation violation (d) printing of 3
Explanation:
The scanf function returns the number of successful matches i.e., 3 in this case.
if a
b
is the input, the output will be:
(a) an error message (b) this can’t be the input
(c) ab (d) a b
9. Let a, b be two positive integers, which of the following options correctly relates / and %?
Solution: Option ( c )
Explanation:
Lets consider two cases when a is a mutiple of b and a is not a multiple of b. (a/b) results in
integer division of a by b. In case a is completely divisible by b then a/b gives the quotient on
dividing a by b and multiplying that with b again gives a, If a is not evenly divisible by b then
(a/b)*b will give the nearest multiple of b which is less than a. To that we add (a%b) which is the
remainder when a is divided by b , to finally get a.
3
10. Consider the following program fragment:
char c= ‘a’
while (c++ ≤ ‘z’)
putchar (xxx);
(a) must yield same value (b) must yield different values
(c) may or may not yield the same value (d) none of the above
Explanation:
If (y-8) is evenly divisible by 9 then the result of both expression will be same.
4
x= (y* =2) + (z= a =y);
printf(“%d”, x);
(a) prints 8
(b) prints 6
(c) prints 6 or 8 depending on the compiler implementation
(d) is syntactically wrong
Solution: Option ( c)
Explanation:
If the compiler uses left associativity then first y will be multiplied by 2 hence value of y will be
made 4. After that the expression z=a=y , will make value of z 4. Hence x is 4+4 =8. If the
compiler uses right associativity then first z will be made 2, after that y will be doubled .
Therefore we get x=4+2 =6. So the result depends on the implementation.
(a) is 3 5 (b) is 4 5
(c) is 4 4 (d) is implementation dependent
(a) x= x –y +1 (b) x= – x –y – 1
(c) x= –x + y +1 (d) x= x – y – 1
Explanation:
5 – 2 – 3 * 5 – 2 will yield 18, if it is treated as (5 – (2 – 3)) * (5 – 2) i.e. if – has precedence over
* and if it associates from the right.
Explanation:
263 in binary form is 100000111. If one tries to print an integer as a character, only the last 8 bits
will be considered, the rest chopped off. So, in this case the ASCII value of 0000111 (i.e.,
decimal 7) will be printed. Look in the ASCII table. It is ringing the bell!