Docsity

Practice Exam for Computer Science I (CS162), Assignments of Computer Science

Toggle sidebar
bg1
Name:________________________________
CS162
Computer Science I
Fall 2008
Practice Exam 2
(Version 2.1)
The real test will look much like this one, except there will be only 7 problems. In addition to the problems
below, be sure to review questions from all previous quizzes, projects, labs, and homework assignments. The
test is Friday, 14 November.
Problems 11 and 12 are due for homework points on Wednesday, 12 November.
Part 1: Write loops
1). (5 points) Write a method boolean hasDoubleLetter(String str) that returns true if str
contains at least one double letter. For example,
• hasDoubleLetter(ā€œabaccaā€) should return true.
• hasDoubleLetter(ā€œabacdbeā€) should return false.
2). Write a method int findSecond(String str, char findMe) that returns the location of the
second occurrence of the character findMe. Return -1 if the requested letter does not occur twice. For
example:
o findMe(ā€œMississippiā€, ā€˜i’) should return 4.
o findMe(ā€œMississippiā€, ā€˜s’) should return 3.
o findMe(ā€œMississippiā€, ā€˜9’) should return 3.
o findMe(ā€œMississippiā€, ā€˜M’) should return -1.
3). Write a method int rollForPoints(GVdice d, int goal) that counts the number of times a
GVdice must be rolled until goal points have accumulated. GVdice has two public methods:
• void roll();
• int getValue();
4). (5 points) Pick one only: Part (a) is worth 15 points. Part (b) is worth at most 10 points. (That means
if you choose part (b), you forfeit the opportunity to earn 5 points.)
a. Write a method int rollUntilStraight(GVdice d) that counts the number of times a
GVdice must be rolled until the sequence 1, 2, 3, 4, 5, 6 is rolled in order.
b. Write a method int rollUntilSmallStraight(GVdice d) that counts the number of
times a GVdice must be rolled until either the sequence 1, 2, 3, 4, 5 or the sequence 2, 3, 4, 5, 6
is rolled in order.
5). Write a method int numPairs(GVdice, int num_rolls) that rolls a GVdice num_rolls
times and counts the number of times two consecutive rolls have the same value. For example:
• If num_rolls is 6, and the 6 rolls are {1, 2, 3, 3, 4, 5, 5} the result should be 2.
• If num_rolls is 6, and the 6 rolls are {1, 2, 2, 2, 4, 5, 4} the result should be 2.
• If num_rolls is 6, and the 6 rolls are {1, 2, 3, 2, 4, 5, 2} the result should be 0.
pf3
pf4

Partial preview of the text

Download Practice Exam for Computer Science I (CS162) and more Assignments Computer Science in PDF only on Docsity!

Name:________________________________

CS162 Computer Science I Fall 2008

Practice Exam 2

(Version 2.1)

The real test will look much like this one, except there will be only 7 problems. In addition to the problems below, be sure to review questions from all previous quizzes, projects, labs, and homework assignments. The test is Friday, 14 November. Problems 11 and 12 are due for homework points on Wednesday, 12 November.

Part 1: Write loops

1). (5 points) Write a method boolean hasDoubleLetter(String str) that returns true if str contains at least one double letter. For example,

  • hasDoubleLetter(ā€œabaccaā€) should return true.
  • hasDoubleLetter(ā€œabacdbeā€) should return false. 2). Write a method int findSecond(String str, char findMe) that returns the location of the second occurrence of the character findMe. Return - 1 if the requested letter does not occur twice. For example: o findMe(ā€œMississippiā€, ā€˜i’) should return 4. o findMe(ā€œMississippiā€, ā€˜s’) should return 3. o findMe(ā€œMississippiā€, ā€˜9’) should return 3. o findMe(ā€œMississippiā€, ā€˜M’) should return - 1. 3). Write a method int rollForPoints(GVdice d, int goal) that counts the number of times a GVdice must be rolled until goal points have accumulated. GVdice has two public methods:
  • void roll();
  • int getValue();

4). (5 points) Pick one only : Part (a) is worth 15 points. Part (b) is worth at most 10 points. (That means

if you choose part (b), you forfeit the opportunity to earn 5 points.)

a. Write a method int rollUntilStraight(GVdice d) that counts the number of times a GVdice must be rolled until the sequence 1, 2, 3, 4, 5, 6 is rolled in order. b. Write a method int rollUntilSmallStraight(GVdice d) that counts the number of times a GVdice must be rolled until either the sequence 1, 2, 3, 4, 5 or the sequence 2, 3, 4, 5, 6 is rolled in order. 5). Write a method int numPairs(GVdice, int num_rolls) that rolls a GVdice num_rolls times and counts the number of times two consecutive rolls have the same value. For example:

  • If num_rolls is 6, and the 6 rolls are {1, 2, 3, 3, 4, 5, 5} the result should be 2.
  • If num_rolls is 6, and the 6 rolls are {1, 2, 2, 2, 4, 5, 4} the result should be 2.
  • If num_rolls is 6, and the 6 rolls are {1, 2, 3, 2, 4, 5, 2} the result should be 0.

6). Write a method named onePerLine that takes an ArrayList of Strings as input and returns an ArrayList of Strings as output. The Strings in the input contain one or more names separated by spaces. The output should be an ArrayList of Strings that contains exactly one name per item. For example, if the input is {ā€œAdam Ben Charlieā€, ā€œDave Edwardā€, ā€œFredā€, ā€œGeorgeā€, ā€œHerbert Ian Johnā€}, then the output should be {ā€œAdamā€, ā€œBenā€, ā€œCharlieā€, ā€œDaveā€, ā€œEdwardā€, ā€œFredā€, ā€œGeorgeā€, ā€œHerbertā€, ā€œIanā€, ā€œJohnā€}.

Part 2: Reading Code

  1. (4 points) Consider the following code: boolean test_boolean(boolean p, boolean q, boolean r) { return !r || p && q; } Given that! has the highest precedence and || the lowest, give the return values of a. test_boolean(true, false, true) b. test_boolean(false, true, false)
  2. (2 points) Given that / has a higher precedence than ==, what is the value of this expression: (1 / 4) == 0.
  3. (8 points) Consider the following code: int g(int x) { return x + 2; } int h(int x) { return 2 * g(x); } Compute the return values of (4 points each): a. g(5); b. h(5); c. g(h(5)); d. h(g(5));
  4. (3 points) Consider the following code: int function(int x) { if (x <= 1) { return 1; } return x * function(x-1); } What is the return value of function(5)?
  1. Does the following code work? If not, give an example input that breaks the code. If it does work correctly, explain why: public boolean xyBalance(String str) { String xy = ā€œā€; for (int j = 0; j < str.length(); j++) { if (str.charAt(j) == ā€˜x’ || str.charAt(j) == ā€˜y’) { xy = xy + str.charAt(j); } // end if } // end for if (xy.length() == 0) { return true; } if (xy.charAt(xy.length() – 1) == ā€˜x’) { return false; } return true; }
  2. In the code above, can the line if (xy.charAt(xy.length() – 1) == ā€˜x’) ever throw an exception? If so, give an example input that results in an exception being thrown. If not, explain why not.