Semester Final Review
By Mickey Arnold
starstarstarstarstarstarstarstarstarstar
Last updated 10 months ago
54 Questions
5 points
5
Question 1
1.
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}?
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}?
5 points
5
Question 2
2.
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}?
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}?
5 points
5
Question 3
3.
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];
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];
5 points
5
Question 4
4.
What is output by the code below?int[] array = {33,14,37,11,27};System.out.println( array[ 6 - 3 ] );
What is output by the code below?
int[] array = {33,14,37,11,27};
System.out.println( array[ 6 - 3 ] );
5 points
5
Question 5
5.
What is output by the code below?int[] array = {7,8,10,11,4,3};Arrays.sort( array );System.out.println(array[2]);
What is output by the code below?
int[] array = {7,8,10,11,4,3};
Arrays.sort( array );
System.out.println(array[2]);
5 points
5
Question 6
6.
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]);
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]);
5 points
5
Question 7
7.
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]);
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 points
5
Question 8
8.
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 ) orderpublic static boolean isIt(int[] array){ for(int spot=0; spot<array.length-1; spot++) { /* blank */ return false; } return true;}
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;
}
5 points
5
Question 9
9.
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 );
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 );
5 points
5
Question 10
10.
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 valuespublic 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;
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;
5 points
5
Question 11
11.
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]);
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]);
5 points
5
Question 12
12.
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());
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());
5 points
5
Question 13
13.
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 );
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 );
5 points
5
Question 14
14.
What is output by the code below?
Integer[] ray;ray = new Integer[4];System.out.println( ray[3] );
What is output by the code below?
Integer[] ray;
ray = new Integer[4];
System.out.println( ray[3] );
5 points
5
Question 15
15.
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 classCat[] bunch = new Cat[5];bunch[4] = new Cat("wile", 5);System.out.println( bunch[4].getAge() );
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 points
5
Question 16
16.
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() );
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() );
5 points
5
Question 17
17.
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;
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;
5 points
5
Question 18
18.
Consider the following instance variable and incomplete method.The method getBiggies should return a new ArrayListthat 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);
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);
5 points
5
Question 19
19.
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] );
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] );
5 points
5
Question 20
20.
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> ?
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> ?
5 points
5
Question 21
21.
What do you call the condition that stops recursion?
What do you call the condition that stops recursion?
5 points
5
Question 22
22.
Consider the following method.public static int myst(int i){ if(i<=0) return 0; if(i<=3) return i; return myst(i-2)+myst(i-1);}What is returned by the call myst(5)?
Consider the following method.
public static int myst(int i)
{
if(i<=0)
return 0;
if(i<=3)
return i;
return myst(i-2)+myst(i-1);
}
What is returned by the call myst(5)?
5 points
5
Question 23
23.
What is returned by the call ben(51) ?public static String ben(int x){ if( x / 5 <= 0 ) return "" + x % 5; else return "" + ( x % 5 ) + ben( x / 5 );}
What is returned by the call ben(51) ?
public static String ben(int x)
{
if( x / 5 <= 0 )
return "" + x % 5;
else
return "" + ( x % 5 ) + ben( x / 5 );
}
5 points
5
Question 24
24.
Consider the following method.public static int alice(int m, int n){ if(m < 3) return n; return alice(n-2, m-1);}What value does of alice(11, 12) return?
Consider the following method.
public static int alice(int m, int n)
{
if(m < 3)
return n;
return alice(n-2, m-1);
}
What value does of alice(11, 12) return?
5 points
5
Question 25
25.
What is returned the call fun(6) ?public static int fun(int x){ if(x < 1) return 1; else return x + fun(x - 1);}
What is returned the call fun(6) ?
public static int fun(int x)
{
if(x < 1)
return 1;
else
return x + fun(x - 1);
}
5 points
5
Question 26
26.
What is returned by the call fun(8) ?public static int fun(int x){ if(x < 1) return 1; else return x + fun(x - 1);}
What is returned by the call fun(8) ?
public static int fun(int x)
{
if(x < 1)
return 1;
else
return x + fun(x - 1);
}
5 points
5
Question 27
27.
What is returned by the call fun(8) ?public static int fun(int x){ if(x < 1) return 1; else return x + fun(x - 2);}
What is returned by the call fun(8) ?
public static int fun(int x){
if(x < 1)
return 1;
else
return x + fun(x - 2);
}
5 points
5
Question 28
28.
What is returned by the call fun(6) ?public static int fun(int x){ if(x < 1) return 1; else return x + fun(x - 2);}
What is returned by the call fun(6) ?
public static int fun(int x){
if(x < 1)
return 1;
else
return x + fun(x - 2);
}
5 points
5
Question 29
29.
What is returned by the call fun(1) ?public static int fun(int x){ if(x < 1) return 1; else return x - fun(x - 3);}
What is returned by the call fun(1) ?
public static int fun(int x)
{
if(x < 1)
return 1;
else
return x - fun(x - 3);
}
5 points
5
Question 30
30.
What is returned by the call fun(10) ?public static int fun(int x){ if (x < 1) return x; else return x + fun(x - 2);}
What is returned by the call fun(10) ?
public static int fun(int x)
{
if (x < 1)
return x;
else
return x + fun(x - 2);
}
5 points
5
Question 31
31.
What is returned by the call wacky(5,5) ?public static int wacky(int x, int y){ if(x <= 1) return y; else return wacky(x - 1,y - 1) + y;}
What is returned by the call wacky(5,5) ?
public static int wacky(int x, int y){
if(x <= 1)
return y;
else
return wacky(x - 1,y - 1) + y;
}
5 points
5
Question 32
32.
What is returned by the call wacky(4,6) ?public static int wacky(int x, int y){ if(x <= 1) return y; else return wacky(x - 1,y - 1) + y;}
What is returned by the call wacky(4,6) ?
public static int wacky(int x, int y){
if(x <= 1)
return y;
else
return wacky(x - 1,y - 1) + y;
}
5 points
5
Question 33
33.
What is returned by the call wacky(2,6) ?public static int wacky(int x, int y){ if(x <= 1) return y; else return wacky(x - 1,y - 1) + y;}
What is returned by the call wacky(2,6) ?
public static int wacky(int x, int y)
{
if(x <= 1)
return y;
else
return wacky(x - 1,y - 1) + y;
}
5 points
5
Question 34
34.
What is returned by the call funny(0) ?public static int funny(int x){ if(x<1) return 1; else return x + funny(x - 1) - funny(x - 2);}
What is returned by the call funny(0) ?
public static int funny(int x)
{
if(x<1)
return 1;
else
return x + funny(x - 1) - funny(x - 2);
}
5 points
5
Question 35
35.
What is returned by the call go(2,6) ?public static int go(int x, int y){ if(x <= 1) return y; else return go(x - 1,y) + y;}
What is returned by the call go(2,6) ?
public static int go(int x, int y)
{
if(x <= 1)
return y;
else
return go(x - 1,y) + y;
}
5 points
5
Question 36
36.
What is returned by the call go(4,2) ?public static int go(int x, int y){ if(x <= 1) return y; else return go(x - 1,y) + y;}
What is returned by the call go(4,2) ?
public static int go(int x, int y)
{
if(x <= 1)
return y;
else
return go(x - 1,y) + y;
}
5 points
5
Question 37
37.
What is returned by the call fly(2) ?public static int fly(int x){ if(x < 1) return 1; else return x + fly(x - 3) - fly(x - 2);}
What is returned by the call fly(2) ?
public static int fly(int x)
{
if(x < 1)
return 1;
else
return x + fly(x - 3) - fly(x - 2);
}
5 points
5
Question 38
38.
What is returned by the call go(7,3)?public static int go(int x, int y){ if(x <= 1) return y; else return go(x - 1,y) + y;}
What is returned by the call go(7,3)?
public static int go(int x, int y)
{
if(x <= 1)
return y;
else
return go(x - 1,y) + y;
}
5 points
5
Question 39
39.
Which of the following answer choices best describes the algorithmic purpose of method ben?public static int ben(int[] ray, int i, int x){ if( i >= ray.length ) return 0; if( ray[i] == x ) return 1 + ben( ray, i+1, x ); return 0 + ben( ray, i+1, x );}
Which of the following answer choices best describes the algorithmic purpose of method ben?
public static int ben(int[] ray, int i, int x)
{
if( i >= ray.length )
return 0;
if( ray[i] == x )
return 1 + ben( ray, i+1, x );
return 0 + ben( ray, i+1, x );
}
5 points
5
Question 40
40.
Which of the following answer choices best describes the algorithmic purpose of method ben?public static int ben(int[] ray, int i){ if( i >= ray.length ) return 0; if( ray[i] % 2 == 0 ) return 1 + ben( ray, i+1 ); return 0 + ben( ray, i+1 );}
Which of the following answer choices best describes the algorithmic purpose of method ben?
public static int ben(int[] ray, int i)
{
if( i >= ray.length )
return 0;
if( ray[i] % 2 == 0 )
return 1 + ben( ray, i+1 );
return 0 + ben( ray, i+1 );
}
1 point
1
Question 41
41.
Which of the following sorts has a partition method that uses a pivot location?
Which of the following sorts has a partition method that uses a pivot location?
1 point
1
Question 42
42.
What is the bigO of the code below?int n = //user inputfor(int i=0; i<n; i++){ for(int j=1; j<n; j=j*2){ System.out.println(i*j); }}
What is the bigO of the code below?
int n = //user input
for(int i=0; i<n; i++){
for(int j=1; j<n; j=j*2){
System.out.println(i*j);
}
}
1 point
1
Question 43
43.
Which of the these algorithms has a O(1) best case runtime and a O(N) worst case runtime?
Which of the these algorithms has a O(1) best case runtime and a O(N) worst case runtime?
1 point
1
Question 44
44.
Which of the these algorithms has a O(N) best case runtime and a O(N*N) worst case runtime?
Which of the these algorithms has a O(N) best case runtime and a O(N*N) worst case runtime?
1 point
1
Question 45
45.
Which of the these algorithms has a O(N*Log2N) best case runtime and a O(N*N) worst case runtime?
Which of the these algorithms has a O(N*Log2N) best case runtime and a O(N*N) worst case runtime?
1 point
1
Question 46
46.
Which of these is the correct BigO for searching a single linked linked-list?
Which of these is the correct BigO for searching a single linked linked-list?
1 point
1
Question 47
47.
Which of the these algorithms has a O(N*N) best case runtime and a O(N*N) worst case runtime?
Which of the these algorithms has a O(N*N) best case runtime and a O(N*N) worst case runtime?
1 point
1
Question 48
48.
Which of the these algorithms has a O(1) best case runtime and a O(Log2N) worst case runtime?
Which of the these algorithms has a O(1) best case runtime and a O(Log2N) worst case runtime?
1 point
1
Question 49
49.
Which of these is the correct BigO for adding an item to the front of an array?
Which of these is the correct BigO for adding an item to the front of an array?
1 point
1
Question 50
50.
Which of these is the correct BigO for adding an item to the end of an array?
Which of these is the correct BigO for adding an item to the end of an array?
1 point
1
Question 51
51.
Which of these is the correct BigO for adding an item to the front of a Java LinkedList?
Which of these is the correct BigO for adding an item to the front of a Java LinkedList?
1 point
1
Question 52
52.
Which of these is the correct BigO for deleting any item from an ArrayList?
Which of these is the correct BigO for deleting any item from an ArrayList?
1 point
1
Question 53
53.
Which of the following would correctly fill < blank 1 > ?public static void sortOne( Comparable[] list ){ for(int i=0; i<list.length-1; i++) { int min = i; for(int j=i+1; j<list.length; j++) { if(list[j]. < blank 1 > (list[min])<0) min = j; } if( min != i) { Comparable temp = list[min]; list[min] = list[i]; list[i] = temp; } }}
Which of the following would correctly fill < blank 1 > ?
public static void sortOne( Comparable[] list )
{
for(int i=0; i<list.length-1; i++)
{
int min = i;
for(int j=i+1; j<list.length; j++)
{
if(list[j]. < blank 1 > (list[min])<0)
min = j;
}
if( min != i)
{
Comparable temp = list[min];
list[min] = list[i];
list[i] = temp;
}
}
}
1 point
1
Question 54
54.
Assuming <blank 1> is filled correctly, what sort is sortOne()?public static void sortOne( Comparable[] list ){ for(int i=0; i<list.length-1; i++) { int min = i; for(int j=i+1; j<list.length; j++) { if(list[j]. < blank 1 > (list[min])<0) min = j; } if( min != i) { Comparable temp = list[min]; list[min] = list[i]; list[i] = temp; } }}
Assuming <blank 1> is filled correctly, what sort is sortOne()?
public static void sortOne( Comparable[] list )
{
for(int i=0; i<list.length-1; i++)
{
int min = i;
for(int j=i+1; j<list.length; j++)
{
if(list[j]. < blank 1 > (list[min])<0)
min = j;
}
if( min != i)
{
Comparable temp = list[min];
list[min] = list[i];
list[i] = temp;
}
}
}