JavaScript Questions & Answers – Shorthand functions and Multiple catch clauses

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Shorthand functions and Multiple catch clauses”.

1. What convenience does the following JavaScript code snippet provide?

let succ = function(x) x+1, yes = function() true, no = function() false;

a) Functional behaviour
b) Modular behaviour
c) No convenience
d) Shorthand expression
View Answer

Answer: a
Explanation: The functions defined in this way behave exactly like functions defined with curly braces and the return keyword. The above code makes the expression short and reduces the line of code.
advertisement
advertisement

2. What does the following JavaScript code snippet do?

data.sort(function(a,b),b-a);

a) Sort in the alphabetical order
b) Sort in the chronological order
c) Sort in reverse alphabetical order
d) Sort in reverse numerical order
View Answer

Answer: d
Explanation: ’a-b’ is used to sort the array in ascending order whereas ‘b-a’ is used to sort the array in descending order. Therefore the above code snippet sorts an array in reverse numerical order.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. What is the code to be used to trim whitespaces?
a) let trimmed = (l.trim() for (l in lines));
b) let trimmed = (trim(l));
c) let trimmed = l.trim();
d) let trimmed = for(l in lines));
View Answer

Answer: a
Explanation: The various types of trim functions are trimLeft(), trimRight() and trim().can use the above code to trim whitespaces and filter out comments and blank lines.
advertisement

4. What will be the reaction when a catch clause has no conditionals?
a) Takes it to be 0
b) Takes it to be 1
c) Takes it to be true
d) Takes it to be false
View Answer

Answer: c
Explanation: The try and catch statement handles some or all of the errors that may occur in a block of code, while still running code. If a catch clause has no conditional, it behaves as if it has the conditional if true, and it is always triggered if no clause before it was triggered.

5. When will the finally block be called?
a) When there is no exception
b) When the catch does not match
c) When there is exception
d) After try-catch execution
View Answer

Answer: d
Explanation: The try and catch statement handles some or all of the errors that may occur in a block of code, while still running code. A finally block is called after try-catch execution.
advertisement

6. What is the return type of typeof for standard JavaScript objects?
a) xml
b) object
c) dom
d) html
View Answer

Answer: b
Explanation: The typeof operator returns “object” for all standard JavaScript objects. It returns “object” for a null, “number” for NaN, “number” for Infinity, “object” for a “new Number(1)” and “object” for an array.

7. Which method to use while working with XML fragments, instead of XML()?
a) XMLInterface()
b) XMLClass()
c) XMLList()
d) XMLArray()
View Answer

Answer: c
Explanation: An XML fragment is an XML document with no single top-level root element. When working with XML fragments, use XMLList() instead of XML().

8. Which of the following is the descendant operator?
a) ..
b) …
c) *
d) @
View Answer

Answer: a
Explanation: While the . operator accesses direct children of the given node, the .. operator accesses all children no matter how deeply nested: The .. operator is the descendant operator; you can use it in place of the normal. member-access operator:

var names = pt..name;

9. Which of the following is an example to perform the most common XML manipulations using the XML objects invocation?
a) insertChildBefore()
b) insertChildAfter()
c) appendChildAfter(…)
d) appendChildBefore(…)
View Answer

Answer: a
Explanation: E4X is designed so that you can perform most common XML manipulations using language syntax. E4X also defines methods you can invoke on XML objects. Here, for example, is the insertChildBefore() method:

pt.insertChildBefore(pt.element[1],<element id="1"><name>Deuterium</name></element>);

10. What is the code required to delete all “weight” tags?
a) delete weight(pt).all;
b) delete pt.element[all];
c) delete pt;
d) delete pt..weight;
View Answer

Answer: d
Explanation: Delete is a keyword in javascript which is used for deleting objects ,pointers and variables. Removing attributes and tags is very easy with the standard delete operator :

delete pt..weight; //delete all <weight> tags

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

<p id="demo"></p>
<script>
var x = 10;
var y = "20";
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>

a) 30
b) 10 20
c) 1020
d) Error
View Answer

Answer: c
Explanation: If a number and a numeric string is added, then the result will be a concatenated string. The two numbers will not be added as they are of different types.

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

<p id="demo"></p>
<script>
var x = 10;
var y = 20;
document.getElementById("demo").innerHTML = x + y;
</script>

a) 1020
b) 10 20
c) 30
d) Error
View Answer

Answer: a
Explanation: The two numbers will not get and rather get concatenated as string. Therefore the output will be 1020.

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

<p id="demo"></p>
<script>
var x = "100";
var y = "10";
var z = x * y;   
document.getElementById("demo").innerHTML = z;
</script>

a) 1000
b) 10
c) 10010
d) Error
View Answer

Answer: a
Explanation: JavaScript will try to convert strings to numbers when multiplying. Therefore the two values will be converted to numbers and multiplied.

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

<p id="demo"></p>
<script>
var x = 500;        // x is a number
var y = new Number(500);  // y is an object
document.getElementById("demo").innerHTML = (x===y);
</script>

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

Answer: b
Explanation: In the above code an object and a number are compared. When different data types are compared then the answer is false.

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

<p id="demo"></p>
<script>
var x = 9.656;
document.getElementById("demo").innerHTML =
  x.toFixed(0)
</script>

a) 10
b) 9.65
c) 9.6
d) 9.656
View Answer

Answer: a
Explanation: toFixed() method rounds off the number to specified number of decimal places. Since the argument is passed with 0 value therefore the output will be 10.

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

<p id="demo"></p>
<script>
function myFunction() 
{
     document.getElementById("demo").innerHTML = Math.trunc(8.76);
}
</script>

a) 8.70
b) 8.76
c) 8.00
d) Error
View Answer

Answer: c
Explanation: The trunc() method returns the integer part of a number. This method will NOT round the number up/down to the nearest ingeger, but simply remove the decimals.

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

<p id="demo"></p>
<script>
function myFunction() 
{
     document.getElementById("demo").innerHTML = Math.LN10;
}
</script>

a) 2.00
b) 2.10
c) 2.20
d) 2.30
View Answer

Answer: d
Explanation: The LN10 property returns the natural logarithm of 10, approximately 2.302. The method is find in the math library of Javascript.

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

<p id="demo"></p>
 
<script>
function myFunction() 
{
     document.getElementById("demo").innerHTML = Math.acosh(2);
}
</script>

a) 1.31
b) 1.20
c) 1.11
d) 1.41
View Answer

Answer: a
Explanation: The acosh() method returns the hyperbolic arccosine of a number. If the parameter x is less than 1, the method will return NaN.

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.