JavaScript Questions and Answers Part-20

1. Which is a more formal way of importing packages and classes as JavaScript objects?
a) import(java.util.*);
b) importClass(java.util.*);
c) import.Class(java.util.*);
d) Class.import(java.util.*);

Answer: b
Explanation: Because packages and classes are represented as JavaScript objects, you can assign them to variables to give them shorter names. But you can also more formally import them, if you want to:
importClass(java.util.HashMap); // Same as : var HashMap = java.util.HashMap

2. What will be the output of the following JavaScript code?
var f = new java.io.File("/tmp/test");
var out = new java.io.FileWriter(f);
out instanceof java.io.Reader
a) error
b) true
c) exception
d) false

Answer: d
Explanation: The output for the above code snippet is false as it is a writer and not a Reader.

3. What does Rhino do when the getter and setter methods exist?
a) It becomes JavaScript properties
b) Java classes are used to avoid them
c) Java classes & JavaScript properties
d) It act as javascript function

Answer: a
Explanation: Rhino allows JavaScript code to query and set the static fields of Java classes and the instance fields of Java objects. Java classes often avoid defining public fields in favor of getter and setter methods. When getter and setter methods exist, Rhino exposes them as JavaScript properties.

4. The JavaScript classes can be instantiated using _____ operator.
a) create
b) new
c) instantiate
d) create.new

Answer: b
Explanation: New is a keyword in JavaScript which is used to define new models. New operator is used to create new instance of the class.

5. The new Java arrays can be created into JavaScript programs using which of the following classes?
a) java.Array
b) java.lang.*
c) java.lang.Array
d) java.lang.reflect.Array

Answer: d
Explanation: There is no natural JavaScript syntax that Rhino can extend to allow JavaScript programs to create new Java arrays, so you have to do that using the java.lang.reflect.Array class.

6. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sin(90 * Math.PI / 180);
</script>
a) 1
b) 0
c) 1.6
d) 0.5

Answer: a
Explanation: Math.sin function evaluates the sine value of a particular input. Math.PI function generates a value of 3.14.

7. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.abs(-4.5);
</script>
a) -4.5
b) 4.5
c) 0
d) error

Answer: b
Explanation: The Math.abs method returns the absolute positive value of the argument passed to it. The above code will hence generate an output of 4.5.

8. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.min(0, 150, 30, 20, -8, -200);
</script>
a) 0
b) -8
c) -200
d) 20

Answer: c
Explanation: Math.min() returns the lowest value in a list of arguments. The lowest value is -200, hence the output will be -200.

9. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(4.7);
</script>
a) 4.5
b) 4
c) 4.7
d) 5

Answer: b
Explanation: Math.floor(x) returns the value of x rounded down to its nearest integer. The value is decreased in the decimal is removed.

10. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sqrt(49);
</script>
a) 6
b) 7
c) 49
d) error

Answer: b
Explanation: Math.sqrt function returns the square root of the argument passed to it. It is found in the math library of Javascript.