The following code segment is intended to print the average of two quiz scores, which is 7.5.int quiz1 = 7;
int quiz2 = 8;
double average = (quiz1 + quiz2) / 2;
System.out.println(average);The segment prints 7.0. Which of the following best describes the problem?
Question 2
A music app keeps three facts about each song: how many times it has been played, its average star rating (such as 4.6), and whether the user has liked it. Which set of declarations is most appropriate?
Question 3
Consider the following code segment.System.out.print("Level ");
System.out.println(3);
System.out.print("Lives:");
System.out.print(2);
System.out.println();
System.out.println("Go");What is printed as a result of executing the code segment?
Question 4
Consider the following code segment.System.out.println("Path: C:\\games\nSave \"slot 1\"");What is printed as a result of executing the code segment?
Question 5
Consider the following code segment.int a = 3;
int b = 4;
System.out.println("Sum: " + a + b);
System.out.println(a + b + " total");
System.out.println("Product: " + a * b);What is printed as a result of executing the code segment?
Question 6
Consider the following code segment.int boxes = 12;
int perShelf = 0;
System.out.println("Shelves:");
System.out.println(boxes / perShelf);What happens when the code segment is executed?
Question 7
Consider the following code segment.int left = 5;
int right = 9;
left = right;
right = left;
System.out.println(left + " " + right);What is printed as a result of executing the code segment?
Question 8
Consider the following code segment.int score;
int bonus = 10;
score = score + bonus;
System.out.println(score);Which of the following best describes the result of compiling and running the code segment?
Question 9
The following code segment is intended to print the average of a, b and c, including any fractional part.int a = 4;
int b = 7;
int c = 8;
double avg = /* missing expression */;
System.out.println(avg);Which of the following can replace /* missing expression */ so that the code segment works as intended?
Question 10
Consider the following code segment.double price = 7.89;
int whole = (int) price;
double back = whole + 0.5;
int total = (int) (back * 2);
System.out.println(whole + " " + back + " " + total);What is printed as a result of executing the code segment?
Question 11
The double variable miles holds a nonnegative value. Which of the following expressions evaluates to miles rounded to the nearest whole number, as an int? A value exactly halfway, such as 2.5, rounds up.
Question 12
Consider the following code segment.int big = Integer.MAX_VALUE;
big = big + 1;Which of the following best describes the value of big after the code segment executes?
Question 13
Consider the following code segment.int made = 7;
int tries = 2;Which of the following statements, placed after the code segment, store 3.5 in r?
I. double r = (double) made / tries;
II. double r = (double) (made / tries);
III. double r = made / (double) tries;
Question 14
Consider the following code segment.int x = 5;
x += 4;
x *= 3;
x %= 7;
x -= 1;
x /= 2;What is the value of x after the code segment executes?
Question 15
Consider the following code segment.int lives = 3;
lives++;
lives++;
lives--;
lives += 2;
System.out.println("Lives: " + lives);What is printed as a result of executing the code segment?
Question 16
A cafe's checkout program adds up prices stored as double values in dollars, such as 0.1 and 0.2. Some totals come out as values like 0.30000000000000004. Which change best avoids this problem?
Question 17
Consider the following code segment.int tokens = 17;
tokens /= 2;
tokens *= 2;
tokens += tokens % 3;What is the value of tokens after the code segment executes?
Question 18
The documentation for a library class Thermostat lists a variable targetTemp, a variable isHeating, a method raise, which takes an int number of degrees, and a method getTargetTemp. Which of the following correctly classifies these?
Question 19
Consider the following method./**
* Returns the average speed, in km per hour.
* Precondition: hours > 0
*/
public double averageSpeed(double km, int hours)
{
return km / hours;
}Which of the following best describes the precondition?
Question 20
Consider the following code segment.int steps = 100;
// steps = steps * 2;
steps = steps + 50; // add a short walk
/* steps = steps - 30;
steps = steps + 5; */
System.out.println(steps);What is printed as a result of executing the code segment?
Question 21
Consider the following methods, which appear in the same class.public static String size(int side)
{
return "square " + side * side;
}
public static String size(int w, int h)
{
return "rect " + w * h;
}
public static String size(double r)
{
return "circle " + (int) (3 * r * r);
}What is printed as a result of executing the following code segment in a method of the same class?System.out.println(size(3));
System.out.println(size(2, 5));
System.out.println(size(2.0));
Question 22
Consider the following method.public static double tip(double bill, int percent)
{
return bill * percent / 100;
}Which of the following statements causes a compile error?
Question 23
Consider the following method.public static int addBonus(int points)
{
points = points + 25;
return points;
}What is printed as a result of executing the following code segment in the same class?int score = 40;
int result = addBonus(score);
System.out.println(score + " " + result);
Question 24
Consider the following class.public class Temp
{
public static double toF(double c)
{
return c * 9 / 5 + 32;
}
public static int nextWhole(double x)
{
return (int) x + 1;
}
}What is printed as a result of executing the following code segment?double f = Temp.toF(20);
System.out.println(f);
System.out.println(Temp.nextWhole(f));
Question 25
Which of the following expressions produces a random integer from 5 to 10, inclusive, with each value equally likely?
Question 26
Consider the following code segment.double a = Math.sqrt(49) + Math.pow(2, 3);
int b = (int) Math.pow(10, 2) / 8;
System.out.println(a + " " + b);What is printed as a result of executing the code segment?
Question 27
Consider the following method.public static int gap(int a, int b)
{
return Math.abs(a - b);
}Which of the following best describes the value returned by gap?
Question 28
A game has a class named Ghost. The program creates two variables, shade and wisp, each referring to a Ghost with its own color and position. Which of the following statements is true?
Question 29
Consider the following class.public class Ticket
{
private String event;
private int row;
public Ticket(String e)
{
event = e;
row = 1;
}
public Ticket(String e, int r)
{
event = e;
row = r;
}
}Which of the following statements, in a method of another class, compiles without error?
Question 30
Consider the following code segment.String nickname = null;
String name = "Rowan";
System.out.println(name.length());
System.out.println(nickname.length());What happens when the code segment is executed?
Question 31
Consider the following class.public class PlantPot
{
private int water;
public PlantPot(int start)
{
water = start;
}
public void pour(int ml)
{
water += ml;
}
public void evaporate()
{
water = water / 2;
}
public int getWater()
{
return water;
}
}What is printed as a result of executing the following code segment?PlantPot fern = new PlantPot(30);
PlantPot cactus = new PlantPot(10);
fern.pour(15);
cactus.evaporate();
fern.evaporate();
System.out.print(fern.getWater() + " ");
System.out.println(cactus.getWater());
Question 32
Consider the following code segment.String tag = "player_one_42";
int u = tag.indexOf("_");
String first = tag.substring(0, u);
String rest = tag.substring(u + 1);
System.out.println(first + " " + rest.indexOf("_"));What is printed as a result of executing the code segment?
Question 33
Consider the following code segment.String a = "lime";
String b = new String("lime");
String c = b;
System.out.println(a.equals(b));
System.out.println(a == b);
System.out.println(c == b);What is printed as a result of executing the code segment?
Question 34
Consider the following code segment.String word = "planet";
word.substring(2);
String end = word.substring(4);
word += end;
System.out.println(word);What is printed as a result of executing the code segment?