Chapter 2

Additional Web Questions

1) Many instances of an object can be created from a single class definition.

a)_____ true

b)_____ false

Answer: True. Once a class is defined, you can create as many instances of the object defined within the class as needed within your program.

2) Which of the following lines is appropriate syntax for defining a variable named myApplet?

a)_____ public class myApplet extends Applet;

b)_____ #import myApplet;

c)_____ String myApplet;

d)_____ void myApplet();

Answer: C. The definition of a String variable named myApplet is shown as item C.

3) Which of the following lines is appropriate syntax for defining a class named MyApplet?

a)_____ public class MyApplet extends Applet;

b)_____ #import MyApplet;

c)_____ String MyApplet;

d)_____ void MyApplet();

Answer: A. The definition of a class named MyApplet is shown correctly as item A.

4) Which of the following lines is appropriate syntax for informing the Java compiler that you will be referencing an external class named MyApplet?

a)_____ public class MyApplet extends Applet;

b)_____ #import MyApplet;

c)_____ String MyApplet;

d)_____ void MyApplet();

Answer: B. Informing the compiler that you will be referencing external classes is done via an import statement. Item B displays the correct syntax for an import statement.

5) Which of the following lines does not demonstrate appropriate syntax for a single-line comment?

a)_____ // This is a single-line comment

b)_____ // This is a single-line comment //

c)_____ /* This is a single-line comment

d)_____ /* This is a single-line comment */

Answer: C. A single-line comment that begins with a double-slash (//) extends to the end of the line on which it began, so both examples above which use the double-slash comment style are acceptable. For the slash-star (/*) star-slash (*/) comment syntax, item C does not contain the necessary ending star-slash.

6) Which of the following set of lines does not demonstrate appropriate syntax for a multi-line comment?

a)_____ // This is a comment

// that spans multiple lines

b)_____ /* This is a comment

that spans multiple lines */

c)_____ /* This is a comment

/* that spans multiple lines

d)_____ /*

* This is a comment

* that spans multiple lines

*/

Answer: C. Multiple-line comments can be constructed with sequential single-line comments, or using the slash-star star-slash syntax. Item C does not end the comment properly with a star-slash, and as such is incorrect.

7) Which of the following lines demonstrates appropriate syntax for declaring a class named MyObject that is derived from an existing class named OurObject?

a)_____ public class OurObject extends MyObject

b)_____ public class MyObject extends OurObject

c)_____ public class MyObject implements OurObject

d)_____ class MyObject : public OurObject

Answer B. The correct syntax for declaring a class that is derived from another class is shown in item B.

8) Which of the following lines demonstrates appropriate syntax for calling a method named doSomething?

a)_____ doSomething();

b)_____ #import doSomething();

c)_____ public class doSomething();

d)_____ /* doSomething */

Answer: A. The appropriate syntax for calling a method named doSomething is shown in item A.

9) Which of the following lines demonstrates appropriate syntax for assigning the value “Hello World” to a String variable named theOutput?

a)_____ theOutput(“Hello World”);

b)_____ “Hello World” = theOutput;

c)_____ theOutput = “Hello World”;

d)_____ theOutput == “Hello World”;

Answer: C. Assigning a value to a String variable named theOutput is done as shown in item C.

10) Gee, learning Java does not seem so difficult after all!

a)_____ True

b)_____ False

Answer: A. Naturally, we would like to believe that the clarity of this text has a lot to do with your ease of learning!