id:

TEST3

Reference classes here
MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
 
For the question(s) below, assume values is an int array that is currently filled to capacity, with the following values:


1)  What is returned by values[3]? 1)
  A)  3
  B)  6
  C)  2
  D)  9
  E)  12

2)  What is the value of values.length? 2)
  A)  7
  B)  6
  C)  0
  D)  18
  E)  5

3)  Which of the following loops would adequately add 1 to each element stored in values? 3)
  A)  for(j=1;j<values.length-1;j++) values[j]++;
  B)  for(j=0;j<values.length;j++) values[j]++;
  C)  for(j=0;j<=values.length;j++) values[j]++;
  D)  for(j=0;j<values.length-1;j++) values[j]++;
  E)  for(j=1;j<values.length;j++) values[j]++;

4)  The statement System.out.println(values[7]); will 4)
  A)  cause an ArrayOutOfBoundsException to be thrown
  B)  output 7
  C)  output 18
  D)  output nothing
  E)  cause a run-time error

5)  Which of the following is a legal way to declare and instantiate an array of 10 Strings? 5)
  A)  String[ ] s = new String;
  B)  String[10] s = new String;
  C)  String s = new String(10);
  D)  String s = new String[10];
  E)  String[ ] s = new String[10];

6)  In Java, arrays are 6)
  A)  objects
  B)  Strings
  C)  interfaces
  D)  primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object
  E)  primitive data types

7)  What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0.
for(j
=0;j<list.length;j++)
if(list[j]
< temp) c++;
7)
  A)  It finds the smallest value and stores it in temp
  B)  It finds the largest value and stores it in temp
  C)  It sorts the values in list to be in ascending order
  D)  It counts the number of elements equal to the smallest value in list
  E)  It counts the number of elements in list that are less than temp

An int array stores the following values. Use the array to answer the question(s) below.


8) 

Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?

(a copy of the algorithm is below:)

for (i=0; i < n-1; i++) {
       min = data[i];
       for (j=i+1; j < n; j++)
           if (data[j] < min) {
              min = data[j];
              min_id = j;
           }
       tmp = data[i];
       data[i] = data[min_id];
       data[min_id] = tmp;
   }
	  
	  

 

8)
  A)  2, 4, 12, 9, 6, 8, 18
  B)  9, 4, 12, 2, 6, 8, 18
  C)  2, 4, 9, 12, 6, 8, 18
  D)  2, 4, 6, 8, 9, 12, 18
  E)  4, 9, 12, 2, 6, 8, 18

For the question(s) below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
9)  What does the following code do?
int value1
= scan.nextInt( );
int value2
= scan.nextInt( );
candy[value1]
+= value2;
9)
  A)  adds 1 to the number of candy bars sold by child value1 and child value2
  B)  inputs a new value for the number of candy bars sold by both child value1 and child value2
  C)  adds value1 to the number of candy bars sold by child value2
  D)  adds value2 to the number of candy bars sold by child value1
  E)  adds 1 to the number of candy bars sold by child value1

10)  Which of the following code could be used to compute the total number of bars sold by the children? 10)
  A)  for(int j=0; j<12; j++) sum+= candy[j];
  B)  for(int j=0; j<12; j++) candy[j] = sum;
  C)  for(int j=0; j<12; j++) sum = candy[j];
  D)  for(int j=0; j<12; j++) sum += [j];
  E)  for(int j=0; j<12; j++) [j] += sum;

11)  What does the following method do?
public int question11( )
{
	int value1 = 0;
	int value2 = 0;
	for(int j=0; j<12; j++)
		if(candy[j] > value1)
		{
			value1 = candy[j];
			value2 = j;
		}
	return value2;
}
	  
11)
  A)  It returns the total number of candy bars sold
  B)  It returns the index of the child who sold the most candy bars
  C)  It returns the total number of children who sold 0 candy bars
  D)  It returns the total number of children who sold more than 0 candy bars
  E)  It returns the number of candy bars sold by the child who sold the most candy bars

12)  Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr? 12)
  A)  It stores 4 elements with legal indices between 1 and 4
  B)  It stores 5 elements with legal indices between 0 and 5
  C)  It stores 6 elements with legal indices between 0 and 5
  D)  It stores 5 elements with legal indices between 0 and 4
  E)  It stores 5 elements with legal indices between 1 and 5

13)  The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only.
foo
= 0;
for(int j
=0; j<list.length; j++)
if (list[j]
> foo) foo = list[j];
13)
  A)  it counts the number of elements in list that are greater than foo
  B)  it stores the largest value in list (the maximum) in foo
  C)  it counts the number of elements in list that are less than foo
  D)  it stores every value in list, one at a time, in foo, until the loop terminates
  E)  it stores the smallest value in list (the minimum) in foo

14)  To initialize a String array names to store the three Strings "Huey", "Duey" and "Louie", you would do 14)
  A)  String names = {"Huey", "Duey", "Louie"};
  B)  String[ ] names = {"Huey", "Duey", "Louie"};
  C)  String[ ] names = new String{"Huey", "Duey", "Louie"};
  D)  String names[3] = {"Huey", "Duey", "Louie"};
  E)  String names; names[0] = "Huey"; names[1] = "Duey"; names[2] = "Louie";

15)  Which of the following statements loops through every element in the ArrayList alist? 15)
  A)  for (Object item : alist)
  B)  for (Object item = null; item : alist)
  C)  for (item in alist)
  D)  for (alist : Object item)
  E)  for (Object alist : item)

16)  Which of the following correctly declares an ArrayList of Car objects? 16)
  A)  ArrayList Car[] cars;
  B)  ArrayList cars = Car;
  C)  ArrayList[Car] cars;
  D)  ArrayList<Car> cars;
  E)  ArrayList Car;

17)  Which of the following statements adds item to the end of the ArrayList alist? 17)
  A)  alist.add(item);
  B)  alist.set(1, item);
  C)  item.addtoEnd(alist);
  D)  alist[alist.size()-1] = item;
  E)  both A and B

18)  For the following three problems: An ArrayList of Strings called names contains "John", "Jack", and "Adam". To change "John" to "Kevin" we would say: 18)
  A)  names.set(1,"Kevin");
  B)  names.set(0,"Kevin");
  C)  names.get(0)="Kevin";
  D)  names.get(1)="Kevin";

19)  Assume the above statement was executed correctly. The effect of the following code fragment would be:
for (i
=0; i<names.size(); i++)
if (names.get(i).length()
==5)
names.remove(i);
19)
  A)  It would remove John
  B)  It would remove all names
  C)  It would remove Kevin
  D)  It would have no effect

 

20)  To declare a two-dimensional int array called twoD, which of the following would you use? 20)
  A)  int [ [ ] ] twoD;
  B)  int[2] twoD;
  C)  int[ , ] twoD;
  D)  int[ ][ ] twoD;
  E)  int[ ] twoD[2];

21)  If an int array is passed as a parameter to a method, which of the following could be the method header? 21)
  A) public void findSomething(int[ ] a)
  B)  public void findSomething(int[ ])
  C) public void findSomething ([]int a)
  D) public void findSomething (a[ ])
  E) public void findSomething (int a)