Java JShell MCQ

1. What is JShell in Java?

a) A Java IDE
b) A Java package manager
c) An interactive Java REPL
d) A Java compiler

Answer:

c) An interactive Java REPL

Explanation:

JShell stands for Java Shell and is an interactive Read-Evaluate-Print Loop (REPL) tool introduced in Java 9, which allows for interactive Java programming.

2. What was JShell primarily introduced for?

a) Debugging complex Java applications
b) Managing Java packages
c) Learning and experimenting with Java code
d) Replacing the traditional Java compiler

Answer:

c) Learning and experimenting with Java code

Explanation:

JShell was introduced as a tool for learning Java and experimenting with Java code snippets interactively.

3. How is JShell launched?

a) By running ‘java -shell’ in the command line
b) By executing the ‘jshell’ command in the terminal
c) Through the Java IDE
d) By compiling a Java file

Answer:

b) By executing the ‘jshell’ command in the terminal

Explanation:

JShell is launched by entering the ‘jshell’ command in the terminal or command prompt.

4. Can JShell be used to execute multi-line Java statements?

a) Yes
b) No
c) Only single-line statements
d) Only in scripting mode

Answer:

a) Yes

Explanation:

JShell can execute both single-line and multi-line Java statements, making it suitable for testing small to complex code snippets.

5. Does JShell support tab completion?

a) Yes
b) No
c) Only for method names
d) Only for variable names

Answer:

a) Yes

Explanation:

JShell supports tab completion, helping users to easily complete commands, method names, and variable names.

6. What happens when you define a method in JShell?

a) It must be compiled
b) It can be immediately invoked
c) It’s stored in a file
d) It’s only for syntax checking

Answer:

b) It can be immediately invoked

Explanation:

Methods defined in JShell can be immediately invoked without the need for compiling or wrapping in a class.

7. How can you view the history of your commands in JShell?

a) Using the /history command
b) It’s not possible to view command history
c) By scrolling up in the terminal
d) Using the /list command

Answer:

a) Using the /history command

Explanation:

The /history command in JShell displays the history of the commands and code snippets entered.

8. Can you import external libraries and use them in JShell?

a) Yes
b) No
c) Only standard Java libraries
d) Only if they are in the same directory

Answer:

a) Yes

Explanation:

JShell allows importing external libraries and using them, provided that the libraries are in the classpath.

9. How do you exit JShell?

a) Using the /exit command
b) Closing the terminal window
c) Pressing Ctrl+C
d) Using the /quit command

Answer:

a) Using the /exit command

Explanation:

The /exit command is used to exit JShell.

10. How does JShell handle undeclared variables?

a) It automatically declares them
b) It throws an error
c) It asks for the type
d) It treats them as Object type

Answer:

b) It throws an error

Explanation:

If a variable is used without being declared, JShell will throw an error.

11. Can JShell be used for scripting?

a) Yes
b) No
c) Only in non-interactive mode
d) Only for Java scripts

Answer:

a) Yes

Explanation:

JShell can be used for scripting, allowing users to write and execute more complex code sequences.

12. What feature does JShell provide for lengthy outputs?

a) Automatic output shortening
b) Pagination
c) Scroll bars in the terminal
d) Output redirection to a file

Answer:

b) Pagination

Explanation:

For lengthy outputs, JShell provides pagination, making it easier to read and navigate through the output.

13. How do you save JShell session commands to a file?

a) /save filename
b) /export filename
c) Save is not supported in JShell
d) /write filename

Answer:

a) /save filename

Explanation:

The /save command followed by a filename saves the current JShell session commands to a file.

14. How can you evaluate expressions quickly in JShell?

a) By creating a class
b) By writing complete Java syntax
c) By typing the expression directly
d) Using a special evaluation command

Answer:

c) By typing the expression directly

Explanation:

JShell allows for the quick evaluation of expressions by directly typing them into the shell.

15. Does JShell provide feedback on syntax errors?

a) Yes
b) No
c) Only for certain types of errors
d) Only if the /feedback command is enabled

Answer:

a) Yes

Explanation:

JShell provides immediate feedback on syntax errors, helping users to quickly identify and correct mistakes in their code.

16. Can JShell execute Java files directly?

a) Yes
b) No
c) Only .jsh files
d) Only small Java files

Answer:

b) No

Explanation:

JShell is primarily designed for executing Java code snippets and expressions interactively, not for executing Java files directly.

17. How do you reload a previous JShell session?

a) /reload command
b) /restore filename
c) /open filename
d) This feature is not available in JShell

Answer:

c) /open filename

Explanation:

The /open command followed by a filename can be used to reload a previously saved JShell session.

18. How do you view the available JShell commands?

a) /commands
b) /help
c) /list commands
d) /show commands

Answer:

b) /help

Explanation:

The /help command in JShell provides a list of available commands and their descriptions.

19. What is a snippet in the context of JShell?

a) A saved session file
b) A small piece of Java code
c) A command
d) An external library

Answer:

b) A small piece of Java code

Explanation:

In JShell, a snippet refers to a small piece of Java code, such as a variable declaration, an expression, a statement, a method, or a class definition.

20. How do you modify the classpath in JShell?

a) Using the /env -class-path command
b) It’s not possible to modify the classpath in JShell
c) Modifying the CLASSPATH environment variable
d) Using the /classpath command

Answer:

a) Using the /env -class-path command

Explanation:

The /env -class-path command is used to modify the classpath in JShell, allowing users to include additional classes and libraries.

Leave a Comment

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

Scroll to Top