CPSC 233: Practice Midterm Questions Section I: Miscellaneous Fill in the blanks 1. For this question please refer to the array declaration listed below. There are __________ elements in this array which has an index that ranges from __________(lowest index) to __________(highest index). int [] array = new int [10]; Answer: 10, 0, 9 Short answer: 2. Briefly describe the difference between a static data field and a non-static field. Answer: There is only one instance of a static data field for all instances of a class (none, one or many). For each instance of a class there exists separate non-static fields. 3. Describe what is meant by the term method overloading. Answer: When two methods have the same name but differ in their parameters (type, number and/or order). Section II: Reading Code 1. In the space provided below, list the output of the following program. class Wrapper { private int num; private char ch; public Wrapper () { num = 1; ch = 'J'; } public Wrapper (int n) { num = n; ch = 'J'; } public Wrapper (char c) { num = 1; ch = c; } public Wrapper (int n, char c) { num = n; ch = c; } public int getNum () { return num; } public char getChar () { return ch; } } class Driver { public static void main (String [] argv) { Wrapper w1 = new Wrapper (); Wrapper w2 = new Wrapper (10); Wrapper w3 = new Wrapper('j'); Wrapper w4 = new Wrapper(34, 'T'); System.out.println(w1.getNum() + " " + w1.getChar()); System.out.println(w2.getNum() + " " + w2.getChar()); System.out.println(w3.getNum() + " " + w3.getChar()); System.out.println(w4.getNum() + " " + w4.getChar()); } } << Put your answer here >> Answer: 1 J 10 J 1 j 34 T Section III: Code Writing 1. For this question please refer to the code in classes Driver and Wrapper. Implement the equals() method in class Wrapper to allow the data for w1 and w2 to be compared using the “if” in the main method of class Driver. class Driver { public static void main (String [] argv) { Wrapper w1 = new Wrapper (); Wrapper w2 = new Wrapper (7, 's'); // Comparing data if (w1.equals(w2)) System.out.println("data equal"); } } class Wrapper { private int num; private char ch; public Wrapper () { num = 1; ch = 'J'; } public Wrapper (int n) { num = n; ch = 'J'; } public Wrapper (char c) { num = 1; ch = c; } public Wrapper (int n, char c) { num = n; ch = c; } public int getNum () { return num; } public char getChar () { return ch; } << Put your answer here >> Answer: public boolean equals (Wrapper w) { if (this.num != w.num) return false; if (this.ch != w.ch) return false; return true; } } JT: Liked the practice exam? You’ll love the real one!