JavaScript MCQs

30 MCQs  — Web Development MCQs  (Page 2 of 3)

What does this log? const obj = { name: 'PMD', show: () => console.log(this.name) }; obj.show();
PMD
ReferenceError
obj
undefined (in a browser module or script, this is not obj)
What is the difference between event.preventDefault() and event.stopPropagation()?
stopPropagation cancels form submission
preventDefault stops the browser's default action (such as following a link); stopPropagation stops the event bubbling to ancestor elements
They are identical
preventDefault stops bubbling
In which phase do event listeners run by default with addEventListener?
Both phases
Neither, until enabled
The capturing phase
The bubbling phase
What is the difference between the DOMContentLoaded and load events?
DOMContentLoaded waits for images
load fires first
DOMContentLoaded fires when the HTML is parsed; load waits for all resources, such as images and stylesheets
They are identical
Which string is valid JSON?
{'name': 'Karachi', 'temp': 35}
{"name": "Karachi", "temp": 35,}
{name: "Karachi", temp: 35}
{"name": "Karachi", "temp": 35}
What happens with fetch('/api/missing') when the server responds with 404?
The promise resolves normally, with response.ok set to false; fetch rejects only on network failure
It retries automatically
The promise rejects with an error
It throws a SyntaxError
What is a closure in JavaScript?
A function bundled with references to the variables of the scope where it was created, even after that scope has finished
A function that closes the browser
A sealed object
A completed promise
What does async/await do?
It lets asynchronous promise-based code be written in a synchronous style; await pauses the async function, not the whole thread
It replaces promises with callbacks
It makes JavaScript multithreaded
It blocks the browser while waiting