LearnFrenzy
Sign in Take a free test

Conditional Statements · Exercise 1 of 4

Conditional Statements Conditional Statements MCQ

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Fifteen multiple-choice questions on JavaScript conditionals: if and else-if, truthy conditions, the dangling else, the ternary operator, switch matching and fall-through, default, switch (true), guard clauses and lookup tables. Pick an option to check your answer instantly.

Question 1 Easy

Which comparison does a switch statement use to match a case?

A Loose equality (==)
B Object.is()
C Less-than-or-equal (<=)
D Strict equality (===)
Question page & discussion
Question 2 Easy

What happens when a matching case has no break (or return)?

A Only that case runs anyway
B A SyntaxError is thrown
C The switch starts again from the top
D Execution continues into the following case(s) until a break or the end of the switch
Question page & discussion
Question 3 Medium

Which statement about default in a switch is correct?

A It must be the last clause or a SyntaxError occurs
B It may appear anywhere and runs only if no case matches; without break it falls through into the cases after it
C It always runs, even when a case matches
D A switch must contain a default
Question page & discussion
Question 4 Easy

In an if / else-if / else chain where several conditions are true, which branches run?

A Every branch whose condition is true
B Only the last true branch
C The else branch as well
D Only the first branch whose condition is true
Question page & discussion
Question 5 Easy

Which of these conditions makes the if-block run?

A if ("false")
B if (0)
C if ("")
D if (null)
Question page & discussion
Question 6 Medium

In if (a) if (b) x(); else y(); written without braces, which if does the else belong to?

A The outer if (a)
B The inner if (b) – an else pairs with the nearest if
C Both
D It is a SyntaxError
Question page & discussion
Question 7 Medium

What is the purpose of switch (true) { case score >= 90: … }?

A To make every case run
B To convert the cases to booleans with ==
C To test ranges or conditions: the first case whose expression is true runs
D It is invalid syntax
Question page & discussion
Question 8 Easy

What can the ternary operator do that an if statement cannot?

A Test more than one condition
B Produce a value that can be assigned or returned directly, because it is an expression
C Run several statements in each branch
D Catch errors
Question page & discussion
Question 9 Easy

A variable is declared with let inside an if block. Where can it be used?

A Anywhere in the function
B Only inside that block
C Anywhere after the if statement
D Only in the else block
Question page & discussion
Question 10 Hard

A switch has case f():, case g(): and case h():, and the value matches what g() returns. Which functions are called?

A f, g and h – all cases are evaluated first
B g only
C f and g only – cases are evaluated in order until one matches
D None, case expressions must be constants
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links