Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

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

  1. What is a Java package, and how is it used?

  2. What is a compilation unit?

  3. How are Java source code files named?

  4. What restrictions are placed on the location of a package statement within a source code file?

  5. Which package is always imported by default?

  6. What is the return type of a program's main() method?

  7. What is the argument type of a program's main() method?

  8. Which non-Unicode letter characters can be used as the first character of an identifier?

  9. Which characters can be used as the second character of an identifier, but not as the first character of an identifier?

  10. Are true and false keywords?

  11. Is null a keyword?

  12. Is sizeof a keyword?

  13. Name the eight primitive Java types.

  14. What is the range of the short type?

  15. What is the range of the char type?

  16. Is "abc" a primitive value?

  17. To what value is a variable of the boolean type automatically initialized?

  18. To what value is a variable of the String type automatically initialized?

Exam Questions

Exam Questions

  1. For the public class MyClass to successfully compile, which of the following must be true?

    1. MyClass must have a correctly formed main() method.

    2. MyClass must be defined in the file MyClass.java.

    3. MyClass must be defined in the MyClass package.

    4. MyClass must be imported.

  2. For a source code file that contains the public class Test to successfully compile, which of the following must be true?

    1. It must import java.lang.

    2. It must declare a public interface named Test.

    3. It must be named Test.java.

    4. It must have a package statement.

  3. For the MyProgram program to be compiled and run, which of the following must be true?

    1. The MyProgram class must be defined in MyProgram.java.

    2. MyProgram must be declared public.

    3. MyProgram must have a correctly formed main() method.

    4. MyProgram must import java.lang.

  4. Which of the following are true?

    1. If a package statement is included in a source code file, it must appear as the first non-blank, non-comment line.

    2. If an import statement is included in a source code file, it must appear as the first non-blank line.

    3. If a main() method is included in a source code file, it must appear as the first non-blank, non-comment line.

    4. If a public interface is declared in a source code file, it must have the same name as the source code file.

  5. Which of the following are valid main() methods?

    1. public static void main() { }

    2. public static void main(String[] argc) { }

    3. void main(String[] args) { }

    4. public static void main(String []args) { }

  6. What is the output of the following program when it is invoked using the command line java Test this is a test?

  7. class Test {
     public static void main(String[] args) {
     System.out.println(args[1]);
     }}
    
    1. this

    2. is

    3. a

    4. test

  8. Which of the following are valid Java comments?

    1. \\ This is a comment.

    2. /* This is a comment. */

    3. /** This is a comment. */

    4. \* This is a comment *\

  9. Which of the following are valid Java identifiers?

    1. %id

    2. $id

    3. _id

    4. #id

  10. Which of the following are valid Java identifiers?

    1. my-id

    2. my_id

    3. 101ids

    4. id101

  11. Which of the following are Java keywords?

    1. interface

    2. sizeof

    3. super

    4. volatile

  12. Which of the following are Java keywords?

    1. NULL

    2. null

    3. extends

    4. main

  13. Which of the following are primitive types?

    1. byte

    2. String

    3. integer

    4. Float

  14. What is the range of the short type?

    1. 0 to 216

    2. –(216) to 216

    3. –(215) to 215

    4. –(215) to 215 – 1

  15. What is the range of the char type?

    1. 0 to 216

    2. 0 to 216 – 1

    3. 0 to 215

    4. 0 to 215 – 1

  16. What is the octal equivalent of the decimal value 123?

    1. 0173

    2. 0123

    3. 0x123

    4. 0x173

  17. What is the hexadecimal equivalent of decimal 123?

    1. 0x173

    2. 0x123

    3. 0x7B

    4. 173

  18. What output is displayed as the result of executing the following statement?

  19. System.out.println("// Looks like a comment.");
    1. // Looks like a comment.

    2. / Looks like a comment.

    3. No output is displayed.

    4. The statement results in a compilation error.

  20. Which of the following are valid double literals?

    1. 1D

    2. 1E-5D

    3. e2d

    4. 1ed

  21. What is the output of the following program?

  22. 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]);
     }}
    
    1. true0.0

    2. true0

    3. false0.0

    4. false0

  23. What is the output of the following program?

  24. 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]);
     }}
    
    1. 0

    2. o0

    3. A NullPointerException is thrown

    4. null0

Answers to Review Questions

  1. 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."

  2. A compilation unit is a Java source code file. See the section "The Structure of Java Programs."

  3. 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."

  4. A package statement must appear as the first line in a source code file (excluding blank lines and comments). See the section "Identifying Packages."

  5. The java.lang package is always imported by default. See the section "Importing Classes and Interfaces from Other Packages."

  6. A program's main() method has a void return type. See the section "The main() Method."

  7. A program's main() method takes an argument of the String[] type. See the section "The main() Method."

  8. The non-Unicode letter characters $ and _ can appear as the first character of an identifier. See the section "Identifiers and Keywords."

  9. 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."

  10. The values true and false are not keywords. See the section "Identifiers and Keywords."

  11. The null value is not a keyword. See the section "Identifiers and Keywords."

  12. The sizeof operator is not a keyword. See the section "Identifiers and Keywords."

  13. The eight primitive types are byte, char, short, int, long, float, double, and boolean. See the section "Primitive Types and Literal Values."

  14. The range of the short type is –(215) to 215 – 1. See the section "Primitive Types and Literal Values."

  15. The range of the char type is 0 to 216 – 1. See the section "Primitive Types and Literal Values."

  16. The String literal "abc" is not a primitive value. See the section "Primitive Types and Literal Values."

  17. The default value of the boolean type is false. See the section "Automatic Initialization."

  18. The default value of the String type is null. See the section "Automatic Initialization."

Answers to Exam Questions

  1. 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."

  2. 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."

  3. 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."

  4. 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."

  5. 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."

  6. B. The String "is" is assigned to args[1]. See the section "The main() Method."

  7. B. and C. Comments use slashes and not backslashes. See the section "Comments."

  8. B. and C. The only special characters that can appear in an identifier are _ and $. See the section "Identifiers and Keywords."

  9. 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."

  10. A., C., and D. The sizeof operator is not a Java keyword. See the section "Identifiers and Keywords."

  11. C. NULL, null, and main are not Java keywords. See the section "Identifiers and Keywords."

  12. A. Neither String, integer, nor Float are primitive types. See the section "Primitive Types and Literal Values."

  13. D. The range of the short type is –(215) to 215 – 1. See the section "Primitive Types and Literal Values."

  14. B. The range of the char type is 0 to 216 – 1. See the section "Primitive Types and Literal Values."

  15. A. The octal value 0173 is equivalent to the decimal value 123. See the section "Primitive Types and Literal Values."

  16. C. The hexadecimal value 0x7B is equivalent to the decimal value 123. See the section "Primitive Types and Literal Values."

  17. A. Comments cannot appear in a String literal. See the section "Comments."

  18. 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."

  19. C. The default value of a boolean is false and the default value of a double is 0.0. See the section "Automatic Initialization."

  20. 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

  1. 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/.)

  • + Share This
  • 🔖 Save To Your Account