What are the two parts of an if statement? a) A condition and a body 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. 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. Which of the following is the correct syntax for an if statement? a) if (x < 10) { size = "Small"; } c) if (x < 10) { size = "Small"; } Assuming that the user provides 303 as input, what is the output of the following code snippet? int x; a) x: 0 c) x: 303 Assuming that the user provides 99 as input, what is the output of the following code snippet? int
a; a) a: 0 a) a: 0 The following code snippet contains an error. What is the error? if (cost > 100); a) Syntax error (won't compile) 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) a) Move the duplicated code outside of the if statement a) Move the duplicated code outside of the if statement What kind of operator is the <= operator? a) Ternary d) Relational Which of the following operators is used as a relational operator? a) =< Answer: b b) <= The operator !> stands for a) not less than. 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); a) There is no output due to compilation errors. c) The number is SMALL! What is the output of the following code snippet if the input is 25? int i =
0; a) 24 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; a) 27 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; b) double discount = 0.10 * price; 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. a) I c) III What is the output of the following code snippet? double income = 45000; a) Minimum income requirement is not met. c) Income requirement is met. In Java, which of the following orderings is used to compare strings? a) Lexicographic 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) 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 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; a) There should be an "else" condition 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; a) Using == to test the double variable average for equality is error-prone. 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) c) if (s1.equals(s2)) Which condition, when supplied in the if statement below in place of (. . .), will correctly protect against division by zero? if (. . .) a) (grade == 0) d) (num != 0) What is the output of the following code snippet? int num = 100; a) There is no output due to compilation errors. c) Not 100 What is the output of the following code snippet? String str1 = "her"; a) her 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) ) 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" Write an if-statement condition that is true if the length of string s1 is greater than 42. a) if (s1.length() > 42) a) if (s1.length() > 42) Which of the following options is a legally correct expression for inverting a condition? a) 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) 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 The switch statement in Java a) is like a
sequence of if statements that compares a single integer value against several constant 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 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 When an if statement is nested inside another if statement, it creates the possibility of a) an infinite loop d) The dangling else What is the output of the following code snippet? int s1 = 20; 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 c) else Consider the following code snippet: int number = 0; Assuming that the user input is 40, which block of statements is executed? a) if (number > 30) { . . . } 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; a) Too small! 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; a) 0 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; a) num1 = 20 num2 = 12 num3 = 20 num4 = 20 num5 =
0 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; a) 42 d) 64 Consider the following code snippet. Assuming that the user inputs 75 as the age, what is the output? int age = 0; a) Impressively old a) Impressively old What is the value of the magicPowers variable after executing the following code snippet? String magicPowers = ""; 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; a) Kid d) Kid When drawing flowcharts, unconstrained branching and merging can lead to a) So-called "spaghetti code" 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 d) conditional tests When testing code for correctness, it always makes sense to a) Test all cases 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 c) Boolean boolean attendance = true; Which of the following if statement s includes a condition that evaluates to true? a) if (attendance == "true") { . . . } b) if (attendance) { . . . } What is the output of the following code snippet? boolean
attendance = false; a) False d) Maybe Which of the following operators is used to combine two Boolean conditions? a) ## d) && Assuming that a user enters 56 for age, what is the output of the following code snippet? int age = 0; a) Teen! 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) 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) 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; a) D d) A. What is the output of the following code snippet? int num = 100; a) 90 a) 90 Which of the following operators is used to invert a conditional statement? a) ! a) ! Which of the following operators is NOT a relational operator? a) <= b) += What is the output of the following code snippet? final int MIN_SPEED = 45; a) 45 b) 55 What is the value of num after you run the following code snippet? int num = 100; a) 99 c) 101 Which of the following options correctly represents a "nested if" structure? a) if (cost < 70) a) if (cost < 70) Which of the following statements is true about the "nested if" structure? a) It cannot have any else branches at all. 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; a) 0 d) 30 What is the output of the following code snippet? int num = 100; a) 95 d) 110 Which of the following options refers to the technique of simulating program execution on a sheet of paper? a) Compiling c) Tracing Which of the following coding techniques can hand-tracing be applied to? a) Pseudocode 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; a) a) 69) Which of the following operators compare using short-circuit evaluation? a) ++ c) && Consider the following code snippet: int
score = 0; 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. 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") Assuming that a user enters 45, 78, and then 12, what is the output of the following code snippet? int num1 = 0; a) 12 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) 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; a) This code snippet ensures that the price value is between 30 and 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; a) if (in.nextInt(floor)) b) if (in.hasNextInt()) Assuming that the user provides 3 as input, what is the output of the following code snippet? int x; a) x: 2 c) x: 6 Assuming that the user provides 49 as input, what is the output of the following code snippet? int x = 0; a) x: 0 b) x: 49 What is the output of the following code snippet? int age = 25; a) There is no output due to compilation errors. c) You have much to learn! What is the output of the following code snippet? int x = 50; a)
49 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; b) double serviceCharge = 0.15 * cost; What is the output of the following code snippet? double salary = 55000; a) Minimum salary requirement is not met. c) Salary requirement is met. What is the output of the following code snippet? int digit = 500; a) There is no output due to compilation errors. c) Not 500 What is the output of the following code snippet? String someString1 = "his"; a) his a) his What is the output of the following code snippet? int num1 = 40; a) F d) FCB What is the output after running the following code snippet?
int number = 600; a) Low spender c) Above average! What is the output of the following code snippet? int x = 25; a) 27 c) 29 What is the value of the cost variable after the following code snippet is executed? int cost = 82; a) 82 c) 184 Consider the following code snippet. What is the output? int score = 68; a) You need to practice! d) Almost respectable !You hit triple digits !Impressive! What is the output of the following code snippet? int shoeSize = 8; a) Petite d) Medium Consider the following code snippet: boolean married = true; Which of the following if statements includes a condition that evaluates to true? a) if (married == "true") { . . . } b) if (married) { . . . } What is the output of the following code snippet? boolean passed = false; a) False b) True What is the output of the following code snippet? int golfScore = 64; a) Astounding! 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) Assuming that a user enters 68 as the score, what is the output of the following code snippet? int score = 68; a) D 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; a) 0 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; a) b) a) 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")) 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; a) This code snippet ensures that the cost value is between 100 and 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; a) if (s == "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; if (numEmployees < 10) } a) Very small business! d) Run-time error which of the following conditions tests for the user to enter the string "Hello"? String s; a) 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)) |