The JavaTM Tutorial
Previous Page Lesson Contents Next Page

Start of Tutorial > Start of Trail

Search
Feedback Form


Trail: Getting Started

Answers to Questions and Exercises: Getting Started

Questions

1: When you compile a program written in the Java programming language, the compiler converts the human-readable source file into platform-independent code that a Java Virtual Machine can understand. What is this platform-independent code called?

Answer: Bytecode.

2: Which of the following is not a valid comment:

a. /** comment */
b. /* comment */
c. /* comment
d. // comment

Answer: c is an invalid comment.

3: What is the first thing you should check if at runtime you see the error: Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp.java.

Answer: Check your classpath; your class cannot be found.

Exercises

1: Change the HelloWorldApp.java(in a .java source file) program so that it displays Hola Mundo! instead of Hello World!.

Answer: This is the only line of code that must change:

System.out.println("Hello World!"); //Display the string.


2: Get the following file from the online Tutorial: Useless.java(in a .java source file) Compile and run this program. What is the output?

Answer: This program prints out your default locale; for example,

en_US

indicates U.S. English.


3: You can find a slightly modified version of HelloWorldApp here: HelloWorldApp2.java(in a .java source file)
The program has an error. Fix the error so that the program successfully compiles and runs. What was the error?

Answer: Here's the error you get when you try to compile the program:

HelloWorldApp2.java:7: unclosed string literal System.out.println("Hello World!); //Display the string. ^ HelloWorldApp2.java:7: ')' expected System.out.println("Hello World!); //Display the string. ^ 2 errors

To fix this mistake, you need to close the quotation marks around the string. Here is the correct line of code:

System.out.println("Hello World!"); //Display the string.

5: Download the following two source files:

Compile the files, and then run the resulting program. What is the output? If you have trouble compiling the files but have successfully compiled before, try unsetting the CLASSPATH environment variable, and then compile again.

Answer: The key is to compile and run the SecondClass. FirstClass does not have a main method, so it cannot run independently.

Here's the output (if your locale is U.S. English):

English (United States)


Previous Page Lesson Contents Next Page

Start of Tutorial > Start of Trail

Search
Feedback Form