Unit 3 Progress Check: MCQ
Consider the following expression. (3 + 4 == 5) != (3 + 4 >= 5) What value, if any, does the expression evaluate to? A) true B) false C) 5 D) 7 E) No value; relational operators cannot be used on arithmetic expressions.
A
Consider the following two code segments. Assume that variables x and y have been declared as int variables and have been assigned integer values. I. int result = 0; if (x > y) { result = x - y; System.out.print(result); } else if (x < y) { result = y - x; System.out.print(result); } else { System.out.print(result); } II. if (x < y) { System.out.print(y - x); } else { System.out.print(x - y); } Which of the following correctly compares the outputs of the two code segments? A) Code segment I and code segment II produce the same output for all values of x and y. B) Code segment I and code segment II produce the same output only when x is equal to y. C) Code segment I and code segment II produce the same output only when x is not equal to y. D) Code segment I and code segment II produce the same output only when x is less than y. E) Code segment I and code segment II do not produce the same output for any values of x and y.
A
In the code segment below, the int variable temp represents a temperature in degrees Fahrenheit. The code segment is intended to print a string based on the value of temp. The following table shows the string that should be printed for different temperature ranges. 31 and below --> "cold" 32-50 --> "cool" 51-70 --> "moderate" 71 and above --> "warm" String weather; if (temp <= 31) {weather = "cold";} else {weather = "cool";} if (temp >= 51) {weather = "moderate";} else {weather = "warm";} System.out.print(weather); Which of the following test cases can be used to show that the code does NOT work as intended? 1. temp = 30 11. temp = 51 111. temp = 60 A I only B II only C I and II only D II and III only E I, II, and III
A
Consider the following code segment. String myString = new String("my string"); String yourString = new String(); yourString = "my string"; boolean dotEquals = myString.equals(yourString); boolean equalsEquals = (myString == yourString); System.out.print(dotEquals + " " + equalsEquals); What is printed as a result of executing the code segment? A) true true B) true false C) false true D) false false E) my string my string
B
Consider the following code segment. String str1 = new String("Happy"); String str2 = new String("Happy"); System.out.print(str1.equals(str2) + " "); System.out.print(str2.equals(str1) + " "); System.out.print(str1 == str2); What is printed as a result of executing the code segment? A) true true true B) true true false C) false true false D) false false true E) false false false
B
Consider the following code segment. boolean a = true; boolean b = false; System.out.print((a == !b) != false); What is printed as a result of executing this code segment? A) false B)true C) 0 D) 1 E) Nothing is printed because the expression (a == !b) != false is an invalid parameter to the System.out.print method.
B
In the following expression, j, k, and m are properly declared and initialized int variables. !((j == k) && (k > m)) Which of the following is equivalent to the expression above? A) (j != k) || (k < m) B) (j != k) || (k <= m) C) (j == k) || (k < m) D) (j != k) && (k <= m) E) (j == k) && (k < m)
B
In the following expression, sweet, salty, and sour are properly declared and initialized boolean variables. sweet && (salty || sour) Which of the following expressions is equivalent to the expression above? A (sweet && salty) || sour B (sweet && salty) || (sweet && sour) C (sweet && salty) && (sweet && sour) D (sweet || salty) && sour E (sweet || salty) && (sweet || sour)
B
Consider the following code segment. int quant = 20; int unitPrice = 4; int ship = 8; int total; if (quant > 10) {unitPrice = 3;} if (quant > 20) {ship = 0;} total = quant * unitPrice + ship; What is the value of total after this code segment has been executed? A) 20 B) 60 C) 68 D) 80 E) 88
C
Consider the following code segment. String first = new String("duck"); String second = new String("duck"); String third = new String("goose"); if (first == second) {System.out.print("A");} else if (second == third) {System.out.print("B");} else if (first.equals(second)) {System.out.print("C");} else if (second.equals(third)) {System.out.print("D");} else {System.out.print("E");} What is printed as a result of executing the code segment? A) A B) B C) C D) D E) E
C
Consider the following code segment. boolean a = true; boolean b = true; System.out.print((b || (!a || b)) + " "); System.out.print(((!b || !a) && a) + " "); System.out.println(!(a && b) && b); What output is produced when this code segment is executed? A) true true true B) true false true C) true false false D) false true false E) false false false
C
In the following expression, sunny and windy are properly declared and initialized boolean variables. !sunny && !windy Which of the following is equivalent to the expression above? A) sunny || windy B) !sunny || !windy C) !(sunny || windy) D) !(sunny && windy) E) !(sunny && !windy)
C
The following code segment prints one or more characters based on the values of boolean variables b1 and b2. Assume that b1 and b2 have been properly declared and initialized. if (!b1 || b2) {System.out.print("A");} else {System.out.print("B");} if (!(b1 || b2)) {System.out.print("C");} else {System.out.print("D");} if (b1 && !b1) {System.out.print("E");} If b1 and b2 are both initialized to true, what is printed when the code segment has finished executing? A) ABCD B) ABD C) AD D) BD E) BDE
C
Consider the following code segment, which is intended to set the Boolean variable inRange to true if the integer value num is greater than min value and less than max value. Otherwise inRange is set to false. Assume that inRange, num, min, and max have been properly declared and initialized. boolean isBigger; boolean isSmaller; boolean inRange; if (num < max) {isSmaller = true;} else {isSmaller = false;} if (num > min) {isBigger = true;} else {isBigger = false;} if (isBigger == isSmaller) {inRange = true;} else {inRange = false;} Which of the following values of num, min, and max can be used to show that the code does NOT work as intended? A num = 20, min = 30, max = 50 B num = 30, min = 20, max = 40 C num = 40, min = 10, max = 40 D num = 50, min = 50, max = 50 E num = 60, min = 40, max = 50
D
Consider the following code segment, which uses properly declared and initialized int variables x and y and the String variable result. String result = ""; if (x < 5) {if (y > 0) {result += "a";} else {result += "b";} } else if (x > 10) {if (y < 0) {result += "c";} else if (y < 10) {result += "d";} result += "e"; } result += "f"; What is the value of result after the code segment is executed if x has the value 15 and y has the value 5 ? A) ad B) adf C) d D) def E) ef
D
Consider the following code segment. int a = 1; int b = 0; int c = -1; if ((b + 1) == a) { b++; c += b; } if (c == a) { a--; b = 4; } What are the values of a, b, and c after this code segment has been executed? A a = 0, b = 4, and c = 0 B a = 0, b = 4, and c = 1 C a = 1, b = 0, and c = -1 D a = 1, b = 1, and c = 0 E a = 1, b = 1, and c = 1
D
Consider the following code segment. int m = 8; int n = 3; if (m + n > 10) {System.out.print(m + n);} if (m - n > 0) {System.out.print(m - n);} What, if anything, is printed as a result of executing the code segment? A) Nothing is printed. B) 5 C) 11 D) 115 E) 511
D
int a = 2; int b = 6; int c = 3; Which of the following expressions evaluates to false ? A) a < b == c < b B) a > b == b < c C) a < b != b < c D) a < b != c < b E) a > b != b > c
D
Assume that the int variables a, b, c, and low have been properly declared and initialized. The code segment below is intended to print the sum of the greatest two of the three values but does not work in some cases. if (a > b && b > c) {low = c;} if (a > b && c > b) {low = b;} else {low = a;} System.out.println(a + b + c - low); For which of the following values of a, b, and c does the code segment NOT print the correct value? A a = 1, b = 1, c = 2 B a = 1, b = 2, c = 1 C a = 1, b = 2, c = 3 D a = 2, b = 2, c = 2 E a = 3, b = 2, c = 1
E
Consider the following code segment in which the int variables a and b have been properly declared and initialized. if (a < b) {a++;} else if (b < a) {b++;} else {a++; b++;} Which of the following code segments is equivalent to the code segment above? A if (a <= b + 1) {a++;} else if (b <= a - 1) {b++;} else {a++; b++;} B if (a + 1 <= b) {a++;} else if (b - 1 <= a) {b++;} else {a++; b++;} C if (a - b < 0) {a++;} else if (b - a > 0) {b++;} else {a++; b++;} D if (a != b) {a++;} else if (b != a) {b++;} else {a++; b++;} E if (a == b) {a++; b++;} else if (a < b) {a++;} else {b++;}
E
Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", and "pearl" that occur in String str. For example, if str has the value "the pear in the bowl", the code segments should both print "pear" and if str has the value "the pea and the pearl", the code segments should both print "pearl". Assume that str contains at least one instance of "pea". I. if (str.indexOf("pea") >= 0) {System.out.println("pea");} else if (str.indexOf("pear") >= 0) {System.out.println("pear");} else if (str.indexOf("pearl") >= 0) {System.out.println("pearl");} II. if (str.indexOf("pearl") >= 0) {System.out.println("pearl");} else if (str.indexOf("pear") >= 0) {System.out.println("pear");} else if (str.indexOf("pea") >= 0) {System.out.println("pea");} Which of the following best describes the output produced by code segment I and code segment II? A) Both code segment I and code segment II produce correct output for all values of str. B) Neither code segment I nor code segment II produce correct output for all values of str. C) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pear" but not "pearl". D) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pearl". E) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".
E