Trail: Getting Started
Answers to Questions and Exercises: Getting Started

Questions

Question 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 1: Bytecode.

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

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

Answer 2: c is an invalid comment.

Question 3: What is the first thing you should check if the interpreter returns the error:

Exception in thread "main" java.lang.NoClassDefFoundError:
HelloWorldApp.java.

Answer 3: Check your classpath. The interpreter cannot find your class.

Exercises

Exercise 1: Change the HelloWorldApp.java program so that it displays Hola Mundo! instead of Hello World!.

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

final JLabel label = new JLabel("Hola Mundo");

Exercise 2: Get the following file from the online Tutorial: Useless.java
Compile and run this program. What is the output?

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

en_US
indicates U.S. English.

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

Answer 3: 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.

Exercise 4: Download the following two source files:

FirstClass.java
SecondClass.java
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 4: 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: Questions and Exercises: Getting Started