=========================preview======================
(COMP201)sp05_midterm_solution.pdf
Back to COMP201 Login to download
======================================================
HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY
COMP 201: Java Programming

Spring 2005
Midterm Examination
March 30, LTG


Student Name: Email Address:


Student Number: Lab Section/TA Name:



Instructions:

1.
This is a close-book, close-note examination.


2.
Check that you have all 15 pages (including this cover page).


1.
Answer all questions in the space provided. Rough work should be done on the back pages.


2.
Dont use pencils.






Question
Score / full score

1
/30

2
/32

3
/5

4
/15

5
/18

Total
/100












Question 1(30 marks total, 3 marks for each question )
a ) How can you initialize an array of two characters to 'a' and 'b'? Select one correct answer. a. char[] charArray = new char[2]; charArray = {'a', 'b'}; b. char[2] charArray = {'a', 'b'}; c. char[] charArray = {'a', 'b'}; d. None of the above.
Answer:________C_________________________________________________


b) Given the following class definition:
1. public class DerivedDemo extends Demo{
2. int M, N, L ;
3. public DerivedDemo( int x, int y ){
4. M = x ; N = y ;
5. }
6. public DerivedDemo( int x ){
7. super( x );
8. }
9. }
Which of the following constructor signatures MUST exist in the Demo class for DerivedDemo to compile correctly? Select all which are correct.


a)
public Demo( int a, int b )


b)
public Demo( int c )



c) public Demo( )
Answer:_________B,C________________________________________________
c) Here is the class hierarchy showing the ActionEvent family tree:
java.lang.Object
|--- java.util.EventObject
|---java.awt.AWTEvent
|---- java.awt.event.ActionEvent

Suppose we have the following code to count events and save the most recent event.
1. int evtCt = 0 ;
2. AWTEvent lastE ;
3. public void saveEvent( AWTEvent evt ){
4. lastE = evt ;
5. evtCt++ ;
6. }
Wh
ich of the following calls of saveEvent would run without causing an exception. Select all which are correct.




a)
call with an AWTEvent object reference


b)
call with an ActionEvent object reference



c) call with an EventObject object reference
d) call with null value
Answer:_____________A,B,D____________________________________________



d)
Once created, some Java objects are "immutable", meaning they can not have their contents changed. Which of the following classes produce immutable objects? Select all which are correct.




a)
java.lang.Double


b)
java.lang.StringBuilder



c) java.lang.Boolean
d) java.lang.Math
Answer:______________A,C___________________________________________
e) In the following code for a class in which methodA has an inner class.
1. public class Ba