Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS61B Exam 1 - Spring 2003 - Problem Set, Exams of Data Structures and Algorithms

The cs61b exam 1 for the spring 2003 semester, which includes instructions, problem statements, and grading rubrics. Students are required to fill in their personal information and solve various problems related to account class, palindromes, and constructing a vector of riddle objects.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61B (Yelick) Spring 2003
Version 2
Exam 1
Febrary 21, 2003
Do not open this booklet until you are told to begin!
There are 20 points on this exam. Read each problem carefully, and avoid spending too much
time on any one question! There is a 1-page appendix at the end of this exam that included code
you have seen before. You may tear that page off your exam if you wish.
Problem 0 (1 point): Fill out all the following information. Once you are told to begin, also fill
out your name and login at the top of each page.
Your name:
Your cs61b login:
Lab time or section #:
Lab TA’s name:
Name of person to your left:
Name of person to your right:
Grading: Do not write below this line. The following grid will be used for grading.
Problem
Score Possible
0 1
1 5
2 5
3 4
4 5
Total 20
Do not open this booklet until you are told to begin!
pf3
pf4
pf5

Partial preview of the text

Download CS61B Exam 1 - Spring 2003 - Problem Set and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS61B (Yelick) Spring 2003

Exam 1

Febrary 21, 2003

Do not open this booklet until you are told to begin!

There are 20 points on this exam. Read each problem carefully, and avoid spending too much time on any one question! There is a 1-page appendix at the end of this exam that included code you have seen before. You may tear that page off your exam if you wish.

Problem 0 (1 point): Fill out all the following information. Once you are told to begin, also fill out your name and login at the top of each page.

Your name:

Your cs61b login:

Lab time or section #:

Lab TA’s name:

Name of person to your left:

Name of person to your right:

Grading: Do not write below this line. The following grid will be used for grading.

Problem Score Possible

0 1

1 5

2 5

3 4

4 5

Total 20

Do not open this booklet until you are told to begin!

Problem 1: (5 points) This problem refers to the Account class from Lab2, which is attached to the end of this exam booklet as an appendix.

Parts A-D: Below are some possible box-and-pointer diagrams for Account objects. Which of the pictures are legal diagrams that could be produced by a program outside the Account class, e.g., a separate test class. Circle “yes” if such a program could produce the picture, “no” if it could not. (For parts A-D only, +1 point for each correct answer, -2 for each incorrect one.)

A) Yes No B) Yes No

C) Yes No D) Yes No

Part B: Fill in the 1-line body of a method clearAcct with the following specification. As in parts A-D, this method is part of a test class that is not the Account class.

/** Modifies the given Account by clearing it of funds; afterwards, the balance() will be 0.

  • @param a is the account to be cleared */ public static void clearAccount(Account a) {

kathy myBalance

myParent

100

myBalance

myParent

100 mike

150

myBalance

myParent

kathy myBalance

myParent

100

myBalance

myParent

kathy

kathy

Problem 4 (5 points) Consider working with a Riddle class about which you have only the following specification of a constructor.

public class Riddle {

/** Creates a new Riddle object.

  • @param c is used in mysterious ways
  • @exception IllegalArgumentException if c is not
  • an acceptable input. / public Riddle (char c) {/ body of constructor unknown */}

/* rest of Riddle class unknown */ }

Write a method that constructs a Vector of Riddle objects according to the specification below. The elements of the result may be in any order. (Your solution need not use every line.)

/** Creates a Vector of Riddle objects from the given String.

  • @param s contains the characters used to construct
  • each Riddle object
  • @return a Vector of Riddle objects, one Riddle for each
  • character in s that does not result in an
  • IllegalArgumentException when passed to the
  • Riddle constructor. */ public static Vector toRiddleVector (String s) { Vector result = new Vector(); for (int i = 0; i < s.length(); i++) {

return result; }

Account class from the Lab 2 solution. (Some methods simplified for brevity, but the simplifications do not affect your answers.) You may detach this page from your exam.

/** This class represents a bank account whose current balance is a

  • non-negative amount in US dollars. */ public class Account {

/** Construct an account with the given initial balance. */ public Account (int balance) { myBalance = balance; myParent = null; }

/** Construct an account with the given balance and parent account.

  • @param balance the initial balance; balance must be >= 0.
  • @param parent a parent account from which overdrafts will be taken. */ public Account (int balance, Account parent) { myBalance = balance; myParent = parent; }

/** Deposit into this account.

  • @param amount is the amount to be deposited; it must be non-negative. */ public void deposit (int amount) { myBalance = myBalance + amount; }

/** Subtract the amount from the account, if possible. If it would leave

  • a negative balance, print an error and leave the balance unchanged.
  • @param amount is the amount to withdraw. */ public Boolean withdraw (int amount) { if (myBalance < amount) { return false; } else { myBalance = myBalance – amount; return true; } }

/** Get the balance.

  • @return the number of dollars in the account. */ public int balance ( ) { return myBalance; }

private int myBalance; private Account myParent; }