Cse 142, Summer 2010 Midterm Exam Key




Yüklə 29.85 Kb.
tarix16.04.2016
ölçüsü29.85 Kb.

CSE 142, Summer 2010
Midterm Exam Key


1. Expressions

For each expression at left, indicate its value in the right column. List a value of appropriate type and capitalization.


e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.

Expression

Value

11 - 7 * 2 + 3 * 4

10 / (5 / 2) + 1.5 * 6 / 2

115 / 10 + 115 % 10 + 11 / 7 * 3.5

"0" + 10 * 5 + "0" + 5 + 6

(4 <= 9 / 2) && !(2 != 10)


9

9.5


19.5

"050056"


false



2. Parameter Mystery

peacock in the study with the dagger

study in the peacock with the mustard

lounge in the mustard with the pipe

pipe in the dagger with the ballroom

miss mustard in the ballroom with the study


3. If/Else Simulation

Method Call

Output

ifElseMystery(20, 8);

ifElseMystery(30, 30);

ifElseMystery(4, 3);

ifElseMystery(30, 4);

ifElseMystery(7, 7);


30 80

40 300


4 4

40 41


7 8

4. While Loop Simulation



Method Call

Output

whileMystery(3, 5); whileMystery(5, 3);

whileMystery(-3, 6);

whileMystery(2, 12);

whileMystery(-9, 10);



3, 4, 2

0

-3, -2, 0, 3



2, 3, 5, 8, 4

-9, -8, -6, -3, 1, 5




5. Assertions





num < 0

first < second

num >= second

Point A

SOMETIMES

NEVER

ALWAYS

Point B

NEVER

SOMETIMES

SOMETIMES

Point C

NEVER

ALWAYS

NEVER

Point D

NEVER

SOMETIMES

SOMETIMES

Point E

ALWAYS

SOMETIMES

SOMETIMES

6. Programming

public static boolean speedingTicket(int speed, int mph, boolean donut) {

if (donut && (speed - mph >= 10)) {

return true;

} else if (!donut && ((speed - mph >= 5) || (mph - speed >= 5))) {

return true;

} else if (speed >= 100) {

return true;

} else {

return false;

}

}
public static boolean speedingTicket(int speed, int mph, boolean donut) {



if ((donut && (speed - mph >= 10))

|| (!donut && (Math.abs(speed - mph) >= 5))

|| (speed >= 100)) {

return true;

} else {

return false;

}

}
public static boolean speedingTicket(int speed, int mph, boolean donut) {



return ((donut && (speed - mph >= 10))

|| (!donut && (Math.abs(speed - mph) >= 5))

|| (speed >= 100));

}

7. Programming

public static void dogHears(String dogName, int lines) {

Random rand = new Random();


for (int i = 1; i <= lines; i++) {

int wpl = rand.nextInt(9) + 2;

for (int j = 1; j <= wpl; j++) {

if (rand.nextInt(4) == 0) {

System.out.print(dogName + " ");

}

else {



System.out.print("blah ");

}

}



System.out.println();

}

}



8. Programming

// boolean flag solution

public static boolean highLow(int number) {

int digit = number % 10;

number = number / 10;

boolean lookForHigh = (digit < 5);


while (number != 0) {

digit = number % 10;

number = number / 10;

if (lookForHigh && (digit >= 5)) {

lookForHigh = false;

} else if (!lookForHigh && (digit < 5)) {

lookForHigh = true;

} else {


return false;

}

}


return true;

}
// "prev and curr" solution

public static boolean highLow(int n) {

int prev = 3; // 3 to start; then 1 for 'low' and 2 for 'high'

while (n != 0) {

int curr;

if (n % 10 <= 4)

{

curr = 1;



} else {

curr = 2;

}

if (prev == curr) {



return false;

}

n /= 10;



prev = curr;

}

return true;



}
// "peek ahead" solution

public static boolean highLow(int n) {

while (n != 0) {

int digit = n % 10;

n = n / 10;

if (digit <= 4 && n > 0 && n % 10 <= 4) {

return false;

}

if (digit >= 5 && n > 0 && n % 10 >= 5) {



return false;

}

}



return true;

}


of


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azrefs.org 2016
rəhbərliyinə müraciət

    Ana səhifə