Chapter Summary
This chapter reviewed the basics of Java programming. You learned how to create packages, import classes and interfaces from other packages, and create a program's main() method. You also learned how command-line variables are accessed, identifiers are formed, and which keywords the Java language reserves. You were introduced to each primitive type, learned its range of values, and learned how to create literal values of each type (as well as the String type). You should now be prepared to test your knowledge of these subjects. The review questions and exam questions in the "Apply Your Knowledge" section will let you know how well you understand this material and will give you an idea of how you will do in related exam questions. They will also indicate which material you need to study further.
Key Terms
Automatic variable
Class
Command-line argument
Comment
Compilation unit
Constant
Constructor
Field variable
Import
Interface
Keyword
Local variable
Method
Naming context
Object
Package
Primitive type
Source code file
Review Questions
What is a Java package, and how is it used?
What is a compilation unit?
How are Java source code files named?
What restrictions are placed on the location of a package statement within a source code file?
Which package is always imported by default?
What is the return type of a program's main() method?
What is the argument type of a program's main() method?
Which non-Unicode letter characters can be used as the first character of an identifier?
Which characters can be used as the second character of an identifier, but not as the first character of an identifier?
Are true and false keywords?
Is null a keyword?
Is sizeof a keyword?
Name the eight primitive Java types.
What is the range of the short type?
What is the range of the char type?
Is "abc" a primitive value?
To what value is a variable of the boolean type automatically initialized?
To what value is a variable of the String type automatically initialized?
Exam Questions
Exam Questions
-
For the public class MyClass to successfully compile, which of the following must be true?
-
MyClass must have a correctly formed main() method.
-
MyClass must be defined in the file MyClass.java.
-
MyClass must be defined in the MyClass package.
-
MyClass must be imported.
-
-
For a source code file that contains the public class Test to successfully compile, which of the following must be true?
-
It must import java.lang.
-
It must declare a public interface named Test.
-
It must be named Test.java.
-
It must have a package statement.
-
-
For the MyProgram program to be compiled and run, which of the following must be true?
-
The MyProgram class must be defined in MyProgram.java.
-
MyProgram must be declared public.
-
MyProgram must have a correctly formed main() method.
-
MyProgram must import java.lang.
-
-
Which of the following are true?
-
If a package statement is included in a source code file, it must appear as the first non-blank, non-comment line.
-
If an import statement is included in a source code file, it must appear as the first non-blank line.
-
If a main() method is included in a source code file, it must appear as the first non-blank, non-comment line.
-
If a public interface is declared in a source code file, it must have the same name as the source code file.
-
-
Which of the following are valid main() methods?
-
public static void main() { }
-
public static void main(String[] argc) { }
-
void main(String[] args) { }
-
public static void main(String []args) { }
-
-
What is the output of the following program when it is invoked using the command line java Test this is a test?
-
this
-
is
-
a
-
test
-
Which of the following are valid Java comments?
-
\\ This is a comment.
-
/* This is a comment. */
-
/** This is a comment. */
-
\* This is a comment *\
-
Which of the following are valid Java identifiers?
-
%id
-
$id
-
_id
-
#id
-
Which of the following are valid Java identifiers?
-
my-id
-
my_id
-
101ids
-
id101
-
Which of the following are Java keywords?
-
interface
-
sizeof
-
super
-
volatile
-
Which of the following are Java keywords?
-
NULL
-
null
-
extends
-
main
-
Which of the following are primitive types?
-
byte
-
String
-
integer
-
Float
-
What is the range of the short type?
-
0 to 216
-
(216) to 216
-
(215) to 215
-
(215) to 215 1
-
What is the range of the char type?
-
0 to 216
-
0 to 216 1
-
0 to 215
-
0 to 215 1
-
What is the octal equivalent of the decimal value 123?
-
0173
-
0123
-
0x123
-
0x173
-
What is the hexadecimal equivalent of decimal 123?
-
0x173
-
0x123
-
0x7B
-
173
-
What output is displayed as the result of executing the following statement?
-
System.out.println("// Looks like a comment.");
-
// Looks like a comment.
-
/ Looks like a comment.
-
No output is displayed.
-
The statement results in a compilation error.
-
-
Which of the following are valid double literals?
-
1D
-
1E-5D
-
e2d
-
1ed
-
-
What is the output of the following program?
-
true0.0
-
true0
-
false0.0
-
false0
-
What is the output of the following program?
-
0
-
o0
-
A NullPointerException is thrown
-
null0
class Test { public static void main(String[] args) { System.out.println(args[1]); }}
public class Question { public static void main(String args[]){ boolean[] b = new boolean[2]; double[] d = new double[2]; System.out.print(b[0]); System.out.println(d[1]); }}
public class Question { public static void main(String args[]){ Object[] o = new Object[2]; byte[] b = new byte[2]; System.out.print(o[0]); System.out.println(b[1]); }}
Answers to Review Questions
-
A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces. See the section "Identifying Packages."
-
A compilation unit is a Java source code file. See the section "The Structure of Java Programs."
-
A Java source code file takes the name of a public class or interface that is defined within the file. A source code file can contain at most one public class or interface. If a public class or interface is defined within a source code file, then the source code file must take the name of the public class or interface. If no public class or interface is defined within a source code file, then the file can take on a name that is different from its classes and interfaces. Source code files are case sensitive and use the .java extension. See the section "The Structure of Java Programs."
-
A package statement must appear as the first line in a source code file (excluding blank lines and comments). See the section "Identifying Packages."
-
The java.lang package is always imported by default. See the section "Importing Classes and Interfaces from Other Packages."
-
A program's main() method has a void return type. See the section "The main() Method."
-
A program's main() method takes an argument of the String[] type. See the section "The main() Method."
-
The non-Unicode letter characters $ and _ can appear as the first character of an identifier. See the section "Identifiers and Keywords."
-
The digits 0 through 9 cannot be used as the first character of an identifier, but they can be used after the first character of an identifier. See the section "Identifiers and Keywords."
-
The values true and false are not keywords. See the section "Identifiers and Keywords."
-
The null value is not a keyword. See the section "Identifiers and Keywords."
-
The sizeof operator is not a keyword. See the section "Identifiers and Keywords."
-
The eight primitive types are byte, char, short, int, long, float, double, and boolean. See the section "Primitive Types and Literal Values."
-
The range of the short type is (215) to 215 1. See the section "Primitive Types and Literal Values."
-
The range of the char type is 0 to 216 1. See the section "Primitive Types and Literal Values."
-
The String literal "abc" is not a primitive value. See the section "Primitive Types and Literal Values."
-
The default value of the boolean type is false. See the section "Automatic Initialization."
-
The default value of the String type is null. See the section "Automatic Initialization."
Answers to Exam Questions
-
B. A class does not need a main() method to compile, nor does it need to be defined in a package or imported. However, a public class needs to be defined in a source code file of the same name. See the section "The Structure of Java Programs."
-
C. A source code file must take the same name as any public class or interface that it declares. See the section "The Structure of Java Programs."
-
C. For a class to be compiled and run, it must have a correctly formed main() method. It does not need to be declared public. See the section "The main() Method."
-
A. and D. Package statements must appear as the first non-blank, non-comment line of a source code file (if they appear at all). If a public class or interface is declared in a source code file, then the source code file must take the name of the public class or interface. See the section "Identifying Packages."
-
B. and D. The main() method of answer A is missing an argument. The main() method of answer C is missing the public and static modifiers. See the section "The main() Method."
-
B. The String "is" is assigned to args[1]. See the section "The main() Method."
-
B. and C. Comments use slashes and not backslashes. See the section "Comments."
-
B. and C. The only special characters that can appear in an identifier are _ and $. See the section "Identifiers and Keywords."
-
B. and D. The only special characters that can appear in an identifier are _ and $. Digits cannot be used as the first character of an identifier. See the section "Identifiers and Keywords."
-
A., C., and D. The sizeof operator is not a Java keyword. See the section "Identifiers and Keywords."
-
C. NULL, null, and main are not Java keywords. See the section "Identifiers and Keywords."
-
A. Neither String, integer, nor Float are primitive types. See the section "Primitive Types and Literal Values."
-
D. The range of the short type is (215) to 215 1. See the section "Primitive Types and Literal Values."
-
B. The range of the char type is 0 to 216 1. See the section "Primitive Types and Literal Values."
-
A. The octal value 0173 is equivalent to the decimal value 123. See the section "Primitive Types and Literal Values."
-
C. The hexadecimal value 0x7B is equivalent to the decimal value 123. See the section "Primitive Types and Literal Values."
-
A. Comments cannot appear in a String literal. See the section "Comments."
-
A. and B. Because the value e2d begins with a letter, it is treated as an identifier. Because no signed integer value appears after the e in 1ed, it is an invalid double literal. See the section "Identifiers and Keywords."
-
C. The default value of a boolean is false and the default value of a double is 0.0. See the section "Automatic Initialization."
-
D. The default value of an object is null and the default value of a byte is 0. See the section "Automatic Initialization."
Suggested Readings and Resources
Gosling, James, Bill Joy, and Guy Steele. The Java Language Specification: Second Edition. Addison Wesley, 2000. (Available at http://java.sun.com/j2se/1.3/docs/.)