JavaScript Questions & Answers – Modules in JavaScript

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Modules in JavaScript”.

1. The functions provide() and require() of Dojo toolkit and Google’s Closure library are used for ___________
a) declaring and loading modules
b) declaring functions
c) declaring modules
d) loading modules
View Answer

Answer: a
Explanation: Both the Dojo toolkit and Google’s Closure library define provide() and require() functions for declaring and loading modules.

2. The maximum number of global symbols a module can define is ____________
a) 2
b) 3
c) 1
d) 4
View Answer

Answer: c
Explanation: Generally, the various modules are allowed to run in the pristine (or near pristine) environment that it expects. The modules should minimize the number of global symbols they define – ideally, no module should define more than one.

3. To define each of the set classes as a property of the sets object (namespace) for the module, the statement is
a) sets = sets.AbstractEnumerableSet.extend();
b) sets.SingletonSet = sets.AbstractEnumerableSet.extend(…);
c) sets.SingletonSet = sets.extend(…);
d) sets = sets.extend(…);
View Answer

Answer: b
Explanation: Singleton is an object which can only be instantiated once. The extend keyword is used in class declarations or class expressions to create a class which is a child of another class. The sets object is the namespace for the module, and we define each of the set classes as a property of this object.
advertisement
advertisement

4. What will be the efficiency quotient of the following JavaScript statements?

var Set = sets.Set;
var s = new Set(1,2,3);

a) The programmer imports at once the frequently used values into the global namespace
b) There is no efficiency quotient, the programmer tries to make it inefficient
c) The programmer needs to import the Sets everytime he wants to use it
d) the programmer imports the set everytime the statement is encountered
View Answer

Answer: a
Explanation: A programmer can import frequently used values into the global namespace. A programmer who was going to make frequent use of the Set class from the sets namespace might import the class like that.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. The scope of a function is also called as ________
a) Predefined function
b) Module function
c) Public function
d) Private function
View Answer

Answer: b
Explanation: The scope of a function can be used as a private namespace for a module. Therefore, the scope of a function is called a module function.
advertisement

6. Modules that have more than one item in their API can ________
a) Assign itself to a global variable
b) Invoke another module of the same kind
c) Return a namespace object
d) Invoke another module of the same kind
View Answer

Answer: c
Explanation: Namespace is a container for a set of identifiers, functions, methods and all that. It gives a level of direction to its contents so that it will be well distinguished and organized. Modules that have more than one item in their API can return a namespace object.

7. The provides() function and the exportsobject are used to _________
a) Register the module’s API and Store their API
b) Call the modules api
c) Store the module’s API
d) Register the modules api
View Answer

Answer: a
Explanation: Frameworks that define module loading systems may have other methods of exporting a module’s API. There may be a provides() function for modules to register their API, or an exports object into which modules must store their API.
advertisement

8. What could be achieved by running the JavaScript code snippet below?

var sets = com.davidflanagan.collections.sets;

a) Importing a single module
b) Importing a module partially
c) Importing a namespace
d) Importing the entire module
View Answer

Answer: d
Explanation: Rather than importing individual classes, a programmer might import the entire module to the global namespace.

9. The properties() method is a ________
a) Enumerable method
b) Non-enumerable method
c) Operational method
d) calling method
View Answer

Answer: b
Explanation: The properties() is a method to get information of the properties of a particular object. properties() method is a non-enumerable method.

10. What can be done in order to avoid the creation of global variables in JavaScript?
a) To use a method that defines all the variables
b) To use an object that has the reference to all the variables
c) To use an object as its namespace
d) To use global functions
View Answer

Answer: c
Explanation: One way for a module to avoid the creation of global variables is to use an object as its namespace. Instead of defining global functions and variables, it stores the functions and values as properties of an object (which may be referenced to a global variable).

11. What will be the output of the following JavaScript code?

<p id="demo"></p>
<script>
var x = 123e5;
document.getElementById("demo").innerHTML = x ;
</script>

a) 0.0123
b) 12300
c) Error
d) Undefined
View Answer

Answer: b
Explanation: Extra large or extra small numbers can be written with scientific (exponential) notation. The number followed by e tells about the number of digits in the number.

12. What will be the output of the following JavaScript code?

<p id="demo"></p>
 <script>
function myFunction() 
{
   var res = "";
   res = res + Number.isFinite(5-2) ;
   document.getElementById("demo").innerHTML = res;
}
</script>

a) 3
b) true
c) false
d) error
View Answer

Answer: b
Explanation: The Number.isFinite() method determines whether a value is a finite number. This method returns true if the value is of the type Number, and equates to a finite number. Otherwise, it returns false.

13. What will be the output of the following JavaScript code?

<p id="demo"></p>
 <script>
function myFunction() 
{
   var res = "";
   res = res + Number.isFinite(0 / 0);
   document.getElementById("demo").innerHTML = res;
}
</script>

a) 3
b) true
c) false
d) error
View Answer

Answer: c
Explanation: The Number.isFinite() method determines whether a value is a finite number. The above code results into an infinte number therefore the output will be false.

14. What will be the output of the following JavaScript code?

<p id="demo"></p>
<script>
function myFunction() 
{
   var res = "";
   res = res + Number.isInteger('123');
   document.getElementById("demo").innerHTML = res;
}
</script>

a) True
b) False
c) Error
d) Undefined
View Answer

Answer: b
Explanation: The Number.isInteger() method determines whether a value an integer. This method returns true if the value is of the type Number.

15. What will be the output of the following JavaScript code?

<p id="demo"></p>
<script>
function myFunction() 
{
   var res = "";
   res = res + Number.isInteger(0.5) + ": 0.5<br>";
   document.getElementById("demo").innerHTML = res;
}
</script>

a) true
b) false
c) error
d) undefined
View Answer

Answer: b
Explanation: isInteger method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.