Array List
Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(new Integer(37)); nums.add(new Integer(3)); nums.add(new Integer(0)); nums.add(1, new Integer(2)); nums.set(0, new Integer(1)); nums.remove(2); System.out.println(nums); What is printed as a result of executing the code segment?
A. [1, 2, 0]
Consider the following code segment. animals.add("dog"); animals.add("cat"); animals.add("snake"); animals.set(2 , "lizard"); animals.add(1 , "fish"); animals.remove(3); System.out.println(animals); What is printed as a result of executing the code segment?
A. [dog, fish, cat]
Assume that myList is an ArrayList that has been correctly constructed and populated with objects. Which of the following expressions produces a valid random index for myList? A. (int) ( Math.random () * myList.size () ) - 1 B. (int) ( Math.random () * myList.size () ) C. (int) ( Math.random () * myList.size () ) + 1 D. (int) ( Math.random () * (myList.size () + 1) ) E. Math.random (myList.size () )
B. (int) ( Math.random () * myList.size () )
Consider the following code segment. ArrayList<Integer> numList = new ArrayList<Integer>(); numList.add(3); numList.add(2); numList.add(1); numList.add(1, 0); numList.set(0, 2); System.out.print(numList); What is printed by the code segment?
B. [2, 0, 2, 1]
Consider the following code segment. ArrayList<String> animals = new ArrayList<>(); animals.add("fox"); animals.add(0, "squirrel"); animals.add("deer"); animals.set(2, "groundhog"); animals.add(1, "mouse"); System.out.println(animals.get(2) + " and " + animals.get(3)); What is printed as a result of executing the code segment?
B. fox and groundhog
Consider the following method, remDups, which is intended to remove duplicate consecutive elements from nums, an ArrayList of integers. For example, if nums contains {1, 2, 2, 3, 4, 3, 5, 5, 6}, then after executing remDups(nums), nums should contain {1, 2, 3, 4, 3, 5, 6}. public static void remDups(ArrayList<Integer> nums) { for (int j = 0; j < nums.size() - 1; j++) { if (nums.get(j).equals(nums.get(j + 1))) { nums.remove(j); j++; } } } The code does not always work as intended. Which of the following lists can be passed to remDups to show that the method does NOT work as intended?
B. {1, 2, 2, 3, 3, 4, 5}
Consider the following code segment. List <String> students = new ArrayList<String>(); students.add("Alex"); students.add("Bob"); students.add("Carl"); for (int k = 0; k < students.size(); k++) { System.out.print(students.set(k, "Alex") + " "); } System.out.println(); for (String str: students) { System.out.print(str + " "); } What is printed as a result of executing the code segment?
C. Alex Bob Carl Alex Alex Alex
Consider the following code segment. ArrayList<Integer> oldList = new ArrayList(); oldList.add(100); oldList.add(200); oldList.add(300); oldList.add(400); ArrayList<Integer> newList = new ArrayList(); newList.add(oldList.remove(1)); newList.add(oldList.get(2)); System.out.println(newList); What, if anything, is printed as a result of executing the code segment?
C. [200, 400]
Consider the following code segment. ArrayList<Double> conditionRating = new ArrayList<Double>(); conditionRating.add(9.84); conditionRating.add(8.93); conditionRating.add(7.65); conditionRating.add(6.24); conditionRating.remove(2); conditionRating.set(2, 7.63); System.out.println(conditionRating); What is printed when this code segment is executed?
C. [9.84, 8.93, 7.63]
Consider the following code segment. ArrayList<String> items = new ArrayList<String>(); items.add("A"); items.add("B"); items.add("C"); items.add(0, "D"); items.remove(3); items.add(0, "E"); System.out.println(items); What is printed as a result of executing the code segment?
C. [E, D, A, B]
Consider the following instance variable and method. Method wordsWithCommas is intended to return a string containing all the words in listOfWords separated by commas and enclosed in braces.For example, if listOfWords contains ["one", "two", "three"], the string returned by the call wordsWithCommas () should be "{one, two, three}". private List<String> listOfWords; public String wordsWithCommas() { String result = " { "; int size Oflist = /* expression */ ; for (int k = 0; k < sizeOfList; k++) { result = result + listOfWords.get(k); if ( /* condition */ ) { result = result + " , "; } } result = result + " } "; return result; } Which of the following can be used to replace /* expression */ and /* condition */ so thatwordsWithCommas will work as intended?
D. / * expression * / / / * condition * / listOfWords.size() / k != sizeOfList - 1
In the following code segment, assume that the ArrayList wordList has been initialized to contain the String values ["apple", "banana", "coconut", "lemon", "orange", "pear"]. int count = 0; for (String word : wordList) { if (word.indexOf("a") >= 0) { count++; } } System.out.println(count); What is printed as a result of executing the code segment?
D. 4
The removeElement method is intended to remove all instances of target from the ArrayList object data passed as a parameter. The method does not work as intended for all inputs. public void removeElement(ArrayList<Integer> data, int target) { for (int j = 0; j < data.size(); j++) { if (data.get(j).equals(target)) { data.remove(j); } } } Assume that the ArrayList object scores and the int variable low_score have been properly declared and initialized. In which of the following cases will the method call removeElement(scores, low_score) fail to produce the intended result?
D. When scores is [8, 8, 4, 3, 3, 6] and low_score is 3
Consider the following code segment. ArrayList<String> numbers = new ArrayList<String>(); numbers.add("one"); numbers.add("two"); numbers.add(0, "three"); numbers.set(2, "four"); numbers.add("five"); numbers.remove(1); Which of the following represents the contents of numbers after the code segment has been executed?
D. ["three", "four", "five"]
In the following code segment, assume that the ArrayList numList has been properly declared and initialized to contain the Integer values [1, 2, 2, 3]. The code segment is intended to insert the Integer value val in numList so that numList will remain in ascending order. The code segment does not work as intended in all cases. int index = 0; while (val > numList.get(index)) { index++; } numList.add(index, val); For which of the following values of val will the code segment not work as intended?
E. 4
The following questions refer to the code in the GridWorld case study. A copy of the code is provided below. Consider the design of a Grasshopper class that extends Bug. When asked to move, a Grasshopper moves to a randomly chosen empty adjacent location that is within the grid. If there is no empty adjacent location that is within the grid, the Grasshopper does not move, but turns 45 degrees to the right without changing its location. Appendix B — Testable API info.gridworld.grid.Location class (implements Comparable) public Location(int r, int c) constructs a location with given row and column coordinates public int getRow() returns the row of this location public int getCol() returns the column of this location public Location getAdjacentLocation(int direction) returns the adjacent location in the direction that is closest to direction public int getDirectionToward(Location target) returns the closest compass direction from this location toward target public boolean equals(Object other) returns true if other is a Location with the same row and column as this location; false otherwise public int hashCode() returns a hash code for this location public int compareTo(Object other) returns a negative integer if this location is less than other, zero if the two locations are equal, or a positive integer if this location is greater than other. Locations are ordered in row-major order. Precondition: other is a Location object. public String toString() returns a string with the row and column of this location, in the format (row, col) Compass directions: public static final int NORTH = 0; public static final int EAST = 90; public static final int SOUTH = 180; public static final int WEST = 270; public static final int NORTHEAST = 45; public static final int SOUTHEAST = 135; public static final int SOUTHWEST = 225; public static final int NORTHWEST = 315; Turn angles: public static final int LEFT = -90; public static final int RIGHT = 90; public static final int HALF_LEFT = -45; public static final int HALF_RIGHT = 45; public static final int FULL_CIRCLE = 360; public static final int HALF_CIRCLE = 180; public static final int AHEAD = 0; info.gridworld.grid.Grid<E> interface int getNumRows() returns the number of rows, or -1 if this grid is unbounded int getNumCols() returns the number of columns, or -1 if this grid is unbounded boolean isValid(Location loc) returns true if loc is valid in this grid, false otherwise Precondition: loc is not null E put(Location loc, E obj) puts obj at location loc in this grid and returns the object previously at that location (or null if the location was previously unoccupied). Precondition: (1) loc is valid in this grid (2) obj is not null E remove(Location loc) removes the object at location loc from this grid and returns the object that was removed (or null if the location is unoccupied) Precondition: loc is valid in this grid E get(Location loc) returns the object at location loc (or null if the location is unoccupied) Precondition: loc is valid in this grid ArrayList<Location> getOccupiedLocations() returns an array list of all occupied locations in this grid ArrayList<Location> getValidAdjacentLocations(Location loc) returns an array list of the valid locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList<Location> getEmptyAdjacentLocations(Location loc) returns an array list of the valid empty locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList<Location> getOccupiedAdjacentLocations(Location loc) returns an array list of the valid occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList<E> getNeighbors(Location loc) returns an array list of the objects in the occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid info.gridworld.actor.Actor class public Actor() constructs a blue actor that is facing north public Color getColor() returns the color of this actor public void setColor(Color newColor) sets the color of this actor to newColor public int getDirection() returns the direction of this actor, an angle between 0 and 359 degrees public void setDirection(int newDirection) sets the direction of this actor to the angle between 0 and 359 degrees that is equivalent to newDirection public Grid<Actor> getGrid() returns the grid of this actor, or null if this actor is not contained in a grid public Location getLocation() returns the location of this actor, or null if this actor is not contained in a grid public void putSelfInGrid(Grid<Actor> gr, Location loc) puts this actor into location loc of grid gr. If there is another actor at loc, it is removed. Precondition: (1) This actor is not contained in a grid (2) loc is valid in gr public void removeSelfFromGrid() removes this actor from its grid. Precondition: this actor is contained in a grid public void moveTo(Location newLocation) moves this actor to newLocation. If there is another actor at newLocation, it is removed. Precondition: (1) This actor is contained in a grid (2) newLocation is valid in the grid of this actor public void act() reverses the direction of this actor. Override this method in subclasses of Actor to define types of actors with different behavior public String toString() returns a string with the location, direction, and color of this actor info.gridworld.actor.Rock class (extends Actor) public Rock() constructs a black rock public Rock(Color rockColor) constructs a rock with color rockColor public void act() overrides the act method in the Actor class to do nothing info.gridworld.actor.Flower class (extends Actor) public Flower() constructs a pink flower public Flower(Color initialColor) constructs a flower with color initialColor public void act() causes the color of this flower to darken Consider the following method that is intended to return an ArrayList of all the locations in grd that contain actors facing in direction dir. public ArrayList<Location> findLocsFacingDir(int dir, Grid<Actor> grd) { ArrayList<Location> desiredLocs = new ArrayList<Location>(); for (Location loc : grd.getOccupiedLocations()) { if ( /* expression */ == dir ) desiredLocs.add(loc); } return desiredLocs; } Which of the following can be used to replace /* expression */ so that findLocsFacingDir will work as intended? A. loc.getDirection() B. getDirection(loc) C. ((Actor) loc).getDirection() D. grd(loc).getDirection() E. grd.get(loc).getDirection()
E. grd.get(loc).getDirection()