PIC20A Midterm 2003 Fall

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 in indicated. The total number of points available is 45. 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!




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






Question
SQ1
SQ2
SQ3
SQ4
SQ5
SQ6
SQ7
SQ8
SQ9
LQ1
LQ2
Total
Scores




























Short Questions

Q1) (2pts)
What is the output of java Test?
public class Test {
    public static void main(String[] args) {
        String[] array = {"Amy", "Benny", "Charles"};
	String output = "";
	for(int i = 0; i < 3; i++) {
	    output = output + array[i] + i;
	}
	System.out.println(output);
    }
}
Answer:




Q2)(2 pts)
what is the output of java Test?
public class Test  {
     public static void upper(String s) {
           s = s.toUpperCase();
     }
     
     public static void main(String[] args) {
          String s = "Test";
	  upper(s);
	  System.out.println(s);
    }
}
Answer:





Q3)(2pts)
What is the output of the following code?
int x = 1, y = 4, z = 6;
if(x > 2) {
   if(y < z) {
     System.out.println("message one");
   }  else {
     System.out.println("message two");
   }
} else if ( z < 5) {
      System.out.println("message three");
} else {
      System.out.println("message four");
}
Answer:






Q4)(2pts)
Find the output of java StaticTest?
public class StaticTest {
    private static int x, y;
    static {
        x = 10;
	y = 20;
    }
    
    public static void main(String[] args) {
        StaticTest t1 = new StaticTest();
	t1.x++;
	
	StaticTest t2 = new StaticTest();
	t2.y++;
	
	StaticTest.x+=StaticTest.y;
	
	t1 = t2;
	t1.y++;
	
	System.out.println("x = " + x);
	System.out.println("y = " + y);
    }
}
Answer:





Q5)(3 pts)
Consider the following programs.
public class Super {
   protected int a ;
   protected int b;
   public Super(int a, int b) {
      this.a = a;
      this.b = b + 1;
   }
   
   public Super(int a) {
     this(a,a);
   }
}
public class Sub extends Super{
   private int c;
   
   public Sub(int c) {
     super(5);
     this.c = c;
   }
   
   public static void main(String[] args) {
       Sub s = new Sub(10);
       System.out.println(s.a);
       System.out.println(s.b);
       System.out.println(s.c);      
   }
}
What is the output of java Sub ?
Answer:







Q6) (3pts)
Which of the followings are not correct names of identifies ? Explain.
(i) student_names
(ii) class-name
(iii) 2names
(iv) final
(v) abc$def
Answer

















Q7) (3pts)
What is the output of java ClassA?
public class ClassA {
    public int x;
    public ClassA(int x) {
	this.x = x;
    }
    
    public  void increase() {
	x++;
    }
    
    public void increase(int x) {
	x++;
    }
  
  public void showX() {
      System.out.println(x);
  }
  
  public static void main(String[] args) {
      ClassA a = new ClassA(5);
      
      System.out.println("Output 1");
      a.showX();
      
      System.out.println("Output 2");
      a.increase();
      a.showX();
      
      System.out.println("Output 3");
      a.increase(a.x);
      a.showX();
  }
}
Answer:











Q8)(3 pts) Circle and correct the compiler errors of the following program:
import java.awt.*;
import javax.swing.*;

public class Circle extends JFrame{
    public void paint(Graphics g) {
	int x = 5;
	Color color;
	if(x = 5) {
	    color = new Color(0.5,0.5,0.5);
	    g.setColor(color);
	    g.drawOval(50,50,100,100);
	} else {
	    color = new Color(10,20,30);
	    g.setColor(color);
	    g.drawOval(50,50,50,50);
	}
    }
    
    public static void main(String[] args) {
        JFrame f = new Circle();
	f.setSize(200,200);
	f.setVisible(true);
    }
}
Answer:



















Q9) (1pts)
What is the output of the following program?
public class ArrayTest {
    public static void main(String[] args) {
        int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
	System.out.println(a[2][2]/a[0][1]);
    }
}
Answer


Long Questions

Q1 (11 pts)
The class LongQuestion1 has 2 methods Example, the output of the following code
       System.out.println(count("How are you?", 'o'));
       System.out.println(count("Hello", 'e'));
       int[] array = position("Java is cool!", 'a');
       for(int i = 0; i < array.length; i++) {
	   System.out.print(array[i]+ " ");
       }
is
2
1
1 3
public class LongQuestion1 {
    public static int count(String s, char ch) {
         // your code
	 






















} public static int[] position(String s, char ch) { // your code
























} }
Q2(13pts)
Write a program such that
java DrawLine x1 y1 x2 y2 .... xk yk draw black lines joining (x1, y1), (x2, y2), ......., (xk, yk).
Here are some sample outputs
C:> java DrawLine 10 20 30
There should be at least 4 arguments.
C:> java DrawLine 10 20 30 40 50 60 70
The number of arguments should be even.
C:> java DrawLine 100 100 150 150

C:>java DrawLine 50 50 150 50 150 125 50 175 100 100

Other points: In below is part of the codes.
import java.awt.*;
import javax.swing.*;

public class DrawLine extends JFrame {
     // member(s) and constructor(s)
     // your code
    



















public void paint(Graphis g) { // your code





























} public static void main(String[] args) { // your code



















DrawLine f = ______________________; f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); f.setSize(200,200); f.setVisible(true); } }