Home > Articles

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

Pearson IT Certification Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Pearson IT Certification and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Pearson IT Certification products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.pearsonitcertification.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020