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);
}
[Link](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;
}
}
[Link](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++) {
[Link](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") {
[Link]("Excellent");
} else if (grade === "B") {
[Link]("Good");
} else if (grade === "C") {
[Link]("Average");
} else {
[Link]("Fail");
}
5. Convert the following for loop into a while loop:
for (let i = 1; i <= 5; i++) {
[Link](i * i);
}