Control Structure Questions with Answers

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

Sample questions

Question 1Medium

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
int i = 10, j = 20;
if(i = 5) && if(j = 10)
printf("Have a nice day");
return 0;
}
  1. Output: Have a nice day
  2. No output
  3. Error: Expression syntax
  4. Error: Undeclared identifier if

Answer: C. Error: Expression syntax

Explanation

"Expression syntax" error occur in this line

if(i = 5) && if(j = 10)

It should be like

if((i == 5) && (j == 10))

Step-by-step solution

  1. The assignment i = 5 is a valid expression, but the keyword if cannot be used as an operand of &&.
  2. The text if(j = 10) after && is not a valid expression, so the compiler reports a syntax error.
  3. Because the program does not compile, printf never runs and nothing is printed.
  4. The answer is Error: Expression syntax.

Why the other options are wrong

  • A. The program does not compile, so it cannot print Have a nice day.
  • B. A compile error is reported, so the program never runs to give no output.
  • D. If is a keyword, so the error is a syntax error, not an undeclared identifier.

Tip: Keywords such as if cannot appear inside an expression.

Question 2Medium

What is the output of this C code?

#include <stdio.h>
void main()
{
int i = 0;
while (i < 10)
  {
   i++;
   printf("hi\n");
  }
while (i < 8)
i++;
printf("hello\n");
}
  1. hi is printed 8 times, hello 7 times and then hi 2
  2. hi is printed 10 times, hello 1 time
  3. hi is printed once, hello 7 times
  4. hi is printed once, hello 7 times and then hi 2 ti

Answer: B. hi is printed 10 times, hello 1 time

Explanation

Hi is printed 10 times, hello 1 timeFirst while loop execute 10 times.
And second for loop does not execute. Because i=10.

Step-by-step solution

  1. The first while loop runs while i < 10, so i goes from 0 to 10.
  2. Each pass prints hi once, so hi is printed 10 times.
  3. The second while has no braces, so only i++ is in its body; i < 8 is already false at i = 10.
  4. So the second loop never runs.
  5. printf(hello) is outside the loops and runs once.
  6. The output is hi is printed 10 times, hello 1 time.

Why the other options are wrong

  • A. The second loop never runs and hello is printed once, not 7 times.
  • C. Hi is printed 10 times, not once.
  • D. Hi is printed 10 times, not once, and hello is printed once.

Tip: Trace the variable value through each loop; a loop without braces only runs one statement.

Question 3Medium

Which of the following sentences are correct about a switch loop in a C program?
1:switch is useful when we wish to check the value of variable against a particular set of values.
2:switch is useful when we wish to check whether a value falls in different ranges.
3:Compiler implements a jump table for cases used in switch.
4:It is not necessary to use a break in every switch statement.

  1. 1,2
  2. 1,3,4
  3. 2,4
  4. 2

Answer: B. 1,3,4

Explanation

Statements 1, 3 and 4 are correct. A switch checks one value against fixed cases, compilers can use a jump table for the cases, and break is not needed in every case. Statement 2 is wrong because switch does not check ranges of values.

Step-by-step solution

  1. Statement 1 is true: switch compares one value with a set of constant case values.
  2. Statement 2 is false: switch matches exact values, not ranges, so ranges need if-else.
  3. Statement 3 is true: compilers often implement case lookup with a jump table.
  4. Statement 4 is true: break only stops fall-through, so it is optional in a switch.
  5. So statements 1, 3 and 4 are correct, which is option B: 1,3,4.

Why the other options are wrong

  • A. It includes statement 2, which is false because switch does not test ranges.
  • C. It includes statement 2 and leaves out statements 1 and 3, which are correct.
  • D. It gives only statement 2, which is false.

Tip: Switch tests exact values only; ranges need if-else.

Question 4Medium

Which of the following is not logical operator?

  1. &
  2. &&
  3. ||
  4. !

Answer: A. &

Explanation

Bitwise operators: & is a Bitwise AND operator.
Logical operators: && is a Logical AND operator.
|| is a Logical OR operator.! is a NOT operator.
So, '&' is not a Logical operator.

Step-by-step solution

  1. The logical operators in C are && (AND), || (OR) and! (NOT).
  2. These work on true and false values and give 1 or 0.
  3. & is the bitwise AND operator, which works on the bits of integers, not on logical values.
  4. So the answer is &.

Why the other options are wrong

  • B. && is the logical AND operator.
  • C. || is the logical OR operator.
  • D. ! is the logical NOT operator.

Tip: Single symbols & and | are bitwise, double symbols && and || are logical.

Question 5Medium

What is the output of this C code?

#include <stdio.h>
#define max(a) a
int main()
 {
 int x = 1;
 switch (x)
  {
   case max(2):
   printf("yes\n");
   case max(1):
   printf("no\n");
   break;
  }
 }
  1. yes no
  2. yes
  3. no
  4. Compile time error

Answer: C. no

Explanation

The value of x is 1. So case 1 is matched and print the output "no".

Step-by-step solution

  1. max(a) expands to a, so case max(2) is case 2 and case max(1) is case 1.
  2. X is 1, so the switch jumps to case 1.
  3. That prints no, and the break ends the switch.
  4. Case 2 is skipped, so yes is not printed.
  5. The output is no.

Why the other options are wrong

  • A. Yes is printed only if case 2 runs, and x is 1, so the switch starts at case 1.
  • B. Yes is not printed, because the switch starts at case 1.
  • D. The code compiles, since the macro expands to valid case labels.

Tip: A switch jumps to the matching case, and falls through until a break.

Question 6Medium

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
int i = 10, j = 15;
if(i % 2 = j % 3)
printf("India\n");
return 0;
}
  1. Error: Expression syntax
  2. Error: L-Value required
  3. Error: R-Value required
  4. The Code runs successfully

Answer: B. Error: L-Value required

Explanation

if(i % 2 = j % 3)

This statement generates "L-Value required error".
There is no variable on the left side of the expression to assign (j % 3).

Step-by-step solution

  1. The = operator is assignment and needs a variable (an lvalue) on its left side.
  2. The left side here is i % 2, which is a value, not a variable.
  3. The compiler reports an error about the left operand of the assignment.
  4. The error is: Error: L-Value required.

Why the other options are wrong

  • A. The syntax is valid; the problem is the assignment target.
  • C. The left side needs an L-value, so R-Value required is the wrong term.
  • D. The code does not compile, so it does not run successfully.

Tip: The left side of = must be a variable; == is the comparison operator.

Question 7Medium

What is the output of this C code?

#include <stdio.h>
const int a = 1,  b = 2;
int main()
{
int x = 1;
switch (x)
  {
   case a: printf("yes ");
   case b: printf("no\n");
   break;
  }
}
  1. yes no
  2. yes
  3. no
  4. Compile time error

Answer: D. Compile time error

Explanation

X and a have same value. When same case is repeated then compilation error is occurred.

Step-by-step solution

  1. Constint is not a C keyword, so const int a = 1, b = 2; is read as a declaration with an unknown type named const int.
  2. Because the type const int is undefined, the compiler reports an error before the program runs.
  3. A program that fails to compile produces no output.
  4. The answer is Compile time error.

Why the other options are wrong

  • A. The code does not compile, so it cannot print yes no.
  • B. The code does not compile, so it cannot print yes.
  • C. The code does not compile, so it cannot print no.

Tip: Check that every keyword and type name is spelled correctly before tracing the output.

Question 8Medium

Which of the following statements are correct about the program?

#include <stdio.h>
int main()
{
int x = 30, y = 40;
if(x == y)
printf("x is equal to y\n");

else if(x > y)
printf("x is greater than y\n");

else if(x < y)
printf("x is less than y\n")
return 0;
}
  1. Error: Statement missing
  2. Error: Expression syntax
  3. Error: Lvalue required
  4. Error: Rvalue required

Answer: A. Error: Statement missing

Explanation

This program will result in error "Statement missing ;"

printf("x is less than y\n")

here; should be added to the end of this statement.

Step-by-step solution

  1. The line printf("x is less than y") has no semicolon after it.
  2. The next line, return 0;, cannot follow the printf call as a complete statement, so the compiler reports a missing statement.
  3. A missing semicolon is a syntax error, reported before the program runs.
  4. So the answer is Error: Statement missing.

Why the other options are wrong

  • B. An expression syntax error would mean a badly written expression, and these expressions are fine.
  • C. Lvalue required would mean assigning to something that is not a variable, which the code does not do.
  • D. Rvalue required would mean a bad value on the left of an assignment, which the code does not have.

Tip: A missing semicolon usually shows as a statement or syntax error.

Question 9Medium

Which of the following sentences are correct about a for loop in a C program?

1. for loop works faster than a while loop.
2. All things that can be done using a for loop can also be done using a while loop.
3. for(;;); implements an infinite loop.
4. for loop can be used if we want statements in a loop get executed at least once.

  1. 1
  2. 1, 2
  3. 2, 3
  4. 2, 3, 4

Answer: C. 2, 3

Explanation

A for loop is not faster than while, and a do-while (not for) runs at least once, so only 2 and 3 are correct.

Step-by-step solution

  1. Statement 1 is false: a for loop is not faster than a while loop.
  2. Statement 2 is true: any for loop can be rewritten as a while loop with its init, test and update placed around it.
  3. Statement 3 is true: for(;;) has an empty test, which counts as always true, so it loops forever.
  4. Statement 4 is false: a for loop tests before the first run, so its body may run zero times; a do-while runs at least once.
  5. Only statements 2 and 3 are correct, which is option C.

Why the other options are wrong

  • A. Lists only statement 1, which is false, and leaves out the correct statements 2 and 3.
  • B. Includes statement 1, which is false: a for loop is not faster than a while loop.
  • D. Includes statement 4, which is false because a for loop may run zero times.

Tip: For 'which statements are correct' questions, check each statement alone; one false statement removes every option that lists it.

Question 10Medium

In mathematics and computer programming, which is the correct order of mathematical operators?

  1. Addition, Subtraction, Multiplication, Division
  2. Division, Multiplication, Addition, Subtraction
  3. Multiplication, Addition, Division, Subtraction
  4. Addition, Division, Modulus, Subtraction

Answer: B. Division, Multiplication, Addition, Subtraction

Explanation

The usual order is BODMAS (Brackets, Of, Division, Multiplication, Addition, Subtraction). Only the option Division, Multiplication, Addition, Subtraction follows this order.

Step-by-step solution

  1. In mathematics and programming, multiplication and division are done before addition and subtraction.
  2. Multiplication and division are done left to right with each other, and so are addition and subtraction.
  3. Option B lists Division, Multiplication, Addition, Subtraction in that priority order.
  4. The correct order is Division, Multiplication, Addition, Subtraction.

Why the other options are wrong

  • A. Addition and subtraction come after multiplication and division, not before.
  • C. Addition is not done before division.
  • D. Addition is placed before division and modulus, which is wrong.

Tip: Multiply and divide first, then add and subtract.

Practise all 15 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