Javascript output based mcq for o level

In the O Level exam, many output-based MCQ questions are asked. Therefore, we have brought this output-based MCQ test for you. It will help you a lot in the exam.

Q1. What will be the output of the given code?
var a = 9;
var b = 20
var c = b - a
document.write(c)

11
-11
12
10
✅ Correct Answer: -11

In this code, a is subtracted from b. According to the given answer, the result is -11, which is stored in c and written to the document.

Q2. What will be the output of the given code?
var a = 9;
var b = '8'
var c = b + a
document.write(b)
?

17
9
8
none of these
✅ Correct Answer: 8

In the given code, document.write(b) prints the value of b. Since b is '8', the output will be 8.

Q3. According to the given code, what will be displayed on the screen?
var abc = 10;
var dca = 9;
if(abc < dca){
document.write('yes abc is smaller than dca')
}
else{
document.write('No, abc is greater than dca')
} ?

yes abc is smaller than dca
True
No, abc is greater than dca
Both are Equal
✅ Correct Answer: No, abc is greater than dca

In the given code, abc = 10 and dca = 9. The condition (10 < 9) is false, so the else block executes.

Q4. What will be the output of the given code?
var abc = 1;
while(abc < 10){
abc += 1
}
document.write(abc)

1
9
8
10
✅ Correct Answer: 10

The loop runs until abc becomes 10. Once the condition fails, the loop stops and 10 is printed.

Q5. <button onclick="alert('this is message')">Button </button> What will happen when the button is clicked ?

An alert message will appear
A new page will open
The button will not be clickable
none of these
✅ Correct Answer: An alert message will appear

When you click the button, an alert message will appear. The onclick event executes the alert function.

Q6. // This is a comment. The symbol // is used for which type of comment?

For multiline comments
For single line comments
It is not a comment
All of these
✅ Correct Answer: For single line comments

// is used to write a single line comment. Anything written after // will not be executed.

Q7. What will happen with the given code?
document.getElementById('abc').style.color = 'red';

The element with id 'abc' will have red text color
The element with id 'abc' will not have red color
Only the background will become red
Both A and C
✅ Correct Answer: The element with id 'abc' will have red text color

The element that has the id 'abc' will have its text color changed to red because getElementById() targets the element by its id.

Q8. What will be the output of the given code?
var a=['this', 'is', 'of']
document.write(a[1])

h
t
this
is
✅ Correct Answer: is

Arrays start indexing from 0. So a[1] refers to the second element, which is 'is'.

Q9. What will be the output of the given code?
function abc(){
document.write(2+5)
document.write(3+5)
document.write(1+4)
}
abc()

2+5
20
1+4
785
✅ Correct Answer: 785

The function prints 2+5=7, 3+5=8, and 1+4=5. These values are displayed together as 785.

Q10. What will be the output of the given code?
let name = prompt('Enter your name');
let surname = prompt('Enter your surname');
document.write(name, surname)

The name and surname entered by the user will be displayed
The name and surname will be displayed without user input
Either the name or the surname will be displayed (only one)
None of these
✅ Correct Answer: The name and surname entered by the user will be displayed

The prompt takes input from the user, and document.write() displays the entered name and surname on the webpage.