Final Conditionals And Loops Test Bank Chapter 4 - Java Foundations 4th Edition | Test Bank with Answer Key by John Lewis by John Lewis, Peter DePasquale, Joe Chase. DOCX document preview.

Final Conditionals And Loops Test Bank Chapter 4

Chapter 4: Conditionals and Loops

Multiple Choice Questions:

1) Which of the following statements best describes the flow of control in the main method of a Java program that has no conditionals or loops?

a) Program statements are all executed at the same time.

b) Program statements are executed according to their priority, which is specified by the programmer.

c) Program statements are executed linearly, with earlier statements being executed first.

d) Program statements are executed linearly, with later statements being executed first.

e) Some program statements are executed at the same time, and others are executed in a linear manner.

2) Which of the following best describes this code snippet?

if (count != 400)

System.out.println("Hello World!");

a) If the variable count is exactly equal to 400, "Hello World" will be printed.

b) If the variable count is not equal to 400, "Hello World" will be printed.

c) If the variable count is close to, but not greater than, 400, "Hello World" will be printed.

d) If the variable count is exactly equal to 399 or 401, "Hello World" will be printed.

e) This code will not compile.

3) In Java, a block statement is

  1. a set of statements that are all indented to the same column.
  2. a set of statements enclosed in { and }.
  3. statements that form a rectangular set of characters on the screen.
  4. a statement that prevents other statements from executing.
  5. a statement that ends the execution of the program.

4) Let a and b be valid boolean expressions. Which of the following best describes the result of the expression a || b?

a) It will evaluate to true if a evaluates to true and b evaluates to true. It will evaluate to false otherwise.

b) It will evaluate to false if a evaluates to false and b evaluates to false. It will evaluate to true otherwise.

c) It will evaluate to true if a evaluates to false and b evaluates to false. It will evaluate to true otherwise.

d) It will evaluate to true if a evaluates to false or b evaluates to false. It will evaluate to true otherwise.

e) None of the above statements correctly describes the evaluation of the expression.

5) Which of the following expressions best represents the condition "if the grade is between 75 and 100"?

  1. if (75 < grade && grade < 100)
  2. if (grade != 75 && grade != 100)
  3. if (75 < grade < 100)
  4. if (75 > grade || grade < 100)
  5. if (75 < grade || grade < 100)

6) A set of statements must be executed an unknown number of times, and possibly not executed at all. Which loop statement should not be used to control the execution of this set of statements?

  1. while
  2. for
  3. do
  4. repeat
  5. Any of these loop statements can be used.

7) Suppose we wanted to process a text file called "input.txt" using the Scanner object. Which of the following lines of code correctly creates the necessary Scanner object?

  1. Scanner inputFile = new Scanner("input.txt");
  2. Scanner inputFile = new Scanner(new InputFile("input.txt");
  3. Scanner inputFile = new Scanner(new File(input.txt);
  4. Scanner inputFile = new Scanner(new InputFile(input.txt);
  5. Scanner inputFile = new Scanner(new File("input.txt");

8) The code below is supposed to add the numbers from 1 up to and including 10. It does not calculate the correct sum. The problem is caused by a(n) ________ error.

int sum = 0;

for (int count = 1; count < 10; count++)

sum += count;

  1. syntax
  2. compilation
  3. requirement
  4. off-by-one
  5. testing

9) What happens if a case in a switch statement does not end with a break statement?

a) The program will not compile.

b) The switch statement will never execute.

c) It will cause an infinite loop.

d) The switch statement will execute the next case statement as well.

e) The case will never be executed.

10) A switch statement does not have a case that matches the value of the expression, and it does not have a default case. What happens?

a) The first case will be executed because there is no default case.

b) The last case will be executed because there is no default case.

c) The case whose value is closest to the expression will be executed.

d) None of the cases will be executed.

e) An execution error will occur if there is no matching case and no default case.

11) Suppose we want to write an if statement to test whether two String objects, referenced by stringOne and stringTwo, are the same. Which of the following is the correct way to achieve this?

  1. if(stringOne == stringTwo)
  2. if(stringOne.compareTo(stringTwo))
  3. if(stringOne.equals(stringTwo))
  4. if(stringOne != stringTwo)
  5. if(stringOne === stringTwo)

12) A(n) _________________ is an object that has methods that allow you to process a collection of items one at a time.

a) iterator

b) loop

c) conditional

d) palindrome

e) nested loop

13) A logical expression can be described by a ________________ that lists all possible combinations of values for the variables involved in an expression.

a) palindrome

b) nested loop

c) equality operator

d) switch statement

e) truth table

14) The expression that is evaluated at the start of a switch statement must not be which primitive data type below?

  1. byte
  2. short
  3. int
  4. char
  5. double

15) Which of the following for loop headers will cause the body of the loop to be executed 100 times?

  1. for(int i = 0; i <= 100; i++)
  2. for(int i = 1; i < 100; i++)
  3. for(int i = 1; i <= 101; i++)
  4. for(int i = 0; i < 100; i++)
  5. none of these for loops will execute the loop body 100 times

1) Every if statement requires an associated else statement, but not every else statement requires an associated if statement.

2) In a nested if statement an else clause is matched to the closest unmatched if.

3) In Java, a boolean expression is limited to having exactly 2 logical operators.

4) A do statement should be used to avoid creating an infinite loop.

5) A while statement always executes its loop body at least once.

6) The initialization portion of a for loop header can be used to declare a variable that is used during loop execution.

7) The relational operators should not be used to test the equality of objects.

8) It is possible to implement a switch statement using if statements.

9) An infinite loop is a compile-time error.

10) The Scanner object can be used to read text files.

1) Using the following declarations and initializations:

int num1 = 5,

num2 = 12,

num3 = 13;

write a boolean expression that is true when the sum of num1 and num2 is more than num3, or when the value of num1 is an odd number.

2) Write a snippet of code that determines which of two integer variables, intOne and intTwo, contains a larger number, and print out the larger one. If they are equal, the output should say that.

3) name is a String object that contains user input. Write a segment of code that determines if name contains "George". If it does, print the message "Hey, that's my name too! "

4) What is output by the following code fragment?

int num = 0;

int max = 10;

while(num < max) {

System.out.print(num + " ");

num += 2;

}

5) Rewrite the following code fragment using a for loop instead of a while loop.

int i = 0;

while(i < 50) {

System.out.println(i);

i+=2;

}

6) Write a short application that takes in a String from the user and prints it out backwards.

7) The following code compiled, but while running it the program appears to hang (e.g. nothing happens). This is a sign that there may be an infinite loop. What part of this code fragment may be causing an infinite loop?

while(i < 50); {

System.out.println(i);

i+=2;

}

8) grade is a char variable that holds a letter grade 'A', 'B', 'C', 'D', or 'F'. Write a switch statement that prints one of the messages in the table below based on the value in grade.

Grade

Message

A

Excellent!

B

Very Good

C

Good

D

You can do better

F

You must do better

any other value

Error – invalid grade detected

9) How many times will the body of a while loop be executed if the boolean expression is false the first time that the while statement is encountered?

10) Write a short code fragment that uses a while loop to verify that the user enters a positive integer as input. You may assume that a Scanner object named input has already been created.

11) Write a do loop that verifies that the user enters an odd value. You may assume that a Scanner object named input has already been created.

12) Write a switch statement that switches on an integer variable named val. If val is 2 or 15, then output "Hello World." For all other values, output "Goodbye World."

13) Write a code fragment that allows a user to continue inputting numbers until she enters a sentinel value of 0. Then print the sum of all the numbers she entered. You may assume that a Scanner object named input has already been created.

14) Write a code fragment that determines how many times the character 'A' appears in a String object called name.

15) line is a String object that holds an unknown number of int values separated by spaces. Write a segment of code that will compute and display the sum of the values in line.

Document Information

Document Type:
DOCX
Chapter Number:
4
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 4 Conditionals And Loops
Author:
John Lewis, Peter DePasquale, Joe Chase

Connected Book

Java Foundations 4th Edition | Test Bank with Answer Key by John Lewis

By John Lewis, Peter DePasquale, Joe Chase

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party