1 / 22

# Midterm review - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Midterm review' - gagan

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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; arr[0]..arr[N-1], where arr is an array of N integers.

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) arr[0]..arr[N-1], where arr is an array of N integers.

{ 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? arr[0]..arr[N-1], where arr is an array of N integers.

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

while ( i < arr.length)

{ i++;

sum =+ arr[i];

}

for ( int i = 2; i <= k; i++) arr[0]..arr[N-1], where arr is an array of N integers.

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}; arr[0]..arr[N-1], where arr is an array of N integers.

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 arr[0]..arr[N-1], where arr is an array of N integers.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>(); arr[0]..arr[N-1], where arr is an array of N integers.

String ch = “ “;

Integer intOb = new Integer(5);

Which statement will cause an error?

What type of data does strList store?

Which declaration is right? Why? arr[0]..arr[N-1], where arr is an array of N integers.

I List<String> stringList = new ArrayList<String>();

II List<int> intList = new ArrayList<int>();

III ArrayList<Comparable> compList = new ArrayList<Comparable>();

public class Address arr[0]..arr[N-1], where arr is an array of N integers.

{ 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 arr[0]..arr[N-1], where arr is an array of N integers.

{ private int idNum;

private double gpa;

//constructors ….

//accessors

public int getIdNum() { return idNum; }

public double getGpa(){ return gpa; }

} P.247-248

• Address[] list = new Address[100]; arr[0]..arr[N-1], where arr is an array of N integers.

/* get list of names */

System.out.println(…….

/* print out a list of address */

public static boolean match(int [] v, int[] w, int N, int M) arr[0]..arr[N-1], where arr is an array of N integers.

{ 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) arr[0]..arr[N-1], where arr is an array of N integers.

{ int total =0, count = 1;

while (count <= b)

{ total *= a;

count ++ ;

}

}

What is the postcondition for method mystery?

Color(238, 9, 63) would be coded as arr[0]..arr[N-1], where arr is an array of N integers.

• #EE093F

Which of the following will evaluate to true only if boolean expressions A, B, C are false?

• !A && !( B && ! C)

• !A || !B ||!C.

• !(A || B ||C )

• !(A && B &&C )

• !A || !(B ||!C)

public class Mystery{ arr[0]..arr[N-1], where arr is an array of N integers.

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?

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;

• swim //frog swims to new position in pond

• eat //frog eats and gains weight

• getWeight //returns weight of frog

• getPondTemperature //return temperature of pond

• die //frog dies based on frog’s age and pond //temperature

Which statement is true following execution of the segment? candidate for being a static method. P111

Temperature t1 = new Temperature(40, “C”);

Temperature t2 = t1;

Temperature t3 = t2.lower(20);

Temperature t4 = t1.toFarenheit()

• t1, t2, t3 and t4 all have the identical temperature, in degrees Celsius.

• t1, t2, t3 and t4 all have the identical temperature, in degrees Fahrenheit.

• t4 represents a Fahrenheit , while t1, t2 and t3 represent all degrees Celsius

• A NullPointerException was thrown.