Expressions Questions with Answers

11 practice questions on Expressions (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 11 free in the AptiNest app.

Sample questions

Question 1Medium

What is the output of this C code?

 #include <stdio.h>
 void main()
 {
   int k = 8;
   int x = 0 == 1 && k++;
   printf("%d%d", x, k);
 }
  1. 0 9
  2. 0 8
  3. 1 8
  4. 1 9

Answer: B. 0 8

Explanation

priority of == operator highest than && expression 0==1 return 0.And return 0 value without evaluating next operand.

Step-by-step solution

  1. X is set by the expression 0 == 1 && k++.
  2. 0 == 1 is false, so the && stops early and k++ is never evaluated.
  3. K stays 8, and x becomes 0.
  4. printf prints x then k with no space, which gives 08.
  5. The output is 0 8.

Why the other options are wrong

  • A. K would be 9 only if k++ ran, but it does not run.
  • C. X would be 1 only if the && test were true, but it is false.
  • D. x = 1 and k = 9 both need k++ to run, which does not happen.

Tip: The right side of && is skipped when the left side is false.

Question 2Medium

Evaluation of expression involving && operator

1. take place from left to right.
2. take place from right to left.
3. stops when one of the operands evaluates to true.
4. stop when one of the operand evaluates false.

  1. 1 and 3
  2. 1 and 4
  3. 2 only
  4. 4

Answer: B. 1 and 4

Explanation

The && operator is checked from left to right. It stops as soon as one operand is false, because the result is then certainly false. Stopping on true is the behaviour of ||.

Step-by-step solution

  1. && evaluates its operands from left to right (statement 1 is correct).
  2. If the left operand is false, the right operand is skipped (short-circuit).
  3. So && stops when one operand is false (statement 4), not when it is true (statement 3).
  4. The correct statements are 1 and 4.

Why the other options are wrong

  • A. Statement 3 is wrong; && stops on false, not true.
  • C. Statement 1 is correct, so 2 only is wrong.
  • D. Statement 1 is also correct, so 4 alone is incomplete.

Tip: AND stops at the first false; OR stops at the first true.

Question 3Medium

What is the output of this C code?

 #include <stdio.h>
 void main()
 {
   int x = 0;
   if (x = 0)
     printf("Its zero");
   else
     printf("Its not zero");
 }
  1. Its not zero
  2. Its zero
  3. Run time error
  4. None

Answer: A. Its not zero

Explanation

If statement takes any integer constant expression and return 0 if value of expression is zero else 1.

Step-by-step solution

  1. x = 0 is an assignment, not a comparison; it sets x to 0.
  2. The value of the assignment expression is 0, which is false in an if.
  3. So the else branch runs and prints its text.
  4. The code wording is 'Its not zero'; option A gives the same meaning as 'Its non zero', so the answer is Its non zero.

Why the other options are wrong

  • B. Its zero is printed only if the condition is true, and it is false here.
  • C. The program compiles and runs; there is no runtime error.
  • D. None is wrong because option A matches the else branch.

Tip: In C, a single = inside an if is an assignment; the if tests the assigned value.

Question 4Medium

In which order do the following gets evaluated?
1. Relational
2. Arithmetic
3. Logical
4. Assignment

  1. 2134
  2. 1234
  3. 4321
  4. 3214

Answer: A. 2134

Explanation

2. Arithmetic operators: *, /, %, +, -
1. Relational operators: >, <, >=, <=, ==, !=
3. Logical operators: !, &&, ||
4. Assignment operators: =.
By the Precedence and Associativity rule of C language.

Step-by-step solution

  1. Operator precedence decides which operators are applied first.
  2. Arithmetic operators (+, -, ×, ÷) bind most tightly, so they are evaluated first.
  3. Relational operators (<, >, ==, !=) come next, then logical operators (&&, ||).
  4. Assignment (=) has the lowest precedence and is evaluated last.
  5. So the order is: 2134.

Why the other options are wrong

  • B. 1234 puts relational before arithmetic, but arithmetic binds more tightly.
  • C. 4321 reverses the order and evaluates assignment first, which is wrong.
  • D. 3214 puts logical operators first, but they have lower precedence than relational operators.

Tip: Precedence from highest to lowest: arithmetic, relational, logical, assignment.

Question 5Medium

Which of the following correctly shows the hierarchy of arithmetic operations in C?

  1. / + * -
  2. * - / +
  3. + - / *
  4. / * + -

Answer: D. / * + -

Explanation

Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).
How Do I Remember? BODMAS !B - Brackets first O - Orders (ie Powers and Square Roots, etc.) DM - Division and Multiplication (left-to-right)AS - Addition and Subtraction (left-to-right)

Step-by-step solution

  1. In C, * / and % have higher precedence than + and -.
  2. So the highest level is * and /, which are evaluated before + and -.
  3. Then + and - come last, so the hierarchy is / * then + -, which is option D: / * + -.

Why the other options are wrong

  • A. It puts + above *, which is wrong.
  • B. It puts - above /, which is wrong.
  • C. It puts + and - at the top, but * and / come first.

Tip: Remember: multiply and divide first, then add and subtract.

Question 6Medium

What will be the output of the program?

#include<stdio.h>
int main()
{
    int i=2;
    int j = i + (1, 2, 3, 4, 5);
    printf("%d\n", j);
    return 0;
}
  1. 4
  2. 7
  3. 6
  4. 5

Answer: B. 7

Explanation

The comma operator evaluates its operands from left to right and gives the value of the last one, which is 5. So j = 2 + 5 = 7.

7

Step-by-step solution

  1. i = 2.
  2. (1, 2, 3, 4, 5) evaluates to 5.
  3. j = i + 5 = 2 + 5 = 7.
  4. Printf outputs 7.

Why the other options are wrong

  • A. The expression (1, 2, 3, 4, 5) does not evaluate to 4.
  • C. The expression (1, 2, 3, 4, 5) does not evaluate to 6.
  • D. The expression (1, 2, 3, 4, 5) does not evaluate to 5.

Tip: Remember: The comma operator discards all intermediate results except the last one.

Question 7Medium

Comment on the output of this C code?

#include<stdio.h>
void main()
{
  int k = 8;
  int m = 7;
  int z = k < m ? k = m : m++;
  printf("%d", z);
}
  1. Run time error
  2. 7
  3. 8
  4. Depends on the compiler

Answer: B. 7

Explanation

The test 8 < 7 is false, so the third part m++ is used. Post-increment gives the old value 7, so z becomes 7 and the program prints 7.

7

Step-by-step solution

  1. k = 8 and m = 7, so the condition k < m is 8 < 7, which is false.
  2. The conditional operator then evaluates its third operand, m++.
  3. M++ gives the old value of m, which is 7, and m becomes 8 after this.
  4. Z is assigned 7.
  5. So the output is 7.

Why the other options are wrong

  • A. There is no run-time error; both branches are valid expressions.
  • C. 8 is k, and k = m is only evaluated in the true branch, which is not taken.
  • D. The conditional operator is fully defined here, so the result does not depend on the compiler.

Tip: Work out the condition first; only the chosen branch is evaluated.

Question 8Medium

Which of the following is the correct usage of conditional operators used in C?

  1. a>b ? c=30 : c=40;
  2. a>b ? c=30;
  3. max = a>b ? a>c?a:c:b>c?b:c
  4. return (a>b)?(a:b)

Answer: C.

Explanation

B: assignment statements are always return in parenthesis in the case of conditional operator. It should be

a>b? (c=30):(c=40);B:

it is syntactically wrong.

D: syntactically wrong, It should be

return(a>b ? a:b);

C: It uses nested conditional operator, this is logic for finding greatest number out of three numbers.

Step-by-step solution

  1. The conditional operator has the form condition? value1: value2.
  2. Option A is invalid because c=40 as the last part is an assignment that C does not allow there without brackets.
  3. Option C is max = a>b? (a>c? a: c): (b>c? b: c), which is valid and gives the largest of a, b and c.
  4. Option B is missing the colon part, and option D has (a:b), which is not valid.
  5. The correct usage is max = a>b? a>c?a:c:b>c?b:c.

Why the other options are wrong

  • A. The last part c=40 is not allowed after the colon in C, so the line is invalid.
  • B. The conditional has no colon and the else part, so it is a syntax error.
  • D. (a:b) is not valid, and the colon part is missing, so return (a>b)?(a:b) fails.

Tip: A conditional needs both the? part and the: part, and nested ones group to the right.

Question 9Medium

What is the output of this C code?

 #include <stdio.h>
 void main()
 {
   int k = 8;
   int m = 7;
   int z = k < m ? k++ : m++;
   printf("%d", z);
 }
  1. 7
  2. 8
  3. Run time error
  4. None of the mentioned

Answer: A. 7

Explanation

ternary operator return second expression because condition evaluated false value i.e. 0.Value of m=7 is store in z variable.
Value m is incremented after statement.

int z = k < m ? k++ : m++;

Step-by-step solution

  1. Start with k = 8 and m = 7.
  2. The condition k < m is 8 < 7, which is false.
  3. So the false branch m++ runs, which gives the value 7 and sets m to 8.
  4. Z receives 7 and printf prints 7.
  5. The output is 7.

Why the other options are wrong

  • B. 8 would be the value of k++, but that branch runs only when the condition is true.
  • C. There is no runtime error; this is valid C.
  • D. The program prints a definite value, so none of the mentioned is wrong.

Tip: In a conditional expression, only the chosen branch is evaluated.

Question 10Medium

What will be the output of the program?

#include <stdio.h>
int main()
{
    int k, num=30;
    k = (num>5?(num <=10?100:200):500);
    printf("%d\n", num);
    return 0;
}
  1. 200
  2. 30
  3. 100
  4. 500

Answer: B. 30

Explanation

Step 1:

int k, num=30;

here variable k and num are declared as an integer type and variable num is initialized to '30'.

Step 2:

k = (num>5 ? (num <=10 ? 100 : 200): 500);

This statement does not affect the output of the program. Because we are going to print the variable num in the next statement.So, we skip this statement.
Step 3:

printf("%d\n", num);

It prints the value of variable num '30'

Step 4: Hence the output of the program is '30'

Step-by-step solution

  1. Num is 30, so num>5 is true and the middle part of the conditional is used.
  2. Inside it, num<=10 is 30<=10, which is false, so the value is 200.
  3. K becomes 200, but k is never printed.
  4. Printf prints num, which was never changed, so it prints 30.
  5. The output is 30.

Why the other options are wrong

  • A. 200 is assigned to k, but only num is printed.
  • C. 100 needs num<=10 to be true, but 30 is not.
  • D. 500 is the value only when num>5 is false.

Tip: Work through nested ternaries from the outside in, and check which variable is printed.

Practise all 11 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics