Professional Documents
Culture Documents
a TRUE
b FALSE
c Depends on the implementation
d None of the above
a True
b False
c Both ‘True’ and ‘False’ are printed
d Compilation error
Solution: (c) ‘a--’ post-increments the value of a. Thus, the if statement is executed as the value
of a is considered as 1 which is true. ‘++a’ pre-increment the value of a. Thus, the decremented
Week 4 Assignment Solution
value of a (which is 0) is incremented first and then assigned. So, both the if statements are
executed ad correspondingly both True and False will be printed.
Solution: (c) Condition1 will be evaluated first; condition2 will be evaluated only if the
condition1 is TRUE. This is called the Short-circuited evaluation of the operators
6. Which one of the following is the correct syntax for C Ternary Operator?
a) condition ? expression1 : expression2
b) condition : expression1 ? expression2
c) condition ? expression1 < expression2
d) condition < expression1 ? expression2
Solution: (a) If the condition is true, expression 1 is evaluated. If the condition is false, expression
2 is evaluated.
This is a well-known method to swap the two variables without using a third variable.
8. What will be the output?
#include <stdio.h>
int main()
{
int x=0;
x = printf("3");
printf("%d",x);
return 0;
}
a 11
b 33
c 31
d 13
Solution: (c ) 31
x = printf("3");
First printf prints 3. printf() returns 1. Now the variable x=1; So 1 is printed next.
a 0
12
b 1
11
c 0
00
d 0
11
Solution: (d)
Inside the first printf statement, i++ results in 0 (due to post increment operation). As the left
operand of && operator is zero, the compiler will not evaluate the right operand (i.e., ++j). So, j
is not incremented after the first printf statement. Finally, the result is i=1 and j=1.
10. What will be the value of a, b, and c after the execution of the following
int a = 5, b = 7, c = 111;
c /= ++a * b--;
Week 4 Assignment Solution