1/43
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Consider the following code segment.
int x = 7;
int y = 3;
if ((x < 10) && (y < 0))
System.out.println("Value is: " + x * y);
else
System.out.println("Value is: " + x / y);
What is printed as a result of executing the code segment?
Value is: 2
Value is: 0
Value is: 21
Value is: 1
Value is: 2.3333333
Value is: 2
Consider the following code segment.
int num = /* initial value not shown */;
boolean b1 = true;
if (num > 0)
{
if (num >= 100)
{
b1 = false;
}
}
else
{
if (num >= -100)
{
b1 = false;
}
}
Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all values of num?
boolean b2 = (num < -100) || (num > 100);
boolean b2 = (num > -100) && (num < 100);
boolean b2 = (num < -100) || (num > 0 && num < 100);
boolean b2 = (num > -100) || (num < 100);
boolean b2 = (num < -100) && (num > 0 || num < 100);
boolean b2 = (num < -100) || (num > 0 && num < 100);
Assume that a, b, and c are boolean variables that have been properly declared and initialized. Which of the following boolean expressions is equivalent to !(a && b) || c ?
!a || !b || c
!a && !b && c
a || b || c
!a && !b || c
a && b && c
!a || !b || c
Assume that object references one, two, and three have been declared and instantiated to be of the same type. Assume also that one == two evaluates to true and that two.equals(three) evaluates to false.
Consider the following code segment.
if (one.equals(two))
{
System.out.println("one dot equals two");
}
if (one.equals(three))
{
System.out.println("one dot equals three");
}
if (two == three)
{
System.out.println("two equals equals three");
}
What, if anything, is printed as a result of executing the code segment?
one dot equals three
two equals equals three
Nothing is printed.
one dot equals two
one dot equals three
one dot equals two
one dot equals two
one dot equals three
two equals equals three
one dot equals two
Consider the following Boolean expression in which the int variables x and y have been properly declared and initialized.
(x <= 10) == (y > 25)
Which of the following values for x and y will result in the expression evaluating to true ?
x = 10 and y = 10
x = 15 and y = 30
x = 10 and y = 30
x = 8 and y = 25
x = 25 and y = 30
x = 10 and y = 30
A school that does not have air conditioning has published a policy to close school when the outside temperature reaches or exceeds image of 95 degrees fahrenheit with the degree symbol. The following code segment is intended to print a message indicating whether or not the school is open, based on the temperature. Assume that the variable degrees has been properly declared and initialized with the outside temperature.
if (degrees > 95)
System.out.println("School will be closed due to extreme heat");
else
System.out.println("School is open");
Which of the following initializations for degrees, if any, will demonstrate that the code segment may not work as intended?
degrees = 95;
degrees = 90;
The code will work as intended for all values of degrees.
degrees = 96;
degrees = 94;
degrees = 95;
Consider the following method.
public String inRangeMessage(int value)
{
if (value < 0 || value > 100)
return "Not in range";
else
return "In range";
}
Consider the following code segments that could be used to replace the body of inRangeMessage.
I.
if (value < 0)
{
if (value > 100)
return "Not in range";
else
return "In range";
}
else
return "In range";
II.
if (value < 0)
return "Not in range";
else if (value > 100)
return "Not in range";
else
return "In range";
III.
if (value >= 0)
return "In range";
else if (value <= 100)
return "In range";
else
return "Not in range";
Which of the replacements will have the same behavior as the original version of inRangeMessage ?
I and III
III only
I only
II and III
II only
II only
Consider the following statement. Assume that a and b are properly declared and initialized boolean variables.
boolean c = (a && b) || (!a && b);
Under which of the following conditions will c be assigned the value false?
When b has the value false
When a and b have the same value
When a has the value false
Always
Never
When b has the value false
Consider the following method.
public void test(int x)
{
int y;
if (x % 2 == 0)
y = 3;
else if (x > 9)
y = 5;
else
y = 1;
System.out.println("y = " + y);
}
Which of the following test data sets would test each possible output for the method?
8, 9, 12
8, 9, 11
8, 11, 13
7, 9, 10
7, 9, 11
8, 9, 11
Assume that a, b, and c are variables of type int. Consider the following three conditions.
I. (a b) && (a c) && (b == c)
II. (a b) || (a c) || (b == c)
III. ((a - b) (a - c) (b - c)) == 0
Assume that subtraction and multiplication never overflow. Which of the conditions above is (are) always true if at least two of a, b, and c are equal?
II and III
III only
II only
I and II
I only
II and III
Consider the following incomplete method, which is intended to return true if the value of y is between the values of the other two parameters and false otherwise.
/** Precondition: x, y, and z have 3 different values. */
public static boolean compareThree(int x, int y, int z)
{
return /* missing condition */ ;
}
The following table shows the results of several calls to compareThree.
compareThree Table
Call
Result
compareThree(4, 5, 6)
true
compareThree(6, 5, 4)
true
compareThree(5, 4, 6)
false
compareThree(3, 4, 4)
violates precondition
Which of the following can be used to replace / missing condition / so that compareThree will work as intended when called with parameters that satisfy its precondition?
(x > y) && (x > z)
(x > y) != (y > z)
(x > y) || (y > z)
(x > y) && (y > z)
(x > y) == (y > z)
(x > y) == (y > z)
The following method is intended to return true if and only if the parameter val is a multiple of 4 but is not a multiple of 100 unless it is also a multiple of 400 . The method does not always work correctly.
public boolean isLeapYear(int val)
{
if ((val % 4) == 0)
{
return true;
}
else
{
return (val % 400) == 0;
}
}
Which of the following method calls will return an incorrect response?
isLeapYear(2010)
isLeapYear (2000)
isLeapYear(2001)
isLeapYear(1900)
isLeapYear (1984)
isLeapYear(1900)
Consider the following code segment.
int x = /* some integer value */ ;
int y = /* some integer value */ ;
boolean result = (x < y);
result = ( (x >= y) && !result );
Which of the following best describes the conditions under which the value of result will be true after the code segment is executed?
The value will never be true.
The value will always be true.
Only when x and y are equal
Only when x >= y
only when x < y
Only when x >= y
Assume obj1 and obj2 are object references. Which of the following best describes when the expression obj1 == obj2 is true?
When obj1 and obj2 are private class variables defined in the same class
When obj1 and obj2 are defined within the same method
When obj1 and obj2 refer to objects that contain the same data
When obj1 and obj2 are instances of the same class
When obj1 and obj2 refer to the same object
When obj1 and obj2 refer to the same object
Assume that the boolean variables a, b, c, and d have been declared and initialized. Consider the following expression.
!(!(a && b)||(c||!d))
Which of the following is equivalent to the expression?
(!a || !b) && (!c && d)
(a && b) && (!c && d)
(a || b) && (!c && d)
!(a && b) && (c || !d)
(a && b) || (c || !d)
(a && b) && (!c && d)
Consider the following statement, which assigns a value to b1.
boolean b1 = true && (17 % 3 == 1);
Which of the following assigns the same value to b2 as the value stored in b1?
boolean b2 = true || (17 % 3 == 1);
boolean b2 = false && (17 % 3 == 2);
boolean b2 = (true && false) || true;
boolean b2 = false || (17 % 3 == 2);
boolean b2 = (true || false) && true;
boolean b2 = false && (17 % 3 == 2);
Consider the following code segment.
int x = 7;
int y = 4;
boolean a = false;
boolean b = false;
if (x > y)
{
if (x % y >= 3)
{
a = true;
x -= y;
}
else
{
x += y;
}
}
if (x < y)
{
if (y % x >= 3)
{
b = true;
x -= y;
}
else
{
x += y;
}
}
What are the values of a, b, and x after the code segment has been executed?
a = true, b = false, x = 3
a = false, b = false, x = 11
a = true, b = false, x = 7
a = false, b = true, x = 3
a = true, b = true, x = -1
a = true, b = false, x = 7
Consider the following method, which returns an int based on its parameter x.
public static int puzzle(int x)
{
if (x > 20)
{
x -= 2;
}
else if (x % 2 == 0) // Line 7
{
x += 4;
}
return x;
}
Consider a modification to the method that eliminates the else from line 7 so that line 7 becomes
if (x % 2 == 0) // Modified line 7
For which of the following values of x would the return values of the original method and the modified method differ?
5
14
25
22
0
22
Consider the following method.
public static void whatIsIt(int a, int b)
{
if ((a < b) && (a > 0))
{
System.out.println("W");
}
else if (a == b)
{
if (b > 0)
{
System.out.println("X");
}
else if (b < 0)
{
System.out.println("Y");
}
{
System.out.println("Z");
}
}
}
Which of the following method calls will cause "W" to be printed?
I. whatIsIt(10, 10)
II. whatIsIt(-5, 5)
III. whatIsIt(7, 10)
III only
I only
II only
II and III
I and III
III only
Which of the following best describes the value of the Boolean expression shown below?
a && !(b || a)
The value is true when either a or b has the value true, and is false otherwise.
The value is always false.
The value is true when a has the value false, and is false otherwise.
The value is true when b has the value false, and is false otherwise.
The value is always true.
The value is always false.
Consider the following code segment.
int count = 0;
int number = 10;
while (number > 0) {
number = number / 2;
count++;
}
What will be the value of count after executing the code segment?
6
4
3
2
4
What is the output of the following code segment?
for (int k = 5; k > -5; k--) {
System.out.print( k + " " );
}
System.out.println( );
-5 -4 -3 -2 -1 0
5 4 3 2 1 0
5 4 3 2 1 0 -1 -2 -3 -4
0 1 2 3 4 5
5 4 3 2 1 0 -1 -2 -3 -4
Consider the following code segment.
int count = 0;
int number = 10;
while (number > 0)
{
number = number / 2;
count++;
}
What will be the value of count after executing the code segment?
3
4
6
2
4
Consider the following code segment, which is intended to print the sum of all the odd integers from 0 up to and including 101.
int r = 0;
int sum = 0;
/* missing loop header */
{
if (r % 2 == 1)
{
sum += r;
}
r++;
}
System.out.println(sum);
Which of the following could replace /* missing loop header */ to ensure that the code segment will work as intended?
while( sum <= 101 )
while( r <= 100 )
while( r < 100 )
while( r <= 101 )
while( r <= 101 )
Consider the following code segment.
int a = 5;
String result = "";
while (a < 10)
{
result += a;
a += 2;
}
System.out.println(result);
What, if anything, is printed as a result of executing the code segment?
579
79
57
Nothing is printed - compiler error
579
Consider the following code segment.
int count = 1;
int value = 31;
while(value >= 10)
{
value = value - count;
count = count + 3;
}
System.out.println(value);
What is printed as a result of executing the code segment?
10
13
9
31
4
9
Consider the following code segment.
int j = 2;
while (j <= 10)
{
j += 2;
System.out.print(j);
}
What would be the output?
4681012
Consider the following code segment.
int count = 5;
while (count < 100)
{
count = count * 2;
}
count = count + 1;
What will be the value of count as a result of executing the code segment?
321
161
160
100
161
Consider the following code segment.
int num = 1;
int count = 0;
while (num <= 10)
{
if (num % 2 0 && num % 3 0)
{
count++;
}
num++;
}
What value is stored in the variable count as a result of executing the code segment?
1
7
3
5
1
Consider the following code segment.
int k = 0;
/* missing loop header */
{
k++;
System.out.print(k + " ");
}
Which of the following can be used as a replacement for /* missing loop header */ so that the code segment prints out the string "1 2 3 4 "?
while( k < 4 )
while( k < 5 )
while( k <= 5 )
while( k <= 4 )
while( k < 4 )
Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable sum.
int a = 100;
int sum = 0;
while( /* missing code */ )
{
sum = sum + a;
a = a - 10;
}
Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended?
a >= 10
a <= 100
a != 10
a < 100
a >= 10
The following method is intended to print the number of digits in the parameter num.
public int numDigits(int num)
{
int count = 0;
while (/* missing condition */)
{
count++;
num = num / 10;
}
return count;
}
Which of the following can be used to replace /* missing condition */ so that the method will work as intended?
num >= 0
num != 0
count != 0
num == 0
num != 0
Consider the following code segment.
int count = 0;
for (int k = 0; k < 100; k++) {
count++;
}
System.out.println(count);
What would be the output?
100
Consider the following code segment.
int num = 0;
for (int i = 0; i < 100; i++) {
num+=2;
}
System.out.println(num);
What would be the output?
200
What is the output of the following code segment?
for (int k = 0; k < 5; k++) {
System.out.print( k + " " );
}
System.out.println( );
0 1 2 3 4
0 1 2 3 4 5
1 2 3 4 5
k k k k k
0 1 2 3 4
Consider the following code segment.
int count = 0;
for (int k = 0; k < 10; k++)
{
count++;
}
System.out.println(count);
Which of the following code segments will produce the same output as the code segment above?
int count = 0;
for (int k = 9; k >= 0; k--)
{
count++;
}
System.out.println(count);
int count = 1;
for (int k = 1; k <= 10; k++)
{
count++;
}
System.out.println(count);
int count = 0;
for (int k = 1; k < 10; k++)
{
count++;
}
System.out.println(count);
int count = 0;
for (int k = 10; k >= 0; k--)
{
count++;
}
System.out.println(count);
int count = 0;
for (int k = 9; k >= 0; k--)
{
count++;
}
System.out.println(count);
What is the output of the following code segment?
for (int k = 5; k > -5; k--)
{
System.out.print( k + " " );
}
System.out.println( );
-5 -4 -3 -2 -1 0
5 4 3 2 1 0 -1 -2 -3 -4
5 4 3 2 1 0
0 1 2 3 4 5
5 4 3 2 1 0 -1 -2 -3 -4
Consider the following two code segments. Code segment II is a revision of code segment I in which the loop header has been changed.
I.
for (int k = 1; k <= 5; k++)
{
System.out.print(k);
}
II.
for (int k = 5; k >= 1; k--)
{
System.out.print(k);
}
Which of the following best explains how the output changes from code segment I to code segment II?
Both code segments produce the same output, because they both iterate four times.
Both code segments produce the same output, because they both iterate five times.
The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.
Code segment II prints more values than code segment I, because it iterates for one additional value of k.
The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.
Consider the following code segment.
int num = 0;
for (int i = 0; i < 100; i++)
{
num+=7;
}
System.out.println(num);
What would be the output?
700
What must the initialization be so that the following code segment prints out the integers -3 -2 -1 ?
for (_________; k < 0; k++)
{
System.out.print( k + " " );
}
System.out.println( );
int k < 0
int k = -4
int k = 0
int k = -3
int k = -3
Consider the following code segment.
for (int j = 1; j < 10; j += 2)
{
System.out.print(j);
}
Which of the following code segments will produce the same output as the code segment above?
int j = 1;
while (j < 10)
{
j += 2;
System.out.print(j);
}
int j = 1;
while (j < 10)
{
System.out.print(j);
j += 2;
}
int j = 1;
while (j >= 10)
{
System.out.print(j);
j += 2;
}
int j = 1;
while (j <= 10)
{
j += 2;
System.out.print(j);
}
int j = 1;
while (j < 10)
{
System.out.print(j);
j += 2;
}
What is the output of the following code segment?
for (int k = 0; k < 5; k++)
{
System.out.print( k + " " );
}
System.out.println( );
k k k k k
0 1 2 3 4
0 1 2 3 4 5
1 2 3 4 5
0 1 2 3 4
onsider the following code segment.
for (int k = 1; k <= 7; k += 2)
{
System.out.print(k);
}
Which of the following code segments will produce the same output as the code segment above?
for (int k = 1; k <= 8; k += 2)
{
System.out.print(k);
}
for (int k = 0; k <= 8; k += 2)
{
System.out.print(k + 1);
}
for (int k = 1; k < 7; k += 2)
{
System.out.print(k + 1);
}
for (int k = 0; k < 7; k += 2)
{
System.out.print(k);
}
for (int k = 1; k <= 8; k += 2)
{
System.out.print(k);
}