📘 Multiple Choice Questions (Ternary
Operator)
1. What is the purpose of the ternary operator in JavaScript?
a) To replace functions
b) To write if-else statements in a shorter form
c) To declare variables
d) To check data types
👉 Answer: b)
2. What is the correct syntax of the ternary operator?
a) condition ? expr1 : expr2
b) condition : expr1 ? expr2
c) ? condition : expr1 , expr2
d) condition ? expr1 , expr2
👉 Answer: a)
3. What will the following code print?
let age = 18;
let result = (age >= 18) ? "Adult" : "Minor";
console.log(result);
a) Adult
b) Minor
c) undefined
d) Error
👉 Answer: a)
4. What will be the output?
let num = 7;
let result = (num % 2 === 0) ? "Even" : "Odd";
console.log(result);
a) Even
b) Odd
c) true
d) false
👉 Answer: b)
5. Which of the following is equivalent to the code below?
let x = (a > b) ? a : b;
a)
if(a > b) { x = a; } else { x = b; }
b)
if(a < b) { x = b; } else { x = a; }
c) Both a and b
d) None of the above
👉 Answer: a)
6. What will this code print?
let score = 40;
let status = (score > 50) ? "Pass" : (score > 30 ?
"Retest" : "Fail");
console.log(status);
a) Pass
b) Retest
c) Fail
d) Error
👉 Answer: b)
7. Which of the following statements about the ternary operator is
correct?
a) It can only be used with numbers.
b) It can replace simple if-else statements.
c) It cannot be nested.
d) It always returns a boolean value.
👉 Answer: b)
8. What will the following code print?
let loggedIn = false;
let message = loggedIn ? "Welcome back!" : "Please
log in.";
console.log(message);
a) Welcome back!
b) Please log in.
c) undefined
d) Error
👉 Answer: b)
9. Which operator has higher precedence in JavaScript?
a) Ternary (? :)
b) Assignment (=)
c) Logical OR (||)
d) Addition (+)
👉 Answer: d) (addition has higher precedence than ?:)
10. What will be the value of y?
let x = 10, y;
y = (x > 5) ? (x < 15 ? "Between" : "High") : "Low";
a) Low
b) High
c) Between
d) Error
👉 Answer: c)