class A { public void foo() { System.out.println("foo"); } } class B extends A { public void bar() { System.out.println("bar"); } } public class ExamReview { public static void main(String[] ) { A[] a = new A[]; B b = new B(); a[0] = b; b.foo(); b.bar(); a[0].foo(); a[0].bar(); try { // try dividing by 0 int y = 5; int x = 0; System.out.println(y/x); } catch (ArithmeticException e) { System.out.println("can't divide " + y + " by 0"); e.printStackTrace(); } } }
line
is a
String variable and in
is a BufferedReader
object? Why or why not?
// LOOP VERSION 1 line = in.readLine(); while (line != null) { System.out.println(line.length()); line = in.readLine(); } // LOOP VERSION 2 while ( (line = in.readLine()) != null) System.out.println(line.length());
public static void someMethod() { final int MAX = 10; int[] list = new int[MAX]; int count = 0; list[count++] = 2; for (int cur = 3; count < MAX; cur++) { boolean isp = true; for (int i = 0; i < count; i++) { if (cur % list[i] == 0) isp = false; } if (isp) list[count++] = cur; } for (int i = 0; i < MAX; i++) System.out.print(list[i] + " "); }