13
Sample Multiple Choice Question #1 Consider the following code segment. int[] list = {56,23,78,54,11,95,60,17,64}; for (int item: list) item = 99; for (int item: list) System.out.print(item + " "); What will be printed as a result of executing the code segment? (A) 56 23 78 54 11 95 60 17 64 (B) 64 17 60 95 11 54 78 23 56 (C) 99 23 78 54 11 95 60 17 64 (D) 56 23 78 54 11 95 60 17 99 (E) 99 99 99 99 99 99 99 99 99

Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

  • Upload
    vominh

  • View
    215

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #1 Consider the following code segment. int[] list = {56,23,78,54,11,95,60,17,64}; for (int item: list) item = 99; for (int item: list) System.out.print(item + " "); What will be printed as a result of executing the code segment? (A) 56 23 78 54 11 95 60 17 64 (B) 64 17 60 95 11 54 78 23 56 (C) 99 23 78 54 11 95 60 17 64 (D) 56 23 78 54 11 95 60 17 99 (E) 99 99 99 99 99 99 99 99 99

Page 2: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #2 Consider the following code segment and method reduce. int[] numbers = {56,34,76,89,32,18,37,75,99,55}; ArrayList<Integer> temp = new ArrayList<Integer>(); for (int nr: numbers) temp.add(nr); reduce(temp); System.out.println(temp); public static ArrayList<Integer> reduce(ArrayList<Integer> temp) { for (int k = 0; k < temp.size(); k++) { temp.add(temp.remove(k)); } return temp; } What is printed as a result of executing the code segment? (A) [34, 76, 89. 32, 18, 37, 75, 99, 55, 56] (B) [18, 37, 75, 99, 55, 56, 34, 76, 89, 32] (C) [34, 89, 18, 75, 55, 76, 37, 56, 99, 32] (D) [56, 76, 32, 37, 99, 34, 89, 18, 75, 55] (E) An IndexOutOfBoundsException message

Page 3: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #3 Consider the following code segment, interface and class. Acrobat ivan = new Acrobat("Ivan"); ivan.act(); interface CircusPerformer { public String getPerformer(); public void act(); public void entrance(); public void performance(); public void exit(); } class Acrobat implements CircusPerformer { private String performerName; public Acrobat(String pN) { performerName = pN;} public void act() { entrance(); performance(); exit(); } public void entrance() { System.out.println("Parades with performers"); } public void performance() { System.out.println("Flips forwards and backwards on trapeze"); } } What is printed as a result of executing the code segment? (A) Flips forwards and backwards on trapeze Parades with performers Lands in safety net (B) Ivan Flips forwards and backwards on trapeze Ivan Parades with performers Ivan Lands in safety net (C) Ivan Flips forwards and backwards on trapeze Parades with performers Lands in safety net (D) A compile error message, due to implementing a method that was not declared in the interface. (E) A compile error message, due to not implementing a method that was declared in the interface.

Page 4: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #4 . A RandomCritter is a Critter, except it moves to any random location on the grid. Consider writing a RandomCritter class. Which of the following Critter methods must be re-defined? (A) act (B) getActors (C) processActors (D) getMoveLocations (E) All of the above

Page 5: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #5 Consider the following MysteryCritter class. public class MysteryCritter extends Critter { public void processActors(ArrayList<Actor> actors) { for (Actor a : actors) { a.removeSelfFromGrid(); } } }

The GridWorld to the left includes MysteryCritter objects, indicated by dark upper-case C letters. How many GridWorld objects will be removed during the next step execution? (A) 3 (B) 5 (C) 7 (D) 8 (E) 10

Page 6: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #6 Consider the following code segment. String s1 = "Djakarta"; String s2 = s1.substring(3,7); String s3 = s1.substring(3); System.out.println(s2); System.out.println(s3); What will be output when the code segment executes? (A) kart karta (B) jakarta akarta (C) karta karta (D) kart karta (E) akart akarta

Page 7: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #7 Consider the following code segment and method. System.out.println(method11("AARD","AARDVARK")); System.out.println(method11("CHARLY","CHARLY")); System.out.println(method11("ZULU","TIGER")); /** Precondition: s1 != null and s2 != null */ public static String method11(String s1, String s2) { String message = ""; int n = s1.compareTo(s2); if (n < 1) message = s1 + " is less than " + s2; else if (n == 0) message = s1 + " is equals to " + s2; else message = s1 + " is greater than " + s2; return message; } What is printed as a result of executing the code segment? (A) AARD is less than AARDVARK CHARLY is less than CHARLY ZULU is greater than TIGER (B) AARD is less than AARDVARK CHARLY is equals to CHARLY ZULU is greater than TIGER (C) AARD is greater than AARDVARK CHARLY is equals to CHARLY ZULU is less than TIGER (D) AARD is greater than AARDVARK CHARLY is greater than CHARLY ZULU is less than TIGER (E) An StringOutOfBoundsException error message

Page 8: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #8 The Least Common Multiple (LCM) of 2 numbers is equal to the product of those 2 numbers divided by the GCF. Assume that a functional getGCF method is available, Consider the Java implementations below. Which of these will compute the LCM properly? public static int getLCM1(int n1, int n2) { int gcf = getGCF(n1,n2); int lcm = (n1 + n2) * gcf; return lcm; } public static int getLCM2(int n1, int n2) { int gcf = getGCF(n1,n2); int lcm = n1 / gcf * n2; return lcm; } public static int getLCM3(int n1, int n2) { return n1 / getGCF(n1,n2) * n2; } (A) getLCM1 only (B) getLCM2 only (C) getLCM2 and getLCM3 only (D) getLCM1 and getLCM2 only (E) getLCM1, getLCM2 and getLCM3

Page 9: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #9 Consider the following method. public static int method1807(int n) { if (n == 0 || n == 1) return 1; else return method1807(n-1) + method1807(n-2); } What value is returned as a result of the call method1807(8) ? (A) 1 (B) 8 (C) 34 (D) 89 (E) Program crashes due to too many recursive calls

Page 10: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Multiple Choice Question #10 . Consider the following two code segments. Code Segment 1 for (int k = 1; k <= 10; k++) { double rnd1 = (int) Math.random() * 100; System.out.print(rnd1 + " "); } Code Segment 2 for (int k = 1; k <= 10; k++) { double rnd2 = (int) (Math.random() * 100); System.out.print(rnd2 + " "); } Which of the following statements is true about the execution comparison of Code Segment 1 and Code Segment 2? I. Code Segment 1 and Code Segment 2 will display different numbers. II. Code Segment 1 and Code Segment 2 will display numbers in the same range. III. Code Segment 1 and Code Segment 2 will display numbers in different ranges. (A) I only (B) II only (C) III only (D) I and II (E) I and III

Page 11: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Free Response Question 1 Consider the following incomplete declaration of a Random1 class. Class Random1 could be an

implementation of the standard Java class Random. class Random1 { double currentNr; public Random1() { double seed = System.currentTimeMillis(); currentNr = seed; } public Random1(double seed) { currentNr = seed; } // postcondition: returns a double x, such that 0 <= x < 1 public double getRandom() { double nextNr = currentNr + Math.PI; nextNr = nextNr * nextNr; int temp = (int) nextNr; currentNr = nextNr = nextNr - temp; return nextNr; } public int nextInteger(int n) { /* be implemented in part (a) */ } } Part (a). Complete method nextInteger below. // precondition: n > 1 // postcondition: returns an int x, such that 0 <= x < n public int nextInteger(int n)

Page 12: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Part (b). Consider the following incomplete class Random2, which is a sub class of Random1. class Random2 extends Random1 { public Random2(double seed) { /* be implemented in part (b) */ } public int nextInteger(int start, int end) { /* be implemented in part (c) */ } } Write constructor Random2, which calls and provides the seed information for constructor Random1. Complete method Random2 below. // precondition: seed > 0 // postcondition: seed is passed to the super class constructor Random1.

public Random2(double seed) Part (c). Write overloaded method nextInteger, which returns an integer in a specified range. Complete method nextInteger below. // precondition: start is an integer

// end is an integer such that end > start // postcondition: returns a random integer x such that for all x,

// start <= x <= end public int nextInt(int start, int end)

Page 13: Sample Multiple Choice Question #1 - Yahoolib.store.yahoo.net/lib/yhst-131101042530630/CSSampleQuestionsWeb.pdffor (int item: list) item = 99; ... Sample Multiple Choice Question #10

Sample Free Response Question 2

This question involves reasoning about the GridWorld case study. A copy of the code is provided as part of this exam. A FlowerCritter behaves like a Critter, except in the way that it interacts with other actors. Each time the FlowerCritter acts it checks to see if any neighboring actor is a Flower. If the neighbor is a Flower, it is plucked from the GridWorld. The FlowerCritter likes to pluck flowers, but the FlowerCritter is conscientious about the environment. Anytime an existing Flower is plucked, FlowerCritter plants a new Flower. The new Flower is planted in a random empty cell. A new Flower object is placed on the grid only if the first random location is available. For the purposes of this question assume that the GridWorld has 10 rows and 10 columns. Write the complete FlowerCritter class, including all instance variables and required methods. Do NOT override the act method.