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
Answer: 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
Answer: 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.
Answer: 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
Answer: 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
Answer: 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
Answer: 3)object
7.Object’s properties are similar to variables and methods are similar to _________.
- Properties
- Operators
- Functions
- Conditionals
Answer: 3)Functions
8.________ is a function contained within an object.
- None of the options
- Object
- Function
- Method
Answer: 4)Method
9.A function in JavaScript is an object. State if the statement is true or false?
- False
- True
Answer: 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
Answer: 4)5
11.For any structured document, __________ defines a standard set of objects.
- XML DOM
- HTML DOM
- Core DOM
Answer: 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.
Answer: 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”);”
Answer: 2)”alert(‘Button1’);”
14.By modifying the DOM, the contents on the page also gets modified
- True
- False
Answer: 1)True
15.Document object is part of ____________ object.
- System
- Window
- Tree
Answer: 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);
Answer: 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
Answer: 2)True
18.Which statement accesses HTML classes?
- class.name
- getElementsByClassName
- getElementById
Answer: 2)getElementsByClassName
19.How many ‘onload’ events can be written in a page?
- 1
- Many
- None
- 2
Answer: 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.
Answer: 1)Registering a listener to an element.