1. Which of these expressions evaluates to 5.5?(1 point)
I. (double)(11 / 2) II. 11 / (double)2 III. 11 / 2.0
Question 1 options:
2. How many columns are in the array? (1 point)
char[][] array1 = new char[15][10];
Question 2 options:
3. If String str = "The There Here", then what is the value of str.indexOf("he");?(1 point)
Question 3 options:
4. What is the value of vals[4][1]?(1 point)
double[][] vals = {1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}
Question 4 options:
| |
| |
| |
| 4) | There is no such value. | |
5. Consider the method total below.(1 point)
public static int total (int result, int a, int b) { if (a == 0) { if (b == 0) { return result * 2; } return result / 2; } else { return result * 3; } }The assignment statementx = total (5, 0, 1);must result in
Question 5 options:
| 1) | x being assigned the value 15 | |
| 2) | x being assigned the value 10 | |
| 3) | x being assigned the value 5 | |
| 4) | x being assigned the value 2 | |
| 5) | e. x being assigned the value 0 | |
6. Which of the following statements creates alpha, a two-dimensional array of 10 rows and 5 columns, wherein each component is of the type int?(1 point)
int[][] alpha = new int[10][5];
int[][] alpha; alpha = new int[10][]; for (int i = 0; i < 10; i++) alpha[i] = new int[5];
Question 6 options:
7. Consider the method total below.(1 point)
public static int total (int result, int a, int b) { if (a == 0) { if (b == 0) { return result * 2; } return result / 2; } else { return result * 3; } } The assignment statement x = total (5, 0, 0); must result in
Question 7 options:
| 1) | x being assigned the value 4 | |
| 2) | x being assigned the value 8 | |
| 3) | x being assigned the value 5 | |
| 4) | x being assigned the value 2 | |
| 5) | x being assigned the value 10 | |
8. Consider the following definitions.
public boolean state (int[] list, int value) { int counter; boolean flag = false; for (counter = 0; counter < list.length; counter++) { flag = (list[counter] != value); } return flag; }
Under which of the following conditions must the method above return true ? (1 point)
Question 8 options:
| |
| 2) | Under the condition that value == list[list.length - 1]. | |
| 3) | Under the condition that value != list[list.length - 1]. | |
| 4) | Under the condition that value != list[i] for all i such that 0 <= i < list.length. | |
| |
9. What is the output of the program shown below? (1 point)
public class SomeClass { private int x, y; public SomeClass (int xValue, int yValue) { x = xValue; y = yValue; } public void m1() { x = 30; System.out.print((y + 1) + " "); } public void m2() { m1(); System.out.print(x + " "); } } public class Tester { public static void main (String[] args) { int x = 10; int y = 20; SomeClass z = new SomeClass(y, x); z.m2(); z.m1(); } }
Question 9 options:
10. What is output by the following code? (1 point)
public class Swapper { private int a, b; public Swapper(int aValue, int bValue) { a = aValue; b = bValue; } public void swap () { a = b; b = a; } public void print () { System.out.println("a = " + a + ", and b = " + b); } } public class Tester { public static void main(String[] args) { Swapper swapObj = new Swapper(10, 20); swapObj.swap(); swapObj.print(); } }
Question 10 options:
11. If the value of f(x, y, z) is always an integer, which of the following conditions ensures that the loop below terminates? (1 point)
while (f(x, y, z) < 100) { }
Question 11 options:
| 1) | x, y, and z are each increased during each iteration. | |
| 2) | The value of f(x, y, z) is increased during each iteration. | |
| 3) | x, y, and z are each decreased during each iteration | |
| 4) | The sum of x, y, and z is decreased during each iteration. | |
| 5) | The value of Math.pow (f(x, y, z), 2) is decreased during each iteration. | |
12. The following program segment is intended to sum a[0] through a[n-1], where n = a.length:
sum = 0; i = 0; n = a.length; while (i != n) { i++; sum += a[i]; }
In order for this segment to perform as intended, which of the following modifications, if any, should be made?(1 point)
Question 12 options:
| 1) | no modification is necessary | |
| 2) | sum = 0; i = 0; should be changed to sum = a[1]; i = 1; | |
| 3) | while (i != n) should be changed to while (i <= n) | |
| 4) | sum += a[i]; should be changed to sum += a[i+1]; | |
| 5) | i++; should be interchanged with sum += a[i]; | |
13.
What is the output of the following program?(1 point)
int sum = 0, k, val = 1; for (k = 0; k <=4; k++) { sum += val; val++; } System.out.println(sum);
Question 13 options:
14. The purpose of a class constructor is to (1 point)
Question 14 options:
| 1) | initialize the class instance variables. | |
| 2) | describe the conditions under which the compiler is to abort compilation. | |
| 3) | describe the conditions under which the method may be called so that it satisfies its postcondition. | |
| 4) | describe the algorithm used by the constructor. | |
| 5) | describe the conditions that are true when the method completes. | |
15. When is it not necessary to invoke method myMethod using dot notation? (1 point)
Question 15 options:
| 1) | When myMethod is private. | |
| 2) | When myMethod is invoked by a method in the same class as myMethod. | |
| 3) | When myMethod is public. | |
| 4) | When myMethod is both private and static. | |
| 5) | When myMethod is invoked by a method in a different class than myMethod. | |
16.
What is a mutator method? (1 point)
Question 16 options:
| 1) | A method that returns an instance variable or a calculation. | |
| 2) | A method that modifies an instance variable. | |
| 3) | A constructor that initializes instance variables. | |
| 4) | Any method that has a return value other than void. | |
| 5) | Any method that has a return value of void. | |
17.
What is the output of the following code?(1 point)
ArrayList grades = new ArrayList(); grades.add(88); grades.add(92); grades.add(95); grades.add(1, 80); grades.add(83); System.out.println(grades.get(2 + 3 / 2));
Question 17 options:
| 1) | An IndexOutOfBoundsException occurs. | |
| |
| |
| |
| |
18.
Consider the following class designed to store weather statistics at a particular date and time.
public class WeatherSnapshot { private int tempInFahrenheit; private int humidity; // value of 56 means 56% humidity private int dewpoint; // in degrees Fahrenheit private Date date; // uses a Date object to store the date private int time; // in military time, such as 1430 = 2:30 pm private boolean cloudy; // true if 25% or more of the sky is covered // constructor not shown, but it initializes all instance variables // postcondition: returns temperature public int getTemp() { return tempInFahrenheit; } // postcondition: returns date public Date getDate() { return date; } // postcondition: returns true if precipitation is likely; false otherwise public boolean precipitationLikely() { // implementation not shown } // other methods not shown }
Which of the following is a class constructor? (1 point) I. getTemp II. precipitationLikely III. WeatherSnapshot
Question 18 options:
19.
Consider the following class designed to store weather statistics at a particular date and time.
public class WeatherSnapshot { private int tempInFahrenheit; private int humidity; // value of 56 means 56% humidity private int dewpoint; // in degrees Fahrenheit private Date date; // uses a Date object to store the date private int time; // in military time, such as 1430 = 2:30 pm private boolean cloudy; // true if 25% or more of the sky is covered // constructor not shown, but it initializes all instance variables // postcondition: returns temperature public int getTemp() { return tempInFahrenheit; } // postcondition: returns date public Date getDate() { return date; } // postcondition: returns true if precipitation is likely; false otherwise public boolean precipitationLikely() { // implementation not shown } // other methods not shown }
Suppose a WeatherSnapshot object named currentWeather has been correctly instantiated in a client class. Which of the following will correctly call the precipitationLikely method? (1 point)
Question 19 options:
| 1) | boolean couldRain = precipitationLikely(); | |
| 2) | boolean couldRain = currentWeather.precipitationLikely(); | |
| 3) | boolean couldRain = currentWeather.precipitationLikely() | |
| 4) | double percentChanceOfRain = precipitationLikely(); | |
| 5) | double percentChanceOfRain = currentWeather.precipitationLikely(); | |
20.
What is meant by the term "encapsulation"? (1 point)
Question 20 options:
| 1) | Encapsulation occurs when client code is allowed to have direct access to instance variables. | |
| 2) | Encapsulation is the operator that gives the remainder after division. | |
| 3) | Encapsulation is the term used when a class constant is created. | |
| 4) | Encapsulation occurs when instance variables are declared as private. | |
| 5) | Encapsulation occurs when a class has mutator methods. | |
21.
What is output by the following code fragment?(1 point)
String[] fruits = { "apple", "banana", "peach", "strawberry" }; String str = "a"; for (String item : fruits) { str += item.substring(1, 2); } System.out.println(str);
Question 21 options:
22.
Consider the following code segment. What would be the expected output?(1 point)
int i = 10; while (i >= 0) { if ((i % 2) > 0) { System.out.print(i + " "); } i -= 2; }
Question 22 options:
23.
Assume that the following code exists inside a method of MyClass, and that this code compiles without errors: int result = book.getYearPublished(); where book is an object of the Book class.
Which of the following is not true about the getYearPublished method?(1 point)
I. It is a mutator method. II. It is a public method in the Book class. III. It returns a String.
Question 23 options:
24.
Complete the Car class shown below. A Car has a fuel tank level, and a miles per gallon value. Assume that all cars have 12 gallon gas tanks. (20 points)
public class Car { // declare your constants and instance variables here // postcondition: all instance variables are initialized public Car(double mpgValue, double gallons) { } // postcondition: the fuel level is increased by the amount given by the parameter public void addFuel(double gallons) { } // postcondition: the fuel tank level is decreased by the amount of gas // that it would take this Car to drive the number of miles given by the parameter // The fuel level cannot be negative. public void drive(double miles) { } // other methods not shown }