ASSIGNMENT 3
1. The precedence of arithmetic operators is (from highest to lowest)
a) %, *, /, +, –
b) %, +, /, *, –
c) +, -, %, *, /
d) %, +, -, *, /
Solution: (a) The precedence order follows the first option (a)
2. What is the output of the following program? (% indicates modulo operation,
which results the reminder of a division operation)
#include <stdio.h>
int main()
{
float i = -3.0;
int k = i % 2;
printf("%d", k);
return 0;
}
a) -1
b) 1
c) 0
d) Compilation error
Solution: (d) In C, the % operator only works with integers, but i is declared as a float (-
3.0), then i % 2 is invalid because i is a floating-point value. The compiler will show
error at this step.
3. Find the output of the following C code.
#include<stdio.h>
int main()
{
int a=10, b=3, c=2, d=4, result;
result=a+a*-b/c%d+c*d;
printf("%d", result);
return 0;
}
a) -42
b) 24
c) 15
d) -34
ASSIGNMENT 3
Solution: (c) Following the Operator Precedence and Associativity rules we can
conclude that the operation steps as follows:
The precedence of operators in C follows this order:
1. Multiplication (*), Division (/), and Modulus (%) → Left to Right
2. Addition (+) and Subtraction (-) → Left to Right
3. Assignment (=) → Right to Left
result = a + a * -b / c % d + c * d;
result= 10 + 10 * -3 / 2 % 4 + 2 * 4;
result= 10 + (-30) / 2 % 4 + 2 * 4; (By solving 10 * -3 = -30)
result= 10 + (-15) % 4 + 2 * 4; (By solving (-30) / 2 = -15)
result= 10 + (-3) + 2 * 4; (By solving (-15) % 4 = -3)
result= 10 + (-3) + 8; (By solving 2 * 4 = 8)
result= 10 - 3 + 8 = 15
4. What is the output of the following C code?
#include <stdio.h>
int main()
{
int h = 8;
int b = 4 * 6 + 3 * 4 < h*5 ?4 : 3;
printf("%d\n", b);
return 0;
}
a) 0
b) 3
c) 4
d) Compilation error
Solution: (c) ‘?:’ is Conditional Expression. If Condition is true ? then value X :
otherwise value Y. After simplifying the expression, we get 36<40?4:3. The condition in
LHS of ? is true. Thus 4 will be stored in b.
5. What will be the output?
#include <stdio.h>
int main ()
{
int a = 1, b = 2, c = 3;
ASSIGNMENT 3
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 ((3 > 2) > 1) which becomes (1 > 1), thus FALSE
6. Find the output of the following C code
#include <stdio.h>
int main()
{
int x=1;
if ((3>5) || (2!=3))
printf("IITKGP\n");
else if (x&=0)
printf("IITD\n");
else
printf("IITM\n");
return 0;
}
a) IITKGP
b) IITD and IITM
c) IITKGP and IITM
d) IITM
Solution: (a)
Logical OR (||) If the first condition is false, but the second is true, the entire condition
evaluates to true.
Bitwise AND assignment (x &= 0) is never reached: The program does not check it since the
first if condition is already satisfied.
The else if and else blocks are irrelevant in this case. Only the first if condition will be executed
as the condition inside the if statement is true. Thus IITKGP will be printed.
ASSIGNMENT 3
7. What will be the output?
#include <stdio.h>
int main()
{
int x=2;
if(x=1)
printf("TRUE");
else
printf("FALSE");
return 0;
}
a) TRUE
b) FALSE
c) Compilation Error
d) Compiler Dependent
Solution: (a) TRUE
if(x=1)... "=" is an assignment operator, so 1 will be assigned to x and condition
will be true due to if(1).
8. Which of the following statement is correct?
a) Operator precedence determines which operator is performed first in an
expression with more than one operator with different precedence.
Associativity is used when two operators of same precedence appear in an
expression
b) Operator associativity determines which operator is performed first in an
expression with more than one operator with different associativity.
Precedence is used when two operators of same precedence appear in an
expression
c) Operator precedence and associativity are same.
d) None of the above
Solution: (a) Operator precedence determines which operator is performed first in an
expression with more than one operator with different precedence. Associativity
determines how operators of the same precedence are grouped in the absence of
parentheses. It can be Left-to-Right or Right-to-Left.
9. Which of the following method are accepted for assignment?
a) 8=x=y=z
b) x=8=y=z
ASSIGNMENT 3
c) x=y=z=8
d) None
Solution: (c)
8 = x = y = z; in invalid because the left-hand side (8) is a constant, and constants cannot
be assigned values. x=8=y=z is invalid because of same reason.
x=y=z=8 is valid as Right to Left evaluation will be as (z = 8), then (y = z), then (x = y).
10. What will be the output?
#include<stdio.h>
int main()
{
int x;
x= 9<5+3 && 7;
printf("%d", x);
return 0;
}
a. 0
b. 1
c. 7
d. Compilation error
Solution: (a) 0
Arithmetic (+) has higher precedence than Relational (<).
Relational (<) has higher precedence than Logical (&&).
This expression is equivalent to:
((9< (5 + 3)) && 7)
i.e., (5 + 3) executes first resulting into 8
then, first part of the expression (9< 8) executes resulting into 0 (FALSE)
Then, (0 && 7) executes resulting into 0 (FALSE)