Chapter 6: Arrays and ArrayLists

¡Supera tus tareas y exámenes ahora con Quizwiz!

(C) III only

Consider this class: public class BingoCard { private int[] card; /** Default constructor: Creates BingoCard with * 20 random digits in the range 1 - 90. */ public BingoCard() { /* implementation not shown */ } /* Display BingoCard. */ public void display() { /* implementation not shown */ } . . . } A program that simulates a bingo card declares an array of BingoCard. The array has NUMPLAYERS elements, where each element represents the card of a different player. Here is a code segment that creates all the bingo cards in the game: /* declare array of BingoCard */ /* construct each BingoCard */ ------------------------------------------------------------------ Assuming that players has been declared as an array of BingoCard, which of the following is a correct replacement for /* construct each BingoCard */? I. for (BingoCard card : players) card = new BingoCard(); II. for (BingoCard card : players) players[card] = new BingoCard(); III. for (int i = 0; i < players.length; i++) players[i] = new BingoCard(); (A) I only (B) II only (C) III only (D) I and III only (E) I, II, and III

(D) BingoCard[] players = new BingoCard[NUMPLAYERS];

Consider this class: public class BingoCard { private int[] card; /** Default constructor: Creates BingoCard with * 20 random digits in the range 1 - 90. */ public BingoCard() { /* implementation not shown */ } /* Display BingoCard. */ public void display() { /* implementation not shown */ } . . . } A program that simulates a bingo card declares an array of BingoCard. The array has NUMPLAYERS elements, where each element represents the card of a different player. Here is a code segment that creates all the bingo cards in the game: /* declare array of BingoCard */ /* construct each BingoCard */ ------------------------------------------------------------------ Which of the following is a correct replacement for /* declare array of BingoCard */? (A) int[] BingoCard = new BingoCard[NUMPLAYERS]; (B) BingoCard[] players = new int[NUMPLAYERS]; (C) BingoCard[] players = new BingoCard[20]; (D) BingoCard[] players = new BingoCard[NUMPLAYERS]; (E) int[] players = new BingoCard[NUMPLAYERS];

(B) Each book in the bookList array will have its checkout status changed, as intended.

Consider this class: public class Book { private String title; private String author; private boolean checkoutStatus; public Book(String bookTitle, String bookAuthor) { title = bookTitle; author = bookAuthor; checkoutStatus = false; } /** Change checkout status */ public void changeStatus() { checkoutStatus = !checkoutStatus; } //Other methods are not shown. } A client program has this declaration: Book[] bookList = new Book [SOME_NUMBER] ; Suppose bookList is initialized so that each Book in the list has a title, author, and checkout status. The following pice of code is written, whose intent is to change the checkout status of each book in bookList. for (Book b : bookList) b.changeStatus(); Which is true about this code? (A) The bookList array will remain unchanged after execution. (B) Each book in the bookList array will have its checkout status changed, as intended. (C) A NullPointerException may occur. (D) A run-time error will occur because it is not possible to modify objects using a for-each loop. (E) A logic error will occur because it is not possible to modify objects in an array without accessing the indexes of the objects

(C) k-1

Consider this program segment for (int i =2; i<=k; i++) if (arr [i] <someValue) System. out. print ("SMALL"); What is the maximum numbere of times that SMALL can be printed? (A) 0 (B) 1 (C) k-1 (D) k-2 (E) k

(A) An array of primitive data is more efficient to manipulate than an ArrayList of wrapper objects that contain numbers.

Consider writing a program that produces statistics for long lists of numerical data. Which of the following is the best reason to implement each list with an array of int (or double), rather than an ArrayList of Integer (or Double) objects? (A) An array of primitive data is more efficient to manipulate than an ArrayList of wrapper objects that contain numbers. (B) Insertion of new elements into a list is easier to code for an array than for an ArrayList. (C) Removal of elements from a list is easier to code for an array than for an ArrayList. (D) Accessing individual elements in the middle of a list is easier for an array than for an ArrayList. (E) Accessing all the elements is more efficient in an array than in an ArrayList..

(D) If any particular text file is unexpectedly long, the ArrayList will automatically be resized. The array, by contrast, may go out of bounds.

Consider writing a program that reads the lines of any text file into a sequential list of lines. Which of the following is a good reason to implement the list with an ArrayList of String objects rather than an array of String objects? (A) The get and set methods of the ArrayList are mire convenient than the [] notation for arrays. (B) The size method of ArrayList provides instant access to the length of the list. (C) An ArrayList can contain objects of any type, which leads to greater generality. (D) If any particular text file is unexpectedly long, the ArrayList will automatically be resized. The array, by contrast, may go out of bounds. (C) The String methods are easier to us with an ArrayList than with an array.

(A) Transaction[] listOfSales = new Transaction[NUMSALES];

Refer to the Ticket and Transaction classes below. public class Ticket { private String row; private int seat; private double price; //constructor public Ticket(String aRow, int aSeat, double aPrice) { row = aRow; seat = aSeat; price = aPrice; } //accessors getRow(), getSeat(), and getPrice() . . . } public class Transaction { private int numTickets; private Ticket[] tickList; //constructor public Transaction(int numTicks) { numTickets = numTicks; tickList = new Ticket[numTicks]; String theRow; int theSeat; double thePrice; for (int i = 0; i < numTicks; i++) { <read user input for theRow, theSeat, and thePrice> . . . /* more code */ } } /** @return total amount paid for this transaction */ public double totalPaid() { double total = 0.0; /* code to calculate amount */ return total; } } ------------------------------------------------------------------ Suppose it is necessary to keep a list of all ticket transactions. Assuming that there are NUMSALES transactions, a suitable declaration would be (A) Transaction[] listOfSales = new Transaction[NUMSALES]; (B) Transaction[] listOfSales = new Ticket[NUMSALES]; (C) Ticket[] listOfSales = new Transaction[NUMSALES]; (D) Ticket[] listOfSales = new Ticket[NUMSALES]; (E) Transaction[] Ticket = new listOfSales[NUMSALES];

(B) tickList[i] = new Ticket(theRow, theSeat, thePrice);

Refer to the Ticket and Transaction classes below. public class Ticket { private String row; private int seat; private double price; //constructor public Ticket(String aRow, int aSeat, double aPrice) { row = aRow; seat = aSeat; price = aPrice; } //accessors getRow(), getSeat(), and getPrice() . . . } public class Transaction { private int numTickets; private Ticket[] tickList; //constructor public Transaction(int numTicks) { numTickets = numTicks; tickList = new Ticket[numTicks]; String theRow; int theSeat; double thePrice; for (int i = 0; i < numTicks; i++) { <read user input for theRow, theSeat, and thePrice> . . . /* more code */ } } /** @return total amount paid for this transaction */ public double totalPaid() { double total = 0.0; /* code to calculate amount */ return total; } } ------------------------------------------------------------------ Which of the following correctly replaces /* more code */ in the Transaction constructor to initialize the tickList array? (A) tickList[i] = new Ticket(getRow(), getSeat(), getPrice()); (B) tickList[i] = new Ticket(theRow, theSeat, thePrice); (C) tickList[i] = new tickList(getRow(), getSeat(), getPrice()); (D) tickList[i] = new tickList(theRow, theSeat, thePrice); (E) tickList[i] = new tickList(numTicks);

(C) for (Ticket t : tickList) total += t.getPrice();

Refer to the Ticket and Transaction classes below. public class Ticket { private String row; private int seat; private double price; //constructor public Ticket(String aRow, int aSeat, double aPrice) { row = aRow; seat = aSeat; price = aPrice; } //accessors getRow(), getSeat(), and getPrice() . . . } public class Transaction { private int numTickets; private Ticket[] tickList; //constructor public Transaction(int numTicks) { numTickets = numTicks; tickList = new Ticket[numTicks]; String theRow; int theSeat; double thePrice; for (int i = 0; i < numTicks; i++) { <read user input for theRow, theSeat, and thePrice> . . . /* more code */ } } /** @return total amount paid for this transaction */ public double totalPaid() { double total = 0.0; /* code to calculate amount */ return total; } } ------------------------------------------------------------------ Which represents correct /* code to calculate amount */ in the totalPaid method? (A) for (Ticket t : tickList) total += t.price; (B) for (Ticket t : tickList) total += tickList.getPrice(); (C) for (Ticket t : tickList) total += t.getPrice(); (D) Transaction T; for (Ticket t : T) total += t.getPrice(); (E) Transaction T; for (Ticket t : T) total += t.price;

(D) System.out.println(a.getName());

Refer to the following classes. public class Address { private String name; private String street; private String city; private String state; private String zip; // constructors . . . //accessors public String getName() { return name; } public String getStreet() { return street; } public String getCity() { return city; } public String getState() { return state; } public String getZip() { return zip; } } public class Student { private int idNum; private double gpa; private Address address; // constructors . . . //accessors public Address getAddress() { return address; } public getIdNum() { return idNum; } public getGpa() { return gpa; } } ------------------------------------------------------------------ A client method has this declaration, followed by code to initialize the list: Address[] list = new Address[100]; Here is a code segment to generate a list of names only. for (Address a : list) /* line of code */ Which is a correct /* line of code */? (A) System.out.println(Address[i].getName()); (B) System.out.println(list[i].getName()); (C) System.out.println(a[i].getName()); (D) System.out.println(a.getName()); (E) System.out.println(list.getName());

(C) System.out.println(student .getAddress().getName());

Refer to the following classes. public class Address { private String name; private String street; private String city; private String state; private String zip; // constructors . . . //accessors public String getName() { return name; } public String getStreet() { return street; } public String getCity() { return city; } public String getState() { return state; } public String getZip() { return zip; } } public class Student { private int idNum; private double gpa; private Address address; // constructors . . . //accessors public Address getAddress() { return address; } public getIdNum() { return idNum; } public getGpa() { return gpa; } } ------------------------------------------------------------------ A client method has this declaration: Student[] allStudents = new Student[NUM_STUDS]; //NUM_STUDS is an int constant Here is a code segment to generate a list of Student names only. (You may assume that allStudents has been initialized.) for(Student student : allStudents) /* code to print list of names */ Which is a correct replacement for /* code to print list of names */? (A) System.out.println(allStudents.getName()); (B) System.out.println(student.getName()); (C) System.out.println(student.getAddress().getName()); (D) System.out.println(allStudents.getAddress().getName()); (E) System.out.println(student[i].getAddress().getName());

(E) II and III only

Refer to the following classes. public class Address { private String name; private String street; private String city; private String state; private String zip; // constructors . . . //accessors public String getName() { return name; } public String getStreet() { return street; } public String getCity() { return city; } public String getState() { return state; } public String getZip() { return zip; } } public class Student { private int idNum; private double gpa; private Address address; // constructors . . . //accessors public Address getAddress() { return address; } public getIdNum() { return idNum; } public getGpa() { return gpa; } } ------------------------------------------------------------------ Here is the method that locates the Student with the highest idNum: /** Precondition: Array stuArr of Student is initialized. * @return Student with highest idNum */ public static Student locate(Student[] stuArr) { /* method body */ } Which of the following could replace /* method body */ so that the method works as intended? I. int max = stuArr[0].getIdNum(); for (Student student : stuArr) if (student.getIdNum() > max) { max = student.getIdNum(); return student; } return stuArr[0]; II. Student highestSoFar = stuArr[0]; int max = stuArr[0].getIdNum(); for (Student student : stuArr) if (student.getIdNum > max) { max = student.getIdNum(); highestSoFar = student; } return highestSoFar; III. int maxPos = 0; for(int i = 1; i < getIdNum() > stuArr.length; i++) if (stuArr[i]. getIdNum() > stuArr[maxPos].getIdNum()) maxPos = i; return stuArr[maxPos]; (A) I only (B) II only (C) III only (D) I and III only (E) II and III only

(B) II only

Refer to the following classes. public class Address { private String name; private String street; private String city; private String state; private String zip; // constructors . . . //accessors public String getName() { return name; } public String getStreet() { return street; } public String getCity() { return city; } public String getState() { return state; } public String getZip() { return zip; } } public class Student { private int idNum; private double gpa; private Address address; // constructors . . . //accessors public Address getAddress() { return address; } public getIdNum() { return idNum; } public getGpa() { return gpa; } } ------------------------------------------------------------------ The following code segment is to print out a list of addresses: for (Address addr : list) { /* more code */ } Which is a correct replacement for /* more code */? I. System.out.println(list[i].getName()); System.out.println(list[i].getStreet()); System.out.println(list[i].getCity() + ", "); System.out.println(list[i].getState() + " "); System.out.println(list[i].getZip()); II. System.out.println(addr.getName()); System.out.println(addr.getStreet()); System.out.println(addr.getCity() + ", "); System.out.println(addr.getState() + " "); System.out.println(addr.getZip()); III. System.out.println(addr); (A) I only (B) II only (C) III only (D) I and II only (E) I, II, and III

(E) A run-time error will occur

Refer to the following code segment. You may assume that arr is an array of integers. int sum = arr [0], i = 0; while (i < arr. length) { i++; sum += arr [i]; } Which of the following will be the result of executing the segment? (A) Sum of arr [0]. arr[1],..., arr [arr.length-1] will be stored in sum (B) Sum of arr [1], arr[2],..., arr [arr.length-1] will be stored in sum (C) Sum of arr [0], arr[1],..., arr [arr.length] will be stored in sum (D) An infinite loop will occur (E) A run-time error will occur

(A) 6, 4, 2

Refer to the following code segment. You may assume that array arr1 contains elements arr1[1],..., arr1[N-1], where N = arr1.length. int count = 0 ; for (int i =0; i< N; i++) if (arr1 [i] != 0) { arr1[count] = arr1[i] ; count ++; } int [] arr2 = new int [count] ; for (int i=0 ; i<count; i++) arr2[i] = arr1[i]; If array arr1 initially contains the elements 0, 6, 0, 4, 0, 0, 2 in this order, what will arr2 contain refer execution of the code segment? (A) 6, 4, 2 (B) 0, 0, 0, 0, 6, 4, 2 (C) 6, 4, 2, 4, 0, 0, 2 (D) 0, 6, 0, 4, 0, 0, 2 (E) 6, 4, 2, 0, 0, 0, 0

(A) v[0] . .v[vIndex - 1] and w[0] . .w[wIndex - 1] contain no common value, vIndex <= N and wIndex <= M.

Refer to the method match below: /** @param v an array of int sorted in increasing order * @param w an array of int sorted in increasing order * @param N the number of elements in array v * @param M the number of elements in array v * @return true if there is an integer k that occurs * in both arrays; otherwise returns false * Precondition: * v[0] . .v[N-1] and W[0] . .w[M-1] initialized with integers. * v[0] < v[1] < . . < v{N-1] and w[0] < w[1] < . . < w[M-1]. */ public static boolean match(int[] v, int[] w, int N, int M) { int vIndex = 0, wIndex = 0; while (vIndex < N && wIndex < M) { if (v[vIndex] == w[wIndex]) return true; else if (v[vIndex] < w[wIndex]) vIndex++; else (v[vIndex] > w[wIndex]) wIndex++; } return false; } Assuming that the method has not been exited, which assertion is true at the end of every execution of the while loop? (A) v[0] . .v[vIndex - 1] and w[0] . .w[wIndex - 1] contain no common value, vIndex <= N and wIndex <= M. (B) v[0] . .v[vIndex] and w[0] . .w[wIndex] contain no common value, vIndex <= N and wIndex <= M. (C) v[0] . .v[vIndex - 1] and w[0] . .w[wIndex - 1] contain no common value, vIndex <= N-1 and wIndex <= M-1. (D) v[0] . .v[vIndex] and w[0] . .w[wIndex] contain no common value, vIndex <= N and wIndex <= M. (E) v[0] . .v[N - 1] and w[0] . .w[M - 1] contain no common value, vIndex <= N and wIndex <= M.

(B) II only

The following code fragment is intended to find the smallest value in arr [0] ... arr [n-1]. /** Precondition: * -arr [0] ... arr [n-1] initialized with integers. * -arr is an array, arr.length = n * Postcondition: min = smallest value in arr [0] ...arr [n-1] */ int min = arr [0]; int i = 1; while (i < n) { i++; if (arr [i] < min) min = arr [i] ; } This code is incorrect. For the segment to work as intended, which of the following modifications could be made? I. Change the line int i = 1 ; to int i = 0 ; Make no other changes II. Change the body of the while loop to { if (arr [i] < min) min = arr [i] ; i++; } Make no other changes. III. Change the test for the while loop as follows: while (i <= n) Make no other changes. (A) I only (B) II only (C) III only (D) I and II only (E) I, II, and III

(C) Whenever arr contains at least one negative integer

The following program segment is intended to find the index of the first negative integer in arr [0] ... arr [ N-1], where arr is an array of N integers. int i = 0; while (arr [i] >= 0) { i++; } location = i ; This segment will work as intended (A) Always (B) Never (C) Whenever arr contains at least one negative integer (D) Whenever arr contains at least one nonnegative integer (E) Whenever arr contains no negative integers

(C) 1 3

What will be output from the following code segment, assuming it is in the same class as the doSomething method? int[] arr = {1, 2, 3, 4} ; doSoething (arr) ; System.out.print (arr[1] + " ") ; System.out.print (arr[3] ) ; . . . public void doSomething (int[] list) { int[] b = list; for( int i = 0 ; i<b. length ; i++) b[i] = i ; } (A) 0 0 (B) 2 4 (C) 1 3 (D) 0 2 (E) 0 3

(E) I, II, and III

Which of the following correctly initializes an array arr to contain four elements each with value 0? I. int [] arr = {0, 0, 0, 0}; II. int [] arr = new int [4]; III. int [] arr = new int [4]; for (int i = 0; i < arr. length; i ++) arr [i] = 0; (A) I only (B) III only (C) I and III only (D) II and III only (E) I, II, and III


Conjuntos de estudio relacionados

Exemplar Chronic Obstructive Pulmonary Disease

View Set

MKTG 4280 Ch. 15 McGraw-Hill Connect

View Set

Final exam chapters 40,42,43,44,47 ?'s

View Set