Consider the following variable declarations and initializations.
int a = 2;
int b = 6;
int c = 3;
Which of the following expressions evaluates to false ?
D. a < b != c < 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?
B. true
Consider the following expression.
(3 + 4 == 5) != (3 + 4 >= 5)
What value, if any, does the expression evaluate to?
A. true
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?
C. 68
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?
D. a = 1, b = 1, and c = 0
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?
D. 115
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.
Temperature RangeString to Print31 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?
I. temp = 30
II. temp = 51
III. temp = 60
A. I only
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?
D. num = 50, min = 50, max = 50
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?
E. a = 3, b = 2, c = 1
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?
E.
if (a == b)
{
a++;
b++;
}
else if (a < b)
{
a++;
}
else
{
b++;
}
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.
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?
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".
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?
C. AD
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 ?
D. def
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?
C. true false false
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?
B. (j != k) || (k <= m)
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?
C. !(sunny || windy)
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?
B. (sweet && salty) || (sweet && sour)
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?
B. true true false
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?
B. true false
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?
C. C