Unit 3
Which of the following is true about the equals method?
The way it works depends on the class of the object calling it. It exists in all classes in Java It is used to check if two objects are equal
What is the symbol for "not equal to" in Java?
!=
Of the following choices, which expression is !(x > y || z == 9) equivalent to?
x <= y && z != 9
Assume grade is an int variable. Which of the following is equivalent to !(grade > 90)?
grade <= 90
What is printed by the following code? int x = 13; x++; if (x == 14) { System.out.println("OK"); } if (x == 16) { System.out.println("Let's go!"); }
OK
Will the following statement evaluate to true or false? Assume: A is true and B is true. !A != !B
False
Which of the following is equivalent to x > 17 && x <= 21?
!(x <= 17 || x > 21)
What number is printed by the following code segment? int a = 13; if (a % 3 == 0) { a++; } else if (a <= 13) { a += 3; } else { a += 5; } System.out.println(a);
16
int a = 4; int b = 3; if (a < 5 || a % b == 0) { System.out.print("yes"); } else { System.out.print("no"); }
yes
Consider again the code from the previous problem: int base = 2; int exponent = scan.nextInt(); int answer = (int) Math.pow(base, exponent); if (answer <= 256) { System.out.println("exponent is 8 or less"); } The if statement from this code could also be written as which of the following without ever changing the output when this code segment runs? I. if (exponent < 8) { System.out.println("exponent is 8 or less"); } II. if (exponent > 8) { System.out.println("exponent is 8 or less"); } III. if (exponent <= 8) { System.out.println("exponent is 8 or less"); }
III
The following truth table matches which boolean condition? A - B - ____ 1 - 1 - 0 1 - 0 - 1 0 - 1 - 1 0 - 0 - 1
!A || !B
What is output? double test = 76.5; if (test == 76.5) { System.out.print("C"); } if (test == 84.5) { System.out.print("B"); } if (test == 92.5) { System.out.print("A"); }
C
Which of the following single lines of code could be used to declare a new variable named x and store the integer value 72 using this variable?
Integer x = new Integer(72); int x = 72; Integer x = 72;
What is output? int a = 7; int b = 8; if (a + b <= 15) { System.out.println("The answer is: " + a + b); }
The answer is: 78
When does this evaluate to true regardless of the unknown code? if ( x < y || /*unknown code*/)
When x is less than y.
What is printed when the following code is executed? int x = 17; int y = 11; if (y < x || ((7 * x - 6 * y) % x) == 1) { System.out.print("yes"); } else { System.out.print("no"); }
yes
What is the symbol for "less than or equal to" in Java?
<=
Consider the following code segment. int r = (int) (10 * Math.random()) + 1; if (r == 1) { System.out.println("bullseye"); } If I run this code segment lots of times, what do you expect to happen?
"bullseye" will be printed occasionally
What is wrong with the following code? Scanner scan = new Scanner(System.in); int x = scan.nextInt(); if (x = 9) System.out.println(x);
= is incorrect, it should be ==
The following code is intended to test whether the int variable num is less than 10, and if so print a warning and set it to 10. The code then prints the value of num. if (num < 10) System.out.println( "This is smaller than 10" ); num = 10; System.out.println("Value of num is " + num); What correction should be made so the code functions as intended?
Curly braces '{ }' should be added to enclose the second and third lines
What would be output by this code? int x = 12; int y = 13; if (x == y) { System.out.println("True"); }
Nothing
Which of these code segments prints "correct" if and only if x is less than or equal to y?
if (x <= y) { System.out.println("correct"); }
What is printed when the following code is evaluated. int x = 12; int y = 13; if (x == y && (2 * x / 3 + 3 * y / 4 == x / 3 * 2 + y / 4 * 3)) { System.out.print("yes"); } else { System.out.print("no"); }
no
Which of the following is a legal variable name in Java?
other_var
Water freezes at or below 32 degrees F. Consider the following code intended to test when rain might switch over to snow. You may assume that scan is a properly initialized Scanner object. int temp = scan.nextInt(); if (/* Missing Code */) { System.out.println("It may snow"); } The code segment should print "It may snow" whenever the value entered by the user is 32 or below. Which of the following could be used to replace /* Missing Code*/?
temp < 33
What are if statements used for in programs?
Controlling the sequence of execution
Consider the following code: int diff = 0; if (Math.abs(num1 - num2) == (num1 - num2)) { diff = num1 - num2; } else if (Math.abs(num2 - num1) == (num2 - num1)) { diff = num2 - num1; } Which of the following will have the exact same result?
II and III only
Consider the following code intended to test if a double entered from the keyboard is positive: double a = scan.nextInt(); if (a == Math.abs(a)) { System.out.println("positive number"); } What is a potential mistake?
The scan.nextInt() should use scan.nextDouble()
When do you use an else statement?
To run some code when an if statement is false
When does this evaluate to false regardless of the unknown code? if ( x < y && /*unknown code*/)
When x is greater than or equal to y.
Which is not a correct boolean condition? A. (x < y && 7 == 9) B. (y ! >= 9 || x == 9 ) C. (x == 9 || y != 9) D. (!(x > y) && y == 9)
B
What is output by the following code segment? int x = 11; int y = 11; if (x != y ) { System.out.print("one"); } else if (x > y) { System.out.print("two"); } else if (y < x) { System.out.print("three"); } else if (y >= x) { System.out.print("four"); } else { System.out.print("five"); }
four
What is wrong with the following code? if (x = 7) System.out.print("It\'s true");
The equals sign, =, should be replaced with a double equals, ==.
Given the variable declarations: int x = 24; int y = 53; Which of the following boolean statements evaluate to true? Choose all correct answers.
!(x <= y && x == y) x >= y || x != y
What will be printed by the following code segment if the user enters 15 and 6 in that order? int x = scan.nextInt(); int y = scan.nextInt(); System.out.print(y / x);
0
What value is printed by the following code? int a = 1; int b = 2; if (a <= 2 && b < a) { a *= 2; } else { b *= 2; } if (!(a == 3 || b == 4)) { a *= 3; } else { b *= 5; } System.out.println(a + " " + b);
1 20
Select all the values of base for which the following condition is true. (base % 10 < 5)
10 102 80 3 24
What is printed to the screen after the following code runs? int x = 21; if (x > 21) { System.out.println(1); } else if (x < 21) { System.out.println(2); } else { System.out.println(3); }
3
The following truth table matches which boolean condition? A - B - ____ 1 - 1 - 1 1 - 0 - 1 0 - 1 - 1 0 - 0 - 0
A || ( A || B)
Which of the following if statements correctly executes all three following commands only when the if condition is true? I. if ( x < 10) int ans; ans = x * 90; System.out.println("answer: " + ans); II. if ( x < 10) { int ans; ans = x * 90; System.out.println("answer: " + ans); } III. if { ( x < 10) int ans; ans = x * 90; System.out.println("answer: " + ans); }
II only
Of the following code blocks, which one correctly executes exactly two commands when the condition is true? I. if (y == 99) { System.out.println("A"); System.out.println("B"); } System.out.println("C"); II. if (y == 99) System.out.println("A"); System.out.println("B"); System.out.println("C"); III. if (y == 99) { System.out.println("A"); System.out.println("B"); }
III only
What is displayed when the following code is compiled then run? int a = 7; int b = 7; if (a.equals(b)) { System.out.println("yes"); } else { System.out.println("no"); }
Nothing is displayed. An error stops the code from compiling.
What is printed when the following code is run? String str = "thinking"; String start = str.substring(0, 2); String end = str.substring(str.length()-3); if (start.equals("th") && end.equals("ing")) { System.out.println("Test 1 passed"); } if(!str.equals("thing")) { System.out.println("Test 2 passed"); }
Test 1 passed Test 2 passed
What does the following if statement do? if (num1 == Math.abs(num1))
Tests if the value in num1 is greater than or equal to zero.
Will the following statement evaluate to true or false? Assume: A is true and B is false. !A != !B
True
What is printed by the following code segment? boolean a = true; boolean b = false; boolean c = true; if (!(a || b) || c) { System.out.print("W"); else { System.out.print("X"); } if ((a && b) || (!b && c)) { System.out.print("Y"); } else { System.out.print("Z"); }
WY
Under what conditions will "correct!" be printed? if (weight <= 10) { if (weight > 3) { System.out.println("correct!"); } }
When weight is less than or equal to 10 and greater than 3.
When does the following Boolean condition evaluate to true? !(x > y || x == y)
When x is less than y.
What is printed by the following code? int x = 2; int y = 4; if (x == 2 && y == 2) { System.out.println("egg"); } else if (x == 2 || y == 2) { System.out.println("bacon"); } else { System.out.println("sausage"); }
bacon
The program below is intended to print a grade given a mark input as shown in the table: Mark - Grade 90 and above - A 80 to 89 - B 70 to 79 - C 60 to 69 - D 59 and below - F Which commands correctly fill in the two blanks in the code? if (mark >= 90) { System.out.println("A"); } else if (mark >= 80) { System.out.println("B"); } ______ (mark >= 70) { System.out.println("C"); } ______ (mark >= 60) { System.out.println("D"); } else { System.out.println("F"); }
else if, else if
Which of the following code segments will print "large enough" when the square of the number var is larger than 25?
if (Math.pow(var, 2) > 25) { System.out.println("large enough"); }
Short circuit evaluation means that in the code: if (x >= z || val == 97)
if x >= z is true it doesn't evaluate val == 97
What is printed by the following code? x = 5; if (!(x > 5 || x <= 2)) { System.out.println("in range 1"); } else { System.out.println("not in range 1"); } if (!(x >= 4 && x < 7)) { System.out.println("in range 2"); } else { System.out.println("not in range 2"); }
in range 1 not in range 2
What is displayed when the following code is run? String s1 = new String("Java"); String s2 = new String("Java"); String s3 = s2; if (s1 == s2) { System.out.print("y"); } else { System.out.print("n"); } if (s1 == s3) { System.out.print("y"); } else { System.out.print("n"); } if (s2 == s3) { System.out.print("y"); } else { System.out.print("n"); }
nny
What will the output be? int x = 7; int y = 7; if (x < y) { System.out.println("x is less than y"); } else{System.out.println("x is not less than y"); }
x is not less than y
Which code tests if the number in the variable num1 is greater than 15?
if (num1 > 15)
What is displayed when the following code is compiled then run? String s1 = "hello"; String s2 = "Hello"; if (s1.equals(s2)) { System.out.println("yes"); } else { System.out.println("no"); }
no
Consider the following code. int a = 10; if (a > 7) { System.out.print("one"); } else if (a % 5 == 0) { System.out.print("two"); } else { System.out.print("three"); } if (a * 2 >= 18) { System.out.print("four"); }
onefour
What will be printed when the following code is run? int x = 12; int y = 17; if (x + y < 30 && x < y) { System.out.print("one"); } if (!(x == 20) || y == 17) { System.out.print("two"); }
onetwo
You are taking temperature readings and need to convert them from Fahrenheit to Celsius. The formula is: Deduct 32 from the Fahrenheit number, then multiply the result by 5, and finally divide by 9. Consider the following two lines of code: double fahrenheit = scan.nextDouble(); double celsius = /* missing code*/ ; Which of the following could replace /* missing code*/ so the calculation works correctly?
(fahrenheit - 32) * 5 / 9
What is output to the screen by the following code? int x = 29 % 10; if (x > 10) { System.out.println(1); } else if (x > 8) { System.out.println(2); } else if (x > 6) { System.out.println(3); } else if (x > 4) { System.out.println(4); } else { System.out.println(5); }
2
What is printed by the following code? int x = 15; if (!(x >= 0 && x % 10 != 0)) { System.out.println("Condition 1"); } else if (!(x % 5 != 0 || x % 3 != 0)) { System.out.println("Condition 2"); } else{System.out.println("Condition 3"); }
Condition 2
After the following code has been executed, will the if statement evaluate to true or false? int x = -6; int y = 3; if (x == y) { //other code }
False
The following code is intended to test if the exponent is 8 or less. You may assume that scan is a properly initialized Scanner object. int base = 2; int exponent = scan.nextInt(); int answer = (int) Math.pow(base, exponent); if (answer <= 256) { System.out.println("exponent is 8 or less"); } What is the problem with the code?
Nothing is wrong, the code works as intended.
Which of the following statements is shown in the final column of the truth table? A - B - ____ 0 - 0 - 1 0 - 1 - 1 1 - 0 - 0 1 - 1 - 1
!(A && !B)
What is printed to the screen after the following code runs? int x = 8 % 2; if (x > 10) { System.out.println(1); } else if (x > 8) { System.out.println(2); } else if (x > 6) { System.out.println(3); } else if (x > 4) { System.out.println(4); } else { System.out.println(5); }
5
Suppose x and y are int variables set to inputs from the user and consider the following code which is intended to check whether x is a multiple of y. if (/* missing condition */) { System.out.println(x + " is a multiple of " + y) } Which of the following should replace /* missing condition */ so the code functions as intended and does not cause an error?
y != 0 && x % y == 0