=========================preview======================
(COMP201)2001_s_mid_comp201_sol.pdf
Back to COMP201 Login to download
======================================================
HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY
COMP 201: Java Programming
Spring 2001
Midterm Examination
7:00pm-9:00pm 29 March, LTB
Student Name: Email Address:
Student Number: Lab Section/TA Name:
Instructions:
1.
This is a closed-book, closed-note examination.
2.
Check that you have all 12 pages (including this cover page).
3.
Answer all questions in the space provided. Rough work should be done on the back pages.
4.
In case you need more space, there is one extra page at the end.
5.
Dont use pencils.
Question
Score / full score
1
/20
2
/5
3
/15
4
/5
5
/5
6
/10
7
/5
8
/8
9
/5
10
/15
11
/7
Total
/100
Question 1(20 marks; 2 marks for each correct answer and -1 mark for each incorrect answer):
Is each of the following statements always true?
1.
An abstract method is a method that does nothing.
F
2.
Suppose method1 calls method2. If method2 throws IOException, so must method1.
F
3. Assigning a double value to an int variable is legal, but might lead to information loss.
T
4. Assigning an object of a subclass to a superclass variable is legal, but might lead to information loss
F
5. In general, the expression x.clone()==x is false.
T
6. You dont need to handle exceptions when you are sure that your code is error-free
F
7. Protected fields of a class can be accessed by its all subclasses.
F
8. All methods in an interface are public.
T
9. There is o need to have a class implement the Cloneable interface if the default implementation of clone is good enough for that class.
F
10. friendly is not a key word in Java.
T
Question 2 (5 marks):
You want to find out the value of the last element of an array. You write the following code. What will
happen when you compile and run it.?
public class MyAr{
public static void main(String argv[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
This code will compile, but at run-time you will get an ArrayIndexOutOfBounds exception. This
because counting in Java starts from 0 and so the 5th element of this array would be i[4].
All or nothing.
Question 3 (15 marks):
Illustrate the concept of polymorphism with an example.
Key ingredients of example:
Two classes, a superclass and a subclass: 1
Both classes have a method with the same signature, say x(): 1
The method in the subclass overrides the one in the superclass: 2
Two o