PIC20A Midterm I 2004 Spring

Instructions

  1. The exam consists of 7 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 50. 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
Q1(19pts)
Q2(15pts)
Q3(16pts)
Total
Scores




















Question 1
(a)
What is the output of the java S1?
public class S1 {
    public  static void method(String s) {
        s = s.toUpperCase();
        s.replace('A', 'b');
        s = s + 1234;
    }
    
    public static void main(String[] args) {
        String s = "abcdefg";
        method(s);
        System.out.println(s);
    }
}
Answer:




(b)
What is the output of the following codes.
String[] names = new String[20];
System.out.println(names[0]);
Circle the correct answer(s)
(c)
What is the output of the java ContTest?
public class ContTest {
    public static void main(String[] args) {
        for(int i = 1; i < 8; i+=2) {
            if(!(i%3 == 1)) {
                continue;
            }
            System.out.print(i);
        }
    }
}
Answer:




(d)
What is the output of java T2?
T1.java
public class T1 {
    public static int x = 5;
    public int y;
    
    public T1() {
        this(x+1);
    }
    
    public T1(int y) {
        this.y = y;
        x++;
    }
    
    public void increase() {
        y++;
        x++;
    }
    
    public void function(T1 t1) {
      t1 = new T1(7);
    }
    
    public String toString() {
        return "x = " + x + " y = " + y;
    }
}
T2.java
public class T2 extends T1 {
    public int z;
    
    public T2() {
        x++;
        z = 1;
    }
    
    public T2(int z) {
        super(2*z);
        this.z = z + 2;
    }
    
    public String toString() {
        return super.toString() + " z = " + z;
    }
    
    public static void main(String[] args) {
        T2 t= new T2();
        System.out.print("a: ");
        System.out.println(t);
        t.increase();
        System.out.print("b: ");
        System.out.println(t);       
        t = new T2(2);
        System.out.print("c: ");
        System.out.println(t);
        t.x++;
        T1.x++;
        T2.x++;
        t.function(new T2(5));
        System.out.print("d: ");
        System.out.println(t);
    }
}
Answser










(e)
For the following program, explain and sketch the output(if possible) of (a)java GTest (b)java GTest Hello World
import java.awt.*;
import javax.swing.*;

public class GTest extends JFrame {
    String s;
    
    public GTest(String s) {
        super(s);
        this.s = s;
    }
    
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawRect(50,50,100,100);
        g.drawOval(100,100,100,100);
        g.drawString(s, 50,50);
    }
    
    public static void main(String[] args) {
        GTest t = new GTest(args[0]);
        t.setSize(200,200);
        t.setVisible(true);
    }
}
Answers


























Question 2
Circle the compiler errors and explain(in few lines) why they are errors. Each program may have more than one mistakes.
(a)
public class ClassA {
    public static int a = 10;
    public int b;
    public int c;
    public ClassA(int x, int y) {
        x  = b;
        y = c;
        a = b + c;
    }
    
    public final int sum() {
        return b + c;
    }
    
    public void changeMe() {
        b+=1;
    }
}
      
Explanation:










(b) Suppose you fix all the error(s) in ClassA given in part (a)
.
public class ClassB extends ClassA {
    public int x;
    public int b;
    public int c;
    public final ClassA z;
    public ClassB(int x, int b, int c) {
        super(b,c);
        this.x = x;
        z = new ClassA(1,1);
    }
    
    public int methodA() {
        return a + b;
    }
    
    public int methodB() {
        z.changeMe();
    }
    
    public final int sum() {
        return x + b + c;
    }
}
  
Explanation:





(c)
public class Alpha {
    private int privateInt;
    protected int protectedInt;
    public double publicDouble;
    protected Alpha(int x, int y, int z) {
      privateInt = x;
      protectedInt = y;
      publicDouble = z;
    }
    
   public void method(int a) {
        privateInt = a;
    }
}



public class Beta extends Alpha {
    public Beta() {
        super(1,2,3);
    }
    
    public void  methodA() {
        privateInt = 3;
        publicDouble = 2;
        protectedInt = 15/publicDouble;
    }
    
    public void MethodB() {
        method(3);
    }
    
    public void MethodC(Alpha a) {
        privateInt = a.privateInt;
        protectedInt = a.protectedInt;
        publicDouble = a.publicDouble;
        a.method(5);
    }
}
      
Explanation:









(d)
import java.awt.Graphics;

public class ColorTest extends javax.swing.JFrame {
    public void paint(Graphics g) {
        Color color= new Color(1.5, 1.5, 1.5);
        g.setColor(color);
        g.drawRect(0,0,50,50);
    }
}
Explanation:










Question 3
Recall the rules of naming a Java idenitifier:
Write a program to test whether a given string is a correct identifier or not.
Hint
public class Question3 {
    // here is a partial list of java keywords, I assume this set is ALL the java keywords
    static String[] javaKeywords= {"if", "else", "while", "do", "switch", 
                                   "case", "int", "float", "double", "char"}; 
    
    
    // the method returns true if String s is a correct java identifier, returns false otherwise
    // my answer is about 10 lines long, of course, you can write more than 10 lines
    public static boolean isJavaIdentifier(String s) {
        // your codes
        


































} }