CS111 Jeopardy Spring 2005 CS111 Jeopardy Spring 05 p.1/27 - - PowerPoint PPT Presentation

cs111 jeopardy
SMART_READER_LITE
LIVE PREVIEW

CS111 Jeopardy Spring 2005 CS111 Jeopardy Spring 05 p.1/27 - - PowerPoint PPT Presentation

CS111 Jeopardy Spring 2005 CS111 Jeopardy Spring 05 p.1/27 Gameboard Conditionals/ Worlds Bugs Lists Potpourri Recursion 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 CS111 Jeopardy Spring 05


slide-1
SLIDE 1

CS111 Jeopardy

Spring 2005

CS111 Jeopardy Spring ’05 – p.1/27

slide-2
SLIDE 2

Gameboard

Conditionals/ Recursion Worlds Bugs Lists Potpourri

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5

CS111 Jeopardy Spring ’05 – p.2/27

slide-3
SLIDE 3

Conditionals/Recursion 1

This is the part of a recursive method that guarantees the recursion will terminate. Back

CS111 Jeopardy Spring ’05 – p.3/27

slide-4
SLIDE 4

Conditionals/Recursion 2

This is the expression exp that makes the the single statement return exp; equivalent to the following statement: if (b == false) { return true; } else { return false; } Back

CS111 Jeopardy Spring ’05 – p.4/27

slide-5
SLIDE 5

Conditionals/Recursion 3

This is the output of the following code snippet when x has the value 16: if ((x > 5) && (x <= 15)) { System.out.println("Swiss Cheese"); } else { if ((x % 2)== 0) { System.out.println("American Cheese"); } else { System.out.println("Cheese Whiz"); } } Back

CS111 Jeopardy Spring ’05 – p.5/27

slide-6
SLIDE 6

Conditionals/Recursion 4

Given the recursive method below, this is the value of tunnel(10). public static int tunnel (int n) { if (n<=1) { return 0; } else { return 1 + tunnel(n/2); } } Back

CS111 Jeopardy Spring ’05 – p.6/27

slide-7
SLIDE 7

Conditionals/Recursion 5

This is a Buggle method that will drop bagels all the way to the wall, which is an unknown distance away. Bagels are dropped in all cells in front of the Buggle, but not under the Buggle’s current cell. This method leaves position and heading invariant. Back

CS111 Jeopardy Spring ’05 – p.7/27

slide-8
SLIDE 8

Worlds 1

Buggles love to eat these. Back

CS111 Jeopardy Spring ’05 – p.8/27

slide-9
SLIDE 9

Worlds 2

Suppose that w is a PictureWorld picture of the wedge shown below in Figure 1. This is a PictureWorld expression that denotes the picture in Figure 2.

Figure 1 Figure 2

Back

CS111 Jeopardy Spring ’05 – p.9/27

slide-10
SLIDE 10

Worlds 3

This is a definition of the buggle method satisfying the following contract: public void forwardTurningLeft(int n); Moves the buggle forward a total of n spaces, turning left whenever the buggle encounters a wall. (Turning does not count as “moving forward a space”.) Back

CS111 Jeopardy Spring ’05 – p.10/27

slide-11
SLIDE 11

Worlds 4

This is a definition of the buggle method satisfying the following contract: public boolean canGoForwardBy (int n); Returns true if the buggle would not encounter a wall in forward(n), and false otherwise. Executing this method should leave the state of the buggle unchanged. Back

CS111 Jeopardy Spring ’05 – p.11/27

slide-12
SLIDE 12

Worlds 5

This is the picture drawn by invoking the turtle method pattern(40) on a new turtle, where pattern is defined as follows:

public void pattern (int n) { if (n < 10) { fd(n) } else { pattern(n/2); lt(90); fd(n); bd(n); rt(90); pattern(n/2);}}

Back

CS111 Jeopardy Spring ’05 – p.12/27

slide-13
SLIDE 13

Bugs 1

This is a bug in the following Buggle method:

public void jello (int n) { if (n = 0) { dropBagel(); } else { forward(); jello(n - 1); backward(); } }

Back

CS111 Jeopardy Spring ’05 – p.13/27

slide-14
SLIDE 14

Bugs 2

This is a bug in the following turtle method: public void crazy (int n) { if (n > 0) { fd(n); lt(45); crazy(n); rt(45); } } Back

CS111 Jeopardy Spring ’05 – p.14/27

slide-15
SLIDE 15

Bugs 3

This is a bug in the following Buggle method: public void dance (int n, Color c) { if ((n > 1) && !(isFacingWall())){ forward(); dance(c, n-2); backward(); } } Back

CS111 Jeopardy Spring ’05 – p.15/27

slide-16
SLIDE 16

Bugs 4

This is a bug in the following turtle method; public int spiral (int n) { if (n == 0) { return 0; } else { fd(n); lt(90); spiral(n/2); rt(90); bd(n); } } Back

CS111 Jeopardy Spring ’05 – p.16/27

slide-17
SLIDE 17

Bugs 5

This is the bug in the code below:

public int eatBagels() { if (isFacingWall()) return 0; forward(); eatBagels(); int count = eatBagels(); if (isOverBagel()) { pickUpBagel(); backward(); return 1 + count; } else { backward(); return count; } }

Back

CS111 Jeopardy Spring ’05 – p.17/27

slide-18
SLIDE 18

Lists 1

This is the shortest list. Back

CS111 Jeopardy Spring ’05 – p.18/27

slide-19
SLIDE 19

Lists 2

This is the length of the list

StringListList.fromString("[[]]")

Back

CS111 Jeopardy Spring ’05 – p.19/27

slide-20
SLIDE 20

Lists 3

This is the problem with this code:

public static boolean isMem(String s, StringList l) { if (s.equals(head(l))) return true; else if (isEmpty(l)) return false; else /* not empty, not found */ return isMem(s, tail(l)); }

Back

CS111 Jeopardy Spring ’05 – p.20/27

slide-21
SLIDE 21

Lists 4

This is the value of

mystery(IntList.fromString("[1,47]"))

where

public static boolean mystery(IntList l) { return isEmpty(l)? true : (head(l)%2 == 1) && mystery(tail(l)); }

Back

CS111 Jeopardy Spring ’05 – p.21/27

slide-22
SLIDE 22

Lists 5

These are the letters of the true expressions given:

public boolean lucky7(IntList l) { if (isEmpty(l)) { return false; } else if (head(l) == 7) { return true; } else { return lucky7(tail(l)); } }

  • a. lucky7(fromString("[1,3,5,7]"))
  • b. lucky7(lucky7(fromString("[1,3,5,7]")))
  • c. lucky7(7)

Back

CS111 Jeopardy Spring ’05 – p.22/27

slide-23
SLIDE 23

Potpourri 1

In the Java Execution Model, this is created when an instance method is invoked. Back

CS111 Jeopardy Spring ’05 – p.23/27

slide-24
SLIDE 24

Potpourri 2

This is the value of apple, after executing the three lines of code below:

apple = 2; apple = apple + 2; apple = apple * -2;

Back

CS111 Jeopardy Spring ’05 – p.24/27

slide-25
SLIDE 25

Potpourri 3

This is betty’s position after the following code has been executed:

Buggle becky = new Buggle(); Buggle betty = becky; becky.forward(5); becky = new Buggle();

Back

CS111 Jeopardy Spring ’05 – p.25/27

slide-26
SLIDE 26

Potpourri 4

This is the value of the expression legal(methodB(4)), given the two contracts: public int methodB (int n); Returns n multiplied by 5. public boolean legal(int n); Returns true if n is greater than or equal to 21 and false otherwise. Back

CS111 Jeopardy Spring ’05 – p.26/27

slide-27
SLIDE 27

Potpourri 5

This is what countBagels() returns for the following method definition given the configuration shown on the board.

public int countBagels() { int n = 0; if (isFacingWall()) { return n; } else { forward(); if (isOverBagel()){ n = n + 1;} countBagels(); backward(); return n; } }

Back

CS111 Jeopardy Spring ’05 – p.27/27