Csc 110 Final - April 1999 - UVic ESS

advertisement
CSC 110 Final Examination
Page 1
April 1999
University of Victoria
April Examinations 1999
Computer Science 110 SO1
Instructor: J. E. Rice
Time: 3 hours
Name:
UVic ID:
Question
Marks
1
14
10
6
5
r7
8
9
10
IO
7
11
I 10
IO
I Total :
100 .
I
Instructions:
l
l
l
l
All questions are to be answered on these pages; the back of each page may
be used for rough work if necessary
This exam consists of 15 pages including one cover page; before beginning
the exam the students must ensure that they have received a complete exam
This is a closed-book, closed-notes, no calculators exam
Marks allocated for each question are shown in square brackets
Page 1 of 15
CSC 110 Final Examination
Page 2
1. [1 mark each - 14 marks total] Answer each of the following questions by selecting the best
choice from the given list.
11
. .
a)
b)
c)
d)
e)
The concept of ~veMx!i~g can be described, in terms of this course, as
declaring two different variables with the same identifier
declaring more than one method with the same identifier, but different signatures
giving the computer too long of a program to compile
declaring a subclass of a superclass
none of the above
Answer:
1.2.
The protected modifier is used when
a) the programmer wishes to protect the entire program from other programmers changing it
b) a variable should not be inherited by a subclass derived from the superclass containing
that variable
c) a variable can be inherited by a subclass derived from the superclass containing the
variable, and anything else outside the superclass should also be able to “view” that
variable
d) a variable can be inherited by a subclass derived from the superclass containing the
variable, BUT anything outside the class should NOT be able to “view” that variable
e) none of the above
Answer:
1.3. A class is defined in Java as
a)
b)
c)
d)
e)
a special type of applet
a collection of applets and data
a special type of program that can be run over the lnternet
a collection of methods and data
none of the above
Answer:
1.4. Which of the following Java statements will declare an array AND instantiate an array
object:?
a)
b)
c)
d)
e)
int prime;
int[ 1 grades = int [30]
long testscores[ 1;
int classgrades[ ] = new int[30];
none of the above
Answer:
Page2ofl5
April 1999
CSC 110 Final Examination
1.5.
Page 3
April 1999
Consider the following syntactically correct loop:
for (int i = 0; i -c 10; i++) {
System.out.println(i);
I
Which of the following Java code fragments are syntactically correct AND semantically
equivalent to the loop above?
a) int i = 0;
while (i -c 10) {System.out.println(i); i = i + 1; 1
b) int i = 0;
while (i <= 10) { i = i + 1;
c) int i = 0;
do {i = i - 1;
System.out.println(i); }
System.out.println(i); ]
d) for (int i = 0; i c 10; i-- 1 i
while (i >= 10);
System.out.println(i); )
e) none of the above
Answer:
1.6.
a)
b)
c)
d)
e)
The return type of a constructor in Java...
Must be of type int
Must be of type void
Can be of any type
Must be of the same type as the formal parameters for the constructor
none of the above
Answer:
1.7. If you were asked to write a logical, or boolean expression in Java that evaluated to true
if an int variable is outside the range 100 to 199 (inclusive) or in the range 150 to 155
(inclusive) the correct expression in Java would be:
a) (k < 100 11 k > 199) 11 (k > 150 && k c 155)
b)
c)
d)
e)
(k c 100 11 k > 199) 11 (k >= 150 &i&i k <= 155)
(k >= 100 11 k <= 199) 11 (k > 150 && k -c 155)
(k < 100 11 k > 199) 11 (k K= 150 &i&i k >= 155)
none of the above
Answer:
Page3of 15
CSC 110 Final Examination
Page 4
1.8. Consider the following line of Java code:
class X extends A extends B { ]
Select which of the following are true statements
a) This will cause a syntax error because a class cannot have the nameX
b) This is a valid statement that declares a class that inherits from super, or parent classes A
and B
c) This is not a valid statement because Java does not support inheritance from multiple
classes
d) This will create a class X that is a subclass of class A, both of which are in package B
e) none of the above
Answer:
1.9.
One of the uses of the static modifier in Java is to
a) Change the visibility of a method or variable so that it can only be used from within the
same class
b) Change the visibility of a method or variable so that it can be inherited by all its
subclasses
c) Declare variables that must be accessed using get and set methods
d) Declare variables that are shared amongst all the instances of the class
e) none of the above
Answer:
1 .lO. The term zM/pzameters (in Java) refers to
a)
b)
c)
d)
e)
The variables defined in the header of the method’s declaration
All the variables within a method
The variables or literals that are passed when a method is called
The global variables that are used in a class
none of the above
Answer:
I .I I. The following is true of primitive data types in Java:
a) Primitive data types are stored using references
b) The primitive data types include char, int, long, and string
c) Primitive data types are the cause of aliasing
d) Primitive data types are passed by value when passed as parameters
e) none of the above
Answer:
Page4of 15
April 1999
CSC 110 Final Examination
Page 5
1.12. The Java API is
A class containing some methods to help the programmer
b) A language for writing web pages that contain applets
c) A set of class libraries containing prewritten classes to help the programmer
d) Automatically included in every Java program you write
e) none of the above
a)
Answer:
1 .13. The difference between an a@et and an application is
An applet must be derived from the applet class, and an application does not need to
be
b) An applet has a paint method, and an application (generally) has a main method
c) An applet is intended to be run over the internet, as part of a browser, while an
application is intended to be run on its own
d) all of the above
e) none of the above
a)
Answer:
1 .14. Java bytecode is
a) A code required to be placed at the start of your Java program
b) A language consisting of l’s and O’s that is generated by the Java compiler and is
machine-independent
c) the machine-language version of a Java program
d) the assembly-language version of a Java program
e) None of the above
Answer:
Page5of 15
April 1999
CSC 110 Final Examination
2.
Page 6
April 1999
[i mark each] Answer each of the following questions by circling the T or F to indicate
whether or not the statement is True or False. You must also give a reason for your answer.
2.1.
A class can on/y have one constructor.
2.2. A subclass only inherits the final and static methods from its superclass.
T
F
T
F
2.3. A private class member is accessible only within the class body in which the member is
declared.
T F
2.4. The following two lines of Java code are syntactically correct and semantically
equivalent:
int k=l; if (k<O) { System.out.println(k); k--; 1
int k=l; while (k<O) { System.out.println(k): k--; ]
.
. . The keyword select is used as part of an if statement
25
T
F
T
F
. I The following two lines of Java code are syntactically correct, but are NOT
26
semantically equivalent..
fort;;) I
while (true) ;
T F
.
Page6of 15
CSC 110 Final Examination
Page 7
April 1999
2.7. The instance variables of a class (its non-static variables) should be
declared public.
T
2.8. The following is a syntactically correct declaration.
public int static x = 3;
T F
2.9. The following is a picture conceptualizing the result of this statement:
String s = "Jackie Rice";
T
F
2.10. There is no way of accessing the constructor of a subclass’ parent class.
T
F
Page7ofl5
F
CSC 110 Final Examination
3.
Page 8
April 1999
[1 mark each - 10 marks total] Evaluate the following syntactically correct Java expressions
and enter the results in the column labeled RESULT. Assume that the following variables
haue been declared:
double d = 10.0;
double f = 3.0;
int k = 10;
int j = 3;
boolean b = false;
intHI1 a = 1 L3,51,
CL4,LW
{OI
Expression
1;
Result
(k > j) && b && ((k+j)*j*j c 101)
Wf)
11
(k>j)
!b
I
(j<k) I[ k*4 ’ 12) )
!b && (
(k%j) + (k/j)*j
aDI UII
4.
[5 marks]The following table is a truth table for the expression given below. That is, it gives
the values of each of the variables A, 6, and C in the appropriately labeled columns and the
value of the expression in the right-most column. Complete the blank entries in this truth
table
B
(A &i&i
A
.
B
&&
C
C 11 A!=C)
(A
&c&i B &&
C 1 1 A!=C)
T
T
T
T
T
T
F
T
T
F
T
T
F
F
F
T
T
F
T
F
F
F
T
F
F
F
F
Page8ofl5
CSC 110 Final Examination
5.
Page 9
[6 marks totaL 3 marks for a) and 3 marks for b)] Draw a picture of the following variable
declarations:
a) Polygon p = new PolygonO;
// note: the Polygon is an object that is part of the Graphics
// library
b) int a[] = new int[5];
Page9of 15
April 1999
CSC 110 Final Examination
6.
Page 10
[8 marks] Consider the following syntactically correct Java program. Write the output
EXACTLY as it would be presented by this program.
public class Looping {
public static void loop1 (int x, int y) {
int i,j;
for (j=O; jcx; j++) {
for (i=j; ixy; i++) {
System.out.print (JfXf’) ;
I
System.out.println();
1
I
public static void main(String[] args) {
loopl(3, 5); loopl(5, 3);
Page 10 of 15
April 1999
CSC 110 Final Examination
7.
Page 11
April 1999
[I 0 marks] Consider the following syntactically correct Java program- Write the output
EXACTLY as it would be presented by this program.
class Hearts {
public int Kings; public int Jacks;
I
class Diamonds {
public int[] suits = new int[4];
I
public class Cards {
static void Game (int k, Diamonds d, Hearts h) {
System.out.println(U2ndM + k + lJ JJ + d.suits[O] + JJ JJ + h.Kings);
d.suits[O] = d.suits[O] + 5;
h.Kings = h-Kings % 3;
k = k/3;
System.out.println(JJ3rdJJ + k + JJ JJ + d.suits[O] + JJ JJ + h.Kings);
1
public static void main (String[] args) {
Diamonds dmond = new Diamondso;
Hearts hrt = new Heartso;
int k = 17;
dmond.suits[O] = 99;
hrt.Kings = 4;
hrt.Jacks = 4;
System.out.println(JJlst JJ + k + JJ JJ +dmond.suits[O] + JJ N +
hrt.Kings);
Game(kJ dmond' hrt);
System.out.println(JJ4th " + k + " “ + dmond.suits[O] + U Jf +
hrt.Kings);
I
Page 11 of 15
CSC 110 Final Examination
8.
Page 12
[i 0 marks] Write a syntactically correct Java program consisting of 3 classes called Pets, Cat
and Ferret. Class Ferret and Class Cat must have one method each: the constructor, which
must get passed one string parameter. The Ferret constructor must output a line that says
“I’m Ferret ---” (where --- is the String passed as the parameter to the Ferret class
constructor) and the Cat class must output a line that says “I’m Cat ---” (where --- is the String
passed as the parameter to the Cat class constructor). The Pets class must instantiate a
Ferret and a Cat object appropriately.
Page 12 of 15
April 1999
CSC 110 Final Examination
9.
Page 13
April 1999
[I 0 marks] You are given the following syntactically correct class declarations:
class Rectangle {
public int height, width;
class RectangleList {
private Rectangle[] rlist;
public Rectangle(int h, int w) {
height = h;
width = w;
I
1
public RectangleList(int k) {
if (k>O)
rlist = new Rectangle[k];
else
rlist = new Rectangle[3];
// there are min 3 Rectangles
// in the list
for (int i=O; i<rlist.length; i++)
rlist[i] = new Rectangle(i,i);
You are to add a method to class RectangleList called IndexofLargestO that looks for the largest
rectangle in the list (by calculating the area of each rectangle) and returns the index at which it
was found. You may use at most 3 variables.
// this method would be in class RectangleList
public int IndexofLargestO {
Page 13 of 15
CSC 110 Final Examination
Page 14
10. [7 marks] Write a syntactically correct public class called Pine. Pine must be a subclass of
class Tree which is shown below. Pine must have one method, the constructor, which calls
the constructor of class Tree. Pine must also have one instance variable called numNeedles
and one class variable called numTrees.
class Tree {
long height;
double diameter;
Tree(long h, double d) {
height = h;
diameter = d;
I
Page 14 of 15
April 1999
CSC 110 Final Examination
Page 15
11. [I 0 marks] Write a syntactically correct class called TwoArrays. Use the template given
below. The class must have a constructor that creates the two arrays with IO elements each.
The class must also have a method called Swap that swaps all of the values in one array with
all the values in the other array. This must work for any length arrays, and may use only 3
local variables. You may assume that the two arrays always have the same length.
class TwoArrays {
private int a[];
private int b[];
// write the constructor here
Public void Swap0 f
Page 15of 15
- END -
April 1999
Download