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 HTML tag is used to embed Java Script scripts in a page?
Option A
The HTML <script> tag is used to embed or reference JavaScript code in an HTML page. JavaScript is commonly used to make webpages interactive and dynamic.
<script>
alert("Hello, Suresh Kulahry");
</script>
It can also be linked to an external JavaScript file using the src attribute as <script src="myscript.js"></script>
Q: 3Which object holds the real content of the page in JavaScript?
Option A
In JavaScript, the Document object represents the actual HTML page and its content that is loaded in the browser.
It provides access to HTML elements such as headings, paragraphs, images, forms, buttons, and other elements.
Q: 4Which 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: 5Any time you include JavaScript verbiage in HTML document, you must enclose those lines inside a _________ tag pair.
Option D
JavaScript is commonly used to make an HTML webpage dynamic and interactive. When JavaScript code is written directly inside an HTML document, it is placed between the <script> and </script> tags.
The <script> tag is used to include JavaScript in an HTML document.
<script>
// JavaScript code
</script>
Q: 6In java script a ___________ function can be defined as a function passed into another function as a parameter.
Option B
In JavaScript, a callback function is a function that is passed as an argument (parameter) to another function. The receiving function can then execute the callback function at the appropriate time.
Callback functions are widely used in JavaScript for event handling, timers, asynchronous operations, array methods, etc.
JavaScript supports functions as first-class objects, which means a function can be stored in a variable, passed as an argument to another function, and returned from another function.
E.g.:
function greet(name, callback) {
console.log("Hello " + name);
callback();
}
function message() {
console.log("Welcome!");
}
greet("Suresh", message);
Here:
Q: 7In JavaScript, the _________ is the master container for all content you view in the web browser.
Option B
In JavaScript, the Window object is the top-level or master container of the browser window. It represents the browser's window or tab and provides access to objects such as the Document, Location, History, and Screen.
Window
│
├── Document
├── Location
├── History
└── Screen
E.g.:
The document object represents the actual HTML document (webpage content) displayed inside the browser window.
Q: 8State whether True or False?
i. Lifetime attribute is used to specify the lifetime of a cookie in a browser.
ii. Cookies are make secure using the Boolean attribute.
Option C
A cookie does not have a standard attribute called Lifetime for specifying how long it remains in the browser. The lifetime of a cookie is normally controlled using the Expires attribute or the Max-Age attribute. Therefore, statement (i) is False.
The Secure attribute is a Boolean attribute of a cookie. When the Secure attribute is present, the browser sends the cookie only over a secure HTTPS connection. Therefore, statement (ii) is True.
Q: 9Which window method generates a dialog box that displays whatever text you pass as a parameter in JavaScript?
Option C
In JavaScript, the alert() method of the Window object is used to display a dialog box containing a message.
E.g.:
Important Window Dialog Methods:
| Method | Purpose |
|---|---|
| alert() | Displays a message with an OK button. |
| confirm() | Displays a message with OK and Cancel buttons. |
| prompt() | Displays a message and allows the user to enter a value. |
Q: 10Comments 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.