computer science ii - juniata...

11
CS 240 Midterm Exam Page 1 Computer Science II Spring 2014, Friday, 3/21/14 100 points Name ___________Key______________ 1. Miscellaneous, basic Java concepts. Matching. Place the letter in the blank that best matches the statement. [10 pts] __B__ The filename containing the compiled bytecodes for the class Test1. __D__ Where Test1 source code is found. __G__ The header line when Test1a is a subclass of Test1 . __F__ The automatic recovery of memory used by objects no longer accessible through any references. __J__ and __M__ are Java primitive types. __O__ Java operation/method to check string equality. __K__ All classes are a subclass of this. __Q__ The method called upon to return a summary string of an object’s contents; usually it is overridden. __S | R__ Used to compare if two objects’ addresses are different. A. java.Test1 B. Test1.class C. Test1.exe D. Test1.java E. Defragmentation F. Garbage collection G. public class Test1a extends Test1 H. public subclass Test1a of Test1 I. String J. double K. Object L. Class M. char N. .println() O. .equals() P. .strcmp() Q. .toString() R. == S. != T. < 2. Object-oriented programming design principles. (True/False) [10 pts] a. _T__ An Abstract Data Type hides its data structure organization and publicizes its operations. b. _ F __ Copying and pasting code is a quality software goal in object- oriented class design.

Upload: others

Post on 02-Jun-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science II - Juniata Collegejcsites.juniata.edu/faculty/rhodes/cs2mm/exams/midterm1s14key.…  · Web view_T__ An Abstract Data Type hides its data structure organization

CS 240 Midterm Exam Page 1Computer Science II

Spring 2014, Friday, 3/21/14100 points

Name ___________Key______________

1. Miscellaneous, basic Java concepts. Matching. Place the letter in the blank that best matches the statement.[10 pts]

__B__ The filename containing the compiled bytecodes for the class Test1.

__D__ Where Test1 source code is found.

__G__ The header line when Test1a is a subclass of Test1 .

__F__ The automatic recovery of memory used by objects no longer accessible through any references.

__J__ and __M__ are Java primitive types.

__O__ Java operation/method to check string equality.

__K__ All classes are a subclass of this.

__Q__ The method called upon to return a summary string of an object’s contents; usually it is overridden.

__S | R__ Used to compare if two objects’ addresses are different.

A. java.Test1B. Test1.classC. Test1.exeD. Test1.javaE. DefragmentationF. Garbage collectionG. public class Test1a extends Test1H. public subclass Test1a of Test1I. StringJ. doubleK. ObjectL. ClassM. charN. .println()O. .equals()P. .strcmp()Q. .toString()R. ==S. !=T. <

2. Object-oriented programming design principles. (True/False)[10 pts]

a. _T__ An Abstract Data Type hides its data structure organization and publicizes its operations.

b. _ F __ Copying and pasting code is a quality software goal in object-oriented class design.

c. _ T __ A method that returns an object can be called as part of a cascade of method calls.

d. _ F __ The this instance reference in a method can always be omitted.

e. __ T _ A method of a subclass may called its parent’s class method using a super prefix.

f. _ F __ The information hiding principle says the user of a class need not know the details of its implementation while the implementer should be made aware of how the class will be used.

g. _ T __ A precondition defines what must initially hold for a method to correctly carry out its task.

h. __ F _ A class may not define multiple methods with the same name even if the parameter set (signature) is different.

i. _ T __ A protected instance variable is like a private instance variable, but remains accessible within subclasses.

j. _ T __ A variable designated as static in a class makes it a class variable of which there can only be one instance.

Page 2: Computer Science II - Juniata Collegejcsites.juniata.edu/faculty/rhodes/cs2mm/exams/midterm1s14key.…  · Web view_T__ An Abstract Data Type hides its data structure organization

CS 240 Midterm Exam Page 2

3. Arrays. For this question just give segments of Java code. You need not give entire methods or classes.[9 pts]

a. Assume a sound mashup is composed as an array of Sound objects called soundClips[ ]. Give Java code segments to 1) create a second array of Sound objects called revSounds with soundClips.length elements and 2) use a single for loop to initialize each element of revSounds to the reversed Sound objects from the soundClips array using the reverse() method (which returns a Sound object) AND reverse the order of the clips in the new array. Only use one loop! The result should be a totally reversed sound mashup. [9]

Sound revSounds [] = new Sound [soundClips.length];...for(int i=0; i<soundClips.length; i++){ revSounds[soundClips.length-i-1] = soundClips[i].reverse();}

4. Fill in Java code in the method makeBlackAndWhite() below to modify a Picture object that generates the photographic negative of the image. This is done by setting the RGB values each to the average of the original RGB values.

[6 pts]public __void___ makeBlackAndWhite() {

Pixel [] pixels = this.getPixels();

for(Pixel pix : __pixels_ ){

_int__ grayscale = _(pix.getRed()+pic.getGreen()+pic.getBlue())_ / 3;

_pix.setRed (__grayscale ____);

_ pix.setGreen (__grayscale ___);

_ pix.setBlue (____grayscale ____); }}

Page 3: Computer Science II - Juniata Collegejcsites.juniata.edu/faculty/rhodes/cs2mm/exams/midterm1s14key.…  · Web view_T__ An Abstract Data Type hides its data structure organization

CS 240 Midterm Exam Page 35. Below is a main program to draw something with turtles.

a. Draw a possible scenario from executing this code in the 400x400 pixel box. This is testing your code reading.

[5 pts]public class SomethingByTurtles { private static final int N = 2; public static void main(String[] args){ World myWorld = new World(); Turtle [][] myTurtles = new Turtle[N][N]; Turtle s; //temporary reference int dist; for (int i=0; i < N; i++) { for (int j=0; j < N; j++) { myTurtles[i][j] = s = new Turtle(myWorld); //cascading assignment //initially centered at 200,200; heading up/north s.penDown(); s.moveTo(i*100+2,j*100+2); //+2 to keep off border } } for (int i=0; i < N; i++) { for (int j=0; j < N; j++) { dist = i*100+j*100; s = myTurtles[i][j]; s.turn(90); s.forward(dist); } }}

1. True/false on asymptotic dominance concepts on cost functions.[10 pts]

__F____ A cost function can relate running time to the number of Java bytecodes.

__ T ___ A cost function can relate running time to the data size.

__ F ____ A cost function can be decreasing.

___ F ___ Asymptotic dominance analysis considers both small data size as well as large data size.

__ F ____ For the definition of big-O, f(n) = O(g(n)), where C*g(n) >= f(n) for some n > n0. C is a value that is dependent on n.

___ T ___ Logarithms of lower base always dominate those of higher base.

__ F ___ f(n) = 2n3 + 3200n2 - 5n + 100 = O(n2)

__ F ___ f(n) = 10n2 + 2k3 + 30 = O(k3)

___ T __ f(n) = 16 log n + 10n = O(n)

__ F __ f(n) = n6 +2n = O(n6)

Page 4: Computer Science II - Juniata Collegejcsites.juniata.edu/faculty/rhodes/cs2mm/exams/midterm1s14key.…  · Web view_T__ An Abstract Data Type hides its data structure organization

CS 240 Midterm Exam Page 42. Rank the algorithm complexities from "fastest" to "slowest" (increasing dominance). 1 is fastest (good, low

cost) and 6 is slowest (bad, high cost).[5 pts]

O(n2) O(log n) O(2n) O(n) O(1) O(n log n)5 2 6 3 1 4

3. Estimate the running times using the big-O notation for each of these code segments. The size of the data set is n. Assume all variables are integer.

[10 pts]

4. True/false on the abstract data type of arrays and lists (general linked lists).[5 pts]

__ T __ A linked list’s and an array’s elements have a single successor and predecessor, except first and last ones.

___ F _ Access to the nth element in an array has the same cost as in a linked list.

__ T __ A list must have a reference variable pointing to its first element.

__ F __ A linked list permits direct access to its elements, costing constant time.

__ F __ Data elements of a linked list node must be a primitive data type.

_____O(n)_____for(j=1; j<=n-20; j++){ sum += j;}

___O(n2)_____

for (i=0; i<n; i++) for(j=0; j<n; j++) for(k=1; k<99; k++){ // 4 assignments // 2 square root function calls }

____ O(n3)____

for (i=n-1; i>=0; i--) for(j=0; j<n; j++) for(k=j; k<n; k++){ // 3 assignments }

__O(n2 log n)___

i = 1;while(i<=n){ for(j=0; j<=n; j=j+2){ foo(j,n); //foo takes linear time } i = i*2;}

____ O(n3)____

i = n;while(i>0){ if(i % 2 == 1){ // if i is odd for(j=i; j<=n; j++){ foo(j,n); //foo executes in linear time } } for(j=i; j<=n; j++){ foo(i,j); //foo executes in log time } i = i-1;}

Page 5: Computer Science II - Juniata Collegejcsites.juniata.edu/faculty/rhodes/cs2mm/exams/midterm1s14key.…  · Web view_T__ An Abstract Data Type hides its data structure organization

CS 240 Midterm Exam Page 5

5. Assume we have an instance array picts of Picture objects to be implemented in a class called Pictures. There is an integer instance variable np that accurately tracks the logical number of pictures in the array.

[10 pts]

private Pictures [] _picts_;

private int _np__ ;

a. Show the code in Java for the default constructor of Pictures to set picts array initially with room for 100 pictures and set np to reflect no elements.

public Pictures(){

picts = new __Pictures[100]____ ;

np = _0___;}

b. Complete the method to delete a picture from the front of the array by shifting all elements one position.

public void deleteFirst(){

for(int i= __1__; i< _np_; i++){

picts [_i-1_] = picts [__i___] ; }

__np___ --;}

6. Linked lists.a) Draw a box and pointer diagram representing a linked list pointed to by a reference variable head. The list

should point to the String objects “dog”, “fish”, and “snake”. That is, the info/data field of the nodes separately points to the String objects. Keep the data in sorted order by name. Remember that Strings are not primitives. Draw all objects as boxes.

[5 pts]

Page 6: Computer Science II - Juniata Collegejcsites.juniata.edu/faculty/rhodes/cs2mm/exams/midterm1s14key.…  · Web view_T__ An Abstract Data Type hides its data structure organization

CS 240 Midterm Exam Page 6

b) Show how the links above are rearranged to insert two nodes containing “bear” and “pig” into the list at the appropriate points. Just add these nodes above and “cut” and redraw adjusted links.

DRAW ABOVE

7. Supply the declarations and assignment statements in the Java PNode class below to partially support the implementation of a linked list in which each node holds a Picture pic pointer and a next pointer.

[15 pts]public class PNode //instance variables declaration Picture pic; _PNode___ next;public PNode(Picture x){//constructor pic = ___x_____ ;

next = _null____ ;}public Picture getPicture(){ return __pic______ ; }

public void setNext(PNode pn){

__next_ = __pn____; }

public PNode getNext(){

return ___next___ ; } //embed each picture into the background bg, left to right on bottom //by traversing the list

public void drawFromMeOn(Picture bg){int X = 0;int Y = bg.getHeight()-1;

PNode curr = ___this___ ;

while ( curr != _null__) {curr.getPicture().drawMeOn(bg,X,Y);X = X + curr.getPicture().getWidth();

curr = _curr__.__getNext()___;}

} //--------------------------------------------public static void main(String args[]){

Page 7: Computer Science II - Juniata Collegejcsites.juniata.edu/faculty/rhodes/cs2mm/exams/midterm1s14key.…  · Web view_T__ An Abstract Data Type hides its data structure organization

CS 240 Midterm Exam Page 7 _Pnode_ head; //track the linked list of nodes// lots of stuff to create the list pointed to by head……….. // now arrange the composite picture from left to right

Picture bg = new Picture(“background.jpg”);

_head_ . drawFromMeOn( __bg___ );

_bg__ .show(); //display the picture}