250+ TOP MCQs on Scripting Java with Rhino and Answers

Javascript Multiple Choice Questions on “Scripting Java with Rhino”.

1. Rhino is originated by _______
a) Microsoft
b) Mozilla
c) Apple
d) Chrome

Answer: b
Clarification: Rhino is a JavaScript engine written fully in Java and managed by the Mozilla Foundation as open source software. Rhino is free software from Mozilla. You can download a copy from http://www.mozilla.org/rhino/.

2. Which of the following are global functions that are not part of core JavaScript?
a) spawn(f);
b) trim();
c) exult();
d) eval()

Answer: a
Clarification: The spawn(f) runs f() or loads and executes file f in a new thread.

3. Which of the following reads the textual contents of a URL and returns as a string?
a) spawn(f);
b) load(filename,…);
c) readFile(file);
d) readUrl(url);

Answer: d
Clarification: Rhino defines a handful of important global functions that are not part of core JavaScript in which readUrl(url) reads the textual contents of a URL and return as a string.

4. Which Rhino command quits Rhino environment?
a) terminate()
b) exit()
c) quit()
d) close()

Answer: c
Clarification: quit() is a predefined command in rhino. The quit() command makes Rhino exit.

5. Which is a useful way to try out small and simple Rhino programs and one-liners?
a) Starting an interactive shell
b) Starting a one to one shell
c) Creating a thread to do simple programs
d) Starting a multiple shell

Answer: a
Clarification: Rhino is distributed as a JAR archive. Start it with a command line like this :
java -jar rhino1_7R2/js.jar program.js
If you omit program.js, Rhino starts an interactive shell, which is useful for trying out simple programs and one-liners.

6. 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
Clarification: 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

7. 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
Clarification: The output for the above code snippet is false as it is a writer and not a Reader.

8. 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
Clarification: 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.

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

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

10. 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
Clarification: 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.

11. 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
Clarification: Math.sin function evaluates the sine value of a particular input.
Math.PI function generates a value of 3.14.

12. 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
Clarification: 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.

13. 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
Clarification: Math.min() returns the lowest value in a list of arguments. The lowest value is -200, hence the output will be -200.

14. 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
Clarification: Math.floor(x) returns the value of x rounded down to its nearest integer. The value is decreased in the decimal is removed.

15. 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
Clarification: Math.sqrt function returns the square root of the argument passed to it. It is found in the math library of Javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *