Download CSC1351, Midterm, Spring 2012, 03/06/2012, page 1 of 6 Name

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSC1351, Midterm, Spring 2012, 03/06/2012, page 1 of 6
Name: __________________
Question 1: Given an example of an interface. Show as many features of the interface construct as you
can (e.g. inheritance, methods, etc.)
Question 2: Give an example of an abstract class. Show as many features of the abstract class construct
as you can (e.g. inheritance, methods, etc.)
Question 3: Write a complete Java source file to define an enum that describes the days of the week.
Question 4: Write a complete example of an anonymous inner class.
CSC1351, Midterm, Spring 2012, 03/06/2012, page 2 of 6
Question 5: Polymorphism
public class Calc {
public static Number add(Number a,Number b) {
return new Double(a.doubleValue()+b.doubleValue());
}
public static void printAdd(Number a,Number b) {
System.out.println(a+" + "+b+" = "+add(a,b));
}
public static void main(String[] args) {
Integer a = 3;
Double b = 9.7;
printAdd(a,b);
}
}
Describe what polymorphism is, and point out where it is used in the above example. Be as specific as
possible.
Question 6: Write the Applet version of a "Hello World" program that prints writes "Hello World" on a
green background.
CSC1351, Midterm, Spring 2012, 03/06/2012, page 3 of 6
Question 7: Callbacks
public class Var {
interface Watcher {
void updated(int oldval, int newval)
}
private int val;
public Watcher w;
public void setVal(int newval) {
if(val != newval) {
int oldval = val;
val = newval;
w.updated(oldval,newval);
}
}
public int getVal() { return val; }
}
Write a complete class called VarMonitor that uses a callback to see the changes in the value of an
object of type Var.
Question 8: Which of the following methods are members of StringBuffer and which are members of
String? (choose one or more)
a) char charAt(int)
b) int length()
c) int size()
d) void append(Object)
e) void setLength(int)
CSC1351, Midterm, Spring 2012, 03/06/2012, page 4 of 6
Question 9: Write the toString() method for a class named Name that is initialized with an array strings.
When Name is used in the class below, it prints all the names in the array inside curly brackets and with
a plus sign between them.
public class Names {
String[] nvals;
public Names(String[] n) { nvals = n; }
public static void main(String[] args) {
Names n = new Names(new String[]{"Amy","Jo","Fred","Will"}");
System.out.println("n = "+n); // prints t = {Amy+Jo+Fred+Will}
}
public String toString() {
// What goes here?
}
}
Question 10: Which of the following are primitive types? (choose one or more)
a) int
b) java.lang.Integer
c) java.lang.String
d) java.util.Arrays
e) java.util.Collections
Question 11: Write a regular expression to match a name that contains both a title, first name, middle
initial, and last name. For example: Ms. Amy J. Cartwright, Dr. Ursla K. Stevens, Mr. Ralph P. Usher.
CSC1351, Midterm, Spring 2012, 03/06/2012, page 5 of 6
Question 12: Which of the following are valid regular expression pattern elements? Give examples of
each one that you choose.
a) Character Class
b) Group
c) Quantifier
d) Lazy Group
e) Reluctant Qualifier
f) Backreference
g) Forwardreference
h) Literal
i) Polymorphic Literal
Question 13: Write a regular expression that matches a sequence of three to nine characters which are
not white space characters.
Question 14: Write a complete program that reads a file and prints the contents one line at a time to the
screen.
Question 15: Which of the following are valid methods for an applet? (choose one or more)
a) void init()
b) void paint(java.awt.Graphics)
c) int parseInt(int)
d) void setBackground(java.awt.Color)
e) void addKeyListener(java.awt.event.KeyListener)
f) void drawString(String,int,int)
CSC1351, Midterm, Spring 2012, 03/06/2012, page 6 of 6
Question 16: For each name listed below, identify whether it is a class, an abstract class, or an interface.
a) java.io.OutputStream
b) java.awt.event.KeyListener
c) java.lang.Integer
d) java.io.FileOutputStream
e) java.io.Serializable
Question 17: Write a complete code example to use regular expressions to match the pattern "hello"
against the string "hello".
Related documents