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)
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)
?
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')
} ?
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)
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 ?
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?
// 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 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])
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()
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 prompt takes input from the user, and document.write() displays the entered name and surname on the webpage.