This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What is the output of the following JavaScript code?
<html>
<body>
<script>
var x=12.89;
var y=3;
var z=’4’+y+x;
document.write(z+”<br>”);
</script>
</body>
</html>
Option C
In JavaScript, the + operator works as both addition and string concatenation. If any operand is a string, the operation becomes concatenation. In the given code, the expression is evaluated from left to right.
First, '4'+3 results in "43" because the number 3 is converted into a string. Then "43"+12.89 results in "4312.89", as the number is again converted into a string and concatenated.
Hence, the final output is 4312.89.
Q: 2Which of the statement is correct in JavaScript regarding var and let keywords?
Option A
In JavaScript, the main difference between var and let lies in their scope. A variable declared with var has either global scope (if declared outside a function) or function scope (if declared inside a function). It does not follow block scope, meaning it can still be accessed outside the block { } in which it is defined.
On the other hand, a variable declared with let is block scoped, which means it is only accessible within the block { } where it is declared.
// Using var
if (true) {
var x = 10;
}
console.log(x); // No Error, print 10 (var is not block scoped).
// Using let
if (true) {
let y = 20;
}
console.log(y); // Error, y is not defined (let is block scoped).
Q: 3Comments in a JavaScript code are ignored by ____________.
Option C
JavaScript is primarily an interpreted language, and its code is executed by the JavaScript Engine inside a Web Browser such as Chrome, Firefox, etc. During execution, the browser reads the code and ignores comments because they are meant only for developers, not for execution.
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.