What is the difference between an if statement an IF ELSE statement and an IF ELSE IF statement quizlet?

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

a) A condition and a body

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.

c) The else block is optional.

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.

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

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"; }

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

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

int x;
int y;
x = 0;
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.

c) x: 303

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

int a;
int b;
a = 0;
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

a) a: 0

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

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

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

a) Move the duplicated code outside of the if statement

What kind of operator is the <= operator?

a) Ternary
b) Arithmetic
c) Inequality
d) Relational

d) Relational

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

a) =<
b) <=
c) =
d) !

Answer: b

b) <=

The operator !> stands for

a) not less than.
b) not greater than.
c) not equal to.
d) this is not an operator in Java

d) this is not an operator in Java

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!

c) The number is SMALL!

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

int i = 0;
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

a) 24

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

int x = 0;
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

c) 29

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;
}

b) double discount = 0.10 * price;
if (price <= 100)
{
discount = 0;
}

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

c) III

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.

c) Income requirement is met.

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

a) Lexicographic
b) Semantic
c) Alphabetic
d) Syntactic

a) Lexicographic

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)

d) if (pitch == 440)

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

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

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

b) The condition does not evaluate to a Boolean value

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.

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

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!");

c) if (s1.equals(s2))
{
System.out.println("Yes!");
}

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)

d) (num != 0)

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

c) Not 100

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

a) her

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))

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

) 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

b) "Aardvandermeer" is first, then "Aardvark"

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())

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

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)

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

) 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");
}
if (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 tax brackets

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

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) makes the break statement optional.
d) requires compound Boolean expressions as alternatives.

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

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

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

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

b) nested if statement

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

d) The dangling else

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

d) 123

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

c) else

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 { . . . }

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

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!

c) High!

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

d) 11

) 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

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

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

d) 64

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

a) Impressively old

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

b) Shining lantern Magic beans

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

d) Kid
YoungAgedOld

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

a) So-called "spaghetti code"

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

d) conditional tests

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

b) Identify boundary cases and test them

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

c) Boolean

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) { . . . }

b) if (attendance) { . . . }

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

d) Maybe

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

a) ##
b) $$
c) %%
d) &&

d) &&

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!

d) Old!

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)

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

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)

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

) 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

d) A.

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

a) 90

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

a) !
b) !=
c) ||
d) ?

a) !

Which of the following operators is NOT a relational operator?

a) <=
b) +=
c) !=
d) ==

b) +=

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

b) 55

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

c) 101

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) { . . . }
}
}

a) if (cost < 70)
{
if (tax_rate < 0.10) { . . . }
}

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.

Answer: c

c) It allows one if statement within another if statemen

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

d) 30

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

d) 110

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

c) Tracing

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

c) Both pseudocode and Java code

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"

a)
price status
0 ""
22 "inexpensive"
"reasonable"

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

a) ++
b) -
c) &&
d) ==

c) &&

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.

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

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

a) if (city != "Chicago" || city != "Dallas")
b) if !(city == "Chicago" || city == "Dallas")
c) if !(city == "Chicago" && city == "Dallas")
d) if (city != "Chicago" || city == "Dallas")

b) if !(city == "Chicago" || city == "Dallas")

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

b) 45

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)

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

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.

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

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())

b) if (in.hasNextInt())

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

c) x: 6

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.

b) x: 49

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!

c) You have much to learn!

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

a) 49

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;
}

b) double serviceCharge = 0.15 * cost;
if (cost <= 150)
{
serviceCharge = 0;
}

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

c) Salary requirement is met.

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

c) Not 500

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.

a) his

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

d) FCB

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!

c) Above average!

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

c) 29

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

c) 184

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!

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

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

d) Medium
Large

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) { . . . }

b) if (married) { . . . }

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

b) True

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!

b) Professional!

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)

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

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

d) A

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

d) 70

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"

a)
brightness description
0 ""
45 "pleasant"
"dim"

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"))

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

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.

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

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"))

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

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

d) Run-time error

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")

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

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))

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