CSC 120A Final Review Questions - Fall 2004

[Course home page]
  1. What is the difference between a class and an object?
     
  2. Explain the concept of inheritance with an explain of how this object-oriented programming feature is used in Java.
     
  3. You have been hired to develop software for the local library. Your software needs to keep track of the following types of materials: Video tapes, audio cassette tapes, adult and children's fiction and non-fiction books, DVD videos, dictionaries, encyclopedias, music CDs, newspapers, and magazines. (1) Draw an inheritance hierarchy of classes that you would use to represent these types of media. You may add new classes to the hierarchy as necessary. (2) In which class might you declare a field called "numOfPages"? (3) In which class might you declare a field called "runningTime"?
     
  4. Find four syntax errors in this code and explain why they are errors:
    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();
    	}
    
        }
    }

     
  5. Do the following loops execute in exactly the same manner and produce the same output, assuming 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());

     
  6. (a) What will the output of the following method be when it is executed? (b) What is a more descriptive/clear name for the "isp" variable? (c) What would be a better name for this method?
        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] + " ");
    
        }

 



Last modified: Wed Nov 24 12:54:30 EST 2004