Java8e_Gaddis_ch02.docx
Document Details
Uploaded by Deleted User
Tags
Full Transcript
**Starting Out with Java: From Control Structures through Objects 8e (Gaddis)** **Chapter 2 Java Fundamentals** **TRUE/FALSE** 1\. Programming style includes techniques for consistently putting spaces and indentation in a program to help create visual cues. ANS: T 2\. Both character and string...
**Starting Out with Java: From Control Structures through Objects 8e (Gaddis)** **Chapter 2 Java Fundamentals** **TRUE/FALSE** 1\. Programming style includes techniques for consistently putting spaces and indentation in a program to help create visual cues. ANS: T 2\. Both character and string literals can be assigned to a **char** variable. ANS: F 3\. A variable\'s scope is the part of the program that has access to that variable. ANS: T 4\. Named constants are initialized with a value and that value cannot change during the execution of the program. ANS: T 5\. When you call one of the **Scanner** class\'s methods to read a primitive value, such as **nextInt** or **nextDouble**, and then call the **nextLine** method to read a string, an annoying and hard-to-find problem can occur. ANS: T 6\. Class names and keywords are examples of variables. ANS: F 7\. The Java API provides a class named **Math** that contains numerous methods which are useful for performing complex mathematical operations. ANS: T 8\. The **System.out.printf** method allows you to format output in a variety of ways. ANS: T 9\. A Java program will not compile unless it contains the correct line numbers. ANS: F 10\. Java is not case sensitive. ANS: F 11\. If the compiler encounters a statement that uses a variable before the variable is declared, an error will result. ANS: T 12\. If you use the var keyword to declare a variable, you cannot initialize the variable with a value. ANS: F **MULTIPLE CHOICE** 1\. Which of the following is a value that is written into the code of a program? ---- ------------------------- ---- ------------- a. a literal c. an operator b. an assignment statement d. a variable ---- ------------------------- ---- ------------- ANS: A 2\. A Java program must have at least one of the following: ---- --------------------------------------- a. a comment b. a class definition c. a **System.out.println();** statement d. a variable declaration ---- --------------------------------------- ANS: B 3\. Which of the following would contain the translated Java byte code for a program named Demo? ---- ----------- ---- ------------ a. Demo.java c. Demo.class b. Demo.code d. Demo.byte ---- ----------- ---- ------------ ANS: C 4\. Which of the following is a named storage location in the computer\'s memory? ---- ------------- ---- ------------ a. a literal c. a constant b. an operator d. a variable ---- ------------- ---- ------------ ANS: D 5\. Which of the following is not a valid Java comment? ---- --------------------------- ---- -------------------------- a. **/\*\* Comment one \*/ Comment three** b. **\*/ Comment two /\*** d. **/\* Comment four \*/** ---- --------------------------- ---- -------------------------- ANS: B 6\. To compile a program named First you would use which of the following commands? ---- ----------------- ---- --------------------- a. java First.java c. javac First.java b. javac First d. compile First.javac ---- ----------------- ---- --------------------- ANS: C 7\. A Java source file must be saved with the extension ---- ------------ ---- ------------ a. **.java** c. **.src** b. **.javac** d. **.class** ---- ------------ ---- ------------ ANS: A 8\. Which of the following is not a rule that must be followed when naming identifiers? ---- --------------------------------------------------------------------------------------------------------------- a. After the first character, you may use the letters a-z, A-Z, an underscore, a dollar sign, or the digits 0-9. b. Identifiers can contain spaces. c. Uppercase and lowercase characters are distinct. d. The first character must be one of the letters a-z, A-Z, an underscore, or a dollar sign. ---- --------------------------------------------------------------------------------------------------------------- ANS: B 9\. Character literals are enclosed in \_\_\_\_\_\_\_\_\_\_ and string literals are enclosed in \_\_\_\_\_\_\_\_\_\_. ---- ------------------------------ a. single quotes, double quotes b. double quotes, single quotes c. single quotes, single quotes d. double quotes, double quotes ---- ------------------------------ ANS: A 10\. Variables are classified according to their ---- -------- ---- ------------ a. names c. locations b. values d. data types ---- -------- ---- ------------ ANS: D 11\. What is the result of the following expression? ---- --------- ---- -------- ---- ------- ---- ------- a. **105** b. **12** c. **7** d. **8** ---- --------- ---- -------- ---- ------- ---- ------- ANS: C 12\. What is the result of the following expression? ---- -------- ---- --------- ---- ------- ---- -------- a. **-5** b. **-50** c. **5** d. **25** ---- -------- ---- --------- ---- ------- ---- -------- ANS: C 13\. In the following Java statement, what value is stored in the variable **name**? ---- ------------------------------------------------------ a. **\"name\"** b. the memory address where **\"John Doe\"** is located c. the memory address where **name** is located d. **John Doe** ---- ------------------------------------------------------ ANS: B 14\. What is the value of **z** after the following statements have been executed? ---- ---------- ---- ------- ---- ------- ---- --------- a. **8.25** b. **4** c. **0** d. **8.0** ---- ---------- ---- ------- ---- ------- ---- --------- ANS: D 15\. What output will be displayed as a result of executing the following code? ---- --------------------- a. **x = 160, y = 80** b. **x = 32, y = 4** c. **x = 37, y = 5** d. **x = 9, y = 52** ---- --------------------- ANS: C 16\. Which of the following statements will correctly convert the data type, if **x** is a **float** and **y** is a **double**? ---- --------------------- ---- ------------------- a. **x = float y;** c. **x = (float)y;** b. **x = \y;** d. **x = y;** ---- --------------------- ---- ------------------- ANS: C 17\. Which of the following statements is invalid? ---- --------------------------- ---- ------------------------- a. **double r = 9.4632E15;** c. **double r = 2.9X106;** b. **double r = 9.4632e15;** d. **double r = 326.75;** ---- --------------------------- ---- ------------------------- ANS: C 18\. To print **\"Hello, world\"** on the monitor, which of the following Java statements should be used? ---- ------------------------------------------- a. **System.out.println(\"Hello, world\");** b. **System Print = \"Hello, world\";** c. **SystemOutPrintln(\'Hello, world\');** d. **system.out.println(Hello, world);** ---- ------------------------------------------- ANS: A 19\. The **boolean** data type may contain which of the following range of values? ---- -------------------------------------- a. **-128 to + 127** b. **true or false** c. **-2,147,483,648 to +2,147,483,647** d. **-32,768 to +32,767** ---- -------------------------------------- ANS: B 20\. Variables of the **boolean** data type are useful for ---- ----------------------------------------------------- a. evaluating conditions that are either true or false b. working with small integers c. working with very large integers d. evaluating scientific notation ---- ----------------------------------------------------- ANS: A 21\. What would be displayed as a result of executing the following code? +-----------------------------------+-----------------------------------+ | a. | **There are 583** | | | | | | **hens in the hen house.** | +-----------------------------------+-----------------------------------+ | b. | **There are 5785** | | | | | | **hens in the hen house.** | +-----------------------------------+-----------------------------------+ | c. | **There are x5\\nhens in the hen | | | house.** | +-----------------------------------+-----------------------------------+ | d. | **There are 5785 hens in the hen | | | house.** | +-----------------------------------+-----------------------------------+ ANS: B 22\. What would be displayed as a result of executing the following code? ---- -------------------- ---- ----------------------------------------- a. **x = 22, y = 26** c. **x = 22, y = 88** b. **x = 22, y = 4** d. Nothing. There is an error in the code. ---- -------------------- ---- ----------------------------------------- ANS: D 23\. What would be displayed as a result of executing the following code? ---- ------------------------------- a. **x = 27, y = 3.333, z = 18** b. **x = 27, y = 2, z = 18** c. **x = 37, y = -14, z = 4** d. **x = 27, y = 3, z = 18** ---- ------------------------------- ANS: D 24\. What is the value of **z** after the following code is executed? ---- --------- ---- --------- ---- --------- ---- ---------- a. **5.6** b. **3.0** c. **5.0** d. **5.60** ---- --------- ---- --------- ---- --------- ---- ---------- ANS: C 25\. Which of the following statements correctly creates a **Scanner** object for keyboard input? ---- ------------------------------------------------- a. **Scanner kbd = new Scanner(System.keyboard);** b. **Scanner keyboard = new Scanner(System.in);** c. **Scanner keyboard(System.in);** d. **Keyboard scanner = new Keyboard(System.in);** ---- ------------------------------------------------- ANS: B 26\. Which **Scanner** class method reads a **String**? ---- -------------- ---- ---------------- a. **nextLine** c. **nextString** b. **charAt** d. **getLine** ---- -------------- ---- ---------------- ANS: A 27\. The primitive data types only allow a(n) \_\_\_\_\_\_\_\_\_\_ to hold a single value. ---- --------- ---- ---------- a. class c. object b. literal d. variable ---- --------- ---- ---------- ANS: D 28\. In Java, \_\_\_\_\_\_\_\_\_\_ must be declared before they can be used. ---- ----------- ---- ---------- a. variables c. keywords b. literals d. comments ---- ----------- ---- ---------- ANS: A 29\. If the following Java statements are executed, what will be displayed? +-----------------------------------+-----------------------------------+ | a. | The top three winners are | | | | | | Jody, the Giant | | | | | | Buffy, the Barbarian | | | | | | Adelle, the Alligator | +-----------------------------------+-----------------------------------+ | b. | The top three winners are Jody, | | | the Giant\\nBuffy, the | | | BarbarianAdelle, and the Albino | +-----------------------------------+-----------------------------------+ | c. | The top three winners are | | | | | | Jody, the Giant\\nBuffy, the | | | BarbarianAdelle, the Alligator | +-----------------------------------+-----------------------------------+ | d. | The top three winners are | | | | | | Jody, the Giant | | | | | | Buffy, the BarbarianAdelle, the | | | Alligator | +-----------------------------------+-----------------------------------+ ANS: D 30\. A value that is written into the code of a program is a(n) \_\_\_\_\_\_\_\_\_\_. ---- ---------------------- ---- ---------- a. literal c. variable b. assignment statement d. operator ---- ---------------------- ---- ---------- ANS: A 31\. When the + operator is used with strings, it is known as the ---- ------------------------------- ---- ------------------------------ a. assignment operator c. addition operator b. string concatenation operator d. combines assignment operator ---- ------------------------------- ---- ------------------------------ ANS: B 32\. What would be printed out as a result of the following code? +-----------------------------------+-----------------------------------+ | a. | The quick brown fox jumped over | | | the \\nslow moving hen. | +-----------------------------------+-----------------------------------+ | b. | The quick brown fox jumped over | | | the | | | | | | slow moving hen. | +-----------------------------------+-----------------------------------+ | c. | The quick brown fox | | | | | | jumped over the | | | | | | slow moving hen. | +-----------------------------------+-----------------------------------+ | d. | Nothing - this is an error | +-----------------------------------+-----------------------------------+ ANS: D 33\. Which of the following is ***not*** a rule that must be followed when naming identifiers? ---- ------------------------------------------------------------------------------------------------------------ a. The first character must be one of the letters a-z, A-Z, and underscore or a dollar sign. b. Identifiers can contain spaces. c. Uppercase and lowercase characters are distinct. d. After the first character, you may use the letters a-z, A-Z, the underscore, a dollar sign, or digits 0-9. ---- ------------------------------------------------------------------------------------------------------------ ANS: B 34\. Which of the following cannot be used as identifiers in Java? ---- ---------------- ---- ---------- a. variable names c. keywords b. class names d. objects ---- ---------------- ---- ---------- ANS: C 35\. Which of the following is not a primitive data type? ---- ------- ---- -------- a. short c. float b. long d. string ---- ------- ---- -------- ANS: D 36\. Which of the following is valid? +-----------------+-----------------+-----------------+-----------------+ | a. | float y; | c. | float w; | | | | | | | | y = 54.9; | | w = 1.0f; | +-----------------+-----------------+-----------------+-----------------+ | b. | float y; | d. | float v; | | | | | | | | double z; | | v = 1.0 | | | | | | | | z = 934.21; | | | | | | | | | | y = z; | | | +-----------------+-----------------+-----------------+-----------------+ ANS: C 37\. If x has been declared an int, which of the following statements is invalid? ---- ------------- ---- ------------ a. x = 0; c. x = 1,000; b. x = -59832; d. x = 592 ---- ------------- ---- ------------ ANS: C 38\. To display the output on the next line, you can use the println method or use the \_\_\_\_\_\_\_\_\_\_ escape sequence in the print method. ---- ----- ---- ----- ---- ----- ---- ----- a. \\n b. \\r c. \\t d. \\b ---- ----- ---- ----- ---- ----- ---- ----- ANS: A 39\. Every Java application program must have ---- --------------------- ---- ------------------------- a. a class named MAIN c. at least two data types b. a method named main d. integer variables ---- --------------------- ---- ------------------------- ANS: B 40\. What will be displayed as a result of executing the following code? +-----------------+-----------------+-----------------+-----------------+ | a. | I am enjoying | c. | I am enjoying | | | this class. | | this class. | | | | | | | | I AM ENJOYING | | I AM ENJOYING | | | THIS CLASS. | | THIS CLASS. | | | | | | | | i am enjoying | | i am enjoying | | | this class. | | this class. | | | | | | | | Character at | | Character at | | | index x = e | | index x = n | | | | | | | | msg has 24 | | msg has 24 | | | characters. | | characters. | +-----------------+-----------------+-----------------+-----------------+ | b. | I am enjoying | d. | I am enjoying | | | this class. | | this class. | | | | | | | | I AM ENJOYING | | I AM ENJOYING | | | THIS CLASS. | | THIS CLASS. | | | | | | | | i am enjoying | | i am enjoying | | | this class. | | this class. | | | | | | | | Character at | | Character at | | | index x = e | | index x = n | | | | | | | | msg has 25 | | msg has | | | characters. | | 25characters. | +-----------------+-----------------+-----------------+-----------------+ ANS: D 41\. What will be displayed as a result of executing the following code? +-----------------+-----------------+-----------------+-----------------+ | a. | 9 | c. | 9 45 16 | | | | | | | | 45 | | | | | | | | | | 16 | | | +-----------------+-----------------+-----------------+-----------------+ | b. | 94516 | d. | Nothing. This | | | | | is an error | +-----------------+-----------------+-----------------+-----------------+ ANS: D 42\. This is an interactive program that lets you enter Java programming statements and immediately see each statement\'s results. ---- ------------ a. PowerShell b. JShell c. JavaShell d. JTerminal ---- ------------ ANS: B 43\. If you use this keyword to declare a local variable, you do not have to specify the variable\'s data type. ---- ------- a. var b. auto c. type d. local ---- ------- ANS: A 44\. How would you rewrite the following statement using the word var to declare the variable? int value = 99; ---- --------------------- a. var int value = 99; b. int var value = 99; c. var value = 99; d. var value;\ value = 99; ---- --------------------- ANS: C