Legal Java - Data Structures and Programming Methodology - Exams, Exams of Data Structures and Algorithms

Main points of this past exam are: Legal Java, Static Void, Public Static, New Boolean, Void Crazy, Without Errors, Return Value, Balance Less, Syntactically Valid, Valid Code

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashi_16star
shashi_16star 🇮🇳

4.6

(20)

99 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
You have 50 minutes to finish this test. Your exam should contain 6 problems (numbered 0-5) on 8
total pages. This is an open-book test. You may consult any books, notes, or other paper-based
inanimate objects available to you. Read the problems carefully. If you find it hard to understand a
problem, please ask a question. Partial credit will be awarded where we can, so do try to answer each
question. Please write your answers in the spaces provided in the test; if you need to use the back of a
page make sure to clearly tell us so on the front of the page.
Good Luck!
0
out of 1 point
1
out of 6 points
2
out of 6 points
3
out of 9 points
4
out of 4 points
5 (part 1)
out of 4 points
5 (part 2)
out of 4 points
Total
out of 34 points
pf3
pf4
pf5
pf8

Partial preview of the text

Download Legal Java - Data Structures and Programming Methodology - Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!

You have 50 minutes to finish this test. Your exam should contain 6 problems (numbered 0-5) on 8 total pages. This is an open-book test. You may consult any books, notes, or other paper-based inanimate objects available to you. Read the problems carefully. If you find it hard to understand a problem, please ask a question. Partial credit will be awarded where we can, so do try to answer each question. Please write your answers in the spaces provided in the test; if you need to use the back of a page make sure to clearly tell us so on the front of the page.

Good Luck!

0 out of 1 point

1 out of 6 points

2 out of 6 points

3 out of 9 points

4 out of 4 points

5 (part 1) out of 4 points

5 (part 2) out of 4 points

Total out of 34 points

Question

a) What is the output of the following code? Write your answer in the box.

int [] myArray = {1, 2, 3, 4, 5}; System. out .println(myArray[4]); myArray = new int [4]; System. out .println(myArray[3]);

b) Fill in the blanks below to indicate what is printed by running the main method of Mystery.java shown

below. There are no compile-time or run-time errors in this program.

public class Mystery {

public static void mystery1( boolean [] bArray){

boolean b;

for ( int i = 0; i< bArray.length; i++){

b = bArray[i];

b = !b;

Mystery. mystery2 (bArray); // 1

bArray[2] = false ;

Mystery. mystery2 (bArray); // 2

bArray = new boolean [4];

Mystery. mystery2 (bArray); // 3

public static void mystery2( boolean [] bArray){

int i = bArray.length - 1;

while (i > 0){

System. out .print(bArray[i] + " ");

i--;

System. out .println();

public static void main(String [] args){

boolean [] bArray = { true , true , true , true , false };

Mystery. mystery1 (bArray);

Mystery. mystery2 (bArray); // 4

Write what is printed

Question

For each example of code, respond whether or not it will compile. If it compiles, please respond whether or not it will run without errors. If it runs without errors and has a return value, please write the return value.

interface X { int method(); } class Y implements X { int method(){ return 0; } private double method(String arg){ return 12.20; } } class Z extends Y{ double method(String arg){ return 3.14; } } public class testXYZ{ public static void main(String[] args) {

// Code – Each group of lines is

independent

Compiles?

Runs

without

errors?

Return

value?

(new Y()).method(“hi”); YES NO YES NO
(new Z()).method(); YES NO YES NO
((Z) (new Y())).method(“yo”); YES NO YES NO
X x1 = new Z();
Y y1 = (Z) x1; YES^ NO^ YES^ NO
X[] xarr = {new Y(), new X()}; YES NO YES NO
Y[] yarr = {new Y(), new Z()}; YES NO YES NO
((Y) (new Z())).method(“hey”); YES NO YES NO
X x 2 = new Z();
Z z 2 = (Y) x 2 ;
z 2 .method();
YES NO YES NO
X x3 = new Z();
x3.method(“hello”); YES^ NO^ YES^ NO

Question

Fill in the blanks below with legal Java to produce the output indicated in each comment. If it is impossible

write “IMPOSSIBLE” in the blank. You may not create any additional objects!

public class Parent { public void feed(Parent p){ System. out .println("Parent feed Parent"); } public void feed(Child c){ System. out .println("Parent feed Child"); } } public class Child extends Parent { public void feed(Parent p){ System. out .println("Child feed Parent"); } public void feed(Child c){ System. out .println("Child feed Child"); } public static void main(String[] args) { Parent p = new Child();

// Child feed Child

// Child feed Parent

// Parent feed Child

// Parent feed Parent

p = new Parent();

// Child feed Child

// Child feed Parent

// Parent feed Child

// Parent feed Parent

} }

Question #5 (continued from previous page)

b) Modify the removePoorParents() method below to fix the bug you demonstrated in part a).

public void removePoorParents() {

if ( this .myParent != null ) {

if ( this .myParent.myBalance < 1000) {

this .myParent = this .myParent.myParent;

if ( this .myParent == null ){

return ;

this .myParent.removePoorParents();

Please fill out the names of people around you

YOU are Here

Question

Write the month and day of your birthday on every page! (worth 1 pt)

Last Name:

First Name:

Login: cs61bl-

TA (circle): Jon^8 -^11

Kaushik 11- 2

David 2 - 5

George 6 - 9

Birthday: Month: _____ Day: _____