Quiz On c2
Quiz On c2
Solution: (a) Operator precedence determines which operator is performed first in an expression with more
than one operator with different precedence, whereas associativity is used when two operators of same
precedence appear in an expression
Solution: (b) All operators with same precedence have same associativity-- This is necessary, otherwise there
won’t be any way for compiler to decide evaluation order of expressions which have two operators of same
precedence and different associativity.
Solution: (d) Modulo (%) gives the reminder and division (/) gives the quotient of a division operation. As
7/5 is an integer division, so the output will be 1. Here, w is a floating variable, so it will store the value of
1.000000. Thus z and w would be 2 and 1.000000 respectively.
#include <stdio.h>
int main ()
{
int a = 4, b = 15, c = 29;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}
a) TRUE
b) FALSE
c) Syntax Error
d) Compilation Error
Solution: (b) FALSE: (c > b > a) is treated as ((c > b) > a), associativity of '>'
is left to right. Therefore, the value becomes ((29 > 15) > 4) which becomes (1 > 4) thus FALSE.
return 0;
}
a) i is 10
b) i is 15
c) i is 20
d) i is not present
Solution: (d) i is not present
None of the conditionals are true hence final else is executed
Any non-zero value is treated as true for condition. Consider the expressions: if( (-10 && 10) &&
(3>4) )
=if( (1) && (1) )
=if(1)
11. What will be the output of the following C code? [Assume it’s a 32-bit system]
#include <stdio.h>
int main()
{
int x= 70;
printf("%c", x);
return 0;
}
a) 70
b) F
c) 70.0
d) Compilation error
Solution: (b) Assume it is a 32-bit machine. So, the compiler allocates 2 bytes for the integer x in its
memory. So it will be 0 0 0 0 0 0 0 0 | 0 1 0 0 0 1 1 0 The size of a character is of 1 byte in C, so it will take
only the first byte from the right hand side i.e. 0 1 0 0 0 1 1 0 and print the corresponding ASCII value i.e.
F.
WEEK 3 ASSIGNMENT SOLUTION
Solution: (a)
!(!x) = x i.e. x and y both are non-Zero value and equal to 1 (True). Therefore, the statement is True.
Solution: (c)
x-x/y*y%z here the operators /, * and % have the same precedence. So they will be calculated from left to
right.
WEEK 3 ASSIGNMENT SOLUTION
a) 0
b) 1
c) 0.5
d) Compilation error
Solution: (b)
x = (7+6)%5/2 = 13%5/2 (As ( operator have higher precedence) = 3/2 (As % and / has same precedence
and will be operator from left to right) = 1 (as only x is an integer)