1/22
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
A code segment will use variables to represent the age of an animal and whether it is considered a house pet. Which of the following variable declarations is most appropriate for the code segment?
int age;
boolean pet;B
what’s missing
int x = 5;
int y = 6;
/* missing code */
z = (x + y) / 2;
int z = 0;C
A program needs to maintain the following information about data captured by a sensor.
Whether the sensor is currently active
The number of items detected by the sensor
The average time per detection, in seconds
Which set of variable declarations is most appropriate for this program?
D
boolean active; int items; double avgTime;
In the following code segment, x and y are properly declared and initialized double variables.
y = x;
double z = y + x;
System.out.println(z);Two times the initial value of x
In the following code segment, x and y are properly declared and initialized int variables.
int z = x;
x = y;
y = z;Which of the following best describes the behavior of the code segment?
D
It swaps the values of x and y.
The following code segment is intended to interchange the values of the int variables x and y. Assume that x and y have been properly declared and initialized.
int temp = x;
/* missing code */Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
A
x = y;
y = temp; ng code segment.
System.out.print(I do not fear computers. ); // Line 1
System.out.println(I fear the lack of them.); // Line 2
System.out.println(--Isaac Asimov); // Line 3
The code segment is intended to produce the following output but may not work as intended.
I do not fear computers. I fear the lack of them.
--Isaac Asimov Which change, if any, can be made so that the code segment produces the intended output?
C
In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
Consider the following code segment.
int a = 5;
int b = 8;
int c = 3;
System.out.println(a + b / c * 2); What is printed as a result of executing the code segment?
D
9Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized.
/* missing code */
System.out.print(onesDigit);
System.out.print(tensDigit);Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
A
int onesDigit = num % 10;
int tensDigit = num / 10;In the following code segment, x and y are properly declared and initialized int variables whose values are positive. The code segment is intended to print the average of x and y. For example, if x is 10 and y is 5, the code segment is intended to print 7.5.
int sum = x + y;
double average = (double) (sum / 2);
System.out.println(average); Which of the following best describes the error, if any, in the code segment?
C
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.
Consider the following code segment.
double num = 9 / 4;
System.out.print(num);
System.out.print(" ");
System.out.print((int) num);What is printed as a result of executing the code segment?
A
2.0 2The following code segment is intended to round val to the nearest integer and print the result.
double val = -0.7;
int roundedVal = (int) (val + 0.5);
System.out.println(roundedVal); Which of the following best describes the behavior of the code segment?
D
The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.
Consider the following code segment.
int a = 5;
int b = 4;
int c = 2;
a *= 3;
b += a;
b /= c;
System.out.print(b); What is printed as a result of executing the code segment?
B
9Consider the following code segment.
int x = 5;
x += 6 * 2;
x -= 3 / 2; What is the value of x after the code segment is executed?
D
16Consider the following code segment.
int a = 4;
int b = 5;
a++;
b++;
int c = a + b;
a -= 1;
System.out.println(a + c); What is printed as a result of executing the code segment?
D
15A PetOwner object is an attribute of a Cat object
B)
The color and size variables are attributes of the poloShirt object.
A)
Attribute:
• title
Behavior:
• play()
A)
Print your name
System.out.println("John Doe");
Print the numbers 1–10
for (int i = 1; i <= 10; i++) {
System.out.println(i)
Find the larger of 15 and 22.
int a = 15; int b = 22;
if (a > b) {
System.out.println(a);
} else {
System.out.println(b); }
Compute the sum of 8 and 17
int a = 8; int b = 17;
2
int sum = a + b;
System.out.println(sum);
Find the area of a rectangle (length × width)
int length = 8;
int width = 5;
int area = length * width;
System.out.println(area);