public class Test { public static void main( String[] args ) { Count myCount = new Count(); int times = 0; for (int i = 0; i < 100; i++) increment(myCount, times); System.out.println("count is " + myCount.cnt); System.out.println("times is " + times); } public static void increment(Count c, int tm) { c.cnt++; tm++; } } public class Count { public int cnt; public Count(int num) { cnt = num; } public Count() { cnt = 0; } }ANSWER: The output would be
count is 100 times is 0In main, myCount is passed to the method increment, and it is modified by c.cnt++; Since myCount is a REFERENCE to an object, the modification in increment indeed makes an effect to myCount (in main) through the parameter reference c. On the other hand, times is a primitive data. So the modification in increment (by tm++;) does NOT affect times in main -- pass by value/copy.
String s1 = "CSC 211"; String s2 = s1; String s3 = "CSC 211";Assume that s1 and s3 have different memory location. What are the results of the following expressions?
ANSWER:
s1 == s2; // (a) true s2 == s3; // (b) false s1.equals(s2); // (c) true s2.equals(s3); // (d) true s1.compareTo(s2); // (e) 0 -- same content s2.compareTo(s3); // (f) 0 -- same content
public class Car2 { /* data members */ ... private int serial_no; private static int object_counter; /* methods */ public Car2(String mdl, String clr, double fl) { ... object_counter++; serial_no = object_counter; } public static int getSerialNo() // error 1 { return serial_no; } public static int getObjectCount() { return object_count; // error 2 } ... }
ANSWER: There is two errors in the code:
ANSWER:
public static int FirstKeyIndex(int[] ar, int key){ for (int i = 0; i < ar.length; i++) { if (ar[i] == key) // when key is found return i; // return the index } return -1; // fall-through case -- key didn't exist in ar }
ANSWER:
public static int LastKeyIndex(int[] ar, int key){ int index = -1; // index initialized to -1 (for not found) for (int i = 0; i < ar.length; i++) { if (ar[i] == key) // when key is found { index = i; // set index to i, and KEEP ON GOING, // thereby index is RE-ASSIGNED } } return index; // return the index of the last occurrence }
double[] a = new double[ 50 ];write a loop that fills the first cell with 0.5 and the second cell with 1.2, and each succeeding cell with the sum of its two predecessors. For example, the third cell would contain 1.7.
ANSWER:
a[0] = 0.5; a[1] = 1.2; for (int i = 2; i < a.length; i++) // index starts from 2 ar[i] = ar[i-1] + ar[i-2]; // two preceding elements
ANSWER:
public class ArrayFns { public static int ArrayMin(int[] ar) // static method { int min = ar[0]; // running min, initalized to a big number for (int i = 1; i < ar.length; i++) { if (ar[i] < min) min = ar[i]; // a value smaller than the running min updates it } return min; } public static void Reverse(int[] ar) // static method { for (int i = 0, j = ar.length-1; i < j; i++, j--) // CORRECTION: used to be j++ { // swap ar[i] and ar[j] using temp int temp = ar[i]; ar[i] = ar[j]; // assignment to ar[i] indeed make changes in ar ar[j] = temp; // assignment to ar[j] indeed make changes in ar } } }
public interface Property { public String get(); public void set(String s) {} } public class C implements Property { private String s; public C() { s = ""; } public String get() { return s; } }ANSWER: There are 2 errors:
ANSWER: See the code linked from the applet html page TriangleApplet.html
ANSWER: See the code linked from the applet html page TriangleApplet.html