Comp Sci Exam 2
15) Which expression can be used to decide if x is not between 10 and 20? a. !(10 < x && x < 20) b. !(x < 10 && x < 20) c. !(x < 10 || x < 20) d. !(x > 10 || x < 20)
a. !(10 < x && x < 20)
11) Both must be true for a person to ride: (1) At least 5 years old, (2) Taller than 36 inches. Which expression evaluates to true if a person can ride? a. (age >= 5) && (height > 36) b. (age >= 5) || (height > 36) c. (age > 5) && (height <= 36) d. (age > 5) || (height <= 36)
a. (age >= 5) && (height > 36)
24) Which expression follows the practice of using parentheses to make the intended order of evaluation explicit? a. (x > y) && (!a == b) b. (x > (y)) && ((!a == b)) c. ((x > y) && !a == (b)) d. ((x > y)) && ((!a == b))
a. (x > y) && (!a == b)
54) Which input value causes "Goodbye" to be output next? int x; x = scnr.nextInt(); while (x >= 0) { // Do something x = scnr.nextInt(); } System.out.println("Goodbye"); a. -1 b. 0 c. 1 d. No such value
a. -1
18) What is the final value of y? int x = 6; int y = 2; if (x < 10) { if (y < 5) { y = y + 1; } else { y = 7; } } else { y = y + 10; } a. 3 b. 7 c. 12 d. 13
a. 3
52) Which value of x results in short circuit evaluation, causing y < 4 to not be evaluated? (x >= 7) && (y < 4) a. 6 b. 7 c. 8 d. No such value
a. 6
50) Why should programmers avoid making the following comparison with double x? x == 0.3 a. Because 0.3 may not be exactly represented as 0.3 b. Because doubles should not be compared with integers c. Because variables should not be compared with literals d. Because the 0.3 should come first: 0.3 == x
a. Because 0.3 may not be exactly represented as 0.3
43) Which is the simplest expression that is true only when myChar is among a-z or A-Z? a. Character.isLetter(myChar) b. Character.isLetter(toLowerCase(myChar)) c. Character.isLetter(myChar) && !Character.isWhitespace(myChar) d. Character.isLetter(toLowerCase(myChar)) || Character.isLetter(toUpperCase(myChar))
a. Character.isLetter(myChar)
59) Which is an essential feature of a while loop having the following form? while (LoopExpression) { LoopBody } a. The LoopExpression should be affected by the LoopBody b. The LoopExpression should not be affected by the LoopBody c. The LoopBody should get user input d. The LoopBody should update at least two variables
a. The LoopExpression should be affected by the LoopBody
10) What value of x outputs "Junior"? if (x < 56) { // Output "Sophomore" } else if (x > 56) { // Output "Senior" } else { // Output "Junior" } a. Value 56 b. Values 57 or larger c. Values 55 or 57 d. No such value
a. Value 56
26) Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first. (x == 5) || (y == 2) && (z == 5) a. false OR (true AND false) --> false OR false --> false b. false OR (true AND false) --> false OR true --> true c. (false OR true) AND false --> true AND false --> false d. (false OR true) AND false --> true AND false --> true
a. false OR (true AND false) --> false OR false --> false
32) Which if branch executes when an account lacks funds and has not been used recently? hasFunds and recentlyUsed are booleans and have their intuitive meanings. a. if (!hasFunds && !recentlyUsed) b. if (!hasFunds && recentlyUsed) c. if (hasFunds && !recentlyUsed) d. if (hasFunds && recentlyUsed)
a. if (!hasFunds && !recentlyUsed)
33) Given string str is "Great", which choice has the most matching expressions? a. str.equals("Great") b. str.equals("Great"), str.equals("great") c. str.equals("Great"), str.equals("Great!") d. str.equals("Great"), str.equals("great"), str.equals("Great!")
a. str.equals("Great")
60) A loop should sum all inputs, stopping when the input is 0. If the input is 2 4 6 0, sum should end with 12. What should XXX, YYY, and ZZZ be? Choices are in the form XXX / YYY / ZZZ. int sum; int currVal; XXX; currVal = scnr.nextInt(); while (YYY) { ZZZ; currVal = scnr.nextInt(); } a. sum = 0 / currVal != 0 / sum = sum + currVal b. sum = currVal / currVal == 0 / sum = currVal c. sum = 1 / currVal != 0 / sum = sum + 1 d. sum = scnr.nextInt() / currVal == 0 / sum = scnr.nextInt()
a. sum = 0 / currVal != 0 / sum = sum + currVal
57) Which XXX and YYY will loop as long as the input is an integer less than 100? Choices are in the form XXX / YYY. w = scnr.nextInt(); while (XXX) { // Do something YYY; } a. w < 100 / w = scnr.nextInt() b. w >= 100 / (nothing) c. w < 100 / (nothing) d. w >= 100 / w = scnr.nextInt()
a. w < 100 / w = scnr.nextInt()
42) What is the ending value of userString? (Note the question asks about userString, not about x). a. "Facebook" b. "FaceBook" c. "Faceook" d. "B"
b. "FaceBook"
14) A restaurant gives a discount for children under 10. They also give the discount for adults over 55. Which expression evaluates to true if a discount should be given? a. (age < 10) && (age > 55) b. (age < 10) || (age > 55) c. (age >= 10) && (age <= 55) d. (age >= 10) || (age <= 55)
b. (age < 10) || (age > 55)
12) Which expression evaluates to false if x is 0 and y is 10? a. (x == 0) && (y == 10) b. (x == 0) && (y == 20) c. (x == 0) || (y == 10) d. (x == 0) || (y == 20)
b. (x == 0) && (y == 20)
16) Grover Cleveland served as president of the United States from 1885 to 1889 and from 1893 to 1897. Which expression correctly detects this range? a. (x > 1885 && x < 1889) || (x > 1893 && x < 1897) b. (x >= 1885 && x <= 1889) || (x >= 1893 && x <= 1897) c. (x >= 1885 && x <= 1889) && (x >= 1893 && x <= 1897) d. (x > 1885 && x <= 1889) || (x > 1893 && x <= 1897)
b. (x >= 1885 && x <= 1889) || (x >= 1893 && x <= 1897)
21) If the input is 5, what is the output? int x; int z = 0; x = scnr.nextInt(); if (x > 9) z = 3; z = z + 1; System.out.print(z); a. 0 b. 1 c. 3 d. 4
b. 1
61) How many times does the while loop execute for the given input values of -1 4 0 9? userNum = 3; while (userNum > 0) { // Do something // Get userNum from input } a. 0 b. 1 c. 2 d. 3
b. 1
84) What is the output, if n is 3? for (int i = 1; i <= n; ++i) { int factorial = 1; factorial = factorial * i; System.out.print(factorial + " "); } a. 1 1 1 b. 1 2 3 c. 1 2 6 d. Error: Cannot use the loop variable i inside the for loop
b. 1 2 3
2) If the input is 12, what is the final value for numItems? int x; int numItems = 12; x = scnr.nextInt(); if (x == 12) { numItems = 100; } else { numItems = 200; } numItems = numItems + 1; a. 100 b. 101 c. 200 d. 201
b. 101
58) How many times will the loop iterate, if the input is 105 107 99 103? x = scnr.nextInt(); while (x > 100) { // Do something x = scnr.nextInt(); } a. 1 b. 2 c. 3 d. 4
b. 2
76) How many x's will be output? Assume row and col are ints. for (row = 0; row < 2; ++row) { System.out.print("x"); for (col = 0; col < 3; ++col) { // Do something } } a. 1 b. 2 c. 3 d. 6
b. 2
64) What is the output, if the input is 3 2 4 5? All variables are integers. num = scnr.nextInt(); for (i = 0; i < num; ++i) { curr = scnr.nextInt(); System.out.print(curr); } a. 24 b. 245 c. 324 d. 3245
b. 245
45) What is the ending value of x? int x; userText = "mississippi"; x = userText.indexOf("i", 3); a. 1 b. 4 c. 7 d. 10
b. 4
31) If the input sets int x with 5 and int y with 7, what is the ending value of z? z is declared as a boolean. z = (x > y); a. True b. False c. Error: No value assigned to z due to a runtime error d. Error: No value assigned to z due to a syntax error
b. False
46) Given strings str1 = "Hello" and str2 = "Goodbye ", what is the ending value of string str3? str3 = str1 + str2; a. Hello Goodbye b. HelloGoodbye c. Goodbye Hello d. Error: Cannot add variables of type string
b. HelloGoodbye
78) A programmer must write a 500 line program. Which is most likely the best approach? a. Write 1 line, run and debug, write 1 more line, run and debug, repeat b. Write 10-20 lines, run and debug, write 10-20 more lines, run and debug, repeat c. Write 250 lines, run and debug, write 250 lines, run and debug d. Write 500 lines, run and debug
b. Write 10-20 lines, run and debug, write 10-20 more lines, run and debug, repeat
20) Which expressions for YYY and ZZZ will output "Young" for ages less than 20 and "YoungAdolescent" for ages between 10 and 20? int u; u = scnr.nextInt(); if (YYY) { System.out.print("Young"); if (ZZZ) { System.out.println(" Adolescent"); } } a. YYY: u < 20 ZZZ: u < 10 b. YYY: u < 20 ZZZ: u > 10 c. YYY: u > 20 ZZZ: u < 10 d. YYY: u > 20 ZZZ: u > 10
b. YYY: u < 20 ZZZ: u > 10
35) Which expression for XXX causes the code to output the strings in alphabetical order? (Assume the strings are lowercase) if (XXX) { System.out.println(firstStr + " " + secondStr); } else { System.out.println(secondStr + " " + firstStr); } a. firstStr.equals(secondStr) b. firstStr.compareTo(secondStr) < 0 c. firstStr.compareTo(secondStr) > 0 d. !firstStr.equals(secondStr)
b. firstStr.compareTo(secondStr) < 0
41) Given userStr = "Doghouse", what is the ending value of char x? x = Character.toLowerCase(userStr.charAt(2)); a. G b. g c. (Space) d. Error: Compiler complains about converting an already-lowercase letter
b. g
70) The program should output even values between -10 and 10 (inclusive), so -10 -8 ... 8 10. What should XXX be? for (XXX) { System.out.print(i + " "); } a. i = -10; i < 10; i = i + 2 b. i = -10; i <= 10; i = i + 2 c. i = -10; (i * 2) < 10; ++i d. i = -10; (i * 2) <= 10, ++i
b. i = -10; i <= 10; i = i + 2
71) Which XXX causes every character in string inputWord to be output? for (XXX) { System.out.println(inputWord.charAt(i)); } a. i = 0; i < (inputWord.length() - 1); ++i b. i = 0; i < inputWord.length(); ++i c. i = 0; i < (inputWord.length() + 1); ++i d. i = 0; i <= inputWord.length(); ++i
b. i = 0; i < inputWord.length(); ++i
66) A loop should output 1 to n. If n is 5, the output is 12345. What should XXX and YYY be? Choices are in the form XXX / YYY. n = scnr.nextInt(); for (XXX; i++) { System.out.println(YYY); } a. i = 0; i < n / i b. i = 0; i < n / i + 1 c. i = 1; i < n / i d. i = 1; i < n / i + 1
b. i = 0; i < n / i + 1
81) Which XXX will output only values up to 3? i is an int. for (i = 1; i < 10; i++) { System.out.print(i + " "); XXX { break; } } a. if (i != 3) b. if (i == 3) c. if (i != 4) d. if (i == 4)
b. if (i == 3)
4) To quit, a user types 'q'. To continue, a user types any other key. Which expression evaluates to true if a user should continue? a. key == 'q' b. key != 'q' c. (!key) == 'q' d. key == (!'q')
b. key != 'q'
39) What is the relation between a string's length and the string's last index? a. lastIndex == length b. lastIndex == length - 1 c. lastIndex == length + 1 d. lastIndex == length + 2
b. lastIndex == length - 1
69) The program should determine the largest integer seen. What should XXX be if the input values are any integers (negative and non-negative)? All variables are integers and have unknown initial values. for (i = 0; i < 10; ++i) { currValue = scnr.nextInt(); if (i == 0) { // First iteration XXX; } else if (currValue > maxSoFar) { maxSoFar = currValue; } } a. maxSoFar = 0 b. maxSoFar = currValue c. currValue = 0 d. currValue = maxSoFar
b. maxSoFar = currValue
6) Which expressions for XXX, YYY, and ZZZ output "Great job" for scores above 90, and "Nice try" otherwise? Choices are in the form XXX / YYY / ZZZ. int score; score = scnr.nextInt(); if (XXX) { System.out.println(YYY); } else { System.out.println(ZZZ); } a. score < 91 / "Great job" / "Nice try" b. score < 91 / "Nice try" / "Great job" c. score > 90 / "Nice try" / "Great job" d. (Not possible for given code)
b. score < 91 / "Nice try" / "Great job"
34) Given string str1 and string str2, which expression is true? str1 = "Hi"; str2 = "Hello"; a. str1.compareTo(str2) < 0 b. str1.compareTo(str2) > 0 c. str1.equals(str2) d. (Comparison not possible)
b. str1.compareTo(str2) > 0
37) For the string "on time", what character is at index 3? a. (Space) b. t c. i d. M
b. t
3) Which expression for YYY outputs "Quarter century!" when the input is 25? int x; x = scnr.nextInt(); if (YYY) { System.out.println("Nothing special"); } else { System.out.println("Quarter century!"); } a. x == 25 b. x != 25 c. x <> 25 d. x == !25
b. x != 25
1) Which expression for YYY will result in an output of "Pass" only if x is exactly 32? if(YYY) { printf("Pass"); } else { printf("Fail"); } a. x != 32 b. x == 32 c. x >= 32 d. x <= 32
b. x == 32
40) Given string s is "Sunday", what is the ending value of char myChar? myChar = s.charAt(s.length() - 1); a. a b. y c. " d. No such character
b. y
25) Which expression correctly evaluates 13 < num < 30? a. (num < 13) && (num < 30) b. (num < 13) || (num < 30) c. (num > 13) && (num < 30) d. (num > 13) || (num < 30)
c. (num > 13) && (num < 30)
48) A restaurant serves breakfast before 11 am, after which they serve lunch. Which expression for XXX outputs the correct string for any time? Variable time ranges from 0 to 23 (e.g., 13 means 1 pm). mealString = XXX; // Output mealString a. (time > 11) ? "Breakfast" : "Lunch" b. (time == 11) ? "Breakfast" : "Lunch" c. (time < 11) ? "Breakfast" : "Lunch" d. (time != 11) ? "Breakfast" : "Lunch"
c. (time < 11) ? "Breakfast" : "Lunch"
68) What is the output? int n; for (n = 0; n < 10; n = n + 3) { System.out.print(n + " "); } a. 0 1 2 3 4 5 6 7 8 9 b. 1 4 7 10 c. 0 3 6 9 d. 0 3 6 9 12
c. 0 3 6 9
80) What is the output? j and k are ints. for (j = 0; j < 2; ++j) { for (k = 0; k < 4; ++k) { if (k == 2) { break; } System.out.print("" + j + k + " "); } } a. 00 01 02 b. 00 01 02 03 c. 00 01 10 11 d. 00 01 02 10 11 12
c. 00 01 10 11
79) What is the output? int num = 10; while (num <= 15) { System.out.print(num + " "); if (num == 12) { break; } ++num; } System.out.print("Done"); a. 10 Done b. 10 11 Done c. 10 11 12 Done d. 10 11 12 13 14 15 Done
c. 10 11 12 Done
65) What is the ending value of sum, if the input is 2 5 7 3? All variables are integers. x = scnr.nextInt(); sum = 0; for (i = 0; i < x; ++i) { currValue = scnr.nextInt(); sum += currValue; } a. 5 b. 10 c. 12 d. 15
c. 12
62) What is the output? int x = 18; while (x > 0) { // Output x and a space x = x / 3; } a. 6 2 b. 6 2 0 c. 18 6 2 d. 18 6 2 0
c. 18 6 2
85) What is the output, if userVal is 5? int x; x = 100; if (userVal != 0) { int tmpVal; tmpVal = x / userVal; System.out.print(tmpVal); } a. 0 b. 5 c. 20 d. Error: The compiler generates an error
c. 20
29) What is the ending value of numItems if myChar = 'X'? switch (myChar) { case 'W': numItems = 1; case 'X': numItems = 2; case 'Y': numItems = 3; case 'Z': numItems = 4; } a. 2 b. 3 c. 4 d. 9
c. 4
67) What is the output if count is 4? for (i = count; i > 0; --i) { // Output i } a. 4 b. 321 c. 4321 d. 43210
c. 4321
47) What is the ending value of y if x is 50? y and x are ints. y = (x < 21) ? 100 : x; a. 'x' b. 21 c. 50 d. 100
c. 50
53) Which value of y results in short circuit evaluation, causing z == 99 to not be evaluated? (y > 50) || (z == 99) a. 40 b. 50 c. 60 d. No such value
c. 60
19) What is the final value of y? int x = 77; int y = 4; if (x == 77) { y = y + 1; } if (x < 100) { y = y + 1; } if (x > 77) { y = y + 1; } y = y + 1; a. 5 b. 6 c. 7 d. 8
c. 7
17) When was Jen unemployed?if ((year >= 2010) && (year <= 2014)) { // Jen employed at Regal Cinemas ... } else if ((year >= 2018)) { // Jen employed at AMC Cinemas ... } else { // Unemployed ... } a. Before 2010 and from 2014 to 2018 b. 2014 to 2018 c. Before 2010 and from 2015 to 2017 d. 2015 to 2017
c. Before 2010 and from 2015 to 2017
5) If the input is 5, what is the output? int x; x = scnr.nextInt(); if (x < 10) { System.out.print("Live "); } else if (x < 20) { System.out.print("long "); } else if (x < 30) { System.out.print("and "); } System.out.print("prosper!"); a. Live b. Live long c. Live prosper! d. Error: The compiler will complain about a missing else statement
c. Live prosper!
49) Which expression best determines if double variable x is 74.7? a. Math.abs(x - 74.7) != 0.0001 b. Math.abs(x - 74.7) > 0.0001 c. Math.abs(x - 74.7) < 0.0001 d. Math.abs(x - 74.7) == 0.0001
c. Math.abs(x - 74.7) < 0.0001
44) What is the ending value of str? str = "Mid life"; str = str.replace(" l", "-W"); a. Mi-Wife b. Mid-wife c. Mid-Wife d. MidWife
c. Mid-Wife
30) For what values of x does the default case execute in the code below? x is declared as an integer. switch (x) { case 2: ... break; case 3: ... break; case 4: ... break; default: ... // When does this execute? } a. Only for value 5 b. Only for all values greater than 4 c. Only for values that are not 2, 3, or 4 d. For any value
c. Only for values that are not 2, 3, or 4
27) A programmer intended to compute (a AND b) OR c, and wrote the following code, which sometimes yields incorrect output. Why? c | (a & b) a. The parentheses are interfering, and should not be present b. The ORing with c should appear after the parentheses, not before c. The bitwise operators should be replaced by logical operators d. If code only sometimes yields incorrect output, the compiler is likely broken
c. The bitwise operators should be replaced by logical operators
8) Which expressions for XXX and YYY correctly output the text for pre-teen and teenager? Choices are in the form XXX / YYY. if (XXX) { // Output "Pre-teen" } else if (YYY) { // Output "Teenager" } a. age <= 13 / age > 13 b. age < 13 / age < 19 c. age <13 / age <= 19 d. age >= 13 / age <= 19
c. age <13 / age <= 19
63) Which for loop will iterate 100 times? a. for (i = 0; i < 99; i++) b. for (i = 1; i < 99; i++) c. for (i = 0; i < 100; i++) d. for (i = 1; i < 100; i++)
c. for (i = 0; i < 100; i++)
73) Which YYY outputs the string in reverse? Ex: If the input is "Oh my", the output is "ym hO". int i; String str; str = scnr.nextLine(); for (YYY) { System.out.print(str.charAt(i)); } a. i = str.length(); i < 0; --i b. i = str.length(); i > 0; --i c. i = str.length() - 1; i >= 0; --i d. i = str.length() + 1; i > 0; --i
c. i = str.length() - 1; i >= 0; --i
36) For the string "Orange", what character is at index 3? a. r b. a c. n d. G
c. n
51) A programmer compares x == y, where x and y are doubles. Many different values are expected for x and y. For values that a programmer expects to be equal, the comparison will _____. a. always evaluate to true b. always evaluate to false c. sometimes evaluate to true, sometimes to false, depending on the values d. sometimes immediately exit the program, for values that are slightly different
c. sometimes evaluate to true, sometimes to false, depending on the values
7) Which expressions for YYY and ZZZ correctly output the indicated ranges? Assume int x's value will be 0 or greater. Choices are in the form YYY / ZZZ. if (YYY) { // Output "0-29" } else if (ZZZ) { // Output "30-39" } else { // Output "40+" } a. x < 29 / x >= 29 b. x < 30 / x >= 30 c. x < 30 / x < 40 d. x > 29 / x > 40
c. x < 30 / x < 40
72) Which input value causes the loop body to execute a 2nd time, thus outputting "In loop" again? String s = "Go"; while ((!s.equals("q")) && (!s.equals("Q"))) { System.out.println("In loop"); s = scnr.next(); } a. "q" only b. "Q" only c. Either "q" or "Q" d. "Quit"
d. "Quit"
9) Which expression for XXX outputs "Modern era" for any year 1980 and later? if (year < 2000) { // Output "Past" } else if (XXX) { // Output "Modern era" } a. year > 1980 b. year >= 1980 c. year >= 2000 d. (No such expression exists)
d. (No such expression exists)
13) Which expression for YYY correctly outputs that x is between 50-100? if (YYY) { // Output "50, 51, ..., 99, 100" } a. (x >= 50) || (x <= 100) b. 50 >= x <= 100 c. 50 <= x <= 100 d. (x >= 50) && (x <= 100)
d. (x >= 50) && (x <= 100)
23) Which operator is evaluated first: x + y < y - z * 2 ? a. + b. < c. - d. *
d. *
82) What is the output? for (i = 0; i <= 10; ++i) { if (i == 6) continue; else System.out.print(i + " "); } a. 0 1 2 3 4 5 b. 0 1 2 3 4 5 6 c. 0 1 2 3 4 5 7 8 9 d. 0 1 2 3 4 5 7 8 9 10
d. 0 1 2 3 4 5 7 8 9 10
75) How many x's will be output? i = 1; while (i <= 3) { j = 1; while (j <= i) { System.out.print("x"); ++j; } System.out.println(); ++i; } a. 0 b. 3 c. 4 d. 6
d. 6
56) What is the output, if the input is 3 2 1 0? x = scnr.nextInt(); while (x > 0) { System.out.print(2 * x + " "); } a. 6 b. 6 4 2 c. 6 4 2 0 d. 6 6 6 6 6 ... (infinite loop)
d. 6 6 6 6 6 ... (infinite loop)
38) Given str = "Cat", what is the result of this statement? str.charAt(1) = "ab"; a. str is "abt" b. str is "Catab" c. str is "Cab" d. Error: Assigning a character with a string is not allowed
d. Error: Assigning a character with a string is not allowed
22) What is y's ending value? int x; int y = 0; x = scnr.nextInt(); if (x = 20) { y = 3; } a. Always 0 no matter what the input b. Always 20 no matter what the input c. 3 when the input is 20, else 0 d. Error: Compiler will not allow the expression x = 20
d. Error: Compiler will not allow the expression x = 20
83) What is the output? public static void main(String[] args) { for (int i = 0; i < 3; ++i) { System.out.print(i); } System.out.print(i); } a. 012 b. 0122 c. 0123 d. Error: The variable i is not declared in the right scope
d. Error: The variable i is not declared in the right scope
28) What is the output for x = 15? switch (x) { case 10: // Output: "First " break; case 20: // Output: "Second " break; default: // Output: "No match " break; } a. First b. Second c. First Second d. No match
d. No match
55) Which input for char c causes "Done" to be output next? c = 'y'; while (c = 'y') { // Do something System.out.println("Enter y to continue, n to quit: "); // Get c from input } System.out.println("Done"); a. 'y' only b. 'n' only c. Any value other than 'y' d. No such value (error comparing c and 'y')
d. No such value (error comparing c and 'y')
77) What is the output? char letter1; char letter2; letter1 = 'p'; while (letter1 <= 'q') { letter2 = 'x'; while (letter2 <= 'y') { System.out.print("" + letter1 + letter2 + " "); ++letter2; } ++letter1; } a. px b. px py c. qx qy d. px py qx qy
d. px py qx qy
74) What is the output? int columns; int rows; for (rows = 0; rows < 2; ++rows) { for (columns = 0; columns < 3; ++columns) { System.out.print("x"); } System.out.println(); } a. xxx xxx b. xx xx xx c. xx xx xx d. xxx xxx
d. xxx xxx