JavaScript Questions & Answers – Scripting Java with Rhino

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Scripting Java with Rhino”.

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

Answer: b
Explanation: 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()
View Answer

Answer: a
Explanation: 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);
View Answer

Answer: d
Explanation: 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.
advertisement
advertisement

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

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.*);
View Answer

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

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

advertisement
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
View Answer

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

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
View Answer

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.

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

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.

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
View Answer

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.

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
View Answer

Answer: a
Explanation: 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
View Answer

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.

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
View Answer

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.

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
View Answer

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.

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
View Answer

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.

Sanfoundry Global Education & Learning Series – Javascript Programming.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.