Java II Pre-Test

Java II Pre‐Test Directions: 1. Evaluate and answer the 10‐pretest questions in this package 2. You may work together or individually. I would prefer you work together as a group to discuss your answers and see if you can come to some agreement on your answers. 3. Feel free to use the BPJ textbook, Internet, or other Java resources you may have. If you use your compiler to help come to a solution, you should evaluate the problem first then see if you answer matches the result. If it doesn’t, walk through the problem again to see where you went wrong. 4. Each student must submit their own set of solutions (even if you work in a group) 5. You will need to research how a Java Enhanced For Loop (a.k.a. “For Each” loop) works and how it differs from a normal For Loop 6. Due date: NLT Monday, 8/26 (the day before we meet again as a class) 7. To submit your answer: a. E‐Mail me at [email protected] b. Your subject line should look like this: 2A Java II – John Doe – Java Pre‐Test where “John Doe” is your first and last name c. The body of the e‐mail should contain your answers 8. We will go over these questions when we meet again as a class 1. The Richter scale is used to measure the strength of an earthquake. A value on the scale is a real number between 0 and 10; and each step in the scale—for example from 5.0 to 6.0—represents a tenfold increase in the strength of the earthquake. Consider the following class, written by a student, whose intent is to describe the effects of an earthquake. public class Earthquake { private double richter; //Constructor. Sets richter to the specified value on the Richter scale. public Earthquake(double value) { richter = value; } //Returns the effect of the earthquake in string form. public String getDescription() { String str; if (richter >= 8.0) str = "Most buildings collapse."; if (richter >= 6.5) str = "Many buildings damaged. Some fall."; if (richter >= 4.5) str = "Poorly‐constructed buildings collapse."; if (richter >= 3.0) str = "Earth shakes. No destruction."; if (richter >= 0) str = "Barely felt by people."; return str; } } Consider the following segment of a client program. double magnitude = IO.readInput(); //read user input of a Richter value Earthquake quake = new Earthquake(magnitude); System.out.println(quake.getDescription()); If the user inputs a Richter magnitude of 4.0, what will be output? a. Earth shakes. No destruction. b. Poorly constructed buildings collapse. c. Barely felt by people. d. Earth shakes. No destruction. Barely felt by people. e. An error message will be output 2. Suppose x, y, and z are variables of type int. Consider the following three conditions: (x == y) && (y == z) && (x == z) (x == y) || (y == z) && (x == z) (x ‐ y) * (x ‐ z) * (y ‐ z) == 0 Which of these conditions is (are) always true if x == y is true? a. I only b. II only c. III only d. I and II only e. II and III only 3. Consider the inheritance heirarchy of classes below." Which of the following declarations will not cause an error? You may assume that each of the classes above has a default constructor. LibraryItem item = new LibraryItem(); Book b = new LibraryItem(); LibraryItem cd = new CD(); a. I only b. II only c. III only d. I and III only e. II and III only 4. Consider the inheritance heirarchy of classes below." Suppose that, of the three classes shown in the diagram, only the LibraryItem class has a method called getPrice defined in it, whose specification is as follows: //Returns the price of this LibraryItem. public double getPrice() Now consider the following declaration that appears in a client program. List <LibraryItem> libraryList = new ArrayList<LibraryItem>(); Assume that libraryList is initialized with LibraryItem objects. Consider the following code segment that computes the total cost of all items stored in libraryList. double total = 0.0; for (LibraryItem item : libraryList) total += item.getPrice(); The use of getPrice in the above code segment is an example of a. An overloaded method b. An overridden method c. Polymorphism d. An inherited method e. An abstract method 5. A charity organization has a large list of donor records, sorted alphabetically by name. Each record contains the amount of money donated in the current year. Which of the following represents an algorithm that will locate the top seven donors for the current year? a.
b.
c.
d.
e.
Seven passes through the outer loop of an insertion sort Seven passes through the outer loop of a selection sort Seven recursive calls to mergesort Seven passes through the loop of a binary search Accessing the last seven entries in the list 6. Consider a method called outside that returns true if value lies outside the range from low to high, and false if value lies between low and high, inclusive. //Precondition: low <= high. //Postcondition: Returns false if value lies between low and high, inclusive. // Otherwise returns true. public boolean outside(int value, int low, int high) { /* code */ } Which of the following can replace /* code */ so that outside will work as intended? a. return high > = value > = low b. return low < value < high c. return value > low && value < high d. return value < low && value > high e. return value < low || value > high 7. Consider the following class definitions. public class Dancer { //...data fields and several methods not shown public void leap() { /* implementation not shown */ } } public class BalletDancer extends Dancer { public void glide() { /* implementation not shown */ } //...other methods not shown } A client program has the following declarations: Dancer d = new BalletDancer(); BalletDancer b = new BalletDancer(); A client program has the following declarations: Dancer d = new BalletDancer(); BalletDancer b = new BalletDancer(); Which of the following statements is (are) valid? I.
b.leap(); II.
d.glide(); III.
d.leap(); a.
b.
c.
d.
e.
I only II only III only II and III only I and III only 8.
Refer to the following data field and method. private int[] arr; //Precondition: arr contains at least one element. public void doSomething() { int sum1 = 0; int sum2 = 0; } for (int num : arr) { if (num > 0 || Math.abs(num) % 2 != 0) sum1 += num; else if (num < 0 && Math.abs(num) % 2 == 0) sum2 += num; } System.out.println(sum1); System.out.println(sum2); Which of the following best describes the value of sum1 output by method doSomething? a.
b.
c.
d.
e.
The sum of all positive values in arr The sum of all positive odd values in arr The sum of all odd values in arr The sum of all positive values and odd negative values in arr The sum of all positive values and even negative values in arr 9.
Refer to the following data field and method. private int[] arr; //Precondition: arr contains at least one element. public void doSomething() { int sum1 = 0; int sum2 = 0; } for (int num : arr) { if (num > 0 || Math.abs(num) % 2 != 0) sum1 += num; else if (num < 0 && Math.abs(num) % 2 == 0) sum2 += num; } System.out.println(sum1); System.out.println(sum2); Which of the following best describes the value of sum2 output by method doSomething? a. The sum of all negative values in arr b. The sum of all negative even values in arr c. The sum of all even values in arr d. The sum of all negative values and positive even values in arr e. The sum of all values in arr 10. When implementing a class, which of the following would be the best reason to use a driver class? a.
b.
c.
d.
e.
A driver is a key feature of data encapsulation A driver provides a way to teach each method in the class A driver guarantees faster compilation and execution of the class A driver is necessary in object‐oriented programming A driver allows implementation of a top‐down design