Table of Contents
Wonderful JavaScript MCQs Series for Beginner .Practice these MCQs to enhance and test the knowledge of JavaScript.
1.What is the output for the following code?
(function f() {
function f() {
return 1;
}
return f();
function f() {
return 2;
}
})();
- 2
- NaN
- 1
- Error
1) 2
2.What is the output of the following expression?
function multi(x, y) {
var c = x * y;
}
multi(20, 3);
- Nothing
- 20
- 3
- 60
1) Nothing
3.Which of the following regarding scope is true?
- Data that is stored in a variable when a function is called is never cleared out.
- Function parameters are visible in the function in which they are used.
- All variables you use in your program have to be declared as global variables.
- Variables that have a local scope are only visible in the function in which they are declared.
4) Variables that have a local scope are only visible in the function in which they are declared.
4.What is true about functions?
I) Functions are objects
II) Can be assigned to a variable
III) Can be anonymous
IV) Return value type has to be defined in a function declaration
- I, II, III, IV
- I, II, III
- I, II, IV
- I, II
2) I, II, III
5.Anonymous functions can be created in JavaScript. What do anonymous function do?
- Process a variable before passing it on to another function
- Overwrite variables that are to be kept updated
- Automatically define the scope of a value inside a parameter
1) Process a variable before passing it on to another function
6.What is the output you get for the following code?
(function() {
return typeof arguments;
})
();
- array
- undefined
- object
- arguments
3) object
7.Object’s properties are similar to variables and methods are similar to _________.
- Properties
- Operators
- Functions
- Conditionals
3) Functions
8.________ is a function contained within an object.
- None of the options
- Object
- Function
- Method
4) Method
9.A function in JavaScript is an object. State if the statement is true or false?
- False
- True
2) True
10.What is the output for the following expression?
function test(x) {
while (x < 5) {
x++;
}
return x;
}
alert(test(2));
- 2
- 3
- 6
- 5
4) 5
11.For any structured document, __________ defines a standard set of objects.
- XML DOM
- HTML DOM
- Core DOM
3) Core DOM
12.Which statement about the name and id attributes of form fields is false?
- The id attribute is what is sent when the form is submitted.
- The name attribute can be used to access the field using getElementsByName().
- It is customary to give form fields both attributes, with the same value if possible.
- Either attribute may be omitted if it is unused.
1) The id attribute is what is sent when the form is submitted.
13.Using HTML button tag, JavaScript command can be executed by using ____ function.
- “alert(Button1);”
- “alert(‘Button1’);”
- “alert(‘Button1’);
- “alert(‘Button”);”
2) “alert(‘Button1’);”
14.By modifying the DOM, the contents on the page also gets modified
- True
- False
1) True
15.Document object is part of ____________ object.
- System
- Window
- Tree
2) Window
16.Your div element in a document has id=”answers”. If a JS fn sets d=document.getElementById(“answers”), then which is the preferred way to add a paragraph containing the word “Hello”? as a child of that div?
- answers.innerHTML = “Hello”;
- d.appendChild(“Hello “);
- d.innerHTML = “Hello “;
- p = createElement(“p”); p.innerHTML = “Hello”; d.appendChild(p);
4) p = createElement(“p”); p.innerHTML = “Hello”; d.appendChild(p);
17.By modifying the DOM, the contents on the page also gets modified
- False
- True
2) True
18.Which statement accesses HTML classes?
- class.name
- getElementsByClassName
- getElementById
2) getElementsByClassName
19.How many ‘onload’ events can be written in a page?
- 1
- Many
- None
- 2
1) 1
20.Which is the most preferred way of handling events?
- Registering a listener to an element.
- Writing the JavaScript as an attribute to an element.
- Referencing an element with the event and assigning a function as a value.
1) Registering a listener to an element.