Q1)
a) null
b) not equal
c) 5
d)(0,1) (1,2) (2,3)
e) E f) (i) D (ii) E g)
7
10
7
9
h)m,a,x,e, Q2)
a)

b)

Q3)
a)
Error 2
Hello C
Hello D
b)
Error 4
Hello C
Hello D
c)
Hello A
Error 2
Hello C
Hello D
d)
Hello A
Error 4
Hello C
Hello D
e)
Hello A
Hello B
Result is 3
Hello C
Hello D
f)
Hello A
Hello B
Error 3
Hello C
Hello D
Q4) a)
Suppse Vector v is a vector consists of String. Write a short program to find the number of strings in vector v that contain substring abc.
   1:import java.util.*;
   2:
   3:public class CountString {
   4:    public static int countString(Vector v, String sub) {
   5:        int count = 0; 
   6:        for(int i=0; i < v.size(); i++) {
   7:            if(((String)v.get(i)).indexOf(sub) >= 0) {
   8:                count++;
   9:            }
  10:        }
  11:        return count;
  12:    }
  13:    
  14:    public static void main(String[] args) {
  15:        // for testing
  16:        Vector v = new Vector(); 
  17:        v.add("abcdef");
  18:        v.add("cgefgh");
  19:        v.add("cdeabccd");
  20:        System.out.println(countString(v, "abc"));
  21:    }
  22:}
Q5)
(a)
   1:import java.io.*;
   2:
   3:public class ProgramA {
   4:    public static void writeString(OutputStream out,  String s) {
   5:        if(out !=null) {
   6:            for(int i = 0; i < s.length(); i++) {
   7:                int x = s.charAt(i);
   8:                try {
   9:                    out.write(x);
  10:                } catch (IOException ex) {
  11:                }
  12:            }
  13:        } 
  14:    }
  15:    
  16:    public static void main(String[] args) {
  17:        writeString(System.out, "hello");
  18:    }
  19:}
(b)
   1:public class ProgramB {
   2:    public static void main(String[] args) {
   3:        double a = 0;
   4:        try {
   5:            a = Double.parseDouble(args[0]);
   6:            double c = a/0;
   7:        } catch (NumberFormatException ex) {
   8:            // do nothing
   9:        }
  10:        
  11:        if(a > 10000) {
  12:            System.out.println("args[0] is bigger than 10000.");
  13:        }
  14:    }
  15:}
(c)
   1:import java.util.*;
   2:
   3:public class ProgramC {
   4:    public static void main(String[] args) {
   5:        LinkedList list = new LinkedList();
   6:        for(int i = 0; i < args.length; i++) {
   7:            list.add(args[i]);
   8:        }
   9:        
  10:        int[] array = {1,2,3};
  11:        for(int i = 0; i < array.length; i++) {
  12:            list.add(new Integer(array[i]));
  13:        }
  14:    }
  15:}
(d)
   1:import java.util.*;
   2:import java.awt.*;
   3:
   4:public class ProgramD {
   5:    private LinkedList list = new LinkedList();
   6:    
   7:    public ProgramD() {
   8:        list.add(Color.red);
   9:        list.add(Color.blue);
  10:        list.add(Color.green);
  11:    }
  12:    
  13:    public Color firstColor() {
  14:        return (Color)list.get(0);
  15:    }
  16:}
(e)
   1:public class ProgramE {
   2:   public static void main(String[] args) {
   3:       double x = 5.3, y =7.7;
   4:       int sum; // you are not allowed to change this line
   5:       sum = (int)(x + y);
   6:   }
   7:}
(f)
no error.
Q6
int count = 0;
if(f.isDirectory()) {
    return -1;
} else {
   String[] files = file.list();
   for(int i = 0; i < files.length; i++) {
       if(files.[i].endsWith("." + ext") {
          count++;
       }
   }
}
return count;
Q7)
   1:import java.applet.*;
   2:import java.awt.*;
   3:import javax.swing.*;
   4:
   5:public class ColorLabel extends Applet {
   6:    public void init() {
   7:        setLayout(new BorderLayout());
   8:        JLabel label = new JLabel(getParameter("text"));
   9:        int r = Integer.parseInt(getParameter("red"));
  10:        int g = Integer.parseInt(getParameter("green"));
  11:        int b = Integer.parseInt(getParameter("blue"));
  12:        label.setOpaque(true);
  13:        label.setBackground(new Color(r,g,b));
  14:        add(label);
  15:    }
  16:}
Q8
   1:import java.awt.*;
   2:import javax.swing.*;
   3:import java.awt.event.*;
   4:
   5:public class WhoAmI extends JFrame implements ActionListener {
   6:    private JLabel topLabel = new JLabel();
   7:    private JPanel middlePanel = new JPanel(new GridLayout(2,5)) ;
   8:    private JLabel bottomLabel = new JLabel();
   9:    private JButton[] buttons;
  10:    private Container contentPane = getContentPane();
  11:    private String[] names = {"Apu", "Bart", "Burns", "Homer", "Krusty", "Lisa",
    : "Maggie", "Marge", "Moe", "Ned"};
  12:    private String who;
  13:    private int trial = 0;
  14:    private int correct = 0;
  15:    
  16:    public WhoAmI() {
  17:        buttons = new JButton[names.length];
  18:        for(int i = 0; i < buttons.length; i++) {
  19:            buttons[i] = new JButton(new ImageIcon("simpson/" + names[i] + ".gif
    :"));
  20:            buttons[i].setActionCommand(names[i]);
  21:            buttons[i].addActionListener(this);
  22:            middlePanel.add(buttons[i]);
  23:        }
  24:        
  25:        contentPane.add(topLabel, BorderLayout.NORTH);
  26:        contentPane.add(bottomLabel, BorderLayout.SOUTH);
  27:        contentPane.add(middlePanel);
  28:        setWho();
  29:        topLabel.setText("Who is " + who + "?");
  30:        bottomLabel.setText("Num of trials : " + trial + "     " + "Correct answ
    :ers : " + correct );
  31:    }
  32:    
  33:    public void setWho() {
  34:        who = names[(int)(names.length*Math.random())];
  35:    }
  36:    
  37:    public void actionPerformed(ActionEvent e) {
  38:        if(e.getActionCommand().equals(who)) {
  39:            correct++;
  40:            trial++;
  41:            setWho();
  42:            topLabel.setText("You are right! Next question: who is " + who + "?"
    :);
  43:        } else {
  44:            trial++;
  45:            setWho();
  46:            topLabel.setText("You are wrong! Next question: who is " + who + "?"
    :);
  47:        }
  48:        bottomLabel.setText("Num of trials : " + trial + "     " + "Correct answ
    :ers : " + correct );
  49:    }
  50:    
  51:    public static void main(String[] args) {
  52:        JFrame f = new WhoAmI();
  53:        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54:        f.setSize(280,180);
  55:        f.setVisible(true);
  56:    }
  57:}
  58:            
Q9)
   1:import java.util.*;
   2:import java.io.*;
   3:
   4:public class FinalScore {
   5:    public static void main(String[] args) throws IOException {
   6:        String inFile = "score.txt";
   7:        String outFile  = "result.txt";
   8:        BufferedReader in = new BufferedReader(new FileReader(inFile));
   9:        PrintStream out = new PrintStream(new FileOutputStream(outFile));
  10:        StringTokenizer st = null;
  11:        String line = null;
  12:        double score = 0;
  13:        while((line = in.readLine()) != null) {
  14:            score = 0;            
  15:            st = new StringTokenizer(line);
  16:            out.print(st.nextToken());
  17:            out.print('\t');
  18:            for(int i = 0; i < 5; i++) {
  19:                score+=0.6*Double.parseDouble(st.nextToken());
  20:            }
  21:            score+=0.5*Double.parseDouble(st.nextToken());
  22:            score+=0.45*Double.parseDouble(st.nextToken());
  23:            out.println(score);
  24:        }
  25:        
  26:    }
  27:}