Chapter 5 Decisions Test Questions & Answers - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.

Chapter 5 Decisions Test Questions & Answers

Package Title: Test Bank

Course Title: Big Java

Chapter Number: 5 Decisions

Question type: Multiple Choice

1) What are the two parts of an if statement?

a) A condition and a body

b) A check and an increment

c) An increment and a body

d) An increment and a return value

Title: What are the two parts of an if statement?

Difficulty: Easy

Section Reference 1: 55.1 The if statement

2) Which of the following statements is true about the if statement?

a) The if statement can have only one condition that evaluates to an integer value.

b) The if block is optional.

c) The else block is optional.

d) The if and else blocks should always be included within curly braces.

Title: Which statement is true about the if statement?

Difficulty: Easy

Section Reference 1: 55.1 The if statement

3) Which of the following statements is correct about an if statement?

a) You must use braces if the body of an if statement contains only a single statement.

b) You can omit an else statement if there is no task defined in the else branch.

c) You cannot use braces if the body of an if statement contains only a single statement.

d) The number of opening braces can be different from the number of closing braces.

Title: Which statement is true about the if statement?

Difficulty: Medium

Section Reference 1: 5.1 The if statement

4) Which of the following is the correct syntax for an if statement?

a)

if (x < 10) { size = "Small"; }

else (x < 20) { size = "Medium"; }

b)

if (x < 10); { size = "Small"; }

else (x < 20) { size = "Medium"; }

c)

if (x < 10) { size = "Small"; }

else { size = "Medium"; }

d)

if { size = "Small"; }

else (x < 20) { size = "Medium"; }

Title: Which is the correct syntax for an if statement?

Difficulty: Medium

Section Reference 1: 5.1 The if statement

5) Assuming that the user provides 303 as input, what is the output of the following code snippet?

int x;

int y;

Scanner in = new Scanner(System.in);

System.out.print("Please enter a number: ");

y = in.nextInt();

if (y > 300)

{

x = y;

}

else

{

x = 0;

}

System.out.println("x: " + x);

a) x: 0

b) x: 300

c) x: 303

d) There is no output due to compilation errors.

Title: What is output of if/else with > test with this input?

Difficulty: Medium

Section Reference 1: 5.1 The if statement

6) Assuming that the user provides 99 as input, what is the output of the following code snippet?

int a;

int b;

Scanner in = new Scanner(System.in);

System.out.print("Please enter a number: ");

b = in.nextInt();

if (b > 300)

{

a = b;

}

else

{

a = 0;

}

System.out.println("a: " + a);

a) a: 0

b) a: 99

c) a: 100

d) a: 300

Title: What is output of (if with > test) with this input?

Difficulty: Easy

Section Reference 1: 5.1 The if statement

7) The following code snippet contains an error. What is the error?

if (cost > 100);

{

cost = cost – 10;

}

System.out.println("Discount cost: " + cost);

a) Syntax error (won’t compile)

b) Logical error: use of an uninitialized variable

c) Logical error: if statement has do-nothing statement after if condition

d) Logical error: assignment statement does not show equality

Title: What is the error in this if statement?

Difficulty: Medium

Section Reference 1: 5.1 The if statement

Section Reference 2: Common Error 5.1 A Semicolon After the if Condition

8) What can be done to improve the following code fragment?

if ((counter % 10) == 0)

{

System.out.println("Counter is divisible by ten: " + counter);

counter++;

}

else

{

System.out.println("Counter is not divisible by ten: "

+ counter);

counter++;

}

a) Move the duplicated code outside of the if statement

b) Shorten variable names

c) Move the brackets to save several lines of code

d) Add semicolons after the if condition and the else reserved word

Title: What can be done to improve the code fragment containing an if statement?

Difficulty: Medium

Section Reference 1: Programming Tip 55.4

9) What kind of operator is the <= operator?

a) Ternary

b) Arithmetic

c) Inequality

d) Relational

Title: What kind of operator is the <= operator?

Difficulty: Easy

Section Reference 1: 3.2 Comparing Values

10) Which of the following operators is used as a relational operator?

a) =<

b) <=

c) =

d) !

Title: Which of the following is a relational operator?

Difficulty: Easy

Section Reference 1: 3.2 Comparing Values

11) The operator !> stands for

a) not less than.

b) not greater than.

c) not equal to.

d) this is not an operator in Java

Title: Which relational operator is this?

Difficulty: Easy

Section Reference 1: 3.2 Comparing Values

12) Assuming that a user enters 15 as input, what is the output of the following code snippet?

Scanner in = new Scanner(System.in);

System.out.print("Please enter a number: ");

int number = in.nextInt();

if (number > 20)

{

System.out.println("The number is LARGE!");

}

else

{

System.out.println("The number is SMALL!");

}

a)

There is no output due to compilation errors.

b)

The number is LARGE!

c)

The number is SMALL!

d)

The number is LARGE!

The number is SMALL!

Title: What is output of (if/else with < test) with this input?

Difficulty: Medium

Section Reference 1: 3.2 Comparing Values

13) What is the output of the following code snippet if the input is 25?

int i;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

i = in.nextInt();

if (i > 25)

{

i++;

}

else

{

i--;

}

System.out.print(i);

a) 24

b) 25

c) 26

d) 27

Title: What is output of (if/else with increment/decrement) with this input?

Difficulty: Medium

Section Reference 1: 3.2 Comparing Values

14) Assuming that a user enters 25 as the value for x, what is the output of the following code snippet?

int x;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

x = in.nextInt();

if (x < 100)

{

x = x + 5;

}

if (x < 500)

{

x = x - 2;

}

if (x > 10)

{

x++;

}

else

{

x--;

}

System.out.println(x);

a) 27

b) 28

c) 29

d) 30

Title: What is output of (if/if/if/else) snippet with this input?

Difficulty: Hard

Section Reference 1: 5.3 Multiple Alternatives

15) A store provides 10 percent discount on all items with a price of at least $100. No discount is otherwise applicable. Which of the following DOES NOT correctly compute the discount?

a)

double discount = 0;

if (price >= 100)

{

discount = 0.10 * price;

}

b)

double discount = 0.10 * price;

if (price <= 100)

{

discount = 0;

}

c)

double discount;

if (price < 100)

{

discount = 0;

}

else

{

discount = 0.10 * price;

}

d)

double discount = 10;

if (price >= 100)

{

discount = 0.1 * price;

}

else

{

discount = 0;

}

Title: Which statement DOES NOT correctly compute the discount?

Difficulty: Easy

Section Reference 1: 5.1 The if statement

16) Which of the following statements is (are) true about an if statement?

I. It guarantees that several statements are always executed in a specified order.

II. It repeats a set of statements as long as the condition is true.

III. It allows the program to carry out different actions depending on the value of a condition.

a) I

b) II

c) III

d) I, II, III

Title: Which statement(s) is true about an if statement?

Difficulty: Easy

Section Reference 1: 5.1 The if statement

17) What is the output of the following code snippet?

double income = 45000;

double cutoff = 55000;

double minIncome = 30000;

if (minIncome > income)

{

System.out.println("Minimum income requirement is not met.");

}

if (cutoff < income)

{

System.out.println("Maximum income limit is exceeded.");

}

else

{

System.out.println("Income requirement is met.");

}

a) Minimum income requirement is not met.

b) Maximum income limit is exceeded.

c) Income requirement is met.

d) There is no output.

Title: What is output of (if/if/else) snippet?

Difficulty: Medium

Section Reference 1: 5.1 The if statement

18) In Java, which of the following orderings is used to compare strings?

a) Lexicographic

b) Semantic

c) Alphabetic

d) Syntactic

Title: Which ordering is used to compare strings?

Difficulty: Easy

Section Reference 1: 5.2 Comparing Values

19) Suppose one needs an if statement to check whether an integer variable pitch is equal to 440 (which is the frequency of the note “A” to which strings and orchestras tune). Which condition is correct?

a) if (pitch – 440 = 0)

b) if ((pitch !< 440) && (pitch !> 440))

c) if (pitch = 440)

d) if (pitch == 440)

Title: Which condition for an if statement is correct?

Difficulty: Medium

Section Reference 1: 5.1 The if statement

20) Which statement about an if statement is true?

a) The condition in an if statement using relational operators will evaluate to a Boolean result

b) The condition in an if statement should make exact comparisons to floating-point numbers

c) The condition in an if statement should always evaluate to true

d) The condition in an if statement should never include integer variables

Title: What is the expected type of the conditional in an if statement?

Difficulty:

Section Reference 1: 5.2 Comparing Values

21) What is the problem with the following if statement?

double count = 15.0;

if (count / 3.0)

{

System.out.println("The value of count is ");

}

a) There should be an “else” condition

b) The condition does not evaluate to a Boolean value

c) The variable count should be part of the string

d) It is never possible to use the “/” operator in an if statement

Title: What is wrong with the given if statement?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

22) Consider the following code snippet. What is the potential problem with the if statement?

double average;

average = (g1 + g2 + g3 + g4) / 4.0;

if (average == 90.0)

{

System.out.println("You earned an A in the class!");

}

a) Using == to test the double variable average for equality is error-prone.

b) The conditional will not evaluate to a Boolean value.

c) The assignment operator should not be used within an if-statement conditional.

d) Literals should never be used in if statement conditionals.

Title: What is wrong with the following if statement?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

Section Reference 2: Common Error 5.3 Using == to Compare Strings

23. Which code snippet will output “Yes!” when two strings s1 and s2 are equal?

a)

if (s1 = s2)

{

System.out.println("Yes!");

}

b)

if (s1 == s2)

{

System.out.println("Yes!");

}

c)

if (s1.equals(s2))

{

System.out.println("Yes!");

}

d)

if (s1.compareTo(s2) == 1)

{

System.out.println("Yes!");

}

Title: Which code snippet correctly implements the required conditional?

Difficulty: Easy

Section Reference 1: 5.2 Comparing Values

Section Reference 2: Special Topic 5.2

24) Which condition, when supplied in the if statement below in place of (. . .), will correctly protect against division by zero?

if (. . .)

{

result = grade / num;

System.out.println("Just avoided division by zero!");

}

a) (grade == 0)

b) ((grade / num) == 0)

c) (num == 0)

d) (num != 0)

Title: Which condition avoids division by zero?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

25) What is the output of the following code snippet?

int num = 100;

if (num != 100)

{

System.out.println("100");

}

else

{

System.out.println("Not 100");

}

a) There is no output due to compilation errors.

b) 100

c) Not 100

d) 100

Not 100

Title: What is output of (if/else with != test)?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

26) What is the output of the following code snippet?

String str1 = "her";

String str2 = "cart";

if (str1.compareTo(str2) < 0)

{

System.out.print(str2);

}

else

{

System.out.print(str1);

}

a) her

b) hercart

c) cart

d) carther

Title: What is output of (if/else with string ordering test)?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

27) What is the conditional required to check whether the length of a string s1 is odd?

a) if ((s1.length() % 2) == 0)

b) if ((s1.length() % 2) != 0)

c) if ((s1.length() / 2))

d) if ((s1.length() * 2))

Title: What condition evaluates to true if the length of a string s1 is odd?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

28) The two strings “Aardvark” and “Aardvandermeer” are exactly the same up to the first six letters. What is their correct lexicographical ordering?

a) They cannot be compared lexicographically unless they are the same length

b) “Aardvandermeer” is first, then “Aardvark”

c) “Aardvark" is first, then “Aardvandermeer”

d) The shorter word is always first

Title: Which is the correct application of lexicographical ordering?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

29) Write an if-statement condition that is true if the length of string s1 is greater than 42.

a) if (s1.length() > 42)

b) if (s1.length() != 42)

c) if (42 > s1.length())

d) if (42 != s1.length())

Title: Which if statement is true when the length of string s1 is greater than 42?

Difficulty:

Section Reference 1: 5.2 Comparing Values

30) Which of the following options is a legally correct expression for inverting a condition?

a) if (!(a == 10))

b) if (!a == 10)

c) if (a !== 10)

d) if (a ! 10)

Title: Which is a correct expression for inverting a condition?

Difficulty: Easy

Section Reference 1: 5.7 Boolean Variables and Operators

31) Suppose you want to write an if statement with multiple alternatives to print out the single tax bracket that someone is in, based on their income. Assume the integer variable income holds the annual income. What is wrong with the following if statement?

if (income < 10000)

{

System.out.println("Lowest tax bracket");

}

7if (income < 20000)

{

System.out.println("Low-Middle tax bracket");

}

if (income < 30000)

{

System.out.println("Middle tax bracket");

}

System.out.println("High tax bracket");

a) The conditions are in the wrong order; the check for the highest bracket should be first

b) The conditions should use an if else/if else sequence, not just independent if statements

c) The conditions should be a switch statement instead

d) Nothing is wrong – the if statement will correctly print out the correct tax brackets

Title: Is the given if statement with multiple alternatives written correctly?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

32. The switch statement in Java

a) is like a sequence of if statements that compares a single integer value against several constant alternatives.

b) is a compound statement that tests all branches against different variables.

c) requires compound Boolean expressions as alternatives.

d) none of the choices is correct.

Title: What is a switch statement in Java?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

Section Reference 2: Special Topic 5.3

33) In a switch statement, if a break statement is missing

a) The break happens at the end of each branch by default

b) The statement will not compile

c) Execution falls through the next branch until a break statement is reached

d) The default case is automatically executed

Title: What happens when a break statement is missing from a switch statement?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

Section Reference 2: Special Topic 5.3

34. An if statement inside another if statement is called a

a) switch statement

b) nested if statement

c) break statement

d) syntax error, since that is not permitted in Java

Title: What is it called when an if statement is inside another if statement?

Difficulty: Easy

Section Reference 1: 5.4 Nested Branches

35) When an if statement is nested inside another if statement, it creates the possibility of

a) an infinite loop

b) the misuse of the break statement

c) type mismatch

d) The dangling else

Title: What is a dangling else ?

Difficulty: Easy

Section Reference 1: 5.4 Nested Branches

Section Reference 2: Common Error 5.4 The Dangling else Problem

36) What is the output of the following code snippet?

int s1 = 20;

if (s1 <= 20)

{

System.out.print("1");

}

if (s1 <= 40)

{

System.out.print("2");

}

if (s1 <= 20)

{

System.out.print("3");

}

a) 1

b) 2

c) 3

d) 123

Title: What is output of snippet (with three nonexclusive ifs)?

Difficulty: Easy

Section Reference 1: 5.3 Multiple Alternatives

37) Consider a situation where multiple if statements are combined into a chain to evaluate a complex condition. Which of the following reserved words is used to define the branch to be executed when none of the conditions are true?

a) if

b) else if

c) else

d) All of the above items

Title: Which word defines the branch to be executed when none of the conditions are true?

Difficulty: Easy

Section Reference 1: 5.3 Multiple Alternatives

38) Consider the following code snippet:

int number = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

number = in.nextInt();

if (number > 30) { . . . }

else if (number > 20) { . . .. }

else if (number > 10) { . . . }

else { . . . }

Assuming that the user input is 40, which block of statements is executed?

a) if (number > 30) { . . . }

b) else if (number > 20) { . . . }

c) else if (number > 10) { . . . }

d) else { . . . }

Title: Which statement (in if/else block) is executed when input is 40?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

39) Assuming that the user enters 60 as the input, what is the output after running the following code snippet?

int num = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

num = in.nextInt();

if (num < 10)

{

System.out.println("Too small!");

}

else if (num < 50)

{

System.out.println("Intermediate!");

}

else if (num < 100)

{

System.out.println("High!");

}

else

{

System.out.println("Too high!");

}

a) Too small!

b) Intermediate!

c) High!

d) Too high!

Title: What is output of (if/else if/else if) snippet with this input?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

40) Assuming that a user enters 5 as the value for num, what is the output of the following code snippet?

int num = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

num = in.nextInt();

if (num < 50)

{

num = num + 5;

}

if (num < 10)

{

num = num - 2;

}

if (num > 5)

{

num++;

}

else

{

num--;

}

System.out.println(num);

a) 0

b) 9

c) 5

d) 11

Title: What is output of (if/if/if/else) snippet with this input?

Difficulty: Hard

Section Reference 1: 5.3 Multiple Alternatives

41) Consider the following code snippet. Assuming that the user enters first 20 and then 12 as the two input values, what is the output of the code snippet?

int num1 = 0;

int num2 = 0;

int num3 = 0;

int num4 = 0;

int num5 = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

num1 = in.nextInt();

System.out.print("Enter a number: ");

num2 = in.nextInt();

if (num1 < num2)

{

num3 = num1;

}

else

{

num3 = num2;

}

if (num1 < num2 + 10)

{

num4 = num1;

}

else if (num1 < num2 + 20)

{

num5 = num1;

}

System.out.println("num1 = " + num1 + " num2 = " + num2

+ " num3 = " + num3 + " num4 = " + num4

+ " num5 = " + num5);

a) num1 = 20 num2 = 12 num3 = 20 num4 = 20 num5 = 0

b) num1 = 20 num2 = 12 num3 = 12 num4 = 0 num5 = 20

c) num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0

d) num1 = 20 num2 = 12 num3 = 20 num4 = 0 num5 = 20

Title: What is output of (if/else if/else if) snippet with two inputs?

Difficulty: Hard

Section Reference 1: 5.3 Multiple Alternatives

42) What is the value of the price variable after the following code snippet is executed?

int price = 42;

if (price < 40)

{

price = price + 10;

}

if (price > 30)

{

price = price * 2;

}

if (price < 100)

{

price = price - 20;

}

a) 42

b) 52

c) 84

d) 64

Title: What is value of price variable after (if/if/if) snippet is executed?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

43) Consider the following code snippet. Assuming that the user inputs 75 as the age, what is the output?

int age = 0;

Scanner in = new Scanner(System.in);

System.out.print("Please enter your age: ");

age = in.nextInt();

if (age < 10)

{

System.out.print("Child ");

}

if (age < 30)

{

System.out.print("Young adult ");

}

if (age < 70)

{

System.out.print("Old ");

}

if (age < 100)

{

System.out.print("Impressively old ");

}

a) Impressively old

b) Child Young adult Old

c) Young adult Old

d) Child Young adult Old Impressively old

Title: What is output of snippet (with 4 nonexclusive ifs) with this input?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

44) What is the value of the magicPowers variable after executing the following code snippet?

String magicPowers = "";

int experienceLevel = 9;

if (experienceLevel > 10)

{

magicPowers = magicPowers + "Golden sword ";

}

if (experienceLevel > 8)

{

magicPowers = magicPowers + "Shining lantern ";

}

if (experienceLevel > 2)

{

magicPowers = magicPowers + "Magic beans ";

}

a) Golden sword Shining lantern Magic beans

b) Shining lantern Magic beans

c) Magic beans

d) An empty string

Title: What is value of variable after (if/if/if) snippet is executed?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

45) Assuming that a user enters 5 as the age, what is the output of the following code snippet?

int age = 0;

Scanner in = new Scanner(System.in);

System.out.print("Please enter your age: ");

age = in.nextInt();

if (age < 10)

{

System.out.println("Kid");

}

if (age < 30)

{

System.out.print("Young");

}

if (age < 70)

{

System.out.print("Aged");

}

if (age < 100)

{

System.out.print("Old");

}

a)

Kid

b)

Kid

Young

c)

Kid

YoungAged

d)

Kid

YoungAgedOld

Title: What is output of (if/if/if/if) snippet with this input?

Difficulty: Hard

Section Reference 1: 5.3 Multiple Alternatives

46) When drawing flowcharts, unconstrained branching and merging can lead to

a) So-called “spaghetti code”

b) A better design

c) Intuitive understanding of the flow of control

d) Clear visualization of the problem solution

Title: What does unconstrained branching and merging produce when drawing flowcharts?

Difficulty: Easy

Section Reference 1: 5.5 Problem Solving: Flowcharts

47) The flow chart shows the order in which steps should be executed, and the diamond-shaped boxes indicate

a) input

b) algorithms

c) tasks

d) conditional tests

Title: What do the diamond-shaped boxes indicate in a flow chart?

Difficulty: Easy

Section Reference 1: 5.5 Problem Solving: Flowcharts

48) When testing code for correctness, it always makes sense to

a) Test all cases

b) Identify boundary cases and test them

c) Check all cases by hand

d) Assume invalid input will never occur

Title: What is reasonable for checking / testing code?

Difficulty: Easy

Section Reference 1: 5.6 Problem Solving: Test Cases

49) Which of the following variables is used to store a condition that can be either true or false?

a) Algebraic

b) Logical

c) Boolean

d) Conditional

Title: Which kind of variable is used to store a true/false condition?

Difficulty: Easy

Section Reference 1: 5.7 Boolean Variables and Operators

50) Consider the following code snippet:

boolean attendance = true;

boolean failed = false;

Which of the following if statement s includes a condition that evaluates to true?

a) if (attendance == "true") { . . . }

b) if (attendance) { . . . }

c) if (failed) { . . . }

d) if (attendance == failed) { . . . }

Title: Which if statement includes a condition that evaluates to true?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

51) What is the output of the following code snippet?

boolean attendance = false;

String str = "Unknown";

attendance = !(attendance);

if (!attendance)

{

str = "False";

}

if (attendance)

{

attendance = false;

}

if (attendance)

{

str = "True";

}

else

{

str = "Maybe";

}

System.out.println(str);

a) False

b) True

c) Unknown

d) Maybe

Title: What is the output of snippet (with if/else and Boolean not)?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

52) Which of the following operators is used to combine two Boolean conditions?

a) ##

b) $$

c) %%

d) &&

Title: Which operator is used to combine two Boolean conditions?

Difficulty: Easy

Section Reference 1: 5.7 Boolean Variables and Operators

53) Assuming that a user enters 56 for age, what is the output of the following code snippet?

int age = 0;

Scanner in = new Scanner(System.in);

System.out.print("Please enter your age: ");

age = in.nextInt();

if (age < 13)

{

System.out.println("Kid!");

}

if (age >= 13 && age < 19)

{

System.out.println("Teen!");

}

if (age >= 19 && age < 30)

{

System.out.println("Young!");

}

if (age >= 30 && age < 50)

{

System.out.println("Adult!");

}

if (age >= 50)

{

System.out.println("Old!");

}

a) Teen!

b) Young!

c) Adult!

d) Old!

Title: What is output of (if with Boolean and) snippet with this input?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

54) Which of the following expressions represents a legal way of checking whether a value assigned to the num variable falls in the range 100 to 200?

a) if (num >= 200 && num <= 100)

b) if (num >= 100 && num <= 200)

c) if (num >= 100 || num <= 200)

d) if (num >= 200 || num <= 100)

Title: Which expression checks whether a value falls between 100 and 200?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

55) Which of the following expressions represents a legal way of checking whether a value for the num variable is either less than 100 or more than 200?

a) if (num <= 100 && num >= 200)

b) if (num < 100 && num > 200)

c) if (num < 100 || num > 200)

d) if (num <= 100 || num >= 200)

Title: Which expression checks whether a value is less than 100 or more than 200?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

56) Assuming that a user enters 64 as his score, what is the output of the following code snippet?

int score = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter your score: ");

score = in.nextInt();

if (score < 40)

{

System.out.println("F");

}

else if (score >= 40 || score < 50)

{

System.out.println("D");

}

else if (score >= 50 || score < 60)

{

System.out.println("C");

}

else if (score >= 60 || score < 70)

{

System.out.println("B");

}

else if (score >= 70 || score < 80)

{

System.out.println("B+");

}

else

{

System.out.println("A");

}

a) D

b) C

c) B

d) A

Title: What is output of (if/else if with Boolean or) with this input?

Difficulty: Hard

Section Reference 1: 5.7 Boolean Variables and Operators

57) What is the output of the following code snippet?

int num = 100;

if (num > 100);

{

num = num - 10;

}

System.out.println(num);

a) 90

b) 100

c) 99

d) 101

Title: What is output of (if with > condition) with this input?

Difficulty: Hard

Section Reference 1: 5.1 The if statement

58) Which of the following operators is used to invert a conditional statement?

a) !

b) !=

c) ||

d) ?

Title: Which operator is used to invert a conditional statement?

Difficulty: Easy

Section Reference 1: 5.7 Boolean Variables and Operators

59) Which of the following operators is NOT a relational operator?

a) <=

b) +=

c) !=

d) ==

Title: Which operator is NOT a relational operator?

Difficulty: Easy

Section Reference 1: 3.2 Comparing Values

60) What is the output of the following code snippet?

final int MIN_SPEED = 45;

final int MAX_SPEED = 65;

int speed = 55;

if (!(speed < MAX_SPEED))

{

speed = speed - 10;

}

if (!(speed > MIN_SPEED))

{

speed = speed + 10;

}

System.out.println(speed);

a) 45

b) 55

c) 65

d) 50

Title: What is output of (if with Boolean not) snippet?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

61) What is the value of num after you run the following code snippet?

int num = 100;

if (num <= 100)

{

num++;

}

if (num <= 200)

{

num--;

}

if (num <= 300)

{

num++;

}

if (num <= 400)

{

num--;

}

if (num <= 500)

{

num++;

}

a) 99

b) 100

c) 101

d) 102

Title: What is value of variable after snippet (with five ifs)?

Difficulty: Hard

Section Reference 1: 5.3 Multiple Alternatives

62) Which of the following options correctly represents a “nested if” structure?

a)

if (cost < 70)

{

if (tax_rate < 0.10) { . . . }

}

b)

if (cost < 70) { . . . }

if (tax_rate < 0.10) { . . . }

c)

if (cost < 70) { . . . }

else { . . . }

if (tax_rate < 0.10) { . . . }

d)

if (cost < 70) { . . . }

{

else

{

if (tax_rate < 0.10) { . . . }

}

}

Title: Which snippet represents a “nested if” structure?

Difficulty: Easy

Section Reference 1: 5.4 Nested Branches

63) Which of the following statements is true about the “nested if” structure?

a) It cannot have any else branches at all.

b) It allows multiple else branches in a single if statement.

c) It allows one if statement within another if statement.

d) It does not allow multiple else branches inside a nested if statement.

Title: Which statement is true about the “nested if” structure?

Difficulty: Easy

Section Reference 1: 5.4 Nested Branches

64) Assuming that a user enters 10, 20, and 30 as input values one after another, separated by spaces, what is the output of the following code snippet?

int num1 = 0;

int num2 = 0;

int num3 = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

num1 = in.nextInt();

System.out.print("Enter a number: ");

num2 = in.nextInt();

System.out.print("Enter a number: ");

num3 = in.nextInt();

if (num1 > num2)

{

if (num1 > num3)

{

System.out.println(num1);

}

else

{

System.out.println(num3);

}

}

else

{

if (num2 > num3)

{

System.out.println(num2);

}

else

{

System.out.println(num3);

}

}

a) 0

b) 10

c) 20

d) 30

Title: What is output of (if/else with nested if/else) snippet with these three inputs?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

65) What is the output of the following code snippet?

int num = 100;

if (num < 100)

{

if (num < 50)

{

num = num - 5;

}

else

{

num = num – 10;

}

}

else

{

if (num > 150)

{

num = num + 5;

}

else

{

num = num + 10;

}

}

System.out.println(num);

a) 95

b) 100

c) 105

d) 110

Title: What is output of (if/else with nested if/else) snippet?

Difficulty: Hard

Section Reference 1: 5.4 Nested Branches

66) Which of the following options refers to the technique of simulating program execution on a sheet of paper?

a) Compiling

b) Prototyping

c) Tracing

d) Debugging

Title: Which refers to the technique of simulating program execution on a sheet of paper?

Difficulty: Easy

Section Reference 1: 5.4 Nested Branches

67) Which of the following coding techniques can hand-tracing be applied to?

a) Pseudocode

b) Java code

c) Both pseudocode and Java code

d) Neither pseudocode nor Java code

Title: Which coding technique can hand-tracing be applied to?

Difficulty: Easy

Section Reference 1: 5.4 Nested Branches

68) Assuming that a user enters 22 as the price of an object, which of the following hand-trace tables is valid for the given code snippet?

int price = 0;

String status = "";

Scanner in = new Scanner(System.in);

System.out.print("Please enter object’s price: ");

price = in.nextInt();

if (price >= 50)

{

status = "reasonable";

if (price >= 75)

{

status = "costly";

}

}

else

{

status = "inexpensive";

if (price <= 25)

{

status = "reasonable";

}

}

a)

price

status

0

“”

22

“inexpensive”

“reasonable”

b)

price

status

0

“inexpensive”

22

“reasonable”

c)

price

status

0

“”

22

“reasonable”

“costly”

d)

price

status

0

“reasonable”

22

“costly”

Title: Which hand-trace table is valid for this snippet?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

69) Which of the following operators compare using short-circuit evaluation?

a) ++

b) –

c) &&

d) ==

Title: Which operator uses short-circuit evaluation?

Difficulty: Easy

Section Reference 1: 5.7 Boolean Variables and Operators

Section Reference 2: Special Topic 5.6

70) Consider the following code snippet:

int score = 0;

double price = 100;

if (score > 0 && price < 200 && price / score > 10)

{

System.out.println("buy");

}

Which of the following statements is true on the basis of this code snippet?

a) The output is buy.

b) The code snippet compiles and runs, but there is no output.

c) The code snippet doesn't compile.

d) The code snippet causes a divide-by-zero error.

Title: Which statement is true on the basis of this code snippet?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

Section Reference 2: Special Topic 5.6

71) Which of the following options checks that city is neither Chicago nor Dallas?

a) if (!city.equals("Chicago") || !city.equals("Dallas"))

b) if (!(city.equals("Chicago") || city.equals("Dallas")))

c) if (!(city.equals("Chicago") && city.equals("Dallas")))

d) if (!city.equals("Chicago") || city.equals("Dallas"))

Title: Which option checks that city is neither Chicago nor Dallas?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

Section Reference 2: Special Topic 5.7

72) Assuming that a user enters 45, 78, and then 12, what is the output of the following code snippet?

int num1 = 0;

int num2 = 0;

int num3 = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

num1 = in.nextInt();

System.out.print("Enter a number: ");

num2 = in.nextInt();

System.out.print("Enter a number: ");

num3 = in.nextInt();

if (!(num1 > num2 && num1 > num3))

{

System.out.println(num1);

}

else if (!(num2 > num1 && num2 > num3))

{

System.out.println(num2);

}

else if (!(num3 > num1 && num3 > num2))

{

System.out.println(num3);

}

a) 12

b) 45

c) 78

d) There is no output due to compilation errors.

Title: What is output of (if/else if/else if) snippet with these three inputs?

Difficulty: Hard

Section Reference 1: 5.7 Boolean Variables and Operators

73) Which of the following statements can be used to validate that the user input for the floor variable is between 0 and 20 inclusive?

a) if (floor >= 0 && floor <= 20)

b) if (floor >= 0 || floor <= 20)

c) if (floor <= 0 && floor >= 20)

d) if (floor <= 0 || floor >= 20)

Title: Which statement validates that input is between 0 and 20?

Difficulty: Easy

Section Reference 1: 3.8 Application: Input Validation

74) Assuming that a valid price should be between 30 and 50, does the following code snippet test this condition correctly?

final int MIN_PRICE = 30;

final int MAX_PRICE = 50;

int price = 0;

Scanner in = new Scanner(System.in);

System.out.print("Please enter the price: ");

price = in.nextInt();

if (price < MIN_PRICE)

{

System.out.println("Error: The price is too low.");

}

else if (price > MAX_PRICE)

{

System.out.println("Error: The price is too high.");

}

else

{

System.out.println("The price entered is in the valid price range.");

}

a) This code snippet ensures that the price value is between 30 and 50.

b) This code snippet only ensures that the price value is greater than 30.

c) This code snippet only ensures that the price value is less than 50.

d) This code snippet ensures that the price value is either less than 30 or greater than 50.

Title: Does this code test whether input is between 30 and 50?

Difficulty: Medium

Section Reference 1: 3.8 Application: Input Validation

75) Which of the following conditions tests whether the user enters an integer value that will then be assigned to the floor variable?

int floor = 0;

Scanner in = new Scanner(System.in);

System.out.print("Floor: ");

( . . . )

floor = in.nextInt();

a) if (in.nextInt(floor))

b) if (in.hasNextInt())

c) if (in.fail)

d) if (in.nextInt())

Title: Which condition tests for (failed user input)?

Difficulty: Easy

Section Reference 1: 3.8 Application: Input Validation

76) Assuming that the user provides 3 as input, what is the output of the following code snippet?

int x;

int y;

x = 0;

System.out.print("Please enter y: ");

Scanner in = new Scanner(System.in);

y = in.nextInt();

if (y > 0)

{

x = 2 * y;

}

else

{

x = 2 + x;

}

System.out.println("x: " + x);

a) x: 2

b) x: 4

c) x: 6

d) There is no output due to compilation errors.

Title: What is output of (if/else with > test) snippet with this input?

Difficulty: Hard

Section Reference 1: 3.2 Comparing Values

77) Assuming that the user provides 49 as input, what is the output of the following code snippet?

int x = 0;

int y = 0;

System.out.print("Please enter y: ");

Scanner in = new Scanner(System.in);

y = in.nextInt();

if (y > 50);

{

x = y;

}

System.out.println("x: " + x);

a) x: 0

b) x: 49

c) x: 50

d) There is no output due to compilation errors.

Title: What is output of (if with > test) snippet with this input?

Difficulty: Hard

Section Reference 1: 3.2 Comparing Values

78) What is the output of the following code snippet?

int age = 25;

if (age > 30)

{

System.out.println("You are wise!");

}

else

{

System.out.println("You have much to learn!");

}

a)

There is no output due to compilation errors.

b)

You are wise!

c)

You have much to learn!

d)

You are wise!

You have much to learn!

Title: What is output of (if/else with > test) with this input?

Difficulty: Medium

Section Reference 1: 3.2 Comparing Values

79) What is the output of the following code snippet?

int x = 50;

if (x > 100)

{

x++;

}

else

{

x--;

}

System.out.println(x);

a) 49

b) 50

c) 51

d) 52

Title: What is output of (if/else with increment/decrement) with this input?

Difficulty: Medium

Section Reference 1: 3.2 Comparing Values

80) A store applies a 15 percent service charge on all items with a price of at least $150. No service charge is otherwise applicable. Which of the following DOES NOT correctly compute the service charge?

a)

double serviceCharge = 0;

if (cost >= 150)

{

serviceCharge = 0.15 * cost;

}

b)

double serviceCharge = 0.15 * cost;

if (cost <= 150)

{

serviceCharge = 0;

}

c)

double serviceCharge;

if (cost < 150)

{

serviceCharge = 0;

}

else

{

serviceCharge = 0.15 * cost;

}

d)

double serviceCharge = 15;

if (cost >= 150)

{

serviceCharge = 0.15 * cost;

}

else

{

serviceCharge = 0;

}

Title: Which statement DOES NOT correctly compute the service charge?

Difficulty: Easy

Section Reference 1: 3.2 Comparing Values

81) What is the output of the following code snippet?

double salary = 55000;

double cutOff = 65000;

double minSalary = 40000;

if (minSalary > salary)

{

System.out.println("Minimum salary requirement is not met.");

}

if (cutOff < salary)

{

System.out.println("Maximum salary limit is exceeded.");

}

else

{

System.out.println("Salary requirement is met.");

}

a) Minimum salary requirement is not met.

b) Maximum salary limit is exceeded.

c) Salary requirement is met.

d) There is no output.

Title: What is output of (if/if/else) snippet?

Difficulty: Medium

Section Reference 1: 3.2 Comparing Values

82) What is the output of the following code snippet?

int digit = 500;

if (digit != 500)

{

System.out.println("500");

}

else

{

System.out.println("Not 500");

}

a)

There is no output due to compilation errors.

b)

500

c)

Not 500

d)

500

Not 500

Title: What is output of (if/else with != test)?

Difficulty: Hard

Section Reference 1: 3.2 Comparing Values

83) What is the output of the following code snippet?

String someString1 = "his";

String someString2 = "cycle";

if (someString1.compareTo(someString2) < 0)

{

System.out.println(someString2);

}

else

{

System.out.println(someString1);

}

a) his

b) hiscycle

c) cycle

d) There is no output due to compilation errors.

Title: What is output of (if/else with string ordering test)?

Difficulty: Medium

Section Reference 1: 3.2 Comparing Values

84) What is the output of the following code snippet?

int num1 = 40;

if (num1 <= 40)

{

System.out.print("F");

}

if (num1 <= 75)

{

System.out.print("C");

}

if (num1 <= 90)

{

System.out.print("B");

}

a) F

b) C

c) B

d) FCB

Title: What is output of snippet (with three ifs)?

Difficulty: Easy

Section Reference 1: 5.3 Multiple Alternatives

85) What is the output after running the following code snippet?

int number = 600;

if (number < 200)

{

System.out.println("Low spender");

}

else if (number < 500)

{

System.out.println("Spending in moderation");

}

else if (number < 1000)

{

System.out.println("Above average!");

}

else

{

System.out.println("High Roller!");

}

a) Low spender

b) Spending in moderation

c) Above average!

d) High Roller!

Title: What is output of (if/else if/else if) snippet with this input?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

86) What is the output of the following code snippet?

int x = 25;

if (x < 100)

{

x = x + 5;

}

if (x < 500)

{

x = x - 2;

}

if (x > 10)

{

x++;

}

else

{

x--;

}

System.out.println(x);

a) 27

b) 28

c) 29

d) 30

Title: What is output of (if/if/if/else) snippet with this input?

Difficulty: Hard

Section Reference 1: 5.3 Multiple Alternatives

87) What is the value of the cost variable after the following code snippet is executed?

int cost = 82;

if (cost < 100)

{

cost = cost + 10;

}

if (cost > 50)

{

cost = cost * 2;

}

if (cost < 100)

{

cost = cost - 20;

}

a) 82

b) 92

c) 184

d) 164

Title: What is value of cost variable after (if/if/if) snippet is executed?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

88) Consider the following code snippet. What is the output?

int score = 68;

if (score < 50)

{

System.out.print("You need to practice!");

}

if (score < 100)

{

System.out.print("Almost respectable!");

}

if (score < 150)

{

System.out.print("You hit triple digits!");

}

if (score < 250)

{

System.out.print("Impressive!");

}

a) You need to practice!

b) Almost respectable!You hit triple digits!

c) You hit triple digits!Impressive!

d) Almost respectable!You hit triple digits!Impressive!

Title: What is output of snippet (with 4 ifs) with this input?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

89) What is the output of the following code snippet?

int shoeSize = 8;

if (shoeSize < 6)

{

System.out.println("Petite");

}

if (shoeSize < 8)

{

System.out.println("Small");

}

if (shoeSize < 10)

{

System.out.println("Medium");

}

if (shoeSize < 14)

{

System.out.println("Large");

}

a)

Petite

b)

Petite

Small

c)

Small

Medium

d)

Medium

Large

Title: What is output of (if/if/if/if) snippet with this input?

Difficulty: Hard

Section Reference 1: 5.3 Multiple Alternatives

90) Consider the following code snippet:

boolean married = true;

boolean engaged = false;

Which of the following if statements includes a condition that evaluates to true?

a) if (married == "true") { . . . }

b) if (married) { . . . }

c) if (engaged) { . . . }

d) if (married == engaged) { . . . }

Title: Which if statement includes a condition that evaluates to true?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

91) What is the output of the following code snippet?

boolean passed = false;

String someStr = "Unknown";

passed = !(passed);

if (!passed)

{

someStr = "False";

}

if (passed)

{

passed = false;

}

if (!passed)

{

someStr = "True";

}

else

{

someStr = "Maybe";

}

System.out.println(some_str);

a) False

b) True

c) Unknown

d) Maybe

Title: What is the output of snippet (with if/else and Boolean not)?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

92) What is the output of the following code snippet?

int golfScore = 64;

if (golfScore < 60)

{

System.out.println("Astounding!");

}

if (golfScore >= 60 && golfScore < 70)

{

System.out.println("Professional!");

}

if (golfScore >= 70 && golfScore < 80)

{

System.out.println("Pretty good!");

}

if (golfScore >= 80 && golfScore < 90)

{

System.out.println("Not so hot!");

}

if (golfScore >= 90)

{

System.out.println("Keep your day job!");

}

a) Astounding!

b) Professional!

c) Pretty good!

d)Keep your day job!

Title: What is output of (if with Boolean and) snippet with this input?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

93) Which of the following expressions represents a legal way of checking whether a value assigned to the number variable falls between 50 and 100 inclusive?

a) if (number >= 100 && number <= 50)

b) if (number >= 50 && number <= 100)

c) if (number >= 50 || number <= 100)

d) if (number >= 100 || number <= 50)

Title: Which expression checks whether a value falls between 50 and 100?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

94) Assuming that a user enters 68 as the score, what is the output of the following code snippet?

int score = 68;

if (score < 50)

{

System.out.println("F");

}

else if (score >= 50 || score < 55) { System.out.println("D"); }

else if (score >= 55 || score < 65) { System.out.println("C"); }

else if (score >= 65 || score < 75) { System.out.println("B"); }

else if (score >= 75 || score < 80) { System.out.println("B+"); }

else { System.out.println("A"); }

a) D

b) C

c) B

d) A

Title: What is output of (if/else if with Boolean or) with this input?

Difficulty: Hard

Section Reference 1: 5.7 Boolean Variables and Operators

95) Assuming that a user enters 50, 70, and 60 as input values one after another, separated by spaces, what is the output of the following code snippet?

int number1 = 0;

int number2 = 0;

int number3 = 0;

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

number1 = in.nextInt();

System.out.print("Enter a number: ");

number2 = in.nextInt();

System.out.print("Enter a number: ");

number3 = in.nextInt();

if (number1 > number2)

{

if (number1 > number3)

{

System.out.println(number1);

}

else

{

System.out.println(number3);

}

}

else

{

if (number2 > number3)

{

System.out.println(number2);

}

else

{

System.out.println(number3);

}

}

a) 0

b) 50

c) 60

d) 70

Title: What is output of (if/else with nested if/else) snippet with these three inputs?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

96) Assuming that a user enters 45 as the brightness of a lamp, which of the following hand-trace tables is valid for the given code snippet?

int brightness = 0;

String description = "";

Scanner in = new Scanner(System.in);

System.out.print("Enter your lamp brightness (in watts): ");

brightness = in.nextInt();

if (brightness >= 120)

{

description = "very bright";

if (brightness >= 100)

{

description = "bright";

}

}

else

{

description = "pleasant";

if (brightness <= 50)

{

description = "dim";

}

}

a)

brightness

description

0

“”

45

“pleasant”

“dim”

b)

brightness

description

0

“very bright”

45

“bright”

c)

brightness

description

0

“”

45

“bright”

“pleasant”

d)

brightness

description

0

“bright”

45

“pleasant”

Title: Which hand-trace table is valid for this snippet?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

97) Which of the following options checks that the string country is neither China nor Denmark?

a) if (!country.equals("China") || !country.equals("Denmark")

b) if (!(country.equals("China") || country.equals("Denmark")))

c) if (!(country.equals("China") && country.equals("Denmark")))

d) if (country.equals("China") || country.equals("Denmark"))

Title: Which option checks that country is neither China nor Denmark?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

98) Assuming that the valid cost should be between 100 and 200, does the following code snippet test this condition correctly?

final int MIN_COST = 100;

final int MAX_COST = 200;

int cost = 0;

Scanner in = new Scanner(System.in);

System.out.print("Please enter the cost: ");

cost = in.nextInt();

if (cost < MIN_COST)

{

System.out.println("Error: The cost is too low.");

}

else if (cost > MAX_COST)

{

System.out.println("Error: The cost is too high.");

}

else

{

System.out.println("The cost entered is in the valid cost range.");

}

a) This code snippet ensures that the cost value is between 100 and 200.

b) This code snippet only ensures that the cost value is greater than 100.

c) This code snippet only ensures that the cost value is less than 200.

d) This code snippet ensures that the cost value is either less than 100 or greater than 200.

Title: Does this code test whether input is between 100 and 200?

Difficulty: Medium

Section Reference 1: 3.8 Application: Input Validation

99) Which of the following conditions tests whether the user enters the single digit 5?

String s;

Scanner in = new Scanner(System.in);

System.out.print("Enter a single digit: ");

s = in.next();

a) if (s == "5")

b) if (s == '5')

c) if (s = "5")

d) if (s.equals("5"))

Title: Which condition tests for single digit input?

Difficulty: Hard

Section Reference 1: 3.2 Comparing Values

Section Reference 2: Common Error 5.3 Using == to Compare Strings

100) Assuming that the user inputs “twenty” as the input, what is the output of the following code snippet?

String numEmployeesStr;

Scanner in = new Scanner(System.in);

System.out.println("Please enter the number of your employees: ");

numEmployeesStr = in.next();

int numEmployees = Integer.parseInt(numEmployeesStr);

if (numEmployees < 10)

{

System.out.println("Very small business!");

}

else

{

System.out.println("Small business");

if (numEmployees > 100)

{

System.out.println("Mid size business");

}

else

{

System.out.println("Large business");

}

}

a) Very small business!

b) Small business

c) Mid size business

d) Run-time error

Title: What is output of (if/else with Integer.parseInt) snippet with this input?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

101) Which of the following conditions tests for the user to enter the string "Hello"?

String s;

Scanner in = new Scanner(System.in);

System.out.print("Enter a word: ");

s = in.next();

a) if (s == "Hello")

b) if (s.substring(0,5) == "Hello")

c) if (s.equals("Hello"))

d) if (s = "Hello")

Title: Which condition tests for String equality?

Difficulty: Hard

Section Reference 1: 5.7 Boolean Variables and Operators

102) Which of the following options checks that character ch is neither a letter nor a white space?

a) if (!Character.isLetter(ch) || !Character.isWhiteSpace(ch))

b) if (!(Character.isLetter(ch) || Character.isWhiteSpace(ch)))

c) if (!(Character.isLetter(ch) && Character.isWhiteSpace(ch)))

d) if (!Character.isLetter(ch) || Character.isWhiteSpace(ch))

Title: Which option checks that character is neither letter nor white space?

Difficulty: Medium

Section Reference 1: 5.7 Boolean Variables and Operators

103) What will be printed by the statements below?

int a = 10;

if (a > 5)

{

System.out.print ("One");

}

else

{

System.out.print ("Two");

}

System.out.print ("Three");

a)One

b)OneThree

c)TwoThree

d)OneTwoThree

Title: What will be printed by the statements below?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

104) A company applies a discount based on the size of the order. If the order is over $50, the discount is 5%. If the order is over $100, the discount is 10%. Otherwise, there is no discount. If the integer variable order contains the size of the order, which of the following will assign the double variable discount the correct value?

    1. if (order > 100)
      discount = 0.10;

else if (order > 50)

discount = 0.05;

else

discount = 0;

    1. if (order > 100)
      discount = 0.10;

if (order > 50)

discount = 0.05;

else

discount = 0;

    1. if (order > 100)
      discount = 0.10;

if (order > 50)

discount = 0.05;

if (order <= 50)

discount = 0;

    1. if (order > 50)
      discount = 0.05;

else if (order > 100)

discount = 0.10;

else

discount = 0;

Title:Which of the following will assign the double variable discount the correct value?

Difficulty: Medium

Section Reference 1: 5.3 Multiple Alternatives

105) What will be printed by the statements below?

int a = 10;

int b = 20;

int count = 0;

if (a > 5)

{

count ++;

}

if (b > 5)

{

count ++;

}

if (a > 10)

{

count ++;

}

if (b > 10)

{

count ++;

}

System.out.print (count);

a)1

b)2

c)3

d)4

Title: What will be printed by the statements below?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

106) What will be printed by the statements below?

int a = 10;

int b = 20;

int count = 0;

if (a > 5)

{

count ++;

}

else if (b > 5)

{

count ++;

}

else if (a > 10)

{

count ++;

}

else if (b > 10)

{

count ++;

}

System.out.print (count);

a)1

b)2

c)3

d)4

Title: What will be printed by the statements below?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

107) What will be printed by the statements below?

int a = 10;

int b = 20;

int count = 0;

if (a > 5)

{

count ++;

if (b > 5)

{

count ++;

}

}

if (a > 10)

{

count ++;

if (b > 10)

{

count ++;

}

}

System.out.print (count);

a)1

b)2

c)3

d)4

Title: What will be printed by the statements below?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

108) What will be printed by the statements below?

int a = 10;

int b = 20;

int count = 0;

if (a > 5)

if (b > 5)

{

count ++;

}

else

count = 7;

System.out.print (count);

a)0

b)1

c)7

Title: What will be printed by the statements below?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

109) What will be printed by the statements below?

int a = 10;

int b = 20;

int count = 0;

if (a > 10)

{

count ++;

if (b > 5)

{

count ++;

}

if (a > 10)

{

count ++;

}

}

if (b > 10)

{

count ++;

}

System.out.print (count);

a)1

b)2

c)3

d)4

Title: What will be printed by the statements below?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

110) Which of the following conditions will correctly check if the String variable greeting is "bonjour"?

a)if (greeting == "bonjour")

b)if (greeting.compareTo("bonjour") < 0)

c) if (greeting.equals("bonjour"))

d)if (greeting.compareTo("bonjour") > 0)

Title: Which of the following conditions will correctly check if the String variable greeting is "bonjour”?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

111) Which of the following conditions will correctly check if the String variable early comes before "middle" alphabetically?

a)if (greeting <= "middle")

b)if (greeting.compareTo("middle") > 0)

c)if (greeting.compareTo("middle") == 0)

d)if (early.compareTo("middle") < 0)

Title: Which of the following conditions will correctly check if the String variable early comes before "middle" alphabetically?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

112) Assume a, b, and c have been defined to be integer variables. Which of the following values make the expression !(a == b) && (c > a) true?

a)a = 10, b = 10, c = 15

b)a = 10, b = 20, c = 15

c)a = 10, b = 2, c = 5

d)a = 10, b = 20, c = 10

Title: Which of the following values make the expression !(a == b) && (c > a) true?

Difficulty: Medium

Section Reference 1: 5.4 Nested Branches

113) Which of the following conditions can be added to the code below so it will assign the larger value of two integer variables a and b to the integer variable maximum?

if (/* put condition here */)

{

maximum = a;

}

else

{

maximum = b;

}

a)a == b

b)b > a

c)a > b

d)a.compareTo (b) > 0

Title: Which of the following conditions can be added to the code?

Difficulty: Hard

Section Reference 1: 5.2 Comparing Values

114) Which of the following conditions is true only when the integer variable number is even?

a)number / 2 == 0

b)number > 2

c)number / 2 > 1

d)number % 2 == 0

Title: Which of the following conditions is true only when the integer variable number is even?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

115) Which of the following conditions is true only when the integer variable middle is between the values 0 and 10?

a)(0 <= middle) && (middle <= 10)

b)0 <= middle <= 10

c)(0 <= middle) || (middle <= 10)

d)(0 <= 10) && (middle <= 10)

Title: Which of the following conditions is true only when the integer variable middle is between the values 0 and 10?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

116) Which of the following conditions is true only when the integer variables a, b, and c contain three different values?

a)(a != b) && (a != c) && (b != c)

b)(a != b) || (a != c) || (b != c)

c)!((a == b) && (b == c) && (a == c))

d)a != b != c

Title: Which of the following conditions is true only when the integer variables a, b, and c contain three different values?

Difficulty: Medium

Section Reference 1: 5.2 Comparing Values

117) Assume the following variables have been declared and given values elsewhere:

boolean isFelon;

int age;

A person may vote if they are 18 or older and not a felon. Which of the following statements assigns the Boolean variable mayVote correctly?

a)mayVote = (age >= 18) || !isFelon;

b)mayVote = !((age >= 18) && isFelon);

c)mayVote = (age >= 18) && !isFelon;

d)mayVote = !((age >= 18) || isFelon);

Title: Which of the following statements assigns the Boolean variable correctly?

Difficulty: Medium

Section Reference 1: 5.7Boolean Variables and Operators

118) Assume the following variables have been declared and given values elsewhere:

boolean completedProject;

int programsDone;

double classPercentage;

A student will pass programming class if and only if they have a percentage of 70.0% or higher and have either completed the project or they have done 5 or more programs. Which of the following statements assigns the Boolean variable passProgramming correctly?

a)passProgramming = (classPercentage >= 0.7) && (programsDone >= 5 || completedProject);

b)passProgramming = (classPercentage >= 0.7) && (programsDone >= 5) && (completedProject);

c)passProgramming = (classPercentage >= 0.7) || (programsDone >= 5) || (completedProject);

d)passProgramming = ((classPercentage >= 0.7) && (programsDone >= 5 )) && completedProject;

Title: Which of the following statements assigns the Boolean variable correctly?

Difficulty: Medium

Section Reference 1: 5.7Boolean Variables and Operators

119) Assume isBusy and isHappy have been defined to be Boolean variables. Which of the following conditions has the same value as !isBusy || isHappy?

a)!(isBusy || !isHappy)

b)isBusy && !isHappy

c)!(isBusy && !isHappy)

d)isBusy || !isHappy

Title: Which of the following statements assigns the Boolean variable correctly?

Difficulty: Medium

Section Reference 1: 5.7Boolean Variables and Operators

120) Assume isBusy has been defined to be a Boolean variables. Which of the following has the same value as (isBusy || true) && !isBusy?

a)isBusy

b)!isBusy

c)true

d)false

Title: Which of the following statements assigns the Boolean variable correctly?

Difficulty: Medium

Section Reference 1: 5.7Boolean Variables and Operators

121) Assume isBusy has been defined to be a Boolean variables. Which of the following has the same value as (isBusy || true) && false?

a)isBusy

b)!isBusy

c)true

d)false

Title: Which of the following statements assigns the Boolean variable correctly?

Difficulty: Medium

Section Reference 1: 5.7Boolean Variables and Operators

122) Which of the following performs the same way as the switch statement below?

switch (value)

{

case 1: System.out.print ("Small"); break;

case 10: System.out.print ("Large"); break;

default: System.out.print ("Other"); break;

}

a)if (value == 1)
System.out.print ("Small");

else if (value == 10)

System.out.print ("Large");

else

System.out.print ("Other");

b)if (value == 1)
System.out.print ("Small");

if (value == 10)

System.out.print ("Large");

else

System.out.print ("Other");

c)if (value <= 1)
System.out.print ("Small");

else if (value >= 10)

System.out.print ("Large");

else

System.out.print ("Other");

d)if (value <= 1)
System.out.print ("Small");

if (value >= 10)

System.out.print ("Large");

else

System.out.print ("Other");

Title: Which of the following performs the same way as the switch statement below?

Difficulty: Medium

Section Reference 1: 5.7Boolean Variables and Operators

Document Information

Document Type:
DOCX
Chapter Number:
5
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 5 Decisions
Author:
Cay S. Horstmann

Connected Book

Big Java Early Objects 5e Complete Test Bank

By Cay S. Horstmann

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