CSE Chapter 2 Quiz

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

"Aardvandermeer" is first, then "Aardvark"

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

&&

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

(num != 0)

Which of the following operators is NOT a relational operator?

+=

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

101

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

11

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

110

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

123

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

184

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

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

29

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

29

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

30

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

45

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

55

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

64

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

70

What is the output of the following code snippet? int num = 100; if (num > 100); { num = num - 10; } System.out.println(num);

90

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

<=

What are the two parts of an if statement?

A condition and a body

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

Above average!

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

Almost respectable !You hit triple digits !Impressive!

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

Boolean

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

Both pseudocode and Java code

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

D

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

D

In a switch statement, if a break statement is missing

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

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

FCB

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

High!

When testing code for correctness, it always makes sense to

Identify boundary cases and test them

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

Impressively old

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

Income requirement is met.

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

Kid YoungAgedOld

The following code snippet contains an error. What is the error? if (cost > 100); { cost = cost - 10; } System.out.println("Discount cost: " + cost);

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

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

Maybe

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

Medium Large

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

Move the duplicated code outside of the if statement

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

Not 100

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

Not 500

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

Old!

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

Professional!

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

Run-time error

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

Salary requirement is met

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

Shining lantern Magic beans

When drawing flowcharts, unconstrained branching and merging can lead to

So-called "spaghetti code"

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?

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

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

The condition does not evaluate to a Boolean value

Which statement about an if statement is true?

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

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

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

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

The number is SMALL!

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

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

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

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

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

Tracing

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

You have much to learn!

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: 0

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

conditional tests

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

d) The dangling else

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?

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

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?

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

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?

else

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

her

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

his

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

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

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

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

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

if (!(a == 10))

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

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

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?

if (attendance) { . . . }

Which of the following options correctly represents a "nested if" structure?

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

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

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

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

if (in.hasNextInt())

Consider the following code snippet: boolean married = true; boolean engaged = false; Which of the following if statements includes a condition that evaluates to true?

if (married) { . . . }

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?

if (num < 100 || num > 200)

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?

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

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?

if (number > 30) { . . . }

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?

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

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?

if (pitch == 440)

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

if (s.equals("5"))

Which code snippet will output "Yes!" when two strings s1 and s2 are equal?

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

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

if (s1.length() > 42)

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

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

An if statement inside another if statement is called a

nested if statement

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

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

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

!

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

&&

What is the output of the following code snippet? int x = 50; if (x > 100) { x++; } else { x--; } System.out.println(x);

49

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.

III only

Which of the following statements is true about the "nested if" structure?

It allows one if statement within another if statement.

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

Lexicographic

What kind of operator is the <= operator?

Relational

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

The else block is optional.

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

True

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

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

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

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

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

brightness description 0 "" 45 "pleasant" "dim"

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

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

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

if (s.equals("Hello"))

The switch statement in Java

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

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

price status 0 "" 22 "inexpensive" "reasonable"

The operator !> stands for

this is not an operator in Java

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

x: 303

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

x: 49

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

x: 6


संबंधित स्टडी सेट्स

10th Grade Advanced English Mark Twain Notes Quiz (Chaney)

View Set

LOS COMPONENTES DEL CUERPO HUMANO

View Set

Chapter 9: The Cardiovascular System

View Set

Karch Pharmacology Ch. 16 - Anti-inflammatory, Anti-arthritis

View Set

INTENSIVE REVIEW GUIDE- Standard 4

View Set

NURS 211 Ch 12 Assessment and Care of Patients With Problems of Acid-Base Balance.

View Set

Review Questions- Chapter 08 Mental Health- Therapeutic Relationships And ATI Chapter 5

View Set

intro to sociology- chapter 9 global inequality

View Set