Log in
Sign up for FREE
arrow_back
Library

Arrays Unit Test

star
star
star
star
star
Last updated about 2 years ago
20 questions
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
Question 1
1.

Question 2
2.

Question 3
3.

Question 4
4.

Question 5
5.

Question 6
6.

Question 7
7.

Question 8
8.

Question 9
9.

Question 10
10.

Question 11
11.

Question 12
12.

Question 13
13.

Question 14
14.

Question 15
15.

Question 16
16.

Question 17
17.

Question 18
18.

Question 19
19.

Question 20
20.

Consider the following method below.
public static double check(int[] vals)
{
double[] amounts = {.15,.2,.5,.01};
if(amounts.length != vals.length){
return 0.0;
}
double total = 0.0;
for(int i = 0; i < vals.length; i++)
{
total += vals[i] * amounts[i];
}
return total;
}
What will the method check() return if passed the array {7, 3, 5, 4}?
4.19
0.86
19
0.0
4.15
Consider the following method below.
public static double check(int[] vals)
{
double[] amounts = {.15,.2,.5,.01};
if(amounts.length != vals.length){
return 0.0;
}
double total = 0.0;
for(int i = 0; i < vals.length; i++)
{
total += vals[i] * amounts[i];
}
return total;
}
What will the method check() return if passed the array {7, 5, 4}?
4.19
0.86
4.15
19
0.0
Which of the following swaps elements 0 and 1 in array?
int[] array = {7,8,10,11,4,3};

I.
array[0] = array[1];
array[1] = array[0];

II.
int save = array[0];
array[0] = array[1];
array[1] = save;

III.
array[0]+= array[1];
array[1] = array[0]+array[1];
array[0]-= array[1];
I and II only
II only
I, II, and II
I only
II and III only
What is output by the code below?
int[] array = {33,14,37,11,27};
System.out.println( array[ 6 - 3 ] );
11
There is no output due to a runtime error.
33
There is no output due to a syntax error.
14
What is output by the code below?
int[] array = {7,8,10,11,4,3};
Arrays.sort( array );
System.out.println(array[2]);
3
11
4
8
7
What is output by the code below?
int[] nums = new int[10];
for (int i=0; i<nums.length; i++)
{
nums[i] = i*3;
}
System.out.println(nums[6]);
18
21
15
24
27
What is output by the code below?
int[] array = {7,8,10,11,4,3};
array[array[0]/2]=15;
array[array[4]-4]=9;
array[array.length/2-1]=5;
array[1]=array[0]+4;
System.out.println(array[0]);
5
15
9
11
7
Which of the following correctly fill /* blank */ in method isIt() ?
//method isIt should return true if all of the numbers
//in array are in increasing ( ascending ) order
public static boolean isIt(int[] array)
{
for(int spot=0; spot<array.length-1; spot++)
{
/* blank */
return false;
}
return true;
}
if( array[spot] > array[spot+1] )
if( array[spot] < array[spot+1] )
if( array[spot-1] > array[spot] )
if( spot > spot+1)
if( array > array+1 )
What is output by the following code?
int[] array = {1,2,3,4,0};
int aplus = array[array[array[array[0]]]];
System.out.println( aplus );
1
3
0
4
2
Consider the following instance variable and incomplete method.
The method getBiggest should return the largest value in tRay.

private int[] tRay; //assume the tRay contains values
public int getBiggest()
{
int big = Integer.MIN_VALUE;

/* blank */

return big;
}

Which of the following code segments shown below could be used to replace
/* blank */ so that getBiggest will work as intended?

I. for( int i=0; i<tRay.length; i++ )
if( tRay[i] > big )
big = tRay[i];

II. for( int item : tRay )
if( tRay[item] > big )
big = tRay[item];

III. for( int item : tRay )
if( item > big )
big = item;
I and II only
I only
I and III only
II only
II and III only
What is output by the code below?
int[] array = {7,8,10,11,4,3};
array[array[0]/2]=15;
array[array[4]+1]=9;
array[array.length/2-1]=5;
array[1]=array[0]+4;
out.println(array[5]);
11
9
5
15
7
What is output by the code below?
String s = "apluscompsci.com";
String words[] = new String[ s.length() ];
for (int b = s.length()-1; b>=0 ;b--)
words[b] = s.substring(b);
Arrays.sort(words);
System.out.println(words[2].length());
7
5
2
6
9
What is output by the code below?
String[] ray = "10 22 5 33 11 4 222".split(" ");
Arrays.sort( ray );
int x = Integer.parseInt( ray[0] );
int r = Integer.parseInt( ray[1] );
x += r;
System.out.println( x );
33
32
9
21
19
What is output by the code below?

Integer[] ray;
ray = new Integer[4];
System.out.println( ray[3] );
null
1
2
3
0
Consider the following class definitions below to determine the output of the provided code segments.
public class Cat
{
private String name;
private int age;
public Cat( String n, int a )
//implementation not shown
public String getName()
//implementation not shown
public int getAge()
//implementation not shown

//other methods not shown
}
//client code in another class
Cat[] bunch = new Cat[5];
bunch[4] = new Cat("wile", 5);
System.out.println( bunch[4].getAge() );
5
null
0
6
15
What is output by the code below?

String[] ray;
ray = new String[5];
ray[1] = "one";
ray[2] = "two";
ray[3] = "three";
ray[0] = ray[2];
ray[2] = null;
System.out.println( ray[4].length() );
3
1
null
2
There is no output due to a runtime exception.
Consider the following class definitions.
public class Cat
{
private String name;
private int age;

public String getName()
//implementation not shown
public int getAge()
//implementation not shown

//constructors and other methods not shown
}
public class CatBarn
{
private Cat[] kitties; //assume kitties has stuff in it

//returns the average age of all of the kitties
public double averageAge( ){
/* blank */
}
//constructors and other methods not shown
}
Which of the following code segments shown below could be used to replace
/* blank */ so that method averageAge() will work as intended?
I.
int cnt = 0;
double sum = 0;
for( Cat c : kitties )
{
sum += c.getAge();
cnt = cnt + 1;
}
return sum / cnt;

II.
int cnt = 0;
double sum = 0;
for( Cat c : kitties )
{
sum = sum + c;
cnt += 1;
}
return sum / cnt;

III.
double sum = 0;
for( int i = 0; i < kitties.length; i++ )
sum += kitties[i].getAge();
return sum / kitties.length;
I and II only
II only
I and III only
III only
I only
Consider the following instance variable and incomplete method.
The method getBiggies should return a new ArrayList
that contains all values in list that are larger than val.

private List<Double> list; //assume list contains values

public List<Double> getBiggies( double val)
{
ArrayList<Double> bigs = new ArrayList<Double>();

/* blank */

return bigs;
}

Which of the following code segments shown below could be used to replace
/* blank */ so that getBiggies will work as intended?

I. for ( int i = 0; i < list.size(); i++)
if( list.get(i) > val )
bigs.add(i);
II. for ( int i = 0; i < list.size(); i++)
if( list.get(i) > val )
bigs.add(val);
III. for ( int i = 0; i < list.size(); i++)
if( list.get(i) > val )
bigs.add(list.get(i));
IV. for ( double item : list )
if( item > val )
bigs.add(item);
III and IV only
I only
II only
III only
II and III only
What is output by the code below?
Double[] ray = {3.1,5.2,6.3,1.4};
for( double r : ray )
r = 0;
System.out.println( ray[2] );
6.3
3.1
5.2
1.4
0.0
Consider the code below.

<blank>[] ray = {12,55,-988,9.6,0};
for( <blank> dog : ray )
System.out.println( dog );
Which of the following could fill <blank> ?
C and D
D. Double
C. double
A. int
B. Integer