PIC20A Midterm 2005 Spring

Instructions

  1. The exam consists of 10 pages(including this page). You are responsible for checking the number of pages
  2. Please read and answer all the questions carefully. The point value for each question is indicated. The total number of points available is 40. If you have trouble with a question, leave it and try another.
  3. Unless otherwise stated, you may assume when code is given that the code compiles and is correct. When you are asked to write code, you are expected to write code in good style and with proper indentation.
  4. Good luck!




Pleaes use blue pen or black pen to write down your name and student ID. You can use pencil to answer the questions of the midterm.


Last Name:_________________ First Name:________________
Student ID:_________________


Circle the name of your TA

Michael Balabanov          Keith Ouellette






Question
Q1(10pts)
Q2(15pts)
Q3(15pts)
Total
Scores




















Q1)
(a)For the following program
public class Test {
    public static void main(String[] args) {
        System.out.println(args[0] + ":" + args[1]);
    }
}
Write down the output of
(i) java Test Charles Li "Hello World"




(ii) java Test "Hello World" Charles Li




(b) For the following programs, what is the output of java Sub?
public class Super {
   protected int x ;
   protected int y;
   public Super(int x, int y) {
      this.x = x;
      this.y = x + y;
   }
}


public class Sub extends Super{
   public Sub(int x, int y) {
       super(x,y);
       this.y++;
   }
   
   public Sub(int x) {
       this(x,x);
   }
   
   public static void main(String[] args) {
       Sub sub = new Sub(2);
       System.out.println(sub.x);
       System.out.println(sub.y);     
   }
}
Answer:




(c)For the following programs, what is the output of java TestA?
public class TestA {
   public static void main(String[] args) {
       ClassA  a= new ClassA(30);
       doub(a);
       System.out.println(a.x);
   }
   
   public static void doub(ClassA a) {
       a = new ClassA(2*a.x);
   }
}

public class ClassA {
   public int x;
   public ClassA(int x) {
     this.x = x;
   }
}
Answer:




(d) Sketch the output of java GTest:
import java.awt.*;
import javax.swing.*;

public class GTest extends JFrame {
    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawRect(50,50,100,100);
        g.drawLine(50,100,150,100);
        g.drawLine(100,50,100,150);
        g.fillOval(100,100,50,50);
    }
    
    public static void main(String[] args) {
       GTest f = new GTest();
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.setSize(200,200);
       f.setVisible(true);
    }
}
Answer









Q2)
Find out the compiling errors of the following programs.
  1. Circle the errors and fix them.
  2. Write your corrections next to the errors you circle.
  3. Explain briefly(one or two sentences) why they are errors.
There is exactly one error in each question.
(a)
public class Error1 {
   public static void main(String[] args) {
       if(args.length = 1) {
           System.out.println(args[0]);
       } else {
           System.out.println("Hello");
           return;
       }
   }
}

(b)
public class Error2 {
   public static final double num1;
   public static int num2;
   static {
       num1 = 2.3d;
       num2 = 1;
   }   
   
   public final void method(int a) {
       a+=num2;
   }
}

public class Error3 extends Error2 {
    public final void method(int a) {
        a+=num1;
    }
}

(c)
public class Error4 {
    int aNumber;
    int[] an_array;
    int 2numbers;
    
    public void method1() {
        System.out.println("Method 1");
    }
    
    public int method2() {
        an_array = new int[10];
        return an_array[0];
    }
}
(d)
import java.awt.Color;

public class Error5 {
    public Error5() {
        Color c1 = new Color(1,2,3);
        Color c2 = new Color(0.5, 0.2, 0.3);
        Color c3 = Color.red.blue;
    }
}
(e)
public class Error6 {
    public int a;
    private double d;
    public Error6(int a, double d) {
        this.a = a;
        this.d = d;
    }
    
    protected double method() {
        return d;
    }
}

class Error7 extends Error6 {
    public Error7() {
        super(5,10);
        d = 15;
    }
    
    public double method(int x) {
        System.out.println(a);
        return x+method();
    }
}
Q3)
(a) Write a function int countComma(String s) to count the number of commas (',') in the String s.
Example countComma("abc,def,ghijk,123") returns 3.
(b) Write a function String[] split(String s) split the string by comma. Example split("abc,def,ghijk,123") returns an array with 4 strings {"abc", "def", "ghijk", "123"}. You can assume You can only use the function provided by the table
Fill in your code below:
public class StringTest {
   public static int countComma(String s) {
      //Your codes
       














} public static String[] split(String s) { // You can dynamically assign the length of an array in Java String[] ans = new String[countComma(s)+1]; //Your codes





























return ans; } }