FACULTY OF
ENGINEERING
CSI1102/CSI1502A
Examination
April 2004
Examiners:
Dr. Diana Inkpen (CSI1102 Groups
A, D)
Dr. Herna L Viktor (CSI1102
Groups B, C)
Dr. Fadi Malek (CSI1502A)
Total marks: 74
Duration: 3 hours
Total number of pages: 18
Important
Regulations:
1. Students are not allowed to bring in any notes.
2. No calculators are allowed.
3. No electronic equipment is allowed, including mobile
phones and laptop computers.
4. Student identification cards (or another photo ID with
signature) are required.
5. An attendance sheet shall be circulated and should be
signed by each student.
6. Please answer all questions on this paper, in the indicated
space.
7. All questions are related to the Java object-oriented
language.
Part A : [15 marks, 1 mark for each question]
State whether the following statements are True or False. Use the answer sheet provided
below to indicate your answer.
1.
An inner class
cannot have static instance variables.
2.
Static instance
variables cannot be private.
3.
A static method
cannot refer to an instance variable in the same class.
4.
The garbage
collector is the mechanism in Java used to automatically free allocated memory.
5.
The
this reference always
refers to the current executing object.
6. An array index cannot be a float, double, boolean
or String.
7. Consider an int array named myarray which consists of 5 elements.
Suppose you want to swap the 3rd
and 4th elements in the myarray. The following code will accomplish the
swap.
myarray[3] = myarray[4];
myarray[4] = myarray[3];
8. In black-box testing, the tester should
have a detailed knowledge of how the program is implemented so that he/she can
more carefully identify what portion(s) of the software are leading to errors.
9. Consider the following code
fragment.
public
class X extends Y
{
private int y;
public X()
{
y = 10;
super( );
}
… }
The instruction super( ); as located in the code fragment
above, causes the
constructor of class Y to be invoked.
10. Java does not support multiple inheritance,
but some of the abilities of multiple inheritance are available by using public
rather than protected or private modifiers.
Questions
11, 12, 13, 14 and 15 refer to the following UML diagram:
11. The Line class and the Shape class are in an
aggregation relation.
12. The penColour variable is not inherited by the Line
and Rectangle classes.
13. The fillColour variable should ideally be declared as public,
to enforce encapsulation.
14. The draw() method is
overloaded.
15. The draw() method in the
Shape class should be abstract.
. Answers:
Part A
Question |
Answer |
1.
|
True |
2.
|
False |
3.
|
True |
4.
|
True |
5.
|
True |
6.
|
True |
7.
|
False |
8.
|
False |
9.
|
False |
10.
|
False |
11.
|
False |
12.
|
False |
13.
|
False |
14.
|
False |
15.
|
True |
Part B: [15 marks, 1 mark for each question]
Indicate which one of the following choices is
correct. (Note: Only one of the answers is correct.) Use the answer sheet
provided below to indicate your answer.
1. Which one of the following statements is
correct?
2. What is the value of counter after the following statements have been executed?
int counter = 0;
for (int x = 3; x <= 7; x =
x + 4) {
for (int y = 1; y < x; y++) {
counter = counter + (x*y);
}
}
a.
9
b. 156
c.
214
d.
868
3. What are the values of a and b after the following loop has been executed?
int a = 0;
int b = 0;
for (a=0; a<10 &&
b<20; a=a+2) {
a = a + b;
b = b + a + 1;
}
a.
a is 10, and b is
16
b.
a is 28, and b is
45
c.
a is 30, and b is
45
d. a is 12 and b is 16
4. Suppose somebody's height in inches is stored in a
variable h. Which one of the following commands will correctly
translate this height into feet-and-inches terminology? (Note that one foot
contains 12 inches, so somebody who is 74 inches tall is usually said to be “6
feet and 2 inches”).
a. int feet = h/12;
int inches = (h -
feet*12)/12;
b. double feet = h/12;
int inches = h%12;
c. int feet = h%12;
int inches = h;
d. int feet = h/12;
int
inches = h%12;
5.
What are the
values of x and y after the following
statements have been executed?
int x =8;
int y = 4;
x = y++ - 2;
a.
x=2, y=4
b. x=2,
y=5
c.
x=3, y=5
d.
x=3, y=4
6. For the following code fragment, suppose list is an int array that stores only positive int values. Which one of the tasks written below is accomplished when
this code is executed?
int x = 0;
for (int j=0; j < list.length; j++)
if (list[j]
> x)
x =
list[j];
a.
it stores the smallest value in list (the
minimum) in x
b. it stores the largest value in list (the maximum) in x
c.
it stores every value in list, one at a
time, in x, until the loop terminates
d. it counts the number of elements in list that are greater than x
e.
it counts the number of elements in list
that are less than x
7. Suppose you want to initialize
a String array names to store the three Strings "Good",
"Luck" and "Today". The only syntactically valid statement
to accomplish this is :
a. String names = {"Good", "Luck", "Today"};
b. String[ ] names =
{"Good", "Luck", "Today"};
c. String[ ] names =
new String{"Good", "Luck", "Today"};
d. String names[3] =
{"Good", "Luck", "Today"};
e. String names; names[0] = "Good"; names[1] = "Luck"; names[2] = "Today";
8.
A bad programming
habit is to build an initial program and then spend a great deal of time
modifying the code until it is acceptable.
This is known as
a.
the prototyping
approach
b.
the waterfall
model
c.
iterative
development
d.
the recursive
approach
e.
the build-and-fix
approach
For
questions 9 and 10, use the following partial class definitions:
public class A1
{
public int x;
private int y;
protected int z;
…}
public class A2 extends A1
{
protected int a;
private int b;
…}
public class A3 extends A2
{
private int q;
…}
9. Which of the following is true with respect to
A1, A2 and A3?
a.
A1 is a subclass of A2 and A2 is a
subclass of A3
b. A1 and A2 are both subclasses of A3
c.
A3 is a subclass of A2 and A2 is a
subclass of A1
d. A2 and A3 are both subclasses of A1
e.
A1, A2 and A3 are all subclasses of the
class A
10. Which of the following lists of instance data
are accessible in A3?
a.
x, y, z, a, b, q
b. a, b, q
c.
a, q
d. x, z, a, q
e.
x, a, q
11. Indirect recursion happens when:
a.
a method calls
itself
b.
a method calls
another method that calls the initial method
c.
a method is
overloaded
d.
a method is
abstract
12. A queue is a data structure of the type:
a.
last-in last-out
b.
first-in
first-out
c.
last-in first-out
d.
first-in last-out
13. Given
the following method, what value does recur(3,6) return?
public int recur(int x,
int y)
{
if (x == y) return
y;
else return recur(x,
y-1) + 1;
}
a.
3
b.
12
c.
6
d.
8
14. A linked list is:
a.
a dynamic data
structure
b.
a static data
structure
c.
a primitive data
type
d. added both a static and dynamic data structure,
depending on the data type used
e.
all of the above
15. Infinite recursion happens when, for example:
a.
there is more
than one base case
b.
a method calls
itself with the same parameter values
c.
a condition in an
if statement is never false
d.
none of the above
Answer: Part B
Question |
Answer |
1.
|
D |
2.
|
B |
3.
|
D |
4.
|
D |
5.
|
B |
6.
|
B |
7.
|
B |
8.
|
E |
9.
|
C or D |
10.
|
D |
11.
|
B |
12.
|
B |
13.
|
C |
14.
|
A |
15.
|
B |
Part C : [15 marks, 1 mark
for each question]
Complete the following sentences by filling in the
missing words. Use the answer sheet
provided below to indicate your answer.
1. A class can simulate multiple inheritance
by implementing more than one ……
2. A …….. variable is shared
among all instances of a class.
3. A ……..class represents a primitive value so that it
can be treated as an object.
4. When an object is passed to a method, the actual and
formal ……. become aliases of each other.
5. The ………. method of a class cannot
have a return type, not even void.
6.
Consider an array declaration int[] height = new
int[11];
The value of height.length is equal to ……
7. In Java, an ArrayList is a ……… data structure.
8. A ……….. reference can refer to different
types of objects over time.
9. A class that cannot be instantiated is called …..
10. The …..…. visibility modifier provides the best possible
encapsulation that permits inheritance.
11. An ….…. is a collection of data and the particular
operations that are allowed on that data. It has a name, a domain of values and
a set of operations that can be performed.
12. Any recursive definition must have a non-recursive
part, called the ........... case,
that permits the recursion to eventually end.
13. In a stack data structure we store data at the top of
the stack using the push() method and
we take data out from the ......... of the stack using the pop() method.
14. If a method throws an exception object of a checked
type of exception, the method has to declare it by adding the keyword …………. to
its signature.
15. UML diagrams are very useful in the ……….. stage of the process of developing a software system.
Answer: Part C
Question |
Answer |
1.
|
Interface |
2.
|
Static |
3.
|
Wrapper |
4.
|
Parameters or arguments |
5.
|
Constructor |
6.
|
11 |
7.
|
Dynamic |
8.
|
Polymorphic |
9.
|
Abstract |
10.
|
Protected |
11.
|
ADT or abstract data type |
12.
|
Base |
13.
|
Top |
14.
|
Throws |
15.
|
Design |
PART D: [29 marks]
1.
Consider the following code fragment that represents a set of classes keeping
track of sport statistics.
public class Players
{ public static void main (String [] args)
{
BaseballStats
player1;
FootballStats
player2;
player1 = new BaseballStats ("Sal
Runner", "Phillies");
player2 = new
FootballStats ("Mel Rogers", "Redskins");
player1.score();
player2.score();
player1.getHit();
player2.gainYards(15);
System.out.println (player1);
System.out.println ();
System.out.println (player2);
}
}
public abstract class
PlayerStats
{
protected String player, team;
protected int score;
public PlayerStats (String playerName, String teamName)
{
player = playerName.substring(0, 5);
team = teamName;
score = 0;
}
public int getScore()
{
return score;
}
public abstract void score();
public String toString()
{
String result
= "Player: " + player;
result += "\nTeam: " +
team.toUpperCase();
result += "\nScore: " + score;
return result;
}
}
public class BaseballStats
extends PlayerStats
{
protected int hits, errors;
public BaseballStats (String player, String team)
{
super (player, team);
hits = 0;
errors = 0;
}
public void score()
{
score += 1;
}
public void getHit()
{
hits += 1;
}
public void commitError()
{
errors += 1;
}
public String
toString()
{
String result
= super.toString();
result += " Hits: " + hits;
result += "\nErrors: " + errors;
return result;
}
}
public class FootballStats
extends PlayerStats
{
protected int yards;
public FootballStats (String player, String team)
{
super (player, team);
yards = 0;
}
public void score()
{
score += 6;
}
public void gainYards (int numYards)
{
yards += numYards;
}
public String toString()
{
String result
= super.toString();
result += "\nYards: " + yards;
return result;
}
}
What
output is produced when Players.class is executed? [10]
Part D, Question 1 [10 marks]
2. What subset of the outputs, as listed in the window
below, will the following program produce? Indicate the correct choices by
choosing “Yes” for the messages that are
printed by the program and “No” for the messages that are not printed, in the answer window below. [6]
public class CreateExceptions
{
public static void main (String[] args)
{
try
{
TestException Obj1 = new TestException();
System.out.println (“Enter level 0”);
Obj1.divide();
System.out.println (“Another message level 0”);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
System.out.println ("Exit level 0.");
}
}
public class TestException
{
public void divide()
{
System.out.println
("Enter level 1");
int N = 25, M = 0;
int z = N / M;
System.out.println
("Exit level 1");
}
}
Part D Question 2 [6]
1 mark for each correct answer
3.
Suppose current and newNode are references to Node objects. Assume current currently
refers to a specific node in a single-linked list and newNode refers to an unattached Node object. The following code fragment attempt to insert
newNode behind current in the
list. However, it contains two errors.
// assume each node has a reference field called “next”
// assume “next” refers to a subsequent node if there is one
newNode.next = current;
current.next = newNode.next;
Provide
the correct code to accomplish this task. [4]
Part D, Question 3
[4]
1 mark for each “program code”; e.g. current.next
newNode.next = current.next; 2
marks
current.next = newNode; 2
marks
4. What output is produced
when the following program is executed? [9]
public class Example
{ public static void main (String[] args)
{
String s = "http://www.BELL.ca/index.html";
URL web = new URL(s);
System.out.println(web.cool(s));
web.write();
System.out.println();
String u = "www.MAGMA.Cola.ca/index.html";
web = new URL(u);
System.out.println(web.cool(u));
web.write();
System.out.println();
String v = "ftp://www.OTTAWA.ca";
web = new URL(v);
System.out.println(web.cool(v));
web.write();
}
}
public class URL
{
public String
service;
public String
host;
public URL() {}
public URL (String
url)
{
cool(url);
setURL(url) ;
}
void setURL(String
s)
{
int i =
s.indexOf("://");
if (i<0)
{
service =
"http";
s = s.substring(0);
}
else
{
service=s.substring(0,i);
s = s.substring(i+3);
}
i = s.indexOf('/');
if (i<0)
host=s;
else
host =
s.substring(0,i);
}
public String cool (String S)
{
int i; char c;
String res = " ";
for (i = 0; i
<= S.length()-1; i++)
{
c = S.charAt(i);
if (c <=
'Z' && c >= 'A')
res = res +
(char)((int)c + (int)'a' - (int)'A');
else
res = res + c;
}
return res;
}
public void write()
{
System.out.println("service="
+ service);
System.out.println("host="
+ host);
}
}
Part D,
Question 4 [9]