# 01. Consider the following code segment. int x = int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n =

## Presentation on theme: "01. Consider the following code segment. int x = int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n ="— Presentation transcript:

01. Consider the following code segment. int x = int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n = 200; } System.out.println(n); What is printed as a result of executing the code segment? (A)Unknown without the value of x (B)0 (C) 100 (D)200 (E)300

02. Consider the following code segment. int n = 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)n 2 (B)n 2 - 1 (C) (n - 1) 2 (D)p * q (E)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)10 (B)11 (C) 12 (D)13 (E)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)10 20 (B)20 10 (C) 10 10 (D)20 20 (E)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) { int temp = x[p]; x[p] = x[q]; x[q] = temp; } What is printed as a result of executing the program? (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)120 (B)108 (C) 12 (D)9 (E)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. class Rumba { private Mambo dance; public Rumba() { System.out.println("Executing the Rumba constructor"); dance = new Mambo(); } class Mambo { public Mambo() { System.out.println("Executing the Mambo constructor"); } Mambo m = new Mambo(); Rumba r = new Rumba(); What is printed by the code segment? (A)Executing the Rumba constructor Executing the Mambo constructor (B)Executing the Mambo constructor Executing the Rumba constructor (C)Executing the Rumba constructor Executing the Mambo constructor Executing the Rumba constructor (D)Executing the Mambo constructor Executing the Rumba constructor Executing the Mambo constructor (E)Executing the Mambo constructor Executing the Mambo constructor Executing the Rumba constructor

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 D)) is equivalent to which of the following expressions? (A)(A D) (B)(A >= B) && (C <= D) (C)(A >= B) || (C <= D) (D)(A > B) && (C < D) (E)(A > B) || (C < D)

13. Person sue = new Person(25); Student tom = new Student(17,12); System.out.println(sue.getAge()); System.out.println(tom.getGrade()); class Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getAge() { return age; } What are the first 2 lines of program output? (A)25 17 (B)Student Constructor Student Constructor class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } public int getGrade() { return grade; } (C)Person Constructor Student Constructor (E)Student Constructor Person 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 Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getAge() { return age; } What are the last 2 lines of program output? (A)25 17 (B)17 25 class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } public int getGrade() { return grade; } (C)17 17 (D)25 25 (E)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(); tom.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) 11 22 33 44 55 66 77 88 99 (B) 1 2 3 4 5 6 7 8 9 (C) 1 1 1 1 1 1 1 1 1 (D) 1 22 33 44 55 66 77 88 99 (E) 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) 100 100 100 100 100 100 100 100 100 (B) 100 200 300 400 500 600 700 800 900 (C) 1 2 3 4 5 6 7 8 9 (D) 10 (E) 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 names = new ArrayList (); 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) [Isolde, John, Diana, Maria, Heidi, David] (B) [Isolde, Diana, Greg, Maria, David] (C) [Isolde, John, Diana, Greg, Maria, David, Heidi] (D) [Isolde, John, Diana, Greg, Maria, Heidi, David] (E) [Isolde, Diana, John, Greg, Maria, David, Heidi]

20. Consider the following code segment. ArrayList names = new ArrayList (); 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) [Isolde, John, Greg, Maria, Greg, Heidi] (B) [Isolde, John, Maria, Greg, Heidi] (C) [Isolde, John, Greg, Maria, Heidi] (D) [Isolde, John, Maria, Maria, Heidi] (E) [Isolde, Greg, John, Maria, Heidi]

21. Consider the following code segment. ArrayList names1 = new ArrayList (); names1.add("Isolde"); names1.add("John"); names1.add("Greg"); names1.add("Maria"); names1.add("Heidi"); ArrayList names2 = names1; names1.set(1,"Jessica"); names2.set(4,"Haley"); System.out.println(names1); System.out.println(names2); What is printed as a result of executing the code segment? (A)[Isolde, Jessica, Greg, Maria, Heidi] [Isolde, John, Greg, Maria, Haley] (B)[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)An empty string (B)E (C)Entr (D)Entre (E)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)"Math" (B)"Computer Science" (C)"Computer Applications" (D)"Science" (E)"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) public Car(String cm, int hp) { Engine engine = new Engine(hp); carMake = cm; } (B) public Car(String cm, int hp) { engine = new Engine(350); carMake = cm; } (C) public Car(String cm, int hp) { engine = new Engine(hp); carMake = cm; } (D) 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)Inheritance (B)Composition (C)Both inheritance & composition (D)No class interaction (E)Improper class interaction

27. Consider this is a partial BoundedGrid class declaration. public class BoundedGrid extends AbstractGrid { 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)generic, two-dimensional (B)static, two-dimensional (C)dynamic, two-dimensional (D)generic, one-dimensional array (E)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)) { found = true; loc = sub; } sub++; } The code segment is an example of (A)a sort algorithm. (B) an algorithm to locate the smallest element. (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)1 bug (B)2 bugs (C)3 bugs (D)4 bugs (E)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)0 (B)90 (C)180 (D)A random number in the range of 90 - 180 (E)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)all the act methods. (B)the getActors method only. (C) the processActors method only. (D)methods getActors and processActors only. (E)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 (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 */ Question-39 continues on the next slide.

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()); Implementation II: for (Shape s: shapes) System.out.println(s.getArea()); 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()); (A)Implementation I only (B)Implementation III only (C)Implementations I & II only (D)Implementations II & III only (E)Implementations I, II & III

40.Consider the following code segment. String[] list1 = {"Tom","Sue","Joe"}; ArrayList list2 = new ArrayList (); 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; public Car (String m, int y) { make = m; year = y; } public String toString () { return "[" + make + ", " + year + "]"; } What is printed as a result of executing the code segment? (A)[Ford, 2010] (B)[make, year] (C)Ford, 2010 (D)Car@ (E)None of the above

42.Consider the following code segment and Car class. Car car1 = Car car2 = System.out.println (car1.equals(car2)); class Car { private String make; private int year; public Car (String m, int y) { make = m; year = y; } public boolean equals (Object other) { return true; } What is printed as a result of executing the code segment? (A)False, since non-standard classes cannot be compared for equality. (B)Neither, since output cannot be determined without specific Car object information (C)True, if both objects are the same year (D)True, if both objects are both the same make and the same year (E)True, for all Car objects

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*/ public interface Interface1 /*2*/ { /*3*/public int FIELDA = 55; /*4*/public int methodA(double d); /*5*/ } /*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)Line 3, because variables in interfaces require the final keyword (B)Line 4, because there should not be a semicolon at the end of the method header (C)Line 8, because FIELDA cannot be modified here (D)Line 9, because the name of the parameter is different from Line 4 (E)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? (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.The data that will need to be stored by the class. II.The method signatures that will access the class data. III.The preconditions and postconditions of each class method (A)I only (B)II only (C)I and II only (D)II and III only (E)I, II and III

47.Consider the following code segment. ArrayList list1 = new ArrayList (); /* program code that enters appropriate values to the list1 object */ ArrayList 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.An ArrayOutOfBoundsException will occur. II.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)I only (B)II only (C)III only (D)I and II only (E)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.

Download ppt "01. Consider the following code segment. int x = int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n ="

Similar presentations