1 / 22

Midterm review

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

gagan
Download Presentation

Midterm review

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Midterm review

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

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

  4. 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?

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

  6. What is wrong with the statement? int sum = arr[0], i=0; while ( i < arr.length) { i++; sum =+ arr[i]; }

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

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

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

  10. List<String> strList = new ArrayList<String>(); 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?

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

  12. 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*/ } …..

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

  14. 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 */

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

  16. 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?

  17. 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? • !A && !( B && ! C) • !A || !B ||!C. • !(A || B ||C ) • !(A && B &&C ) • !A || !(B ||!C)

  18. 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?

  19. 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?

  20. 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?

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

  22. 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() • 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.

More Related