Midterm review
Download
1 / 22

Midterm review - PowerPoint PPT Presentation


  • 110 Views
  • Uploaded on

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

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

PowerPoint Slideshow about 'Midterm review' - gagan


An Image/Link below is provided (as is) to download presentation

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

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;


Midterm review

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.


Midterm review

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?


Midterm review

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


Midterm review

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

}


Midterm review

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?


Midterm review

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;

}


Midterm review

  • 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


Midterm review

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?

  • strList.add(ch);

  • strList.add(new String(“handy andy”));

  • strList.add(intOb.toString());

  • strList.add( ch + 8);

  • strList.add(intOb + 8);

    What type of data does strList store?


Midterm review

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>();


Midterm review

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*/ }

…..


Midterm review

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

{ 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


Midterm review

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

    for (Address a: list)

    /* get list of names */

    System.out.println(…….

    for (Address addr: list)

    /* print out a list of address */


Midterm review

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


Midterm review

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

}

return total;

}

What is the postcondition for method mystery?


Midterm review

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)


Midterm review

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?


Midterm review

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?


Midterm review

  • 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?


Midterm review

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


Midterm review

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.