JavaScript Questions and Answers – Scripting Java with Rhino

This article we are go to through some of the most expected javascript questions and answers to help you get going in job interviews and make a good impression with your knowledge.

This set of Top Ten Javascript Multiple Choice questions and answers (MCQs) focuses on “Scripting Java with Rhino”.

1. Rhino is originated by
a. Microsoft
b. Mozilla
c. Apple
d. None of the mentioned

Explanation : Rhino is free software from Mozilla. You can download a copy from http://www.mozilla.org/rhino/

2. 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. Both a and b
d. None of the mentioned
c. Both a and b
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.

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);

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.

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

Explanation : 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 interative shell
b. Starting a one to one shell
c. Creating a thread to do simple programs
d. None of the mentioned

Explanation : Rhino is distributed as a JAR archieve. 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.
);

Explanation : Because packages and classes are represented as JavaScript objects, you can assign themto 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. Consider the following code snippet ?

var f = new java.io.File(“/tmp/test”); var out = new java.io.FileWriter(f); out instanceof java.io.Reader advertisements

What will be the output for the above code snippet?
a. Error
b. True
c. Exception
d. False

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

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

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

Explanation : Just like the JavaScript classes use new operator to instantiate, so does the Java classes.

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

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.

So, what other JavaScript questions are there about Scripting Java with Rhino? Let us know in the comments and we will include them in the article!!