An if statement looks simple until a question combines nearest-if binding, nested conditions, truth values, assignments and precedence. The same tracing discipline applies from a two-value comparison to a multi-branch coverage problem.
Choose an option before reading each explanation, trace the condition on paper, record every changed variable, and compare your route with the solution so that each answer becomes a method you can reuse. If basic traces expose a gap or you want broader programming practice, continue through Coding & DSA Courses for Placements.
1. Basic if-else decisions: follow the condition, not the wording
Write the condition, substitute values, mark it true or false, then follow the branch.
Q1. Reversing a comparison changes which value is printed
Start
Read n1, n2
If n1 > n2 then
Print n1
Else
Print n2
End if
Stop
What will be the output if we change the condition to n1<n2?A. n1 will be displayed
B. Largest of the two numbers will be displayed
C. Smallest of the two numbers will be displayed
D. n2 will be displayed
Answer: C. Solution.
When n1 < n2 is true, the algorithm prints the smaller value, n1. Otherwise, it prints n2, which is smaller than or equal to n1. Equal inputs print the same value, so the algorithm displays the smallest number.
Q2. Testing parity with a remainder
Start
set num = 0
display " enter the number"
read num
if ( remainder (num / 2 ) = 0 )
display "yes"
else
display "no"
end
If the output of the above algorithm is "yes", it indicates that num isA. Even number
B. Amstrong number
C. Odd number
D. Prime number
Answer: A. Solution.
For num = 18, the remainder after division by 2 is 0, so the true branch prints yes. An integer with this remainder is even. The condition does not establish primality or an Armstrong number.
2. Nested if and the dangling-else rule
An else binds to the nearest unmatched if. Follow the syntax, not the indentation.
Q3. Identify the if that owns the else
Consider the following program fragment
if(a > b)
if(b > c)
s1;
else s2;s2 will be executed if
A. a <= b
B. b > c
C. b >= c and a <= b
D. a > b and b <= c
Answer: D. Solution.
The outer test a > b must be true, or the inner statement is skipped. The else belongs to if (b > c), so s2 requires b <= c. Together, the conditions are a > b && b <= c.
Q4. Trace both nested conditions with x = 10
Consider the following C code snippet.
int x = 10;
if (x > 5)
if (x < 20)
printf("x is between 5 and 20");
else
printf("x is greater than 20");
What will be the output of this code?A. No output is displayed
B. The program fails to compile
C. x is greater than 20
D. x is between 5 and 20
Answer: D. Solution.
Both 10 > 5 and 10 < 20 are true, so the program prints x is between 5 and 20. The else attached to the inner if is skipped. Braces make this ownership explicit.
3. Compound Boolean conditions in if-else decisions
For &&, apply the truth rules in Propositional and Predicate Logic: Truth Tables and Quantifiers, then evaluate both C operands.
Q5. One false operand sends control to else
What is the result after execution of the following code if ‘a’ is 10, ‘b’ is 5 and ‘c’ is 10?
if ((a>b)&&(a<c))
a = a + 1;
else
c = c + 1;A. a = 10, c = 10
B. a = 11, c = 10
C. a = 10, c = 11
D. a = 11, c = 11
Answer: C. Solution.
10 > 5 is true, while 10 < 10 is false. Therefore true && false is false, and the else branch changes c from 10 to 11. The value of a remains 10.
4. Nested conditional operators and equivalent branch expressions
Treat ?: as a decision tree: evaluate the outer condition, then only its selected arm.
Q6. A nested ternary that selects the maximum
Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression?
a = ( x > y ) ? (( x > z ) ? x : z) : (( y > z ) ? y : z )A. x = 3, y = 4, z = 2
B. x = 6, y = 5, z = 3
C. x = 6, y = 3, z = 5
D. x = 5, y = 4, z = 5
Answer: A. Solution.
The expression returns the maximum of the three values. For A, 3 > 4 is false and 4 > 2 is true, so a = 4. Options B, C and D produce 6, 6 and 5.
Q7. Replace an if-else toggle with arithmetic
Let x be an integer which can take a value of 0 or 1. The statement if (x == 0) x = 1; else x = 0; is equivalent to which one of the following?
A. x = 1 + x;
B. x = 1 - x;
C. x = x - 1;
D. x = 1 % x;
Answer: B. Solution.
At x = 0, 1 - x = 1; at x = 1, 1 - x = 0. It toggles both allowed inputs correctly.
5. Precedence and assignment side effects inside conditions
Precedence sets grouping. An assignment can change a value and determine a condition.
Q8. Order five operator families correctly
Consider the operators used in C Programming given below:
A. &&
B. +=
C. >>
D. >=
E. ?:
Choose among the following the correct order of precedence of the operators given above (higher to lower):A. C, D, E, B, A
B. C, A, D, E, B
C. C, D, A, E, B
D. D, C, A, B, E
Answer: C. Solution.
From higher to lower, these operators are shift >>, relational >=, logical AND &&, conditional ?:, and assignment +=. Translating the operator names back to their question labels gives C, D, A, E, B.
Q9. An assignment expression becomes the ternary condition
Consider the following statements
int x = 10, y = 15;
x = ((x = y) ? (y + x) : (y - x));
What will be the value of x after executing these statements?A. 5
B. 25
C. 15
D. 30
Answer: D. Solution.
First, x = y changes x to 15. The non-zero result selects the true arm, where y + x = 15 + 15 = 30. The outer assignment stores 30 in x.
Q10. Assignment in if across three loop iterations
Consider the following C code:
#include <math.h>
void main()
{
double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf("%d ",1);
else printf("%d ", 0);
}What would the program print?
A. 000
B. 010
C. 101
D. 111
Answer: C. Solution.
At i = 0, cos(0) converts to integer 1 and prints 1. At i = 1, cos(pi/2) converts to integer 0 and prints 0. At i = 2, cos(pi) converts to integer -1, a true value, and prints 1, giving 101. void main() is non-standard C; production code should use int main(void).
6. Statement coverage versus branch coverage in nested decisions
Statement coverage executes every statement. Branch coverage takes every true and false decision outcome.
Q11. Minimum tests for full statement and branch coverage
Consider the following C function:
#include <stdbool.h>
int f(int m, int n, bool x, bool y)
{
int res=0;
if (m<0) {res=n-m;}
else if (x || y)
{
res=-1;
if(n==m){res=1;}
}
else {res=n;}
return res;
} /*end of f */
If 𝑃 is the minimum number of tests to achieve full statement coverage for 𝑓(), and 𝑄 is the minimum number of tests to achieve full branch coverage for 𝑓(), then (𝑃,𝑄) = A. (3,4)
B. (4,3)
C. (2,3)
D. (3,2)
Answer: A. Solution.
Three tests cover all statements: (-1,5,false,false) gives res = 5 - (-1) = 6; (2,2,true,false) executes res = -1 and then res = 1; (2,3,false,false) gives res = 3. Thus P = 3. Branch coverage also needs (2,3,true,false), which makes x || y true and n == m false, leaving res = -1. That fourth path makes Q = 4, so (P,Q) = (3,4).

7. Key lessons for testing if-else statements
The recurring moves are:
Reverse a comparison and follow the result.
Bind an
elsebefore tracing nested conditions.Evaluate every Boolean operand separately.
Unpack a ternary from the outside in.
Apply precedence before doing arithmetic.
Distinguish assignment
=from equality==.Design tests for every branch.
If you missed Q1 to Q5, practise condition tracing. For Q6 to Q10, revise precedence and expression tracing. For Q11, draw the control-flow paths. Retry only the missed group after one day, then attempt the complete set after one week. For concept-first revision, use the C Programming Course. Continue with Data Structures MCQs, and use Why PYQs Beat Buying Another Question Bank to sharpen the practice method behind your next attempt.




