Você está na página 1de 22

Midterm review

Which of the following correctly initializes 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;
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 ;
The segment will work as intended
always
never.
whenever arr contains at least one negative integer.
whenever arr contains at least one nonnegative
integer
whenever arr contains no negative integers.
int count = 0;
for (int i =0; i < arr1.length; 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
after execution of the code segment?
public static void changeStr(String bStr)
{ bStr="CDE"; }
public static void changeStr(char[] bStr)
{ bStr[0]='D';
bStr[1]='E';
bStr[2]='F'; }
public static void changeStr(char a, char b, char c)
{ a=X';
b=Y';
c=Z'; }
P115
What is wrong with the statement?

int sum = arr[0], i=0;


while ( i < arr.length)
{ i++;
sum =+ arr[i];
}
for ( int i = 2; i <= k; i++)
if (arr [i] < someValue)
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

What is this a worst case?


What is the best case?
int [] arr = {1, 2, 3, 4};
doSomething(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;
}
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) object?
P245, Q8 and Q9
List<String> strList = new ArrayList<String>();
String ch = ;
Integer intOb = new Integer(5);
Which statement will cause an error?
1.strList.add(ch);
2.strList.add(new String(handy andy));
3.strList.add(intOb.toString());
4.strList.add( ch + 8);
5.strList.add(intOb + 8);

What type of data does strList store?


Which declaration is right? Why?
I List<String> stringList = new
ArrayList<String>();
II List<int> intList = new ArrayList<int>();
III ArrayList<Comparable> compList = new
ArrayList<Comparable>();
public class Address
{ private String myName;
private String myStreet;
private String myCity;
private String myZip;
//constructors
//accessors
public String getName(){ /* code 1*/ }
public String getStreet(){ /* code 2*/ }
public String getCity() { /* code 3*/ }
..
public class Student
{ private int idNum;
private double gpa;
private Address myAddress;
//constructors .
//accessors
public Address getAddress()
{ return myAddress; }
public int getIdNum() { return idNum; }
public double getGpa(){ return gpa; }
} P.247-248
Address[] list = new Address[100];
for (Address a: list)
/* get list of names */
System.out.println(.

for (Address addr: list)


/* print out a list of address */
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
wIndex++;
}
return false;
} P252
public static int mystery(int a, int b)
{ int total =0, count = 1;
while (count <= b)
{ total *= a;
count ++ ;
}
return total;
}
What is the postcondition for method mystery?
Color(238, 9, 63) would be coded as
#EE093F

Which of the following will evaluate to true


only if boolean expressions A, B, C are false?
1.!A && !( B && ! C)
2.!A || !B ||!C.
3.!(A || B ||C )
4.!(A && B &&C )
5.!A || !(B ||!C)
public class Mystery{
public static void strangeMethod(int x, int y)
{ x += y;
y *= x;
System.out.println(x + + y);
}
public static void main(String [] args)
{ int a = 6, b = 3;
strangeMethod(a, b);
System.out.println(a + + b)
} } pass by value? Pass by reference?
Just before the end of execution, what are the values
of x, y and temp?
public class Tester
{ public void someMethod(int a, int b)
{ int temp = a;
a = b;
b = temp; } }
public class TesterMain
{ public static void main(String[] args)
{ int x = 6, y = 8;
Tester tester = new Tester();
tester.someMethod(x, y); }
} scope of a, b, temp, x, y?
An interface cannot implement any method,
wheras an abstract class can => T?
A class can implement many interfaces but can
have only one superclass => T?
An unlimited number of unrelated classes can
implement the same interface => T?
It is not possible to construct either an abstract
class object or an interface object => T?
All of the methods in both an abstract class
and an interface are public => T?
Which of the following methods in the Frog class is the best
candidate for being a static method. P111
public class Frog
{ private String mySpecies;
private int myAge;
private double myWeight;
private Position myPosition;
private boolean amAlive;

1.swim //frog swims to new position in pond
2.eat //frog eats and gains weight
3.getWeight //returns weight of frog
4.getPondTemperature //return temperature of pond
5.die //frog dies based on frogs age and pond
//temperature
Which statement is true following execution of the
segment?
Temperature t1 = new Temperature(40, C);
Temperature t2 = t1;
Temperature t3 = t2.lower(20);
Temperature t4 = t1.toFarenheit()
a)t1, t2, t3 and t4 all have the identical temperature,
in degrees Celsius.
b)t1, t2, t3 and t4 all have the identical temperature,
in degrees Fahrenheit.
c)t4 represents a Fahrenheit , while t1, t2 and t3
represent all degrees Celsius
d)A NullPointerException was thrown.

Você também pode gostar