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
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
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
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.
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
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.
5. The scope of a function is also called as ________
a) Predefined function
b) Module function
c) Public function
d) Private function
View Answer
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.
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
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
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.
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
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
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
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
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
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
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
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
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.
- Check JavaScript Books
- Practice MCA MCQs
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Programming Books