Skip to main content

TEST BANK for Starting Out With Java Control Structures through Objects, 8th edition By Tony Gaddis

Page 1

Starting Out with Java: From Control Structures through Objects 8e, GE (Gaddis) Chapter 1 Introduction to Computers and Java TRUE/FALSE 1. Logical errors are mistakes that cause the program to produce erroneous results. ANS: T 2. The Java Virtual Machine is a program that reads Java byte code instructions and executes them as they are read. ANS: T 3. Colons are used to indicate the end of a Java statement. ANS: F 4. Compiled byte code is also called source code. ANS: F 5. Application software refers to programs that make the computer useful to the user. ANS: T 6. Each byte is assigned a unique number known as an address. ANS: T 7. Encapsulation refers to the combining of data and code into a single object. ANS: T 8. Java source files end with the .class extension. ANS: F 9. A procedure is a set of programming language statements that, together, perform a specific task. ANS: T 10. A solid-state drive has no moving parts and operates faster than a traditional disk drive. ANS: T 11. The computer is a tool used by so many professionals that it cannot be easily categorized.

© 2024 Pearson Education Ltd. All Rights Reserved.


ANS: T 12. Without programmers, the users of computers would have no software, and, without software, computers would not be able to do anything. ANS: T 13. The contents of a variable cannot be changed while the program is running. ANS: F 14. Java source files end with the .class extension. ANS: F 15. When an object's internal data is hidden from outside code and access to that data is retricted to the object's methods, the data is protected from accidental corruption. ANS: T MULTIPLE CHOICE 1. While __________ is centered on creating procedures, __________ is centered on creating objects. a. b. c. d.

procedural programming, object-oriented programming object-oriented programming, procedural programming routine programming, method programming procedural programming, class programming

ANS: A 2. RAM is usually a. b. c. d.

an input/output device a volatile type of memory, used for temporary storage secondary storage a static type of memory, used for permanent storage

ANS: B 3. The two primary methods of programming in use today are a. b. c. d.

procedural and object-oriented hardware and software practical and theoretical desktop and mobile

ANS: A 4. Validating the results of a program is important to a. b. c. d.

correct runtime errors make sure the program solves the original problem create a model of the program correct syntax error

© 2024 Pearson Education Ltd. All Rights Reserved.


ANS: B 5. Software refers to a. b. c. d.

programs the physical components a computer is made of firmware data stored in RAM

ANS: A 6. An object typically hides its data but allows outside code access to a. b. c. d.

the pseudocode the methods that operate on the data private data members the data files

ANS: B 7. Variables are a. symbolic names made up by the programmer and once created, their values cannot be changed b. operators that perform operations on one or more operands c. symbolic names made up by the programmer that represent memory locations d. reserved words ANS: C 8. __________ refers to the physical components that a computer is made of. a. Input b. Main memory

c. Control unit d. Hardware

ANS: D 9. A characteristic of __________ is that only an object's methods are able to directly access and make changes to an object's data. a. classes b. procedures

c. data hiding d. component reusability

ANS: C 10. Because Java byte code is the same on all computers, compiled Java programs a. b. c. d.

are nonexistent must be re-compiled for each different machine before they can be run are highly portable cannot run on computers with different operating systems

ANS: C 11. A cross between human language and a programming language is called a. a compiler b. pseudocode

c. the Java Virtual Machine d. the Java language

© 2024 Pearson Education Ltd. All Rights Reserved.


ANS: B 12. Byte code instructions are a. b. c. d.

machine code instructions syntax errors read and interpreted by the JVM another name for source code

ANS: C 13. One type of design tool used by programmers when creating a model of a program is a. the ALU b. byte code

c. syntax d. pseudocode

ANS: D 14. Keywords are a. b. c. d.

symbols or words that perform operations on one or more operands words or characters representing values that are defined by the programmer the data names in your program words that have a special meaning in the programming language

ANS: D 15. Computers can do many different jobs because they are a. programmable b. reliable

c. automated d. electronic

ANS: A 16. A(n) __________ is a software entity that contains data and procedures. a. object b. program

c. class d. method

ANS: A 17. Application software refers to a. b. c. d.

the programs that make the computer useful to the user the operating system keywords pseudocode

ANS: A 18. The end of a Java statement is indicated by a ________. a. bracket (}) b. asterisk (*)

c. semicolon (;) d. colon (:)

ANS: C 19. What is syntax? a. the rules that must be followed when writing a program

© 2024 Pearson Education Ltd. All Rights Reserved.


b. the words that have a special meaning in the programming language c. the symbols or words that perform operations in a program d. the words or characters that are defined by the programmer ANS: A 20. A set of programming language statements that perform a specific task is a(n) a. pseudocode chart b. source code

c. object d. procedure

ANS: D 21. The central processing unit (CPU) consists of two parts which are a. b. c. d.

the input and output devices the control unit and the arithmetic and logic unit (ALU) the control unit and main memory the arithmetic and logic unit (ALU) and main memory

ANS: B 22. There are __________ bits in a byte. a. 4 b. 8

c. 16 d. 32

ANS: B 23. A runtime error is usually the result of a. a logical error b. a syntax error

c. a compiler error d. bad data

ANS: A 24. A(n) __________ is used to write computer programs. a. pseudocode document b. operating system

c. application d. programming language

ANS: D 25. __________ refers to the physical components that a computer is made of. a. The device b. Hardware

c. Software d. The system

ANS: B 26. A computer program is a. b. c. d.

the same as main memory only used for desktop computers a set of instructions that allow the computer to solve a problem or perform a task another name for an operating system

ANS: C 27. Which of the following is not part of the programming process?

© 2024 Pearson Education Ltd. All Rights Reserved.


a. b. c. d.

defining and modeling the problem entering code and compiling it testing and debugging All of these are parts of the programming process

ANS: D 28. Computer programming is a. an art b. a science

c. both of the above d. neither of the above

ANS: C 29. The original name for Java was a. JavaScript b. HotJava

c. Elm d. Oak

ANS: D 30. A program is a sequence of instructions stored in a. the CPU c. software b. the computer's memory d. firmware ANS: B 31. Which of the following is not one of the major components of a typical computer system? a. the CPU b. input/output devices c. main memory d. secondary storage devices e. All of the above are major components ANS: E 32. Byte code instructions are a. another name for source code b. syntax errors c. machine code instructions d. read and interpreted by the JVM ANS: D 33. Which of the following is a software entity that contains data and procedures? a. a method c. a class b. an object d. a program ANS: B 34. Each different type of CPU has its own a. syntax b. firmware

c. machine language d. software

ANS: C 35. Internally, the central processing unit (CPU) consists of two parts which are __________ a. the control unit and the arithmetic/logic unit (ALU)

© 2024 Pearson Education Ltd. All Rights Reserved.


b. the control unit and main memory c. the arithmetic/logic unit (ALU) and main memory d. the input and output devices ANS: A 36. Java was developed by a. Microsoft b. IBM

c. Sun Microsystems d. Hewlett-Packard

ANS: C 37. Which of the following will compile a program called ReadIt? a. java ReadIt.java c. javac ReadIt.java b. java ReadIt.javac d. javac ReadIt.javac ANS: C 38. Which of the following will run the compiled program called ReadIt? a. java ReadIt.java c. run ReadIt b. java ReadIt d. go ReadIt ANS: B

© 2024 Pearson Education Ltd. All Rights Reserved.


Starting Out with Java: From Control Structures through Objects 8e, GE (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.

© 2024 Pearson Education Ltd. All Rights Reserved.


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 b. an assignment statement

c. an operator d. a variable

ANS: A 2. A Java program must have at least one of the following: a. b. c. d.

a comment a class definition a System.out.println(); statement 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 b. an operator

c. a constant d. a variable

ANS: D 5. Which of the following is not a valid Java comment? a. /** Comment one */ b. */ Comment two /*

c. // Comment three 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 javac First b. d. compile First.javac ANS: C

© 2024 Pearson Education Ltd. All Rights Reserved.


7. A Java source file must be saved with the extension a. .java b. .javac

c. .src 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. b. c. d.

single quotes, double quotes double quotes, single quotes single quotes, single quotes double quotes, double quotes

ANS: A 10. Variables are classified according to their a. names b. values

c. locations d. data types

ANS: D 11. What is the result of the following expression? 17 % 3 * 2 - 12 + 15 a. 105

b. 12

c. 7

d. 8

c. 5

d. 25

ANS: C 12. What is the result of the following expression? 10 + 5 * 3 - 20 a. -5

b. -50

ANS: C 13. In the following Java statement, what value is stored in the variable name? String name = "John Doe"; a. b. c. d.

"name" the memory address where "John Doe" is located the memory address where name is located John Doe

ANS: B

© 2024 Pearson Education Ltd. All Rights Reserved.


14. What is the value of z after the following statements have been executed? int x = 4, y = 33; double z; z = (double) (y / x); 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? int x = 5, y = 20; x += 32; y /= 4; System.out.println("x = " + x + ", y = " + y); a. b. c. d.

x = 160, y = 80 x = 32, y = 4 x = 37, y = 5 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; b. x = <float>y;

c. x = (float)y; d. x = y;

ANS: C 17. Which of the following statements is invalid? a. double r = 9.4632E15; b. double r = 9.4632e15;

c. double r = 2.9X106; 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. b. c. d.

System.out.println("Hello, world"); System Print = "Hello, world"; SystemOutPrintln('Hello, world'); system.out.println(Hello, world);

ANS: A 19. The boolean data type may contain which of the following range of values? a. b. c. d.

-128 to + 127 true or false -2,147,483,648 to +2,147,483,647 -32,768 to +32,767

ANS: B

© 2024 Pearson Education Ltd. All Rights Reserved.


20. Variables of the boolean data type are useful for a. b. c. d.

evaluating conditions that are either true or false working with small integers working with very large integers evaluating scientific notation

ANS: A 21. What would be displayed as a result of executing the following code? int x = 578; System.out.print("There are " + x + 5 + "\n" + "hens in the hen house."); 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? final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y) a. x = 22, y = 26 b. x = 22, y = 4

c. x = 22, y = 88 d. Nothing. There is an error in the code.

ANS: D 23. What would be displayed as a result of executing the following code? int x = 15, y = 20, z = 32; x += 12; y /= 6; z -= 14; System.out.println("x = " + x + ", y = " + y + ", z = " + z); a. b. c. d.

x = 27, y = 3.333, z = 18 x = 27, y = 2, z = 18 x = 37, y = -14, z = 4 x = 27, y = 3, z = 18

ANS: D 24. What is the value of z after the following code is executed? int x = 5, y = 28; float z; z = (float) (y / x);

© 2024 Pearson Education Ltd. All Rights Reserved.


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. b. c. d.

Scanner kbd = new Scanner(System.keyboard); Scanner keyboard = new Scanner(System.in); Scanner keyboard(System.in); Keyboard scanner = new Keyboard(System.in);

ANS: B 26. Which Scanner class method reads a String? a. nextLine b. charAt

c. nextString d. getLine

ANS: A 27. The primitive data types only allow a(n) __________ to hold a single value. a. class b. literal

c. object 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? System.out.println("The top three winners are\n"); System.out.print("Jody, the Giant\n"); System.out.print("Buffy, the Barbarian"); System.out.println("Adelle, the Alligator"); 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

© 2024 Pearson Education Ltd. All Rights Reserved.


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? System.out.println("The quick brown fox" + "jumped over the \n" "slow moving hen."); 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; y = 54.9; b. float y; double z; z = 934.21; y = z;

c. float w; w = 1.0f; d. float v; v = 1.0

© 2024 Pearson Education Ltd. All Rights Reserved.


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 b. a method named main

c. at least two data types d. integer variables

ANS: B 40. What will be displayed as a result of executing the following code? int x = 6; String msg = "I am enjoying this class."; String msg1 = msg.toUpperCase(); String msg2 = msg.toLowerCase(); char ltr = msg.charAt(x); int strSize = msg.length(); System.out.println(msg); System.out.println(msg1); System.out.println(msg2); System.out.println("Character at index x = " + ltr); System.out.println("msg has " + strSize + "characters."); a. I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = e msg has 24 characters.

c. I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = n msg has 24 characters.

b. I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = e msg has 25 characters.

d. I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = n msg has 25characters.

ANS: D 41. What will be displayed as a result of executing the following code? public class test { public static void main(String[] args) { int value1 = 9; System.out.println(value1);

© 2024 Pearson Education Ltd. All Rights Reserved.


int value2 = 45; System.out.println(value2); System.out.println(value3); value = 16; } } a. 9 45 16

c. 9 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. b. c. d.

PowerShell JShell JavaShell 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. b. c. d.

var auto type local

ANS: A 44. How would you rewrite the following statement using the word var to declare the variable? int value = 99; a. b. c. d.

var int value = 99; int var value = 99; var value = 99; var value; value = 99;

ANS: C

© 2024 Pearson Education Ltd. All Rights Reserved.


Starting Out with Java: From Control Structures through Objects 8e, GE (Gaddis) Chapter 3 Decision Structures TRUE/FALSE 1. Programs never need more than one path of execution. ANS: F 2. The if-else statement will execute one group of statements if its boolean expression is true or another group if its boolean expression is false. ANS: T 3. In a switch statement, if two different values for the CaseExpression would result in the same code being executed, you must have two copies of the code, one after each CaseExpression. ANS: F 4. All it takes for an OR expression to be true is for one of the subexpressions to be true. ANS: T 5. All it takes for an AND expression to be true is for one of the subexpressions to be true. ANS: F 6. When two strings are compared using the String class's compareTo method, the comparison is not case sensitive. ANS: F 7. When testing for character values, the switch statement does not test for the case of the character. ANS: F 8. If the expression on the left side of the && operator is false, the expression on the right side will not be checked. ANS: T 9. Unicode is an international encoding system that is extensive enough to represent all the characters of all the world's alphabets. ANS: T 10. A local variable's scope always ends at the closing brace of the block of code in which it is declared.

© 2024 Pearson Education Ltd. All Rights Reserved.


ANS: T 11. When testing for character values, the switch statement does not test for the case of the character. ANS: F 12. In a switch statement, each of the case values must be unique. ANS: T 13. The String.format method works exactly like the System.out.printf method, except that it does not display the formatted string on the screen. ANS: T 14. The System.out.printf method formats a string and displays it in the console window. ANS: T 15. When you write a switch statement using arrow case syntax, you must have a break statement in each case section. ANS: F 16. In the following statement, the switch expression is written correctly. message = switch(statusCode) { case 200 -> statusType = "Successful"; yield "OK"; case 404 -> statusType = "Client Error"; yield "Not Found"; default -> statusType = "Unsupported"; yield "Unknown Status"; }; ANS: F 17. If you are using a recent version of Java, you can specify multiple values in a case statement. ANS: T 18. A switch expression must have case statements that cover every possible value of the testExpression. ANS: T

© 2024 Pearson Education Ltd. All Rights Reserved.


Turn static files into dynamic content formats.

Create a flipbook
TEST BANK for Starting Out With Java Control Structures through Objects, 8th edition By Tony Gaddis by welldoneassistant - Issuu