Course: JavaScript

Progress (0%)

JavaScript Number MethodsQuiz

Quiz 4 18 questions

Are you ready?

18 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does num.toPrecision(3) round num to?
The toPrecision() method converts a number to a given amount of significant digits. Hence, in this case, num.toPrecision(3) rounds num to 3 s.f. This goes with choice (B). For more details, refer to JavaScript Number Methods — toPrecision().
What does the following code log?
var num = 38.6;
console.log(num.toPrecision(2));
Rounding 38.6 to 2 s.f. gives us the number 39. This is returned in the form of a string i.e. '39'. Hence, the correct choice is (B). For more details, refer to JavaScript Number Methods — toPrecision().
What does the following code log?
var num = 50.2;
console.log(num.toPrecision(4));
Rounding 50.2 to 4 s.f. gives us the number 50.20. This is returned in the form of a string i.e. '50.20'. Hence, the correct choice is (C). For more details, refer to JavaScript Number Methods — toPrecision().
What does the following code log?
var num = 123;
console.log(num.toPrecision(1));
Rounding 123 to 1 s.f. gives us the number 100. Since, the number 123 has more than 1 digit in its integral part, the return value of toPrecision() is in exponential form. That is, instead of '100', '1e+2 is returned. Hence, the correct choice is (C). For more details, refer to JavaScript Number Methods — toPrecision().
What does the following code log?
var num = 123;
console.log(num.toPrecision(2));
Rounding 123 to 2 s.f. gives us the number 120. Since, the number 123 has more than 2 digits in its integral part, the return value of toPrecision() is in exponential form. That is, instead of '120', '1.2e+2 is returned. Hence, the correct choice is (C). For more details, refer to JavaScript Number Methods — toPrecision().
What does the following code log?
var num = -1234.56;
console.log(num.toPrecision(5));
Rounding -1234.56 to 4 s.f. gives us the number -1234.6. Hence, the correct choice is (B). For more details, refer to JavaScript Number Methods — toPrecision().
What does the following code log?
var num = 15;
console.log(num.toPrecision());
Without an argument, the toPrecision() method throws an error. This is exactly what happens in the code above. Hence, the correct choice is (D). For more details, refer to JavaScript Number Methods — toPrecision().
The return value of toFixed(2) is the same as that of toPrecision(2) for the number 0.5. Yes or no?
For the number 0.5, toFixed(2) returns '0.50', and toPrecision(2) also returns '0.50'. They both return the same value back. Hence, the correct choice is (A). For more details, refer to JavaScript Number Methods.
The invocation toFixed(1) is the same as toPrecision(2) for the number 5. Yes or no?
For the number 5, toFixed(1) returns '5.0', and toPrecision(2) also returns '5.0'. They both return the same value back. Hence, the correct choice is (A). For more details, refer to JavaScript Number Methods.
The invocation toFixed(1) is the same as toPrecision(2) for the number 10. Yes or no?
For the number 10, toFixed(1) returns '10.0', and toPrecision(2) returns '10'. The return value of both the methods is not the same in this case. Hence, the correct choice is (B). For more details, refer to JavaScript Number Methods.
What does the following code log?
var num = 16;
console.log(num.toFixed(2));
The toFixed() method converts a number into fixed-point notation having a given amount of decimal places. In this case, num.toFixed(2) converts 16 to '16.00'. This goes with choice (D). For more details, refer to JavaScript Number Methods — toFixed().
What does the following code log?
var num = 123;
console.log(num.toFixed(-1));
Calling toFixed() with a negative argument is invalid, and leads to an error.. Hence, in this case, the correct choice is (D). For more details, refer to JavaScript Number Methods — toFixed().
What does the following code log?
var num = 100.1806;
console.log(num.toFixed(3));
In this case, toFixed(3) rounds num to 3 decimal places, i.e. 100.181, and returns back the result in the form of a string, i.e. '100.181'. This goes with choice (B). For more details, refer to JavaScript Number Methods — toFixed().
What does the following code log?
console.log(-3.14.toFixed(3));
Owing to operator precedence, the - operator is evaluated last in the code above. First 3.14.toFixed(3) is evaluated. This returns '3.140'. Then the - operator converts this string into a number and negates its sign. This gives the number -3.14. Hence, the correct choice is (A). For more details, refer to JavaScript Number Methods — toFixed().
What does the following code log?
var num = 106;
console.log(num.toExponential(1));
toExponential() converts a number into scientific notation with given digits in the fractional part, and then returns the result in the form of a string. In this case, num.toExponential(1) converts num to '1.1e+2' (having 1 digit after the decimal point). For more details, refer to JavaScript Number Methods — toExponential().
What does the argument to the number method toString() specify?
The number method toString() accepts an optional argument which specifies the base to which convert the given number to. This goes with choice (A). For more details, refer to JavaScript Number Methods — toString().
What does the following code log?
console.log(10 .toString(16));
Called on the number 10, toString(16) converts it into hexadecimal representation. Since in hexadecimal, 10 is denoted as 'A' (or even as 'a'), the method returns 'a'. Hence, the correct choice is (C). For more details, refer to JavaScript Number Methods — toString().
What does the following code log?
console.log( parseInt(100 .toString(2), 2) );
100 .toString(2) converts 100 into binary representation, and then parseInt(), with the second argument as 2, converts this binary string back into a decimal notation. That is, we get the number 100 back in the end. Likewise, the correct choice is (B). For more details, refer to JavaScript Number Methods — toString().

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage