1. What are the three characteristics of object-oriented languages?
(3 pts)
encapsulation, inheritance, and polymorphism
2. What are the two parts of a class? (2 pts)
data and functions
3. What is the difference between a class and an object? (2 pts)
class is like a blueprint; an object is an instance of that blueprint
(there may be zero or more instances of a class)
4. What is a good example to distinguish between a handle and an object?
(3 pts)
Point p = new Point();
left side defines a handle and right side assigns an object to
that handle
5. What are the eight primitive types? (8 pts)
boolean, char, byte, short, int, long, float double
6. What is garbage collection and how does it apply to Java? (2
pts)
reclaiming inaccessible memory previously dynamically allocated
Java uses garbage collection in place of explicit destructor
like C++
7. What terminology is generally used in place of data member and member
function? (2 pts)
field and method
8. What are the three kinds of comments? (3 pts)
//, /* */, /** */ (for single line, multiple line, and JavaDoc)
9. What is the easiest way to deal with operator precedence? (1
pts)
use parentheses to show intended precedence
10. What are four syntactic ways to say increment a variable i?
(4 pts)
++i; i++; i += 1; i = i + 1
11. Write a "Hello, Java!" application which is contained in the file
Hello.java. (10 pts)
public class Hello { // 2 pts
public static void main(String[] args) { // 4 pts
System.out.println("Hello, Java!");
// 2 pts
} // 1 pt
} // 1 pt
12. Write a simple global class Contestant which has two local data called name (String) and score (int). (40 pts)
The class should include two global constants called MIN_SCORE (0) and MAX_SCORE (300).
The class should include a global constructor, two global set accessors, two global get accessors, a global toString method, and a global perfect method.
The constructor argument types should be String and int in that order. The constructor should use the appropriate accessors.
Indirectly, the constructor should check that the score is from MIN_SCORE through MAX_SCORE (or be given the minimum value) and that the name is not changed by any later outside-of-the-class action.
The format for the toString method should be "name = <name> and score = <score>" where <name> and <score> are replaced by the appropriate values.
The perfect method should return whether or not the score is the maximum possible.
To save writing, you may omit comments in this answer.
Place your answer on the front side of the next page.
// (5 pts) for following class and constants
public class Contestant {
public static final int MIN_SCORE = 0;
public static final int MAX_SCORE = 300;
// (5 pts) for following constructor
public Contestant(String name, int score) {
setName(name);
setScore(score);
}
// (10 pts total - 5 pts each) for following two set accessors
public void setName(String n) {
name = new String(n);
}
public void setScore(int s) {
if ((s < MIN_SCORE) || (s > MAX_SCORE))
{
score = MIN_SCORE;
} else {
score = s;
}
}
// (5 pts) for following two get accessors
public String getName() {
return name; // or return new
String(name);
}
public int getScore() {
return score;
}
// (10 pts total - 5 pts each) for the following two methods
public String toString() {
return "name = " + name + " and score
= " + score;
}
public boolean perfect() {
return score == MAX_SCORE;
}
// (5 pts) for following fields and end of class
private String name;
private int score;
}
13. Why is the difference between = and == less of a problem in Java?
(2 pts)
in Java, comparisons in decisions and loops must return boolean
assignments (=) normally do not return boolean
14. Why does Java not have a sizeof operator? (2 pts)
all data types (primitive and classes) are the same size on all
architectures
(therefore, the main use of sizeof (for portability) is not needed
in Java)
15. What does "Ulcer Addicts Really Like C A lot" have to do with anything?
(1 pt)
it is a mnemonic for operator precedence
16. What is overloading (and what is overloaded in Java)? (2 pts)
multiple uses of operators with different types or methods with
different arguments;
in Java, method names and + for string concatenation
17. How are multiple constructors in the same class distinguished?
(2 pts)
by the number and type of their parameters
18. What is the importance of the word "this"? (1 pt)
this refers to (handle for) the current object
19. What three points about garbage collection does your CD text make?
(3 pts)
"Garbage collection is not destruction."
"Your objects might not get garbage collected."
"Garbage collection is only about memory."
20. What is the difference between the finalize and delete methods?
(2 pts)
In Java, finalize should be used very sparingly for specially
allocated memory release
In C++, delete should be used in destructor for releasing dynamically
created fields
21. What does "int a1[]" allocate? (2 pts)
handle a1 for integer array
22. What must be done to define a ten-element integer array? (3
pts)
e.g., int[] a1 = new int[10];