01. Consider the following code segment. int x = <some integer greater than zero> int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } What is printed as a result of executing the else code segment? { if (x < 300) (A) Unknown without the value of x n = 300; (B) 0 else (C) 100 n = 200; (D) 200 } (E) 300 System.out.println(n); 02. Consider the following code segment. int n = <some integer greater than zero> int count = 0; int p = 0; int q = 0; for (p = 1; p < n; p++) for (q = 1; q < n; q++) count++; System.out.println(count); What is the value of count when the code finishes executing? (A) (B) (C) (D) (E) n2 n2 - 1 (n - 1)2 p*q both C and D 03. Consider the following code segment. int count = 10; for (int p = 0; p < 15; p+=3) if (p % 2 == 0) count++; else count--; System.out.println(count); What is printed as a result of executing the code segment? (A) (B) (C) (D) (E) 10 11 12 13 14 04. Consider the following program. public class Method04 { public static void main (String args[]) { int p = 10; int q = 20; swap(p,q); System.out.println(p + " " + q); } public static void swap(int x, int y) { int temp = x; x = y; y = temp; } } What is printed as a result of executing the program? (A) (B) (C) (D) (E) 10 20 20 10 10 10 20 20 0 0 05. Consider the following program. public class Method05 { public static void main (String args[]) { int[] list = {1,2,3,4,5,6,7,8,9}; swap(list,3,4); System.out.println(list[3] + " " + list[4]); } } public static void swap(int[] x, int p, int q) { What is printed as a result of int temp = x[p]; executing the program? x[p] = x[q]; x[q] = temp; (A) 3 4 } (B) 4 3 (C) 4 5 (C) 5 4 (D) Exception error 06. public class Method06 { public static void main (String args[]) { System.out.println(tango(120,108)); } public static int tango(int n1, int n2) { int temp = 1; int rem = 1; while (rem != 0) { rem = n1 % n2; if (rem == 0) temp = n2; else { n1 = n2; n2 = rem; } } return temp; } } What is printed as a result of executing the program? (A) (B) (C) (D) (E) 120 108 12 9 1 07. Consider the following Aardvark class and code segment. Aardvark a = new Aardvark(100); System.out.println(a.getAardvarks()); class Aardvark { private int numAardvarks; public void Aardvark(int aardvarks) { numAardvarks = aardvarks; } public int getAardvarks() { return numAardvarks; } } What is printed by the code segment? (A) 0 (B) 75 (C) The program compiles, but there is a logic error. (D) The program compiles, but there is a runtime error. (E) The program does not compile. 08. Consider the following code segment and List class. List list = new List(100,55); list.showList(); System.out.println(); class List { private int[] list; /* missing constructor */ public void showList() { for (int k = 0; k < list.length; k++) System.out.print(list[k] + " "); } } The output of executing the code segment is a row of one hundred 55 numbers. Which of the following constructor implementations of /* missing constructor */ will make the List class work as intended? (A) public List(int size, int value) { list = new int[size]; for (int k = 0; k < size; k++) list[k] = value; } (B) public List() { list = new int[10]; for (int k = 0; k < size; k++) list[k] = 55; } (C) public List(int size, int value) { for (int k = 0; k < size; k++) list[k] = value; } (D) public List(int size, int value) { list = new int[size,value]; } (E) public List(int size, int value) { list = new int[size]; for (int k = 0; k <= size; k++) list[k] = value; } 09. Consider the following two classes and code segment. What is printed by the code segment? class Rumba { (A) Executing the Rumba constructor private Mambo dance; Executing the Mambo constructor public Rumba() { (B) Executing the Mambo constructor System.out.println("Executing the Rumba constructor"); Executing the Rumba constructor dance = new Mambo(); } (C) Executing the Rumba constructor } Executing the Mambo constructor Executing the Rumba constructor class Mambo (D) Executing the Mambo constructor { Executing the Rumba constructor public Mambo() Executing the Mambo constructor { System.out.println("Executing the Mambo constructor"); (E) Executing the Mambo constructor } Executing the Mambo constructor Executing the Rumba constructor } Mambo m = new Mambo(); Rumba r = new Rumba(); 10. The Boolean expression (A || B) && A is true (A) whenever A is true. (B) whenever B is true. (C) whenever either A is true or B is true. (D) whenever both A is true and B is true. (E) in all cases. 11. Consider the expression below. A || ( (B || !C) && (!D && F) || !(G && H) && !(!J || !K) ) If all you know is that A equals true, what can you determine about the expression above? (A) The expression evaluates to true. (B) The expression evaluates to false. (C) The expression evaluates to true only if B equals true. (D) The expression evaluates to true only if B equals false. (E) The expression cannot be evaluated with the provided information. 12. The Boolean expression !((A < B) && (C > D)) is equivalent to which of the following expressions? (A) (A < B) || (C > D) (B) (A >= B) && (C <= D) (C) (A >= B) || (C <= D) (D) (A > B) && (C < D) (E) (A > B) || (C < D) 13. class Student extends Person { private int grade; Person sue = new Person(25); Student tom = new Student(17,12); System.out.println(sue.getAge()); System.out.println(tom.getGrade()); public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } class Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getGrade() { return grade; } } public int getAge() { return age; } } What are the first 2 lines of program (C) output? Person Constructor Student Constructor (A) 25 17 (E) Student Constructor Person Constructor (B) Student Constructor Student Constructor (E) Person Constructor Person Constructor 14. Person sue = new Person(25); Student tom = new Student(17,12); System.out.println(sue.getAge()); System.out.println(tom.getGrade()); class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } class Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getGrade() { return grade; } } public int getAge() { return age; } (C) } 17 17 What are the last 2 lines of program output? (D) (A) 25 25 17 (E) (B) 17 25 25 Person Constructor Student Constructor 15. Consider the following two classes and code segment. class Person { private int age; public Person(int a) { age = a; } public void showData() { System.out.println(age); } } class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; } public void showData() { System.out.println(grade); super.showData(); } } Person sue = new Person(25); Student tom = new Student(17,12); sue.showData(); What is printed by the code segment? (A) 25 17 (B) 17 25 (C) 25 17 12 (D) 17 12 25 16. Consider the following code segment. int[ ] list = {11,22,33,44,55,66,77,88,99}; for (int k = 0; k < list.length; k++) list[k] = list[k]/list[0]; for (int k = 0; k < list.length; k++) System.out.print(list[k] + " "); What is printed as a result of executing the code segment? (A) (B) (C) (D) (E) 11 22 33 44 55 66 77 88 99 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 22 33 44 55 66 77 88 99 11 22 33 44 55 66 77 88 9 17. int[ ] list1 = {10,20,30,40,50,60,70,80,90}; int[ ] list2 = list1; for (int k = 0; k < list2.length; k++) list2[k] = list1[0]; for (int k = 0; k < list1.length; k++) list1[k] *= list2[k]; for (int k = 0; k < list1.length; k++) System.out.print(list1[k] + " "); What is printed as a result of executing the code segment? (A) (B) (C) (D) (E) 100 100 100 100 100 100 100 100 100 100 200 300 400 500 600 700 800 900 1 2 3 4 5 6 7 8 9 10 100 18. Consider the following code segment. double [ ][ ] values = new double[10][15]; int sum = 0; Each row in the array holds (in order) a student number, 10 homework grades of equal weight, and 4 exam grades. Choose the code segment below that would sum the homework grades in the sixth row and place them in the integer variable sum. (A) for (int j = 0; j < 15; j++) sum += values[6][j]; (B) for (int j = 0; j < 10; j++) sum += values[5][j]; (C) for (int j = 1; j < 11; j++) sum += values[j][6]; (D) for (int j = 0; j < 10; j++) sum += values[j][5]; (E) for (int j = 1; j < 11; j++) sum += values[5][j]; 19. Consider the following code segment. ArrayList<String> names = new ArrayList<String>(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); names.add(2,"Diana"); names.add(5,"David"); System.out.println(names); What is printed as a result of executing the code segment? (A) (B) (C) (D) (E) [Isolde, John, Diana, Maria, Heidi, David] [Isolde, Diana, Greg, Maria, David] [Isolde, John, Diana, Greg, Maria, David, Heidi] [Isolde, John, Diana, Greg, Maria, Heidi, David] [Isolde, Diana, John, Greg, Maria, David, Heidi] 20. Consider the following code segment. ArrayList<String> names = new ArrayList<String>(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); names.set(2,names.get(3)); names.set(3,names.get(2)); System.out.println(names); What is printed as a result of executing the code segment? (A) (B) (C) (D) (E) [Isolde, John, Greg, Maria, Greg, Heidi] [Isolde, John, Maria, Greg, Heidi] [Isolde, John, Greg, Maria, Heidi] [Isolde, John, Maria, Maria, Heidi] [Isolde, Greg, John, Maria, Heidi] 21. Consider the following code segment. ArrayList<String> names1 = new ArrayList<String>(); names1.add("Isolde"); names1.add("John"); names1.add("Greg"); names1.add("Maria"); What is printed as a result of executing the code names1.add("Heidi"); segment? ArrayList<String> names2 = names1; names1.set(1,"Jessica"); names2.set(4,"Haley"); System.out.println(names1); System.out.println(names2); (A) (B) [Isolde, Jessica, Greg, Maria, Heidi] [Isolde, John, Greg, Maria, Haley] [Isolde, Jessica, John, Greg, Maria, Heidi] [Isolde, John, Greg, Maria, Haley, Heidi] (C) [Isolde, John, Jessica, Greg, Maria, Heidi] [Isolde, John, Greg, Maria, Heidi, Haley] (D) [Isolde, John, Jessica, Greg, Maria, Haley, Heidi] [Isolde, John, Jessica, Greg, Maria, Haley, Heidi] (E) [Isolde, Jessica, Greg, Maria, Haley] [Isolde, Jessica, Greg, Maria, Haley] 22. Consider the following method. public static String method1(String str) { String temp = ""; int eSpot = str.indexOf('e'); temp = str.substring(0,eSpot); return temp; } What value does the method return if it is passed Entree as an argument? (A) (B) (C) (D) (E) An empty string E Entr Entre Entree 23. Consider the following code segment. String s1 = "Invalid"; String s2 = "valid"; s1 = s1.substring(2); boolean isSame; if (s1 == s2) isSame = true; else isSame = false; What is the value of isSame after the code segment executes? (A) True, because the two strings both hold the same value. (B) False, because the == operator compares immediate values, which are memory references for String objects. (C) Nothing, because using == to compare strings will give a run-time error. (D) True, because s1 and s2 now reference the exact same String object. (E) False, because s1 and s2 are not the same variable name. 24. Consider the following method. public static String method2(String[] values) { String temp = values[0]; for (int i = 1; i < values.length; i++) { if (values[i].compareTo(temp) > 0) temp = values[i]; } return temp; } What will the method return if the following array is sent as the argument? String[] arr = {"Math","Computer Science","Computer Applications","Science","Graphic Design"}; (A) (B) (C) (D) (E) "Math" "Computer Science" "Computer Applications" "Science" "Graphic Design" 25. Consider the following program. Car car = new Car("Ford250",350); class Engine { private int horsePower; public Engine(int hp) { horsePower = hp; } } class Car { private Engine engine; private String carMake; /* MISSING CONSTRUCTOR */ } Question-25 continues on the next slide 25 continued. Which of the following implementations of MISSING CONSTRUCTOR will correctly initialize the instance variables of Engine and Car ? (A) (B) public Car(String cm, int hp) { Engine engine = new Engine(hp); carMake = cm; } public Car(String cm, int hp) { engine = new Engine(350); carMake = cm; } (C) (D) public Car(String cm, int hp) { engine = new Engine(hp); carMake = cm; } public Car(String carMake, int hp) { Engine = new Engine(hp); carMake = carMake; } 26. public class Test26 { public static void main(String args[]) { Pidgit p = new Pidgit(10,20); } } class Widgit { private int numWidgits; public Widgit(int nw) { numWidgits = nw; } } class Pidgit extends Widgit { private int numPidgits; private Widgit widgit; public Pidgit(int np, int nw) { numPidgits = np; widgit = new Widgit(nw); } } What is the class interaction? (A) (B) (C) (D) (E) Inheritance Composition Both inheritance & composition No class interaction Improper class interaction 27. Consider this is a partial BoundedGrid class declaration. public class BoundedGrid<E> extends AbstractGrid<E> { private Object[ ][ ] occupantArray; public BoundedGrid(int rows, int cols) { if (rows <= 0) throw new IllegalArgumentException("rows <= 0"); if (cols <= 0) throw new IllegalArgumentException("cols <= 0"); occupantArray = new Object[rows][cols]; } The BoundedGrid class uses a _______________ array to store data. (A) (B) (C) (D) (E) generic, two-dimensional static, two-dimensional dynamic, two-dimensional generic, one-dimensional array dynamic, one-dimensional array 28. Consider the following code segment. String[ ] stID = {"ABC", "DEF", "GHI", "JKL", "MNO"}; String str1 = "GHI"; boolean found = false; int loc = -1, sub = 0; while (!found && sub < stID.length) { if (stID[sub].equals(str1)) { The code segment is an example of found = true; loc = sub; (A) a sort algorithm. } (B) an algorithm to locate the smallest element. sub++; (C) an algorithm to locate the largest element. } (D) a sequential search algorithm. (E) an algorithm to sum the values of an array. 29. When using a binary search, what is the MAXIMUM number of comparisons necessary to locate a specific item in a list of 1025 elements? (A) 1 (B) 9 (C) 10 (D) 11 (E) 1024 30. Consider the following array: int[ ] array = {4, 8, 2, 7, 1, 9, 3, 5}; What would array hold after three iteration passes of the outer loop in an ascending selection sort algorithm? (A) {1, 2, 3, 4, 8, 7, 9, 5} (B) {2, 4, 7, 8, 1, 9, 3, 5} (C) {1, 2, 3, 7, 4, 9, 8, 5} (D) {4, 2, 7, 8, 1, 9, 3, 5} (E) {4, 3, 2, 5, 1, 7, 8, 9} 34. Consider the following GridWorld display. How many Bug objects will be left after the next Step iteration? (A) (B) (C) (D) (E) 1 bug 2 bugs 3 bugs 4 bugs 5 bugs 35. Consider the following MysteryCritter class. class MysteryCritter extends Critter { public void turn() { int add = (int) (Math.random()*91)+90); setDirection(getDirection() + add); } } How many degrees will a MysteryCritter object turn during one step of a GridWorld execution? (A) (B) (C) (D) (E) 0 90 180 A random number in the range of 90 - 180 A random number in the range 0 - 90 36. You are asked to implement a new GridWorld class, called DynamiteCritter. DynamiteCritter objects act exactly like Critter objects, except that they only process Rock objects by blowing them up. Implementing this new DynamiteCritter class will require re-defining (A) (B) (C) (D) (E) all the act methods. the getActors method only. the processActors method only. methods getActors and processActors only. methods getMoveLocations, selectMoveLocation and makeMove only. 37. Consider the following code segment from the step method used by the ActorWorld class. The step method is called each time the Step button is clicked on the Gridworld display or repeatedly after the Run button is clicked. for (Actor a : actors) { if (a.getGrid() == gr) a.act(); } Which of the following statements completes a correct explanation how the loop executes, shown above? Every object which occupies a cell on the current grid, when the step method is called, will execute according to the act method definition of (A) the Actor class. (B) its own class. (C) its own class, provided the object is still a member of the grid. (D) its own class or a superclass. (E) its own class or a superclass, provided the object is still a member of the grid. 38. Consider the following class definitions and code segment. ClassA item1 = new ClassA(); ClassA item2 = new ClassB(); ClassA item3 = new ClassC(); item1.method(10); item2.method(20); item3.method(30); class ClassA { public ClassA() { } public void method(int a) {System.out.println("Class A " + a);} } class ClassB extends ClassA { public ClassB(){ } public void method(int b) {System.out.println("Class B " + b);} } class ClassC extends ClassB { public ClassC(){ } } What is printed as a result of executing the code segment? (A) Class A 10 Class A 20 Class A 30 (B) Class A 10 Class B 20 Class B 30 (C) Class A 10 Class B 20 (D) Class A 10 Class B 20 Class B 20 (E) Exception error message 39. Consider the following interface, two classes and incomplete code segment. abstract interface Shape { public abstract double getArea(); } class Circle implements Shape { private double radius; public Circle (double r) {radius = r;} public double getArea() {return Math.PI * radius * radius;} } class Triangle implements Shape { private double base; private double height; public Triangle (double b, double h) {base = b; height = h;} public double getArea() {return (base * height) / 2;} } Shape[ ] shapes = new Shape[2]; shapes[0] = new Circle(3); shapes[1] = new Triangle(4,3); /* missing code segment */ slide. Question-39 continues on the next Question 39 Continued. Which of the following implementations of /* missing code segment */ will correctly display the areas of the shapes array elements. Implementation I: for (int k = 0; k < shapes.length; k++) System.out.println(shapes[k].getArea()); (A) Implementation I only Implementation II: (B) Implementation III only for (Shape s: shapes) (C) Implementations I & II only System.out.println(s.getArea()); (D) Implementations II & III only (E) Implementations I, II & III Implementation III: for (int k = 0; k < shapes.length; k++) if (shapes[k] instanceof Circle) System.out.println(Circle.getArea()); else if (shapes[k] instanceof Triangle) System.out.println(Triangle.getArea()); 40. Consider the following code segment. String[] list1 = {"Tom","Sue","Joe"}; ArrayList<String> list2 = new ArrayList<String>(); list2.add("Meg"); list2.add("Bob"); list2.add("Ann"); System.out.println(list1); System.out.println(list2); What is printed as a result of executing the code segment? (A) [Tom, Sue, Joe] [Meg, Bob, Ann] (B) [Tom, Sue, Joe] [Ljava.util.ArrayList:memory reference (C) [Ljava.lang.String:memory reference [Meg, Bob, Ann] (D) [Ljava.lang.String:memory reference [Ljava.util.ArrayList:memory reference (E) None of the above, because array objects can only be displayed with a loop structure, which accesses each array element. 41. Consider the following code segment and Car class. Car car = new Car("Ford",2010); System.out.println(car); class Car { private String make; private int year; What is printed as a result of executing the code segment? (A) (B) public Car (String m, int y)(C) (D) { (E) make = m; year = y; } [Ford, 2010] [make, year] Ford, 2010 Car@<some memory reference> None of the above public String toString () { return "[" + make + ", " + year + "]"; } } 42. Consider the following code segment and Car class. Car car1 = <some Car object> Car car2 = <some Car object> System.out.println (car1.equals(car2)); What is printed as a result of executing the code segment? class Car { (A) private String make; private int year; (B ) public Car (String m, int y) { (C) make = m; (D) year = y; (E) } False, since non-standard classes cannot be compared for equality. Neither, since output cannot be determined without specific Car object information True, if both objects are the same year True, if both objects are both the same make and the same year True, for all Car objects public boolean equals (Object other) { return true; } } 43. When a class implements an interface, what must it do? (A) It must redefine each constant from the interface. (B) It must declare and provide a method body for each method in the interface. (C) It must declare a variable for each constant in the interface. (D) It must declare abstract methods for each method in the interface (E) It must include a private method for each method in the interface. 44. Consider the following erroneous interface and class definition. /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ public interface Interface1 { public int FIELDA = 55; public int methodA(double d); } /*6*/ public class ClassA implements Interface1 /*7*/ { /*8*/ public int FIELDA = 60; /*9*/ public int methodA(double a) /*10*/ { return (int) a; } /*11*/ } Which line contains an error? (A) (B) (C) (D) (E) Line 3, because variables in interfaces require the final keyword Line 4, because there should not be a semicolon at the end of the method header Line 8, because FIELDA cannot be modified here Line 9, because the name of the parameter is different from Line 4 Line 10, because it is illegal to cast a double to an int 45. Why is there an abstract class implementation, which partially implements the Grid interface methods, followed by BoundedGrid and UnboundedGrid classes, which completely implement the Grid interface? Why is there an abstract class implementation, which partially implements the Grid interface methods, followed by BoundedGrid and UnboundedGrid classes, which completely implement the Grid interface? (A) A class cannot implement an abstract interface. Only an abstract class can implement an abstract interface. (B) An abstract class must be used when there are multiple class that implement interface methods. (C) BoundedGrid and UnboundedGrid implement some Grid interface methods differently. All the methods that have an identical implementation for BoundedGrid and UnBoundedGrid are implemented in the AbstractGrid class. (D) Abstract classes give greater readability than concrete classes. (E)The use of polymorphism requires abstract class implementation of an interface. 46. A team of programmers works together on a large project. Each team member is assigned to create, write and test one class that will be used in the project. Which of the following is the minimal required information for each programmer to start his/her assigned task? I. II. III. The data that will need to be stored by the class. The method signatures that will access the class data. The preconditions and postconditions of each class method (A) (B) (C) (D) (E) I only II only I and II only II and III only I, II and III 47. Consider the following code segment. ArrayList<Integer> list1 = new ArrayList<Integer>(); /* program code that enters appropriate values to the list1 object */ ArrayList<Integer> list2 = list1; /* code statements to perform various processing functions */ The completed code has some potential problems that can negatively impact program reliability. Which of the following are potential problems? I. II. An ArrayOutOfBoundsException will occur. list2 makes a shallow copy of list1, which can result in unwanted changes to the state of list1 or list2. III.list2 is never constructed with the new operator. (A) (B) (C) (D) (E) I only II only III only I and II only I and III only 48. A data structure needs to store information in about 20,000 college student records. A variety of information needs to be stored in each student record, such as name, age, gpa, date of birth, address and social security number. The data structure is to be used in a program with the following two data processing requirements: [1]Frequent quick access to any student records to update student information. [2]End-of-semester access to print a student GPA list from highest to lowest. Consider the following two implementations to satisfy the two processing requirements. Implementation-1 stores all the student records in a data structure that is sorted according to GPA so as to allow efficient printing of a GPA list, but a linear search is required to access any individual student information according to social security number. Implementation-2 stores all the student records in a data structure that is sorted according to student social security number. The data structure must be sorted according to GPA before a sorted GPA list can be printed. Question-48 continues on the next slide. Question 48 Continued. Which of the following statements is true about the manner in which Implementation-1 and Implementation-2 satisfy the two data processing requirements? (A) Implementation-1 is better, because it keeps the data sorted, such that all 20,000 records can be easily printed according to GPA order. (B) Implementation-1 is better, because it takes longer to sort records than it takes to search for one specific record. (C) Implementation-2 is better, because frequent searches take longer than occasional sorting. (D) Implementation-2 is better, because frequent sorting take longer than occasional searching. (E) Implementation-1 and Implementation-2 are essentially identical and each works as efficiently as the other does.