MCQs on Conditional Statements | JavaScript

Conditional statements in programming control the flow based on specific conditions. Mastering if, else, and else if statements, the Ternary Operator, and switch case statements will enhance decision-making capabilities in coding. Below are 30 multiple-choice questions covering these essential topics.


Section 1: If, Else, Else If Statements

  1. Which of the following is true about an if statement?
    • A. It must always be followed by an else statement.
    • B. It executes code only if a specified condition is true.
    • C. It requires a switch statement to work.
    • D. It checks multiple conditions simultaneously.
  2. Which syntax is correct for an if-else statement?
    • A. if (condition) {code}
    • B. if {code}
    • C. if (condition) {code} else {code}
    • D. if else {code}
  3. What is the purpose of the else clause in an if-else statement?
    • A. To execute code when the if condition is true.
    • B. To execute code when the if condition is false.
    • C. To check multiple conditions.
    • D. To terminate the if block.
  4. When multiple conditions need to be checked, which statement is used?
    • A. switch
    • B. else
    • C. else if
    • D. try-catch
  5. Which of the following is an example of a nested if statement?
    • A. if (x > 10) { if (y > 5) { // code } }
    • B. if (x > 10 && y > 5) { // code }
    • C. if (x > 10) { // code } else if (y > 5) { // code }
    • D. if (x > 10 || y > 5) { // code }
  6. What is the result of the following code?




  1. int x = 5; if (x > 10) { x += 1; } else { x -= 1; }
    • A. x = 6
    • B. x = 5
    • C. x = 4
    • D. x = 10
  2. Which of these is NOT true about an else if ladder?
    • A. It allows checking multiple conditions.
    • B. Only the first true condition is executed.
    • C. All conditions are evaluated.
    • D. It starts with an if statement.
  3. An else if statement can:
    • A. Only follow an if statement.
    • B. Be used without an if statement.
    • C. Only appear at the beginning of code.
    • D. Be used as the last statement.
  4. Which keyword ends an if-else statement block in most programming languages?
    • A. end
    • B. close
    • C. }
    • D. ;
  5. In the expression if (x > 0 && y < 5), which logical operator is used?
    • A. ||
    • B. &&
    • C. !
    • D. ==

Section 2: Ternary Operator

  1. The ternary operator can replace which type of statement?
    • A. switch statement
    • B. while loop
    • C. if-else statement
    • D. for loop
  2. What is the correct syntax for the ternary operator?
    • A. condition ? if_true : if_false
    • B. condition : if_true ? if_false
    • C. condition ? if_false : if_true
    • D. if_true : condition ? if_false
  3. Which of the following statements is equivalent to x = (a > b) ? a : b;?
    • A. if (a > b) { x = a; } else { x = b; }
    • B. if (a < b) { x = a; } else { x = b; }
    • C. x = a + b;
    • D. x = (a > b);
  4. What value does y have after the following code?




  1. int x = 10; int y = (x > 5) ? 1 : 0;
    • A. 5
    • B. 10
    • C. 1
    • D. 0
  2. In the expression result = (age >= 18) ? "Adult" : "Minor";, what does "Adult" represent?
    • A. The condition
    • B. The true expression
    • C. The false expression
    • D. The variable name
  3. Which of these ternary expressions is valid?
    • A. result = (x > 0) : x : -x;
    • B. result ? (x > 0) : x : -x
    • C. result = (x > 0) ? x : -x;
    • D. result = x : x : -x
  4. How does a ternary operator handle multiple conditions?
    • A. By chaining if-else statements
    • B. Using switch statements
    • C. With nested ternary operators
    • D. It cannot handle multiple conditions.
  5. Which is an advantage of using the ternary operator?
    • A. Simplifies code for simple conditions
    • B. Always faster than if-else
    • C. It can replace all if-else structures
    • D. It requires multiple lines of code
  6. Which of these could replace x = (y > 10) ? y : 10;?
    • A. if (y > 10) { x = 10; } else { x = y; }
    • B. if (y < 10) { x = 10; } else { x = y; }
    • C. x = (y < 10) ? 10 : y;
    • D. x = 10;
  7. In a ternary operation, what does the colon : symbol do?
    • A. Separates the condition from expressions
    • B. Ends the statement
    • C. Separates true and false expressions
    • D. Indicates a logical operator

Section 3: Switch Case Statements

  1. Which data type can a switch statement use?
    • A. boolean
    • B. float
    • C. string
    • D. array
  2. What keyword is used to end a case block in a switch statement?
    • A. break
    • B. end
    • C. case
    • D. continue
  3. When is the default case executed in a switch statement?
    • A. If the first case matches
    • B. Only if there’s no break
    • C. If none of the cases match
    • D. If all cases match
  4. Which of the following is a valid switch syntax?
    • A. switch (x) { case 1; //code break; }
    • B. switch x { case 1; //code break }
    • C. switch (x) { case 1: //code break; }
    • D. switch x ( case 1; //code break; )
  5. What happens if there’s no break statement in a switch case?
    • A. The next case is skipped.
    • B. It exits the switch block.
    • C. It executes the next case’s code.
    • D. It throws an error.
  6. The switch statement is ideal for:
    • A. Simple, exact-match conditions
    • B. Complex conditions with ranges
    • C. Only true/false values
    • D. Continuous loop execution
  7. Which is true about the default case in a switch statement?
    • A. It must always be the first case.
    • B. It is mandatory.
    • C. It runs if no other case matches.
    • D. It is only executed if a break is missing.
  8. A switch statement cannot handle:
    • A. Multiple cases with the same output
    • B. int data types
    • C. float values
    • D. Nested conditions
  9. In a switch statement, how do you execute the same code for multiple cases?
    • A. Use a loop in the switch block
    • B. Omit break in multiple cases
    • C. Write switch for each case
    • D. It’s impossible
  10. Which of these scenarios is NOT ideal for a switch statement?
    • A. When checking for exact matches
    • B. When there are many different cases
    • C. For complex conditionals
    • D. For short code sections

Answers

QnoAnswer
1B. It executes code only if a specified condition is true
2C. if (condition) {code} else {code}
3B. To execute code when the if condition is false
4C. else if
5A. if (x > 10) { if (y > 5) { // code } }
6C. x = 4
7C. All conditions are evaluated
8A. Only follow an if statement
9C. }
10B. &&
11C. if-else statement
12A. condition ? if_true : if_false
13A. if (a > b) { x = a; } else { x = b; }
14C. 1
15B. The true expression
16C. result = (x > 0) ? x : -x;
17C. With nested ternary operators
18A. Simplifies code for simple conditions
19B. if (y < 10) { x = 10; } else { x = y; }
20C. Separates true and false expressions
21C. string
22A. break
23C. If none of the cases match
24C. switch (x) { case 1: //code break; }
25C. It executes the next case’s code
26A. Simple, exact-match conditions
27C. It runs if no other case matches
28C. float values
29B. Omit break in multiple cases
30C. For complex conditionals

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top