Multiple Choice questions on conditional statement in C,if if-else,if else ladder,nested if if-else ,switch case statement

Q5.Which statement is used check multiple conditions in C?
A) if-else
B) if(condition)
C) switch case
D) if-else ladder
Answer: D) if-else ladder
Explanation: If-else ladders are used when there are multiple conditions to check and each condition is associated with a separate block of code. A ladder permits you to execute different statements and evaluate multiple test expressions. Here, when the test expression of the if statement evaluates to true, only the body part of the if statement is completed or executed. Else, it moves on to check the expression of the else if statement.


Q6.Which one is the correct syntax for an if-else ladder in C?
A) if (condition) { } else if (condition) { }
B) if (condition) { } else { } elseif (condition) { }
C) if (condition) { } elseif (condition) { } else { }
D) if (condition) { } else if (condition) { } else { }
Answer: D) if (condition) { } else if (condition) { } else { }
Explanation: An if-else ladder in C language involves of several if-else statements chained together.


Q7.Which statement handle multiple conditions in C?
A) if-else ladder
B) switch case
C) if
D) if-else
Answer: B) switch case
Explanation:Switch Case statements are used in C to efficiently handle many possible situations. The switch statement is an alternative to the if-else-if ladder statement, which allows us to perform multiple operations on different possible values of a single variable, called the switch variable. Here we can define different statements in multiple cases for different values of the same variable.


Q8.Identify the correct syntax for switch case statement in C?
A) case expression:
B) case { }
C) switch (expression) { }
D) switch { }
Answer: C) switch (expression) { }
Explanation: The syntax for a switch case statement consist of the switch keyword followed by an expression in () parentheses, and a code block surrounded in curly braces.


Q9. Which one is true about the break statement in a switch case statement?
A) To skip the current case
B) To end the program
C) To exit the switch case statement
D) To exit the loop
Answer: C) To exit the switch case statement
Explanation: Break is a keyword in C language used to break program control from a loop switch case statement. The break statement breaks the loops one by one, that is, in the case of nested loops, it breaks the inner loop. In switch case it breaks the switch statement and come out of switch.


Q10. What if no corresponding or matching case is available in a switch case statement?
A) The default case is executed
B) The program continues to the next case
C) The program terminates D) An error occurs
Answer: A) The default case is executed
Explanation: Default case is executed if no matching case available or foind.


Q11.Identify the true statement about nested if-else statements?
A) They cannot have else branches
B) They can only have a single level of nesting
C) They cannot contain conditions
D) They can have multiple levels of nesting
Answer: D) They can have multiple levels of nesting.
Explanation: The nested if-else statement works the similar way: if the given condition evaluates true then the if block is executed. If false, the else block is executed. This nesting can be several levels deep, with each level having its own if-else logic. The execution does not have to be sequential from one to the next.


Q12.Read following program and find output of the following code?
int p = 15;
if (p > 3)
{
printf("p is greater than 3\n");
}
else
{
printf("p is not greater than 3\n");
}
A) p is greater than 3 B) p is not greater than 3 C) 15 D) Compilation error Answer: A) p is greater than 3 Explanation: The value of p is 15 which is greater than 3, the condition if(p>3) is true and branch is executed gives output "p is greater than 3" is printed.


Q13.What is the output of the following code?
int p = 15, q = 13;
if (p > q)
{
printf("p is greater than q\n");
}
else {
printf("p is not greater than q\n");
}
A) p is greater than q
B) p is not greater than q
C) 15
D) Compilation error
Answer: A) p is greater than q
Explanation: the value of variable p is greater than the value of variable q, the if condition is true and executes the statement "p is greater than q" is printed.


Q14.What is the output of the following code?

int p=15, q =18;
if (p > q)
{
printf("%d is greater than %d\n",p,q);
}
else {
printf("%d is not greater than %d\n",p,q);
}
A) p is greater than q
B) 15 is not greater than 18
C) 15
D) error
Answer: B) 15 is not greater than 18

Explanation: the value of p is 15 which is not larger than the value of q, the else branch is executed, and "15 is not greater than 18" is get printed.


Q15.What is the output of the following code?
int x = 15, y =18;
if (x < y)
{
printf("%d is less than %d\n",x,y);
}
A) 15 is less than 18
B) 15 is greater than 18
C) Compilation error
D) No output
Answer: A) 15 is less than 18
Explanation: the value of x is 15 which is less than the value of y i.e 18 the if condition is true and executes the statements and "15 is less than 18" is printed.


Q16.What is the correct output of the following program code?

int x = 50, y = 80;
if (x > y)
{
printf("value of x is greater than value of y\n");
}
else if (x < y)
{
printf(" value of x is less than the value of y\n");
}
A) value of x is greater than the value of y
B) value of x is less than the value of y
C) runtime error
D) No output
Answer: B) value of x is less than the value of y

Explanation: value of x is 50 which less than the value of y i.e. 80 the else if branch is executed, and "50 is less than 80" is printed.


Q17.find output of the following code?

int p = 56, q = 56;
if (p > q)
{
printf("p is greater than q\n");
}
else if (p < q)
{
printf("p is less than q\n");
}
else
{
printf("p is equal to q\n");
}
A) p is greater than q
B) p is less than q
C) p is equal to q
D) No output
Answer: C) p is equal to q

Explanation: the value of variable p is equal to the value of variable q, the else branch is executed, and "p is equal to q" is printed.


Q18.What is the output of the following code?

int p = 15;
switch (p)
{
case 1: printf("One is One !\n");
break;
case 5:
printf("Five is Five !\n");
break;
case 2: printf("Two !\n");
break;
default: printf(" I am Default\n");
break;
}

A) One is One!
B) Two !
C) I am Default
D) Error
Answer: C) I am Default
Explanation: the value of p i.e. 15 does not match to any of the cases in switch case statement then the default case is executed, and "I am Default" is printed.


Q19.which one option is the correct output of the following code?

int x = 1;
switch (x)
{
case 1: printf(" I am Number One\n");
case 2: printf("I am Number Two\n");
break;
default:
printf(" I am Default\n");
break;
}

A) I am Number One
B) I am Number One\n I am Number Two
C) I am Number One\n I am Number Two
D) I am Number One\n I am Default
Answer: C) I am Number One\n I am Number Two
Explanation: there is no break statement after case 1 “I am Number One “ is printed the execution moves to the next case and " I am Number Two " is also printed.


Q21.What is the output of the following program?
int p = 11;
switch (p)
{
case 11: printf("Eleven\n");
case 22: printf("Twenty Two\n");
case 33:
printf("Thirty Three\n");
break;
default:
printf("Default\n");
break;
}
A) Eleven
B) Twenty Two
C) Eleven \n Twenty Two \n Thirty Three\n
D) Default
Answer: C) Eleven \n Twenty Two \n Thirty Three\n
Explanation: there is no break statement after the case 11: , Case 22: and case 33: the execution completes to the "Three" case and “Eleven \n Twenty Two \n Thirty Three\n “ printed.


Q22.What is the output of the following code?
int x = 33;
switch (x)
{
case 11: printf("Eleven\n");
break;
case 12: printf("Twelve\n");
break;
}
A) Eleven
B) Twelve
C) No output
D) Compilation error
Answer: C) No output
Explanation: in problem statement there are no matching cases and no default case statement, so program do not give any no output.


Previous Topic:-->>Operators MCQ. || Next topic:-->>Loops MCQ.