These are meant to be an example of the type and style of exam questions you
can expect for CP2003 year. Note that the actual exam is actually about
twice as long as this.
- True or false - A method has exactly one return statement.
- True or false - All elements of an Java array are of the same type.
- True or false - A method can change the length of an array.
- Explain the difference between
s = 0;
if (x > 0) s++;
if (y > 0) s++;
and
s = 0;
if (x > 0) s++;
else if (y > 0) s++;
- What does the term "virtual machine" mean? What are its advantages and
disadvantages?
- What is inheritance and how does Java implement it? Draw a
hierarchy scheme for various bank accounts (savings account, cheque account,
credit account, passbook account etc.).
- What is the point of Java wrapping lots of objects around each
other when you are (say) trying to read a file?
- Explain the difference between a Java String type and a character array.
- Find all potential errors in the following code (there are at least 5):
MyFunction(bufferedReader theReader) {
String NextLine = null;
try {
// read the first line (if any) from the file
NextLine = theReader.readLine();
}
catch (Exception e) { System.out.println("Error: " + e); };
while (theReader != null) {
try {
// read the next line from the file
NextLine = theReader.readLine();
}
catch (Exception e) { System.out.println("Error: " + e);
} // end of while
} // end of function
- In the context of garbage collection in Java, to what does the word
"garbage" actually refer?
- What is wrong with test to see whether r is
null? What would happen when the code runs?
Rectangle r;
// ...
if (r.equals(null))
r = new Rectangle(5,10,20,30);
- In DOS/Windows and UNIX environments, there is no special "end of file"
character stored. How do you test for end of file in a Java program?
- What is the point of Java forcing you to use try/catch blocks?
- Intuitively, what is the likely balance of b after the
following operations?
SavingsAccount b = new SavingsAccount(10);
b.deposit(5000);
b.withdraw(b.getBalance() / 2);
b.addPercentageInterest(8.5);
- How does a cast of class references such as (SavingsAccount)b
differ from a cast of number values such as (int)x?
- What are access modifiers (public, private, protected) and what
part do these play in inheritance?
- Explain the relationship between the keyword super and the
keyword this in Java.