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

CS 61B, Spring 2001 Midterm 1 - Java Programming Exam, Exams of Data Structures and Algorithms

The cs 61b midterm 1 exam from spring 2001, focusing on java programming. The exam includes multiple-choice problems related to java assignment statements, error identification in given code, and writing java methods. The exam also covers the islamicdate and account classes.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 61B, Spring 2001
Midterm #1
Professor Paul N. Hilfinger
Problem #0 (1 point, 1 minute)
Put your login name on each page. Also make sure you have provided the information requested on the first page.
Problem #1 (2 points, 4 minutes)
Write a single Java assignment statement that declares and assigns the variable maryAcct to have the value shown
in the picture. Solutions of more than one statement may earn partial credit. The Account class definition from lab
assignment 2 appears at the end of this exam.
Your solution:
_______________________________________________________________
Problem #2 (8 points, 20 minutes)
The two parts of this problem both involve an IslamicDate class that is somewhat simpler than that used for
homework assignment 2. An implementation of this class appears near the end of this exam.
Part a
Given below is an implementation of a DateTester class that could be used with the abbreviated IslamicDate class
provided at the end of this exam. The statements in the body of the main method are not necessarily correct; errors
include both compile-time and run-time errors.
For each statement, indicate whether it is OK or will result in an error by circling the appropriate word. You may
assume that all methods in the abbreviated IslamicDate work correctly and that each line with an error is fixed
before you consider the lines that follow it.
public class DateTester {
public static void main (String [ ] args) {
IslamicDate d1 = IslamicDate (1, 1); OK error
System.out.println ("" + d1); OK error
IslamicDate d2 = d1.makeTomorrow (); OK error
IslamicDate d3 = null; OK error
System.out.println (d3.tomorrow ()); OK error
}
CS 61B, Midterm #1, Spring 2001
CS 61B, Spring 2001 Midterm #1 Professor Paul N. Hilfinger 1
pf3
pf4
pf5

Partial preview of the text

Download CS 61B, Spring 2001 Midterm 1 - Java Programming Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 61B, Spring 2001

Midterm

Professor Paul N. Hilfinger

Problem #0 (1 point, 1 minute)

Put your login name on each page. Also make sure you have provided the information requested on the first page.

Problem #1 (2 points, 4 minutes)

Write a single Java assignment statement that declares and assigns the variable maryAcct to have the value shown in the picture. Solutions of more than one statement may earn partial credit. The Account class definition from lab assignment 2 appears at the end of this exam.

Your solution:


Problem #2 (8 points, 20 minutes)

The two parts of this problem both involve an IslamicDate class that is somewhat simpler than that used for homework assignment 2. An implementation of this class appears near the end of this exam.

Part a Given below is an implementation of a DateTester class that could be used with the abbreviated IslamicDate class provided at the end of this exam. The statements in the body of the main method are not necessarily correct; errors include both compile-time and run-time errors.

For each statement, indicate whether it is OK or will result in an error by circling the appropriate word. You may assume that all methods in the abbreviated IslamicDate work correctly and that each line with an error is fixed before you consider the lines that follow it.

public class DateTester { public static void main (String [ ] args) { IslamicDate d1 = IslamicDate (1, 1); OK error System.out.println ("" + d1); OK error IslamicDate d2 = d1.makeTomorrow (); OK error IslamicDate d3 = null; OK error System.out.println (d3.tomorrow ()); OK error }

CS 61B, Spring 2001 Midterm #1 Professor Paul N. Hilfinger 1

Part b Give the Java code for a DateTester method isLaterThan that determines if the first of its argument dates is later than the second. The isLaterThan method will be used by adding the statement

System.out.println ("IS " + d1 + " later in the year than "

  • d2 + "? " + isLaterThan (d1, d2));

to the end of the (Corrected) main method in the DateTester class of part a. (As in part a, DateTester will be used with the abbreviated IslamicDate class, not the class you implemented in homework assignment 2. You aren't allowed to change the IslamicDate class.)

// REQUIRES: two nonnull IslamicDate references, representing dates // in the same year. // MODIFIES: nothing // EFFECT: returns true if the first argument is later in the year // than the second; returns false otherwise.

Problem #3 (5 points, 15 minutes)

Consider the following (incorrect) version of the contains1MoreThan method, similar to those you worked with in lab assignment 3. Assume that it's defined as part of a class StringToCheck5 ; the rest of the code from lab assignment 3 appears at the end of this exam.

// REQUIRES: s is not null. // EFFECT: returns true when myString is the result of inserting // exactly one character into s; returns false otherwise.

public boolean contains1MoreThan (String s) { if (s.length( )==0 { return true; } else if (myString.length( )==0 { return false; } else { StringToCheck5 remainder = new StringToCHeck5 (myString.substring(1)); if (myString.charAt(0) == s.charAt(0)) { return remainder.contains1MoreThan (s.substring(1)); } else { return remainder.contains1MoreThan (s); } } }

Part a Can this method crash when given an argument that satisfies the REQUIRES clause? Briefly explain.

Part b Describe all pairs of Strings myString and s for which contains1MoreThan should return false but doesn't. For partial credit, you may give a single pair of Strings for which contains1MoreThan should return false but doesn't.

Problem #2 (8 points, 20 minutes) 2

return; } myDay = 1; myMonth++; if (isLegal ( )) { return; } myMonth = 1; }

// EFFECT: returns a date that represents the next calendar day. public IslamicDate tomorrow ( ) { IslamicDate d = new IslamicDate (myMonth, myDay); d.makeTomorrow ( ); return d; }

// EFFECT: returns true if this represents a legal Islamic date; // returns false otherwise. private boolean isLegal ( ) { // body of isLegal would go here. }

private int myMonth; private int myDay; }

Framework for the Account class used in lab assignment 2

public class Account { /**

  • This class represents a bank account whose current
  • balance is a nonnegative amount in US dollars, which may have
  • an auxiliary account to provide overdraft protection. */
  • REQUIRES: balance > 0.
  • EFFECT: Initialize an Account object with the given balance. */ public Account (int balance) { myBalance = balance; myParentAccount = null; }

/**

  • REQUIRES: balance > 0.
  • EFFECT: Initialize an Account object with the given balance,
  • and the given auxiliary account for overdraft protection. */ public Account (int balance, Account overdraftAcct) {

Framework of an abbreviated IslamicDate class 4

myBalance = balance; myParent = overdraftAcct; }

  • REQUIRES: amount >= 0.
  • MODIFIES: this.
  • EFFECT: Adds the given amount to this account's balance. */ public void deposit (int amount) { myBalance += amount; }
  • REQUIRES: amount >= 0.
  • MODIFIES: this.
  • EFFECT: If this account's balance is at least the given amount,
  • deducts the amount from the balance and returns true.
  • If there is no overdraft protection, prints an error message
  • and returns false. Otherwise, requests a withdrawal of the given
  • amount from the parent account, and returns true if that withdrawal
  • succeeds and false otherwise. */ public boolean withdraw (int amount) { // body of withdraw goes here }

/**

  • EFFECT: Returns the number of dollars in the account. */ public int balance ( ) { // body of withdraw goes here }

private int myBalance; private Account myParent; }

Lab3Tester.java

public class Lab3Tester { // Test teh various contains1MoreThan methods. public static void main (String [ ] args) throws Exception { ... (various calls to check) }

// Call one of the StringToCheck contains1MoreThan methods // to see if the String tryLarger is the result of inserting // exactly one character into the String trySmaller. // The value of whichTest indicates which StringToCheck class // is used: whichTest == 1 means StringToCheck1, 2 means

Framework for the Account class used in lab assignment 2 5

Posted by HKN (Electrical Engineering and Computer Science Honor Society)

University of California at Berkeley

If you have any questions about these online exams

please contact examfile@hkn.eecs.berkeley.edu.

Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley If y 7