




















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















































































Test Bank for Starting Out With Java: Control Structures through Objects, 8th Edition (Tony Gaddis) Chapter 1: Introduction to Computers and Java Question 1: Which part of the computer fetches instructions, carries out the operations commanded by the instructions, and produces some outcome or resultant information? A. Memory B. CPU C. Secondary storage D. Input device Correct Answer: B Rationale: The CPU (Central Processing Unit) is the component of the computer that fetches instructions, decodes them, and carries out the operations commanded by the instructions. It is often referred to as the "brain" of the computer. Memory holds data and instructions, while secondary storage provides long-term storage. Question 2: Which of the following is a type of memory that can hold data for long periods of time, even when there is no power to the computer? A. RAM B. Main memory C. Secondary storage D. CPU
Correct Answer: C Rationale: Secondary storage (such as a hard drive, USB drive, or cloud storage) can hold data for long periods of time, even when there is no power to the computer. RAM and main memory are volatile, meaning their contents are lost when power is removed. Question 3: What is the term for a program that translates a source program into a machine- language program that can be executed by the computer? A. Interpreter B. Compiler C. Assembler D. Linker Correct Answer: B Rationale: A compiler translates source code written in a high-level language into machine language that the computer can execute. An interpreter translates and executes code line by line. Question 4: What does JVM stand for? A. Java Virtual Machine B. Java Version Manager C. Java Variable Method D. Java Visual Model Correct Answer: A Rationale: JVM stands for Java Virtual Machine. It is the component that runs Java bytecode, allowing Java programs to be platform-independent. Question 5: What is the term for the code that a programmer writes in a high-level language such as Java? A. Machine code B. Bytecode C. Source code D. Object code
processing, spreadsheets, and games. Microsoft Word is an example of application software. Operating systems and device drivers are system software. Question 9: What is the purpose of the main method in a Java application? A. It is the first method that is executed when the program runs B. It is the last method that is executed when the program ends C. It is a method for creating objects D. It is a method for reading input Correct Answer: A Rationale: The main method is the entry point of a Java application. When the program is executed, the JVM calls the main method, which is where execution begins. Question 10: Which of the following is the correct way to write a single-line comment in Java? A. /* This is a comment / B. // This is a comment C. # This is a comment D. -- This is a comment Correct Answer: B Rationale: Single-line comments in Java begin with // and extend to the end of the line. Multi- line comments begin with / and end with */. Question 11: What is the term for a set of instructions that a computer follows to perform a task? A. Hardware B. Software C. Firmware D. Malware Correct Answer: B Rationale: Software is a set of instructions that a computer follows to perform a task. Hardware refers to the physical components of the computer.
Question 12: Which of the following is NOT a primary component of a computer system? A. CPU B. Memory C. Compiler D. Secondary storage Correct Answer: C Rationale: The primary components of a computer system are the CPU, memory, secondary storage, input devices, and output devices. A compiler is software, not a hardware component. Question 13: What is the name for a program that runs on a computer and provides services to other programs? A. Application software B. System software C. Utility software D. Malware Correct Answer: B Rationale: System software runs on a computer and provides services to other programs. The operating system is a key example of system software. Application software is designed for end- users. Question 14: What is bytecode? A. Code that is written by a programmer B. Code that is generated by the Java compiler and executed by the JVM C. Code that is specific to a particular type of CPU D. Code that is generated by the operating system Correct Answer: B Rationale: Bytecode is the intermediate code generated by the Java compiler. It is platform- independent and is executed by the Java Virtual Machine (JVM). Question 15: Which of the following is a valid Java identifier? A. 2ndPlace B. myVariable
Correct Answer: B Rationale: Hardware refers to the physical devices that make up a computer system, such as the CPU, memory, monitor, keyboard, and mouse. Software refers to the programs that run on the hardware. Question 19: Which of the following is a valid Java statement to print "Hello, World!" to the console? A. System.out.println("Hello, World!"); B. print("Hello, World!"); C. console.log("Hello, World!"); D. echo "Hello, World!"; Correct Answer: A Rationale: In Java, System.out.println("Hello, World!"); is the correct statement to print a line of text to the console. System.out is the standard output stream. Question 20: What is the purpose of the public keyword in the main method declaration? A. It makes the method accessible from outside the class B. It makes the method private C. It makes the method static D. It makes the method final Correct Answer: A Rationale: The public keyword makes the main method accessible from outside the class. The JVM calls the main method from outside the class, so it must be public. Chapter 2: Java Fundamentals Question 21: Which of the following is a valid declaration of a variable that can hold a whole number? A. double num = 10; B. int num = 10; C. String num = "10"; D. boolean num = true;
Correct Answer: B Rationale: The int data type is used to store whole numbers (integers). double is used for floating-point numbers, String for text, and boolean for true/false values. Question 22: What is the value of the variable x after the following code is executed? int x = 5; x += 3; A. 5 B. 3 C. 8 D. 2 Correct Answer: C Rationale: The += operator adds the right operand to the left operand and assigns the result to the left operand. x += 3 is equivalent to x = x + 3, so x becomes 8. Question 23: Which of the following is NOT a primitive data type in Java? A. int B. double C. String D. char Correct Answer: C Rationale: String is a class (reference type), not a primitive data type. The primitive data types in Java are byte, short, int, long, float, double, char, and boolean. Question 24: What is the output of the following code? System.out.println(10 / 3); A. 3. B. 3 C. 3. D. 4 Correct Answer: B Rationale: When both operands of the division operator are integers, Java performs integer division, which truncates the fractional part. 10 / 3 yields 3, not 3.3333. To get the decimal result, at least one operand must be a floating-point number.
Correct Answer: B Rationale: The decrement operator -- subtracts 1 from the variable. x-- is equivalent to x = x - 1, so x becomes 9. Question 29: Which of the following is the correct way to concatenate two strings in Java? A. String result = "Hello" + "World"; B. String result = "Hello". "World"; C. String result = "Hello" & "World"; D. String result = "Hello" concat "World"; Correct Answer: A Rationale: The + operator is used to concatenate strings in Java. "Hello" + "World" produces "HelloWorld". Question 30: What is the output of the following code? System.out.println("Result: " + 5 + 3); A. Result: 8 B. Result: 53 C. Result: 5 + 3 D. Result: 15 Correct Answer: B Rationale: The + operator is left-associative. First, "Result: " + 5 produces "Result: 5", and then "Result: 5" + 3 produces "Result: 53". To get 8, you would need "Result: " + (5 + 3). Question 31: Which of the following is a valid Java statement to declare and initialize a boolean variable? A. boolean flag = true; B. boolean flag = "true"; C. boolean flag = 1; D. boolean flag = 'true';
Correct Answer: A Rationale: The boolean data type can hold only two values: true and false. boolean flag = true; is the correct declaration and initialization. Question 32: What is the result of the following expression? 7 % 3 A. 2 B. 1 C. 2. D. 0 Correct Answer: B Rationale: The modulus operator % returns the remainder of division. 7 % 3 yields 1 because 7 divided by 3 is 2 with a remainder of 1. Question 33: Which of the following is the correct way to convert a string to an integer in Java? A. int num = Integer.parseInt("123"); B. int num = (int) "123"; C. int num = Integer.toString("123"); D. int num = String.parseInt("123"); Correct Answer: A Rationale: The Integer.parseInt() method converts a string to an integer. Integer.toString() does the opposite (converts an integer to a string). Question 34: What is the value of x after the following code? int x = 5; x *= 2; A. 5 B. 10 C. 2 D. 7 Correct Answer: B Rationale: The *= operator multiplies the left operand by the right operand and assigns the result to the left operand. x *= 2 is equivalent to x = x * 2, so x becomes 10.
Correct Answer: C Rationale: The - = operator subtracts the right operand from the left operand and assigns the result to the left operand. x - = 4 is equivalent to x = x - 4, so x becomes 6. Question 39: Which of the following is a valid Java statement to display the value of a variable named score? A. System.out.println(score); B. System.out.println("score"); C. print(score); D. console.log(score); Correct Answer: A Rationale: System.out.println(score); displays the value of the variable score. Using quotes would display the literal text "score" instead of the variable's value. Question 40: What is the result of the following expression? 10 + 5 * 2 A. 30 B. 20 C. 25 D. 15 Correct Answer: B Rationale: Multiplication has higher precedence than addition. 5 * 2 is evaluated first (10), and then 10 + 10 yields 20. Chapter 3: Decision Structures Question 41: What is the value of discountRate after the following statements are executed? double discountRate = 0.00; int purchase = 100; if (purchase > 1000) discountRate = .05; else if (purchase > 750) discountRate = .03; A. 0. B. 0.
Correct Answer: C Rationale: The purchase amount is 100, which is not greater than 1000 or 750, so neither condition is true. The discountRate remains 0.00. Question 42: Which of the following is a boolean expression? A. x + y B. x > y C. x = y D. x * y Correct Answer: B Rationale: A boolean expression evaluates to either true or false. x > y is a relational expression that evaluates to true or false. Question 43: What is the output of the following code? int x = 10; if (x > 5) System.out.println("Greater than 5"); else System.out.println("Less than or equal to 5"); A. Greater than 5 B. Less than or equal to 5 C. Error D. No output Correct Answer: A Rationale: Since x is 10, which is greater than 5, the first branch executes, printing "Greater than 5". Question 44: Which of the following operators is used to test for equality in Java? A. = B. == C. != D. <= Correct Answer: B Rationale: The == operator is used to test for equality. The = operator is used for assignment.
Question 48: What is the output of the following code? boolean flag = true; if (!flag) System.out.println("Yes"); else System.out.println("No"); A. Yes B. No C. Error D. No output Correct Answer: B Rationale: The! operator negates the boolean value. Since flag is true, !flag is false, so the else branch executes, printing "No". Question 49: What is the purpose of the switch statement? A. To execute a block of code based on the value of a variable B. To execute a block of code repeatedly C. To declare a variable D. To test a boolean condition Correct Answer: A Rationale: The switch statement is a multi-way decision structure that executes different blocks of code based on the value of a variable (the switch expression). It is often used as an alternative to a long if-else if chain. Question 50: What is the output of the following code? int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Other"); } A. Monday B. Tuesday C. Wednesday D. Other Correct Answer: C Rationale: The value of day is 3, which matches case 3, so "Wednesday" is printed. The break statement causes the switch to terminate. Question 51: What happens if a break statement is omitted from a case in a switch statement?
A. A compilation error occurs B. The program crashes C. Execution falls through to the next case D. The switch statement is skipped Correct Answer: C Rationale: If a break statement is omitted, execution "falls through" to the next case, and the code for subsequent cases will also execute until a break is encountered or the switch ends. Question 52: Which of the following is the correct way to compare two strings for equality in Java? A. str1 == str B. str1.equals(str2) C. str1 = str D. str1.compareTo(str2) == 0 Correct Answer: B Rationale: The equals() method is used to compare the contents of two strings. The == operator compares references (memory addresses), not contents. compareTo() returns an integer, not a boolean, so it is not typically used directly in an if condition without comparison. Question 53: What is the output of the following code? int x = 5; int y = 10; if (x > 3 && y < 15) System.out.println("Both true"); A. Both true B. No output C. Error D. Both false Correct Answer: A Rationale: Both conditions are true: x > 3 (5 > 3) is true, and y < 15 (10 < 15) is true. The && operator requires both operands to be true, so "Both true" is printed. Question 54: Which of the following is a nested if statement? A. An if statement inside another if statement B. An if statement inside a loop
C. Error D. 0 Correct Answer: A Rationale: The || operator returns true if at least one operand is true. 5 > 3 is true, and 10 < 5 is false. Since one operand is true, the result is true. Question 58: What is the purpose of the default case in a switch statement? A. It is executed if no case matches the switch expression B. It is executed if the switch expression is true C. It is executed first D. It is required in every switch statement Correct Answer: A Rationale: The default case is optional and is executed if none of the case values match the switch expression. It is often used to handle unexpected or invalid values. Question 59: What is the output of the following code? int x = 7; switch (x) { case 1: case 2: case 3: System.out.println("Low"); break; case 4: case 5: case 6: System.out.println("Medium"); break; default: System.out.println("High"); } A. Low B. Medium C. High D. Error Correct Answer: C Rationale: The value of x is 7, which does not match any of the case values (1-6). Therefore, the default case executes, printing "High". Question 60: Which of the following is a valid boolean expression? A. x + y B. x > y && y > z C. x = y D. x * y
Correct Answer: B Rationale: A boolean expression evaluates to either true or false. x > y && y > z is a compound boolean expression that uses relational and logical operators. Chapter 4: Loops and Files Question 61: Which of the following is a pretest loop? A. while loop B. do-while loop C. for loop D. Both A and C Correct Answer: D Rationale: Both while and for loops are pretest loops, meaning they test their condition before each iteration. A do-while loop is a posttest loop, meaning it tests its condition after each iteration. Question 62: What is the output of the following code? int x = 0; while (x < 5) { System.out.print(x + " "); x++; } A. 0 1 2 3 4 B. 1 2 3 4 5 C. 0 1 2 3 4 5 D. 1 2 3 4 Correct Answer: A Rationale: The loop starts with x = 0 and continues while x < 5. Inside the loop, x is printed and then incremented. The output is "0 1 2 3 4 ". Question 63: What is the output of the following code? int x = 0; do { System.out.print(x + " "); x++; } while (x < 5); A. 0 1 2 3 4 B. 1 2 3 4 5 C. 0 1 2 3 4 5 D. 1 2 3 4