Class 12 JavaScript Output Questions
1. Predict the output of the following JavaScript code:
function calculate(num) {
if (num <= 1) return num;
return calculate(num - 1) + calculate(num - 2);
}
console.log(calculate(5));
(a) 8 (b) 5 (c) 13 (d) 15
2. What will be the output of the following code?
let sum = 0;
for (let i = 1; i <= 5; i++) {
if (i % 2 === 0) {
sum += i * 2;
} else {
sum += i;
}
}
console.log(sum);
(a) 15 (b) 20 (c) 25 (d) 30
3. Identify the error in the following JavaScript code:
let num = 5;
for (num < 10; num++) {
console.log(num);
}
(a) Incorrect loop condition
(b) Incorrect use of for loop syntax
(c) Missing braces
(d) No error
4. Convert the following if-else structure into a switch-case:
let grade = "B";
if (grade === "A") {
console.log("Excellent");
} else if (grade === "B") {
console.log("Good");
} else if (grade === "C") {
console.log("Average");
} else {
console.log("Fail");
}
5. Convert the following for loop into a while loop:
for (let i = 1; i <= 5; i++) {
console.log(i * i);
}