1/24
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
A computer game should print different messages based on a player’s score.
"Great!" is printed for scores greater than 500.
"Good!" is printed for scores between 250 and 500, inclusive.
"Try again." is printed for scores less than 250.
Assume that score is a properly declared and initialized int that represents a player’s score.
Which of the following code segments will print the correct message for any valid value of score?
Responses
A
String message = "";
if (score < 250)
{
message = "Try again.";
}
else if (score <= 500)
{
message = "Good!";
}
else
{
message = "Great!";
}
System.out.println(message);B
String message = "";
if (score < 250)
{
message = "Try again.";
}
if (score <= 500)
{
message = "Good!";
}
else
{
message = "Great!";
}
System.out.println(message);C
String message = "";
if (score >= 250)
{
message = "Good!";
}
else if (score > 500)
{
message = "Great!";
}
else
{
message = "Try again.";
}
System.out.println(message);D
String message = "";
if (score > 500)
{
message = "Great!";
}
else if (score <= 500)
{
message = "Good!";
}
else
{
message = "Try again.";
}
System.out.println(message);String message = "";
if (score < 250)
{
message = "Try again.";
}
else if (score <= 500)
{
message = "Good!";
}
else
{
message = "Great!";
}
System.out.println(message);The Fruit class contains attributes for a fruit’s type, color, and quantity. The class also contains methods to allow the attributes to be accessed outside the class.
A class design diagram for the Fruit class contains three sections. The first section contains the class name, the second section contains three instance variables and their data types, and the third section contains three methods and their return types. The + symbol indicates a public designation, and the - symbol indicates a private designation.
The diagram has 3 sections containing text as follows:
Section 1:
Fruit
Section 2:
− type : String
− color : String
(missing instance variable)
Section 3:
+ getType() : String
+ getColor() : String
(missing method)
Which of the following are the most appropriate replacements for (missing instance variable) and (missing method)?
Responses
A
Replacing (missing instance variable) with - quantity : String and replacing (missing method) with + getQuantity() : String
B
Replacing (missing instance variable) with - quantity : int and replacing (missing method) with + getQuantity() : int
C
Replacing (missing instance variable) with - quantity : int and replacing (missing method) with + getQuantity() : String
D
Replacing (missing instance variable) with - quantity : String and replacing (missing method) with + getQuantity() : int
Replacing (missing instance variable) with - quantity : int and replacing (missing method) with + getQuantity() : int
Consider the following code segment.
double cost = 25.0;
double discount = 0.0;
if (cost >= 100.0)
{
discount = 10.0;
}
else if (cost >= 50.0)
{
discount = 5.0;
}
else
{
discount = 2.0;
}
System.out.println(cost - discount);What is printed as a result of executing this code segment?
Responses
A
25.0B
23.0C
20.0D
15.023.0Consider the following code segment.
for (int outer = 1; outer <= 4; outer++)
{
for (int inner = outer; inner <= 4; inner++)
{
System.out.print(inner + " ");
}
System.out.println();
}What is printed as a result of executing the code segment?
Responses
A
1 1 1 1
2 2 2
3 3
4 B
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4 C
1 2 3 4
2 3 4
3 4
4 D
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4 1 2 3 4
2 3 4
3 4
4 Which of the following code segments produces the output "987654321"?
Responses
A
int num = 10;
while (num > 0)
{
System.out.print(num);
num--;
}B
int num = 10;
while (num > 1)
{
System.out.print(num);
num--;
}C
int num = 10;
while (num > 1)
{
num--;
System.out.print(num);
}D
int num = 10;
while (num >= 1)
{
num--;
System.out.print(num);
}int num = 10;
while (num > 1)
{
num--;
System.out.print(num);
}Consider the following code segment.
int[] numbers = new int[5];
numbers[0] = 2;
numbers[1] = numbers[0] + 1;
numbers[numbers[0]] = numbers[1];
for (int x = 3; x < numbers.length; x++)
{
numbers[x] = numbers[x - 1] * 2;
}
Which of the following represents the contents of the array numbers after the code segment is executed?
Responses
A
{2, 3, 0, 0, 0}
B
{2, 3, 1, 2, 4}
C
{2, 3, 3, 6, 9}
D
{2, 3, 3, 6, 12}
{2, 3, 3, 6, 12}
A fitness application categorizes daily step counts into different activity levels. The "Active" level is defined as any step count between 10,000 and 15,000, inclusive. The following code segment is intended to determine if a user’s step count falls within the "Active" category.
if (steps > 10000 && steps < 15000)
{
activityLevel = "Active";
}Which of the following best explains the error, if any, in this code segment?
Responses
A
There is no error in the code segment as it correctly identifies “Active” step counts.
B
The condition should be if (steps >= 10000 && steps <= 15000) to properly include 10,000 and 15,000.
C
The condition should be if (steps > 10000 || steps < 15000) to include the correct range of step counts.
D
The condition should be if (steps >= 10000 || steps <= 15000) to include all step counts from 10,000 and above, as well as 15,000 and below.
The condition should be if (steps >= 10000 && steps <= 15000) to properly include 10,000 and 15,000.
The following categories are used by some researchers to categorize regions as urban, suburban, or rural based on population density.
An urban region is a region with more than 3,000 people per square mile.
A suburban region is a region with between 1,000 and 3,000 people, inclusive, per square mile.
A rural region is a region with fewer than 1,000 people per square mile.
Consider the following method, which is intended to categorize a region as urban, suburban, or rural based on the population density of the region.
public static String getCategory(int density)
{
/* missing code */
}Which of the following code segments can replace /* missing code */ so the getCategory method works as intended?
Responses
A
String category;
if (density > 3000)
{
category = "urban";
}
else if (density >= 1000)
{
category = "suburban";
}
else
{
category = "rural";
}
return category;B
String category;
if (density > 3000)
{
category = "urban";
}
else if (density <= 3000)
{
category = "suburban";
}
else
{
category = "rural";
}
return category;C
String category = "rural";
if (density > 3000)
{
category = "urban";
}
if (density >= 1000)
{
category = "suburban";
}
return category;D
String category = "urban";
if (density < 1000)
{
category = "rural";
}
if (density >= 1000)
{
category = "suburban";
}
return category;String category;
if (density > 3000)
{
category = "urban";
}
else if (density >= 1000)
{
category = "suburban";
}
else
{
category = "rural";
}
return category;Consider the following method.
public static String mystery(String input)
{
String output = "";
for (int k = 1; k < input.length(); k = k + 2)
{
output += input.substring(k, k + 1);
}
return output;
}What is returned as a result of the call mystery("computer")?
Responses
A
"computer"B
"cmue"C
"optr"D
"ompute""optr"Consider the following method.
/** Precondition: num > 0 */
public static int doWhat(int num)
{
int var = 0;
for (int loop = 1; loop <= num; loop = loop + 2)
{
var += loop;
}
return var;
}Which of the following best describes the value, if any, returned from a call to doWhat?
Responses
A
The sum of all integers between 1 and num, inclusive
B
The sum of all even integers between 1 and num, inclusive
C
The sum of all odd integers between 1 and num, inclusive
D
No value is returned because of an infinite loop.
The sum of all odd integers between 1 and num, inclusive
Consider the following code segment.
for (int j = 0; j < 4; j++)
{
for (/* missing code */)
{
System.out.print(j + " ");
}
System.out.println();
}
This code segment is intended to produce the following output.
0
1 1
2 2 2
3 3 3 3Which of the following can be used to replace /* missing code */ so that this code segment works as intended?
Responses
A
int k = 0; k < j; k++B
int k = 0; k <= j; k++C
int k = j; k < 4; k++D
int k = j; k <= 4; k++int k = 0; k <= j; k++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?
Responses
A
a || b || cB
!a && !b || cC
!a && !b && cD
!a || !b || c!a || !b || cIn the following code segment, n is a properly declared and initialized int variable whose value is positive.
while (n > 0) // Line 1
{ // Line 2
n /= 10; // Line 3
} // Line 4
System.out.println(n); // Line 5 This code segment is intended to print the leftmost digit of n. For example, if n is 302, this code segment should print 3. However, this code segment is not working as intended. Which of the following changes can be made so that this code segment works as intended?
Responses
A
In line 1, changing the condition n > 0 to n / 10 > 0
B
In line 1, changing the condition n > 0 to n % 10 > 0
C
In line 3, changing the statement n /= 10 to n -= 10
D
In line 3, changing the statement n /= 10 to n %= 10
A. In line 1, changing the condition n > 0 to n / 10 > 0
In the following code segment, a and b are properly declared and initialized boolean variables.
boolean result = (a != b);
System.out.println(result);Which of the following best describes the behavior of the code segment?
Responses
A
It prints true if a is true and b is false, and prints false otherwise.
B
It prints true if a and b have different values, and prints false otherwise.
C
It prints true if a and b have the same value, and prints false otherwise.
D
It prints false for all possible values of a and b.
It prints true if a and b have different values, and prints false otherwise.
In the following code segment, num is a properly declared and initialized int variable.
boolean b1 = true;
if (num > 0)
{
if (num >= 100)
{
b1 = false;
}
}
else
{
if (num >= -100)
{
b1 = false;
}
}Which of the following statements always assigns the same value to b2 as the code segment assigns to b1?
Responses
A
boolean b2 = (num > -100) && (num < 100);B
boolean b2 = (num > -100) || (num < 100);C
boolean b2 = (num < -100) && (num > 0 || num < 100);D
boolean b2 = (num < -100) || (num > 0 && num < 100);boolean b2 = (num < -100) || (num > 0 && num < 100);Consider the following code segment.
int val = 1;
while (val <= 6)
{
for (int k = 0; k <= 2; k++)
{
System.out.println("Surprise!"); // Line 6
}
val++;
} How many times will the statement in line 6 be executed as a result of running the code segment?
Responses
A
3
B
12
C
15
D
18
D. 18
Q17 Compound Boolean expression - describe behavior
00:40
1/1 MC pointCorrectly answered
In the following statement, j and k are properly declared and initialized int variables.
boolean result = ((j > 0) && (k > 0)) || ((j < 0) && (k < 0));
Which of the following best describes the conditions in which result is assigned the value true?
Responses
A
When j and k are both equal to 0
B
When j and k are both positive or both negative
C
When j is greater than k
D
When j is positive and k is negative or when j is negative and k is positive
When j and k are both positive or both negative
Assume that isEven, isPositive, and isPrime are boolean variables that have been properly declared and initialized. Which of the following expressions is equivalent to the expression !(isEven && isPositive) && isPrime?
Responses
A
(!isEven || !isPositive) && isPrimeB
(!isEven || !isPositive) && !isPrimeC
(!isEven && !isPositive) && isPrimeD
(!isEven && !isPositive) && !isPrime(!isEven || !isPositive) && isPrimeConsider the following two code segments, which are intended to produce identical outputs.
Code Segment 1
for (int i = 0; i < 10; i++)
{
System.out.println("counting " + i);
}
Code Segment 2
int x = 0;
while (x < 10)
{
x = x + 1;
System.out.println("counting " + x);
} Which of the following changes can be made to Code Segment 2 so that the outputs of the two code segments are identical as intended?
Responses
A
Move the statement x = x + 1; so that it follows the print statement inside the loop body.
B
Rename the variable x to be i so that it is the same variable name as in Code Segment 1.
C
Remove the statement x = x + 1; because this statement is not in the loop body in Code Segment 1.
D
Change the statement x = x + 1; to be x++; so that the increment of the loop control variable is the same in both code segments.
Move the statement x = x + 1; so that it follows the print statement inside the loop body.
The following incomplete method is intended to return the minimum value among its three parameters.
public static int minOfThree(int a, int b, int c)
{
/* missing code */
}Which of the following can be used to replace /* missing code */ so that the method works as intended?
Responses
A
if (a <= b)
{
if (a <= c)
{
return a;
}
else
{
return c;
}
}
else
{
return b;
}B
if (a <= b)
{
if (a <= c)
{
return a;
}
else if (b <= c)
{
return b;
}
}
else
{
return c;
}C
if (a <= b || a <= c)
{
return a;
}
else if (b <= a || b <= c)
{
return b;
}
else
{
return c;
}D
if (a <= b && a <= c)
{
return a;
}
else if (b <= a && b <= c)
{
return b;
}
else
{
return c;
}if (a <= b && a <= c)
{
return a;
}
else if (b <= a && b <= c)
{
return b;
}
else
{
return c;
}Consider the following code segment.
int j = 0;
int sum = 0;
while (j <= 5)
{
j++;
sum += j;
}Which of the following code segments assigns the same value to sum as the preceding code segment?
Responses
A
int sum = 0;
for (int j = 0; j <= 5; j++)
{
j++;
sum += j;
}B
int sum = 0;
for (int j = 0; j <= 6; j++)
{
j++;
sum += j;
}C
int sum = 0;
for (int j = 1; j <= 5; j++)
{
sum += j;
}D
int sum = 0;
for (int j = 1; j <= 6; j++)
{
sum += j;
}int sum = 0;
for (int j = 1; j <= 6; j++)
{
sum += j;
}Consider the following code segment.
for (int j = 10; j >= 0; j -= 2) // Line 1
{ // Line 2
System.out.print(j – 1); // Line 3
} // Line 4
This code segment is intended to produce only the following output. It does not work as intended.
97531 Which of the following changes can be made so that this code segment works as intended?
Responses
A
In line 1, changing the condition j >= 0 to j > 0
B
In line 1, changing the initialization j = 10 to j = 9
C
In line 3, changing the expression j - 1 to j + 1
D
In line 3, changing the expression j - 1 to j - 2
In line 1, changing the condition j >= 0 to j > 0
Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0.
Code Segment 1
for (int i = 0; i < m * n; i++)
{
System.out.print("*");
}
Code Segment 2
for (int j = 1; j <= m; j++)
{
for (int k = 1; k < n; k++)
{
System.out.print("@");
}
}Assume that the initial values of m and n are the same in Code Segment 1 as they are in Code Segment 2. Which of the following correctly compares the number of times that "*" and "@" are printed when each code segment is executed?
Responses
A
"*" is printed m fewer times than "@".
B
"*" is printed n fewer times than "@".
C
"*" is printed m more times than "@".
D
"*" is printed n more times than "@".
"*" is printed m more times than "@".
Consider the following code segment.
for (int outer = 0; outer < 5; outer++)
{
for (int inner = 4; inner >= 0; inner--)
{
System.out.println("*"); // Line 5
}
}How many times will the statement in line 5 be executed as a result of running this code segment?
Responses
A
20
B
24
C
25
D
30
25
In the following code segment, num is a properly declared and initialized int variable.
if (num > 0)
{
if (num % 2 == 0)
{
System.out.println("X");
}
else
{
System.out.println("Y");
}
}Which of the following best describes the behavior of the code segment?
Responses
A
When num is a negative odd integer, "X" is printed; otherwise, "Y" is printed.
B
When num is a positive even integer, "X" is printed; otherwise, "Y" is printed.
C
When num is a positive even integer, "X" is printed; when num is a positive odd integer, "Y" is printed; otherwise, nothing is printed.
D
When num is a positive odd integer, "X" is printed; when num is a positive even integer, "Y" is printed; otherwise, nothing is printed.
When num is a positive even integer, "X" is printed; when num is a positive odd integer, "Y" is printed; otherwise, nothing is printed.