| Last Name:_________________ | First Name:________________ |
| Student ID:_________________ |
| Question | MC | Short Q1 | Short Q2 | Short Q3 | Long Q1 | Long Q2 | Long Q3 | Total |
| Score |
String s = new String("PIC20A is great");
Which of the calls listed here modify the string? s.toUpperCase();s.substring(2);s.indexOf('P');
1. class A {
2. public static void main(String[] args) {
3. Integer i = new Integer(5);
4. i.setIntValue(i.intValue()+1);
5. System.out.println("i = " + i);
6. }
7. }
A) Compiler error.
1. public class X {
2. int[] intarr;
3.
4. public static void main(String[] args) { new X(); }
5.
6. X() {
7. int s = 100;
8. intarr = new int[s];
9. System.out.println("value = " + intarr[s-1]);
10. System.out.println("value = " + intarr[s]);
11. }
12. }
A) Compiler error on line 8.1. int x = 3; int y = 10; 2. System.out.println(y % x);A) 0
1. final class Aaa
2. {
3. int xxx;
4. void yyy() { xxx = 1; }
5. }
6.
7.
8. class Bbb extends Aaa
9. {
10. final Aaa finalref = new Aaa();
11.
12. final void yyy()
13. {
14. System.out.println("In method yyy()");
15. finalref.xxx = 12345;
16. }
17. }
A) On line 1, remove the final modifier.1. Object ob = new Object(); 2. String stringarr[] = new String[50]; 3. Float floater = new Float(3.14f); 4. 5. ob = stringarr; 6. ob = stringarr[5]; 7. floater = ob; 8. ob = floater;A) Line 3
Animal
|
Mammal
|
--------------------------------------------
| | | |
Dog Cat Raccoon SwampThing
(implements (implements
Washer) Washer)
Consider the following code:
1. Raccoon rocky; 2. SwampThing pogo; 3. Washer w; 4. 5. rocky = new Raccoon(); 6. w = rocky; 7. pogo = w;Which of the following statements is true? (Choose one.)
1. String s1 = "abc" + "def";
2. String s2 = new String(s1);
3. if (s1 == s2)
4. System.out.println("== succeeded");
5. if (s1.equals(s2))
6. System.out.println(".equals() succeeded");
A) Lines 4 and 6 both execute.1. g.setColor(Color.black); 2. g.drawLine(10, 10, 10, 50); 3. g.setColor(Color.red); 4. g.drawRect(100, 100, 150, 150);A) A red vertical line that is 40 pixels long and a red square with sides of 150 pixels
1. File f1 = new File("dirname");
2. File f2 = new File(f1, "filename");
A) A new directory called dirname is created in the current working directory.
1. class Switcheroo {
2. public static void main(String[] args) {
3. int i = 0;
4. try {
5. i = Integer.parseInt(args[0]);
6. }
7. catch (Exception e) { }
8.
9. switch (i) {
10. case 0:
11. System.out.println("zero");
12. case 1:
13. System.out.println("one");
14. default:
15. System.out.println("default");
16. }
17. }
18. }
java.lang.Exception
\
java.io.IOException
/ \
java.io.StreamCorruptedException java.net.MalformedURLException
1. try {
2. // assume s is previously defined
3. URL u = new URL(s);
4. // in is an ObjectInputStream
5. Object o = in.readObject();
6. System.out.println("Success");
7. }
8. catch (MalformedURLException e) {
9. System.out.println("Bad URL");
10. }
11. catch (StreamCorruptedException e) {
12. System.out.println("Bad file contents");
13. }
14. catch (Exception e) {
15. System.out.println("General exception");
16. }
17. finally {
18. System.out.println("Doing finally part");
19. }
20. System.out.println("Carrying on");
1. public class Base {
2. public void method(int i) {
3. System.out.println("Value is " + i);
4. }
5. }
1. public class Sub extends Base {
2. public void method(int j) {
3. System.out.println("This value is " + j);
4. }
5. public void method(String s) {
6. System.out.println("I was passed " + s);
7. }
8. public static void main(String args[]) {
9. Base b1 = new Base();
10. Base b2 = new Sub();
11. b1.method(5);
12. b2.method(6);
13. }
14. }
What output results when the main method of the class Sub is run?
java Test 1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class Test { 5: public static void main(String[] args) { 6: try { 7: JButton[] buttons = new JButton[10]; 8: for(int i = 0; i < 20; i++) { 9: buttons[i].setBackground(Color.red); 10: } 11: } catch(IndexOutOfBoundsException ex) { 12: System.out.println("IndexOutOfBoundsException"); 13: } catch(NullPointerException ex) { 14: System.out.println("NullPointerException"); 15: } catch(Exception ex) { 16: System.out.println("Exception"); 17: } 18: 19: System.out.println("End of the program"); 20: } 21:}
public class Citizen {
public String name;
public String SSN; // 9 digit social security number
public Citizen(String name, String SSN) {
this.name = name;
this.SSN = SSN;
}
public boolean equals(Object obj) {
if(obj instanceof Citizen) { // if obj is of type citizen
return ((Citizen)obj).SSN.equals(this.SSN);
} else {
return false;
}
}
public String toString() {
return "name : " + name + "\n"
+ "SSN : " + SSN;
}
}
Write down the output of the following program
import java.util.*;
public class CitizenTest {
public static void main(String[] args) {
LinkedList list = new LinkedList();
Citizen c1 = new Citizen("Amy", "111223333");
Citizen c2 = new Citizen("Ben", "222334444");
list.add(c1);
list.add(c2);
list.add(new Citizen("Charles", "333445555"));
list.add(new Citizen("David", "444556666"));
list.add(new Citizen("Eve", "555667777"));
list.remove(c2);
list.add(2, new Citizen("Florence", "666778888"));
System.out.println("Output1:");
System.out.println(list.get(1));
list.set(1, new Citizen("Charles", "777889999"));
list.removeLast();
System.out.println("Output2:");
for(int i = 1; i <=3; i++) {
System.out.println(list.get(i));
}
System.out.println("Output3:");
System.out.println(list.contains(new Citizen("Unknown","111223333")));
System.out.println("Output4:");
System.out.println(list.indexOf(new Citizen("Amy", "777889999")));
}
}
BufferedInputStream and BufferedOutputStream?
Color random(). The function randomly returns a new color.Color(float r, float g, float b) with r,g,b between 0 and 1. static double random() method in Math class. it returns a random
number between 0 and 1.
Color random() {
// your code
}
Question 3(10pts)int count(File f, String extension) returns
f is not a folderf with the given extension (that is the file name ends with dot extension, e.g. .jpg or .html)f
Example
In a folder namedhw4, there are 6 filesa1.jpg, a2,jpg, a3.jpg, a4.jpg, hw4.java, hw4.class
count(new File('hw4"), "jpg") returns 4 because there are 4 files end with ".jpg"count(new File("hw4"), "class") returns 1 because there is one file ends with ".class"count(new File("hw4"), "gif") returns 0endsWith method in String class.
int count(File f, String ext) {
// your codes
}
char encrypt(char ch). The table below shows us how to evaluate the function
| ch | returns |
| a | v |
| b | d |
| c | s |
| d | a |
| s | b |
| v | c |
| other characters | returns the same character |
boolean encrypt(File in, File out)
encrypt the content of File in by the above encrypt function
and write the content to the File out. a, b, c, d are the first 4 letters.Then the content of
File out will be
v, d , s, a vre the firbt 4 letterb.The function returns
true if there is no exception. Returns false otherwise.
Fill in your answer below
public class Question1 {
public static char encrypt(char ch) {
// your codes
}
public boolean encrypt(File in, File out) {
// your codes
}
}
DoubleVector.
The class represents a list of doubles.
Here is a constructor and methods
2.1 3.4 4.5
DoubleVector with all the negative elements
(i.e. < 0) removed.
import java.util.*;
public class DoubleVector {
private Vector v;
// your codes
}
}

JFrame, with 4 JButtons and 1 JLabel.
![]() homer.gif |
![]() marge.gif |
![]() bart.gif |
![]() lisa.gif |
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Simpson extends JFrame implements ActionListener
{
// write your codes here
// variables
// constructors
public Simpson() {
}
public void actionPerformed(ActionEvent e) {
}
// other codes (if any)
public static void main(String[] args) {
JFrame f = new Simpson();
f.setSize(200,100);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}