📝 You're taking this test as a guest — your score won't be saved. Create a free account to track your progress.
0 / 40 answered
Question 1
Consider the following method. public static int compute(int a, int b) { int result = 0; for (int i = b; i <= a; i++) { result += i; } return result; } What is returned by the call compute(x, y)?
Question 2
Assume that p and q are Boolean variables that have been initialized. Consider the following code segment. p = p || q; q = p && q; Which of the following statements is true? I. The final value of p is the same as the initial value of p. II. The final value of q is the same as the initial value of q. III. The final value of q is the same as the initial value of p.
Question 3
Consider the following declaration and assignment statements: int x = 9; int y = 2; double z; z = x / y; After the assignment statement is executed, what is the value of z?
Question 4
A university wants to store information about its courses. For each course it tracks the course name, course code, number of credits, and whether it is a lecture or lab. Lecture courses track the classroom size. Lab courses track the equipment list. Which of the following is the best object-oriented design?
Question 5
Consider the following incomplete and incorrect class declaration: public class Rectangle implements Comparable { private int width; private int height; public boolean compareTo(Rectangle other) { return (width * height) > (other.width * other.height); } // constructors and other methods not shown } For which of the following reasons is the above class declaration incorrect? I. Objects may not access private data fields of other objects of the same class. II. The Comparable interface requires that compareTo be passed an Object rather than a Rectangle. III. The Comparable interface requires that compareTo return an int rather than a boolean.
Question 6
Consider the following method. public void countdown(int n) { int k; for (k = n; k > 0; k--) { countdown(k - 1); System.out.print(k); } } What is printed as a result of the call countdown(3)?
Question 7
Consider the following code segment: Library lib[] lib = new Library[5]; Assuming that a shelf has been stocked and has at least three books on it, what is the correct way of getting the third book from the 4th shelf of the second library in lib?
Question 8
Consider the following code segment: int n; n = /* initialized to an integer */ if (n % 3 == 0 && n / 4 == 2) System.out.print("Yes"); For what values of n will the word 'Yes' be printed when the code segment is executed?
Question 9
What is the correct way to define the signature of a constructor for a class Car that takes a String for the model and an int for the year?
Question 10
Consider the following code segment. List<Integer> list = new ArrayList<Integer>(); list.add(new Integer(10)); list.add(new Integer(20)); list.add(1, new Integer(15)); list.add(0, new Integer(5)); list.add(new Integer(25)); list.set(3, new Integer(18)); list.add(2, new Integer(12)); System.out.println(list); What is printed as a result of executing this code segment?
Question 11
Consider the method transfer, not part of the BankAccount class. public void transfer(BankAccount from, BankAccount to, double amount) { <missing code> } The BankAccount class has methods getBalance(), withdraw(double amount), and deposit(double amount). Which of the following code segments can replace <missing code> so that the method transfer correctly moves amount from from to to? I. BankAccount temp; temp = from; from = to; to = temp; II. double temp; temp = from.balance; from.balance -= amount; to.balance += amount; III. double temp = from.getBalance(); from.withdraw(amount); to.deposit(amount);
Question 12
The following method is intended to sort an integer array in ascending order using insertion sort. public static void insertSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { // Missing code } arr[j + 1] = key; } } Which of the following can be used to replace '// Missing code' so that the method executes properly?
Question 13
Consider the following method. public void display(int count, String s) { if (count <= 0) { return; } if (count % 2 == 0) { System.out.print(s + "+" + s); } else { System.out.print(s); } display(count - 1, s); } What is output by the call display(4, "O");?
Question 14
Consider the following code segment. int y; y = 72; if (y > 50) { System.out.print("P"); } if (y > 60) { System.out.print("Q"); } else if (y > 65) { System.out.print("R"); } if (y > 70) { System.out.print("S"); } if (y > 80) { System.out.print("T"); } What is output when the code is executed?
Question 15
Consider the following code: public int mystery(int n) { if (n == 0) return <missing value>; else return mystery(n - 1) + 3; } Which of the following can be used to replace <missing value> so that mystery(5) returns 16?
Question 16
Consider s1 and s2 defined as follows. String s1 = "java"; String s2 = new String("java"); Which of the following correctly evaluates to true when comparing s1 and s2 as equal strings? I. s1 == s2 II. s1.equals(s2) III. s1.compareTo(s2) == 0
Question 17
Consider the use of a Vector class to add two vectors v1 = (2, 5) and v2 = (3, 7). Vector v1; Vector v2; Vector result; v1 = new Vector(2, 5); v2 = new Vector(3, 7); /* missing code */ The Vector class has an instance method add(Vector other) that returns a new Vector representing the sum. Which of the following could be used to replace /* missing code */?
Question 18
Consider the following output: 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 Which of the following code segments produces the above output when executed?
Question 19
What is the correct way to implement the getTotalCost method of the ShoppingCart class? The total cost is the sum of the price of all items in the cart. The ShoppingCart class has a private ArrayList<Item> items field. Item has a method getPrice() that returns a double.
Question 20
Consider the following code segment: int k; int B[]; B = new int[6]; for (k = 0; k < B.length; k++) { B[k] = k + 1; } for (k = B.length - 1; k > 0; k--) { B[k - 1] = B[k]; } What values will B contain after the code segment is executed?
Question 21
Consider the following expression: a * b - c + d / e % f Which of the expressions given below is equivalent to the one given above?
Question 22
Consider the following methods. public void run() { int x = 3; int y = 8; scale(x, y); System.out.print(y); System.out.print(x); } public void scale(int a, int b) { a = a * 3; b = b * 2; System.out.print(a); System.out.print(b); } What is printed as the result of the call run()?
Question 23
Consider a search engine index with 500 documents, each containing 200 words. Three search methods are compared: Method 1: Binary search to find the document, then sequential search within the document. Method 2: Sequential search through all documents to find the matching one, then sequential search within it. Method 3: Sequential search through all words across all documents. Which best characterizes the greatest number of words examined using each method? Method 1 | Method 2 | Method 3
Question 24
The following incomplete class declaration is intended to extend the Animal class so that animals can be categorized with a habitat. public class Animal { private String name; private int age; public Animal(String n, int a) { name = n; age = a; } public String getName() { return name; } public int getAge() { return age; } } public class WildAnimal extends Animal { private String habitat; } Consider the following proposed constructors for WildAnimal: I. public WildAnimal() { habitat = "unknown"; } II. public WildAnimal(String n, int a, String h) { name = n; age = a; habitat = h; } III. public WildAnimal(String n, int a, String h) { super(n, a); habitat = h; } Which of these constructors would be legal for the WildAnimal class?
Question 25
Which of the following is NOT a software application?
Question 26
Consider the following variable and method declarations: String firstName; String lastName; public void modify(String a, String b) { a = b.toUpperCase(); b = a + b; } Assume that firstName has the value "alice" and lastName has the value "smith" and modify(firstName, lastName) is called. What are the values of firstName and lastName after the call?
Question 27
Consider the following declarations. public interface Shape { double area(); double perimeter(); } public class Circle implements Shape { public double area(double radius) { return Math.PI * radius * radius; } } Which of the following methods must be included in the declaration of the Circle class in order for the class to successfully compile? I. public double area() II. public double perimeter() III. public double perimeter(double r)
Question 28
Consider the following code segment: int[] P; int[] Q; P = initP(); Q = initQ(); for (int i = 0; i < P.length && P[i] != Q[i]; i++) { /* some code */ } Assuming P.length == Q.length after initialization, which of the following must be true after executing this code segment?
Question 29
Which of the following arrays would be sorted the fastest using insertion sort?
Question 30
Consider the following incomplete method intended to print box numbers of all boxes that contain mail. public void printBoxesWithMail(PostOffice p[]) { for (int k = 0; k < p.length; k++) { for (int x = 0; x < p[k].getNumBoxes(); x++) { // missing expression } } } Which of the following could be used to replace // missing expression so that the method works as intended?
Question 31
Assume that a program declares and initializes w as follows: String[] w; w = new String[8]; initialize(w); // Fills array w with valid strings each of length 4 Which of the following code segments correctly traverses the array and prints out the first character of all eight strings, then the second character of all eight strings, and so on? I. for (int i = 0; i < 8; i++) for (int j = 0; j < 4; j++) System.out.print(w[i].substring(j, j+1)); II. for (int i = 0; i < 4; i++) for (int j = 0; j < 8; j++) System.out.print(w[j].substring(i, i+1)); III. for (int i = 0; i < 4; i++) for (int j = 0; j < 8; j++) System.out.print(w[i].substring(j, j+1));
Question 32
Consider an array of integers. 7 3 9 1 5 8 2 6 If selection sort is used to order the array from smallest to largest values, which of the following represents a possible state of the array at some point during the selection sort process?
Question 33
Which of the following is the MOST effective way of ensuring software reliability?
Question 34
Consider the following two classes. public class Vehicle { public String fuelType = "gasoline"; public void move() { System.out.println("moving"); } } public class ElectricCar extends Vehicle { public void move() { System.out.println("silent glide"); } public String fuelType = "electric"; } Which of the following is the correct output after the following code segment is executed? Vehicle v = new ElectricCar(); System.out.println(v.fuelType); v.move();
Question 35
Assume that x and y are properly initialized variables of type Integer. Which of the following is an equivalent expression to: x.intValue() == y.intValue()
Question 36
Consider the following code for descending insertion sort. What change can be made so the array sorts in ascending order instead? public static void insertSort(int[] sort) { for (int index = 1; index < sort.length; index++) // Line 3 { int temp = sort[index]; while (index > 0 && sort[index-1] < temp) // Line 6 { sort[index] = sort[index-1]; index--; } sort[index] = temp; } }
Question 37
Consider the following abstraction of a while loop where <1>, <2>, and <3> represent legal code: while (<1>) { <2> <3>; } Which of the following for loops has the same functionality as the above while loop, assuming <init> initializes a variable used in <1>?
Question 38
Which of the following would cause a compile-time error rather than a run-time exception?
Question 39
Consider the following code segment: int j; int k; for (j = 1; j <= 3; j = j + 1) { for (k = 0; k < j + 2; k++) { System.out.print(k + " "); } } What is the output when the code is executed?
Question 40
Consider the following method. /** * Precondition: a > b >= 0 */ public static int compute(int a, int b) { int result = 1; for (int i = b + 1; i <= a; i++) { result *= i; } return result; } What is returned by the call compute(x, y)?