• Home
  • Textbooks
  • Barron's AP Computer Science A
  • Arrays and Array Lists

Barron's AP Computer Science A

Roselyn Teukolsky

Chapter 6

Arrays and Array Lists - all with Video Answers

Educators


Chapter Questions

01:39

Problem 1

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 ; \text { i }<\text { arr. length; } i++)$ $\operatorname{arr}[\mathrm{i}]=0$
(A) I only
(B) III only
(C) I and III only
(D) II and III only
(E) I, II, and III

Manik Pulyani
Manik Pulyani
Numerade Educator
01:38

Problem 2

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.

Manik Pulyani
Manik Pulyani
Numerade Educator
01:39

Problem 3

Refer to the following code segment. You may assume that arr is an array of int values.
int sum $=\operatorname{arr}[0], \quad i=0$
while $(\mathrm{i}<\text { arr. } \text { length })$
{
$i++$
sum $+=\operatorname{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], \ldots,$ 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.}

Manik Pulyani
Manik Pulyani
Numerade Educator
01:37

Problem 4

Refer to the following code segment. You may assume that array arr 1 contains elements arr1 $[0], \operatorname{arr} 1[1], \ldots, \operatorname{arr} 1[\mathrm{N}-1],$ where $\mathrm{N}=\operatorname{arr} 1 .$ length.
int count $=0$ for (int $i=0 ; \quad i < N ; \quad i++)$
if $(\operatorname{arr} 1[i] \quad !=0)$
{
$\operatorname{arr} 1[\text { count }]=\operatorname{arr} 1[i]$
count++; 2 }
int []$\text { arr } 2=\text { new int [count }]$ for (int $i=0 ; \text { i }< \text { count } ; \quad i++)$ $\operatorname{arr} 2[i]=\operatorname{arr} 1[i]$
If array arr 1 initially contains the elements 0,6,0,4,0,0,2 in this order, what will arr2 contain after 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

Manik Pulyani
Manik Pulyani
Numerade Educator
01:52

Problem 5

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

Manik Pulyani
Manik Pulyani
Numerade Educator
01:39

Problem 6

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

Manik Pulyani
Manik Pulyani
Numerade Educator
01:38

Problem 7

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 ArrayList are more 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.
(E) The String methods are easier to use with an ArrayList than with an array.

Manik Pulyani
Manik Pulyani
Numerade Educator
00:44

Problem 8

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 number types 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.

Ernest Castorena
Ernest Castorena
Numerade Educator
01:55

Problem 9

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 $* 1$
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());

Ernest Castorena
Ernest Castorena
Numerade Educator
01:39

Problem 10

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.print(list[i].getcity() + ", "); System.out.print(list[i].getState() $+"$ "); System.out.println(list [i].getzip());
II System.out.println(addr.getName()); System.out.println(addr.getStreet()); System.out.print(addr.getcity() + ", "); System.out.print(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

Aaron Goree
Aaron Goree
Numerade Educator
02:44

Problem 11

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()) ;

Akash M
Akash M
Numerade Educator
01:13

Problem 12

Here is a method that locates the Student with the highest idNum:
/** Precondition: Array stuArr of Student is initialized.
* Oreturn 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]$\cdot$ get IdNum () for (Student student : stuArr) if (student.getIdNum ()$ > \max$ )
{
$\max =$ student. get IdNum ()
return student;
}
return stuArr [0] ;
II Student highestSoFar = stuArr [0] ; int $\max =$ stuArr [0]$\cdot$ get $\mathrm{IdNum}()$
for (Student student : stuArr) if (student. getIdNum ()$ > \max )$
{
$\max =$ student. get IdNum ()
highestSoFar $=$ student }
return highestSoFar;
III int maxpos $=0$ for (int $i=1 ; \text { i } < \text { stuArr. } \text { length; } i++)$ if (stuArr [i].getIdNum() > stuArr [maxPos].getIdNum()) maxpos $=\mathrm{i}$
return stuArr [maxpos];
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only

SS
Sarvesh Somasundaram
Numerade Educator
02:23

Problem 13

Which of the following correctly replaces / * more code*/ in the Transaction constructor to initialize the tickList array?
(A) tickList $[\mathrm{i}]=$ new Ticket (getRow(), getSeat(), getPrice())
(B) tickList $[\mathrm{i}]=$ new Ticket(theRow, theSeat, thePrice)
(C) tickList[i] = new tickList(getRow(), getSeat(), getPrice());
(D) tickList $[\mathrm{i}]=$ new tickList(theRow, theSeat, thePrice)
(E) tickList [i] = new tickList(numTicks) ;

Ben Clark
Ben Clark
Numerade Educator
01:24

Problem 14

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 \cdot$ getPrice ()
(D) Transaction T; for (Ticket t : T) total $+=\mathrm{t} \cdot$ getPrice ()
(E) Transaction T; for (Ticket t : T) total $+=t .$ price

Manik Pulyani
Manik Pulyani
Numerade Educator
01:18

Problem 15

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];

James Kiss
James Kiss
Numerade Educator
01:37

Problem 16


The following code fragment is intended to find the smallest value in $\operatorname{arr}[0] \ldots \operatorname{arr}[\mathrm{n}-1]$
/** Precondition:
$* \quad-$ arr is an array, arr.length $=\mathrm{n}$ $* \quad-\operatorname{arr}[0] \ldots \operatorname{arr}[n-1]$ initialized with integers.
* Postcondition: min = smallest value in arr [0] \ldots.arr[n-1].
$* /$
int $\min =\operatorname{arr}[0]$
int $i=1$ while $(i < n)$
{
$i++i$
if $(\operatorname{arr}[i] < \min )$
$\min =\operatorname{arr}[i]$
}
This code is incorrect. For the segment to work as intended, which of the follow-
ing 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
\{
$$
\begin{array}{l}
\text { if }(\operatorname{arr}[\mathrm{i}]<\min ) \\
\quad \min =\operatorname{arr}[\mathrm{i}]
\end{array}
$$
$1++$
\}
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

Manik Pulyani
Manik Pulyani
Numerade Educator
03:06

Problem 17

Refer to method match below:
/** Oparam v an array of int sorted in increasing order
$*$ Qparam $w$ an array of int sorted in increasing order
$*$ Oparam $\mathrm{N}$ the number of elements in array $\mathrm{v}$
$*$ Qparam $\mathrm{M}$ the number of elements in array $\mathrm{w}$
$*$ Qreturn true if there is an integer $\mathrm{k}$ that occurs
$*$ in both arrays; otherwise returns false
$* \quad$ Precondition:
* $\quad v[0]$... $v[N-1]$ and $w[0] \ldots w[M-1]$ initialized with integers.
$* \quad v[0] < v[1] < \ldots < v[N-1]$ and $w[0] < w[1] < \ldots < w[M-1]$

$* /$
public static boolean match(int [] v, int [] w, int $N$, int $M$ )
\{
int vindex $=0,$ windex $=0$ while (vIndex $<\mathrm{N} \& \&$ wIndex $<\mathrm{M}$ )
{
if $(\mathrm{v}[\mathrm{v} \operatorname{Index}]==w[\mathrm{w} \operatorname{Index}])$
return true; else if $(\mathrm{v}[\mathrm{v} \text { Index }] < \mathrm{w}[\mathrm{w} \text { Index }])$
vIndex++;
else wIndext+; }
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 $\leq \mathrm{N}$ and $\mathrm{w}$ Index $\leq \mathrm{M}$.
(B) v [0] .. v [vIndex] and w [0] ...w [wIndex] contain no common value, vIndex $\leq \mathrm{N}$ and $\mathrm{w}$ Index $\leq \mathrm{M}$
(C) v [0] .. v [vIndex-1] and w [0] ...w [wIndex-1] contain no common value, vIndex $\leq \mathrm{N}-1$ and $\mathrm{wIndex} \leq \mathrm{M}-1$
(D) v[0] ..v [vIndex] and w[0] ...w[wIndex] contain no common value, vIndex $\leq \mathrm{N}-1$ and $\mathrm{wIndex} \leq \mathrm{M}-1$
(E) v [0] .. v [N-1] and w [0] ...w [M-1] contain no common value, vIndex $\leq \mathrm{N}$ and $\mathrm{w}$ Index $\leq \mathrm{M}$.

Clarissa Noh
Clarissa Noh
Numerade Educator
29:58

Problem 18

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()
$\{\text { checkoutstatus }=! \text { 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 piece 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 the 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.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
01:39

Problem 19

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] ;

Manik Pulyani
Manik Pulyani
Numerade Educator
01:46

Problem 20

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 ; \text { i < players. length; } i++)$ players $[\mathrm{i}]=$ new Bingocard ()
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) I, II, and III

Manik Pulyani
Manik Pulyani
Numerade Educator
01:39

Problem 21

Which declaration will cause an error?
I List < String > stringList = new ArrayList < String > ();
II List < int > intList = new ArrayList < int > ();
III ArrayList < String > compList = new ArrayList < String > ();
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only

Manik Pulyani
Manik Pulyani
Numerade Educator
04:52

Problem 22

Consider these declarations:
List < String > strList = new ArrayList < String >(); String $\mathrm{ch}="$ "; Integer $\mathrm{intOb}=$ new Integer (5)
Which statement will cause an error?
(A) strList.add(ch);
(B) strList.add(new String("handy andy"));
(C) strList.add(int0b.toString());
(D) strList.add(ch $+8)$
(E) strList.add(int0b + 8);

Samriddhi Singh
Samriddhi Singh
Numerade Educator
01:14

Problem 23

Let list be an ArrayList < Integer > containing these elements:
$$
257601
$$
Which of the following statements would not cause an error to occur? Assume that each statement applies to the given list, independent of the other statements.
(A) Object ob = list.get(6);
(B) Integer int0b = list.add(3.4);
(C) list.add(6, 9);
(D) Object $x=$ list.remove (6)
(E) Object $y=$ list.set (6,8)

Aditya Modi
Aditya Modi
Monroe Township High School
03:23

Problem 24

Refer to method insert below:
/** Oparam list an ArrayList of String objects
$*$ Qparam element a String object
$*$ Precondition: list contains String values sorted
$*$
in decreasing order.
* Postcondition: element inserted in its correct position in list.
$* /$
public void insert(List<String> list, String element)
{
int index $=0$ while (element.compareTo(list.get(index)) $ < 0$ ) index++; list.add(index, element);
}
Assuming that the type of element is compatible with the objects in the list, which is a true statement about the insert method?
(A) It works as intended for all values of element.
(B) It fails for all values of element.
(C) It fails if element is greater than the first item in 1 ist and works in all other cases.
(D) It fails if element is smaller than the last item in 1 ist and works in all other
cases.
(E) It fails if element is either greater than the first item or smaller than the last item in 1 ist and works in all other cases.

Tarandeep Singh
Tarandeep Singh
Numerade Educator
01:55

Problem 25

Consider the following code segment, applied to list, an ArrayList of Integer values.
int $\operatorname{len}=1$ ist.size ()
for (int $i=0 ; i < \operatorname{len} ; \quad i++)$
{
Iist.add $(\mathrm{i}+1, \text { new Integer }(\mathrm{i}))$
Object $x=$ list.set $(i, \text { new Integer }(i+2))$
}
If 1 ist is initially 6 1 $8,$ what will it be following execution of the code segment?
(A) 234218 (A) 342 .
(B) 234622018
(C) 234012
(D) 234618
(E) 2332

Ernest Castorena
Ernest Castorena
Numerade Educator
01:07

Problem 26

Here is the getTotal method from the Purse class:
/** Oreturn the total value of coins in purse * / public double getTotal ()
{
double total $=0$
$/ *$ more code $* /$ return total;
}
(A) for (coin c : coins)
{
$c=\operatorname{coins} \cdot \operatorname{get}(i)$
total $+=c \cdot$ getValue ()
}
(B) for (coin c : coins)
{
Coin value $=\mathrm{c} \cdot$ getValue () total $+=$ value
}
(C) for (Coin c : coins)
{
Coin $c=$ coins $\cdot$ get $(i)$ total $+=\mathrm{c} \cdot$ getValue ()
}
(D) for (coin c : coins)
{
total $+=$ coins $\cdot$ get Value ()
}
(E) for (coin c : coins)
{
" total $+=\mathrm{c} \cdot$ get Value ()
}
"

Dale Sanford
Dale Sanford
Numerade Educator
01:45

Problem 27

Two coins are said to match each other if they have the same name or the same value. You may assume that coins with the same name have the same value and coins with the same value have the same name. A boolean method find is added
to the Purse class:
/** Qreturn true if the purse has a coin that matches acoin,
$*$ false otherwise
$*$
public boolean find(Coin aCoin)
{
for (Coin $c: \text { coins })$
{
/* code to find match *
}
2
return false;
}
Which is a correct replacement for /* code to find match */?
I if (c.equals(acoin)) return true;
II if $((c . \text { get Name }()) \text { . equals (acoin.getName }())$ ) return true;

III if $((c . \text { getvalue }()) \text { . equals(acoin.getvalue }())$ ) return true;
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

Florencia Cuzmar
Florencia Cuzmar
Numerade Educator
00:22

Problem 28

Which of the following initializes an $8 \times 10$ matrix with integer values that are perfect squares? (0 is a perfect square.)
I int [][] mat $=$ new int [8][10]
II int $[\text { ] }[] \text { mat }=\text { new int }[8][10]$ for (int $r=0 ; r<\text { mat. } \text { length } ; \quad r++)$ for $(\text { int } c=0 ; c < \text { mat }[r] . \text { length } ; \quad c++)$ mat $[\mathrm{r}][\mathrm{c}]=\mathrm{r} * \mathrm{r}$
III int [][] mat $=$ new int [8][10] for (int $c=0 ; c < \text { mat }[r] . \text { length } ; \quad c++)$ for (int $r=0 ; r < \text { mat. } \text { length } ; \quad r++)$
$$
\operatorname{mat}[r][c]=c * c

$$
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

Amy Jiang
Amy Jiang
Numerade Educator
00:33

Problem 29

Consider a class that has this private instance variable:
private int [][] mat
The class has the following method, alter.
public void alter(int c)
{
for (int $i=0 ; \quad i < \text { mat. } \operatorname{length} ; \quad i++)$
for (int $j=c+1 ; j < \text { mat }[0] . \text { length } ; j++)$ mat $[i][j-1]=$ mat $[i][j]$
}
If a $3 \times 4$ matrix mat is
1357
2468
3579
then alter(1) will change mat to
$( A.) \ \begin{array}{llll}1 & 5 & 7 & 7 \\ 2 & 6 & 8 & 8 \\ 3 & 7 & 9 & 9\end{array}$
$( B.) \begin{array}{lll}1 & 5 & 7 \\ 2 & 6 & 8 \\ 3 & 7 & 9\end{array}$
$( C.)\begin{array}{llll}1 & 3 & 5 & 7 \\ 3 & 5 & 7 & 9\end{array}$
$( D.)\begin{array}{llll}1 & 3 & 5 & 7 \\ 3 & 5 & 7 & 9 \\ 3 & 5 & 7 & 9\end{array}$
$( E.)\begin{array}{llll}1 & 7 & 7 & 7 \\ 2 & 8 & 8 & 8 \\ 3 & 9 & 9 & 9\end{array}$

Thomas Emment
Thomas Emment
Numerade Educator
03:16

Problem 30

Consider the following method that will alter the matrix mat:
/** Oparam mat the initialized matrix
$*$ Qparam row the row number
$* /$
public static void matStuff(int [] [] mat, int row)
{
int numcols $=$ mat $[0] .$ length
for (int $\mathrm{col}=0 ;$ col $< $ numcols $;$ colt $+$ ) mat $[\mathrm{row}][\mathrm{col}]=\mathrm{row}$
}
Suppose mat is originally
$$
\begin{array}{llll}
1 & 4 & 9 & 0 \\
2 & 7 & 8 & 6 \\
5 & 1 & 4 & 3
\end{array}
$$
After the method call matStuff(mat,2), matrix mat will be
$(A.) \ \begin{array}{llll}1 & 4 & 9 & 0 \\ 2 & 7 & 8 & 6 \\ 2 & 2 & 2 & 2\end{array}$
$(B.) \ \begin{array}{llll}1 & 4 & 9 & 0 \\ 2 & 2 & 2 & 2 \\ 5 & 1 & 4 & 3\end{array}$
$(C.) \ \begin{array}{llll}2 & 2 & 2 & 2 \\ 2 & 2 & 2 & 2 \\ 2 & 2 & 2 & 2\end{array}$
$(D.) \ \begin{array}{llll}1 & 4 & 2 & 0 \\ 2 & 7 & 2 & 6 \\ 5 & 1 & 2 & 3\end{array}$
$(E.) \ \begin{array}{llll}1 & 2 & 9 & 0 \\ 2 & 2 & 8 & 6 \\ 5 & 2 & 4 & 3\end{array}$

Faizanullah Kazmi
Faizanullah Kazmi
Numerade Educator
01:00

Problem 31

Assume that a square matrix mat is defined by
int [][] mat $=$ new int $[\mathrm{SIZE}][\mathrm{SIZE}]$ $/ / \mathrm{SIZE}$ is an integer constant $>=2$
What does the following code segment do?
What does the following code segment do?
$$
\begin{array}{l}
\text { for (int }\mathrm{i}=0 ; \mathrm{i}<\mathrm{SIZE}-1 ; \mathrm{i}++) \\
\begin{aligned}
\text { for }(\text { int } \mathrm{j}=0 ; \mathrm{j}<\mathrm{SIZE}-\mathrm{i}-1 ; \mathrm{j}++) & \\
\text { swap(mat, }\mathrm{i}, \mathrm{j}, \mathrm{SIZE}-\mathrm{j}-1, \mathrm{SIZE}-\mathrm{i}-1)
\end{aligned}
\end{array}
$$
You may assume the existence of this swap method:
/** Interchange mat [a] [b] and mat [c] [d]. */ public void swap(int [][]$\text { mat, int } a, \text { int } b, \text { int } c, \text { int } d)$
(A) Reflects mat through its major diagonal. For example,
2 6
4 3 $\rightarrow$ 2 4
6 3
(B) Reflects mat through its minor diagonal. For example,2 6
4 3 $\rightarrow$ 3 6
4 2
(C) Reflects mat through a horizontal line of symmetry. For example,
2 6
4 3 $\rightarrow$ 4 3
26
(D) Reflects mat through a vertical line of symmetry. For example,
2 6
4 3 $\rightarrow$ 2 6
3 4
(E) Leaves mat unchanged.

WZ
Wen Zheng
Numerade Educator
01:10

Problem 32

Consider a class MatrixStuff that has a private instance variable:
private int [] [] mat;
Refer to method alter below that occurs in the MatrixStuff class. (The lines are numbered for reference.)
Line 1: /** Oparam mat the matrix initialized with integers
Line $2: \quad * \quad$ Oparam $c$ the column to be removed
Line 3: $*$ Postcondition:
Line 4: $* \quad-$ Column c has been removed.
Line 5: * - The last column is filled with zeros.
Line 6: $* /$
Line $7: \text { public void alter(int }[][] \text { mat, int } c)$
Line 8: {
Line $9: \quad$ for $(\text { int } i=0 ; \quad i < \text { mat. } \text { length } ; \quad i++)$
Line 10: $\quad$ for $(\text { int } j=c ; \quad j<\text { mat }[0] . \text { length } ; \quad j++)$
Line 11:
$\operatorname{mat}[i][j]=\operatorname{mat}[i][j+1]$
Line $12: \quad / /$ code to insert zeros in rightmost column
Line 13:
Line $14: \quad\}$
The intent of the method alter is to remove column c. Thus, if the input matrix mat is
$$\begin{array}{llll}
2 & 6 & 8 & 9 \\
1 & 5 & 4 & 3 \\
0 & 7 & 3 & 2
\end{array}$$
the method call alter(mat, 1 ) should change mat to
$$\begin{array}{llll}
2 & 8 & 9 & 0 \\
1 & 4 & 3 & 0 \\
0 & 3 & 2 & 0
\end{array}$$
The method does not work as intended. Which of the following changes will correct the problem?
I Change line 10 to
for (int $j=c ; j < \text { mat }[0] . \text { length }-1 ; j++)$ and make no other changes.
II Change lines 10 and 11 to
for $(\text { int } j=c+1 ; j < \text { mat }[0] \text { . length } ; j++)$ $\operatorname{mat}[i][j-1]=\operatorname{mat}[i][j]$
and make no other changes.
III Change lines 10 and 11 to
for $(\text { int } j=\text { mat }[0] \text { . length }-1 ; j > c ; j--)$ $\operatorname{mat}[i][j-1]=\operatorname{mat}[i][j]$
and make no other changes.
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

Varsha Aggarwal
Varsha Aggarwal
Numerade Educator
05:03

Problem 33

This question refers to the following method:
public static boolean isThere(String[] [] mat, int row, int col, String symbol?
{
boolean yes; int $i,$ count $=0$ for $(i=0 ; \quad i < \operatorname{SIZE} ; \quad i++)$
if (mat [i] [col] .equals (symbol)) count++; yes $=(\text { count }==\mathrm{SIZE})$
count $=0$ for $(i=0 ; i< \operatorname{SIZE} ; i++)$
if (mat [row] [i]. equals (symbol)) count++ ; return (yes II count $==\mathrm{SIZE})$
}

Now consider this code segment:
public final int $\mathrm{SIZE}=8$ String [][] mat $=$ new String $[\mathrm{SIZE}][\mathrm{SIZE}]$
Which of the following conditions on a matrix mat of the type declared in the code segment will by itself guarantee that
isThere(mat, 2, 2, "\$")
will have the value true when evaluated?
I The element in row 2 and column 2 is "\$" II All elements in both diagonals are "\$" III All elements in column 2 are " $\$ "$
(A) I only
(B) III only
(C) I and II only
(D) I and III only
(E) II and III only

Mirza  Aslam Beig
Mirza Aslam Beig
Numerade Educator
04:18

Problem 34

The method changeNegs below should replace every occurrence of a negative integer in its matrix parameter with 0 .
/** Oparam mat the matrix
$*$ Precondition: mat is initialized with integers.
$*$ Postcondition: All negative values in mat replaced with 0 .
$* /$
public static void changeNegs(int [] [] mat)
{
/* code * /
}
Which is correct replacement for $/ *$ code $* / ?$
$$
\begin{array}{l}
\text { I for (int }r=0 ; r < \text { mat. } \text { length; } r++) \\
\qquad \begin{aligned}
\text { for (int }c=0 ; c < m \text { at }[r] . \text { length; } c++) \\
\text { if }(\operatorname{mat}[r][c]& < 0) \\
& \operatorname{mat}[r][c]=0
\end{aligned}
\end{array}
$$
II for (int $c=0 ; c < \text { mat }[0] . \text { length; } c++)$ for (int $r=0 ; r < \text { mat. } \text { length } ; \quad r++)$
$$
\begin{aligned}
\text { if } &(\operatorname{mat}[r][c] < 0) \\
& \text { mat }[r][c]=0
\end{aligned}
$$
III for (int [] row : mat)
for (int element : row)
$$
\begin{aligned}
\text { if (element } & < 0) \\
\text { element } &=0
\end{aligned}
$$
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

Madi Sousa
Madi Sousa
Numerade Educator
View

Problem 35

A two-dimensional array of double, rainfall, will be used to represent the daily rainfall for a given year. In this scheme, rainfall [month] [day] represents the amount of rain on the given day and month. For example,
rainfall[1][15] is the amount of rain on Jan. 15 rainfall [12][25]$\quad$ is the amount of rain on Dec. 25
The array can be declared as follows:
double $[\text { ] }[\text { ] rainfall }=\text { new double }[13][32]$
This creates 13 rows indexed from 0 to 12 and 32 columns indexed from 0 to 31 all initialized to 0.0. Row 0 and column 0 will be ignored. Column 31 in row 4 will be ignored, since April 31 is not a valid day. In years that are not leap years, columns $29,30,$ and 31 in row 2 will be ignored since Feb. $29,30,$ and 31 are not valid days. Consider the method average Rainfal1 below:
/** Precondition:
$* \quad-$ rainfall is initialized with values representing amounts
* of rain on all valid days.
* - - Invalid days are initialized to 0.0.
* - Feb 29 is not a valid day.
* Postcondition: Returns average rainfall for the year.
$* /$
public double averageRainfall(double rainfall[] [])
{
double total $=0.0$
/* more code * /
}
Which of the following is a correct replacement for /* more code */ so that the postcondition for the method is satisfied?
Which of the following is a correct replacement for /* more code */ so that the postcondition for the method is satisfied?
I for (int month $=1 ;$ month $ < $ rainfall. length; month $++$ )
for (int day $=1 ;$ day $ < $ rainfall [month]. length; day++) total $+=$ rainfall [month] [day] return total / (13 * 32);
II for (int month $=1 ;$ month $<$ rainfall. length; montht+) for (int day $=1 ;$ day $ < $ rainfall [month]. length; day++) total $+=$ rainfall [month] [day] return total / 365;
III for (double [] month : rainfall) for (double rainAmt : month) total $+=$ rainAmt return total / 365;
(A) None
(B) I only
(C) II only
(D) III only
(E) II and III only

Victor Salazar
Victor Salazar
Numerade Educator
30:42

Problem 36

This question is based on the Point class below:
public class Point
{$/ * *$ The coordinates. $* /$ private int $\mathrm{x}$ private int $\mathrm{y}$
public Point (int xValue, int yValue)
{
$\mathrm{x}=\mathrm{x}$ Value
$\mathrm{y}=\mathrm{y}$ Value
}

/** Oreturn the x-coordinate of this point */ public int getx()
$\{\text { return } x ;\}$
/** Oreturn the y-coordinate of this point */ public int gety()
$\{\text { return } \mathrm{y} ;\}$
/** Set $x$ and $y$ to new $x$ and new $_{-} y$. */ public void setpoint(int new_x, int new_y)
{
$\mathrm{x}=$ new_x
$\mathrm{y}=$ new_ $\mathrm{y}$
}
//Other methods are not shown.
}
The method changeNegs below takes a matrix of Point objects as parameter and replaces every Point that has as least one negative coordinate with the Point
(0,0)
/** Oparam pointMat the matrix of points
$*$ Precondition: pointMat is initialized with Point objects.
$*$ Postcondition: Every point with at least one negative coordinate
" has been changed to have both coordinates
$\begin{array}{ll}* & \text { equal to zero. }\end{array}$
$* /$
public static void changeNegs (Point [] [] pointMat)
{
$/ *$ code $* /$}
Which is a correct replacement for / * code */?
I for (int $r=0 ; r < \text { pointMat. } \text { length; } r++)$ for $(\text { int } c=0 ; c < \text { point Mat }[r] . \text { length } ; \quad c++)$ if (pointMat $[r][c] \cdot \operatorname{get} x() < 0$
$$
\begin{array}{l}
\text { II pointMat }[\mathrm{r}][\mathrm{c}] \cdot \operatorname{gety}() < 0) \\
\text { pointMat }[\mathrm{r}][\mathrm{c}] . \text { setPoint }(0,0)
\end{array}
$$
II for $(\text { int } c=0 ; c < \text { pointMat }[0] \text { . length; } c++)$
$$
\text { for (int }r=0 ; r < \text { point Mat. } \operatorname{length} ; r++)
$$
if (pointMat $[\mathrm{r}][\mathrm{c}] \cdot \operatorname{get} \mathrm{x}() < 0$
$$
\begin{array}{l}
\text { | I pointmat }[\mathrm{r}][\mathrm{c}] \cdot \text { gety }() < 0) \\
\text { pointMat }[\mathrm{r}][\mathrm{c}] . \text { setPoint }(0,0)
\end{array}
$$
III for (Point [] row : pointMat) for (Point $p:$ row if $(p \cdot \operatorname{get} x() < 0 \text { II } p \cdot \text { gety }() < 0)$
p. setpoint (0,0)
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
00:56

Problem 37

A simple Tic-Tac-Toe board is a $3 \times 3$ array filled with either $\mathrm{X}$ 's, O's, or blanks. Here is a class for a game of Tic-Tac-Toe:
public class Tictactoe
{
private String [] [] board; private static final int $\mathrm{ROWS}=3$ private static final int $\mathrm{COLS}=3$
$/ * *$ Construct an empty board. $* /$ public TicTactoe()
{
board $=$ new String [ROWS] [COLS]
for (int $r=0 ; r < R O W S ; r++)$
for (int $c=0 ; c < $ COLS $; \quad$ c++) board $[\mathrm{r}][\mathrm{c}]="$ "
}
/** Qparam r the row number
$*$ Qparam c the column number
$*$ Qparam symbol the symbol to be placed on board $[\mathrm{r}][\mathrm{c}]$
$*$ Precondition: The square board $[\mathrm{r}][\mathrm{c}]$ is empty.
* Postcondition: symbol placed in that square.
$* /$
public void makeMove(int $r,$ int $c,$ String symbol)
{
board $[\mathrm{r}][\mathrm{c}]=$ symbol
}
/** Creates a string representation of the board, e.g.
$*$ Qreturn the string representation of board
$* /$
public String toString()
{
String $s=" "$
//empty string
/* more code * / return s;
}
}
Which segment represents a correct replacement for /* more code */ for the toString method?
(A) for (int $r=0 ; r<\text { ROWS; } r++)$
{
for (int $c=0 ; c<\cos S ; c++)$
{
$S=s+" | "$
$s=s+$ board $[r][c]$
$\mathbf{s}=\mathbf{s}+" | \mathbf{l n}^{n}$
}
}
(int $r=0 ; r<R O W S ; r++)$
(B) for $\mathbf{s}=\mathbf{s}+\left."\right|^{n}$
for (int $c=0 ; c < \operatorname{COLS} ; \quad c++)$
{
$s=s+$ board $[r][c]$
$s=s+" | \backslash n^{n}$
}
(C) for (int $r=0 ; r<R O W S ; r++)$ $\mathbf{s}=\mathbf{s}+" | "$
for (int $c=0 ; c < \cos S ; c++)$
$s=s+$ board $[r][c]$
}
$s=s+" | \backslash n^{n}$
(D) for (int $r=0 ; r<R O W S ; r++)$ $\mathbf{s}=\mathbf{s}+" | "$
for (int $c=0 ; c<\operatorname{COLS} ; \quad c++)$
{
$s=s+$ board $[r][c]$
$s=s+" | \backslash n^{n}$
}
(E) for (int $r=0 ; r<R O W S ; r++)$
{ $\mathbf{s}=\mathbf{s}+\left."\right|^{n}$
for (int $c=0 ; c<\operatorname{COLS} ; \quad c++)$
$s=s+$ board $[r][c]$
$s=s+" | \backslash n^{\prime \prime}$
}

Manik Pulyani
Manik Pulyani
Numerade Educator