Computer Science 61B - Fall 1996 - Canny - Midterm 1

Computer Science 61B - Fall 1996 - Canny - Midterm 1 -...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: <!--CS61b, Midterm #1, Fall 1996)-->Problem #1(10 points) The program below has a series of print statements. Each one has a comment line after it with a letter in parenttheses like lis :// (A) Next to each letter, write the output that you expect to see from that statement when the program runs. All these statements are part of one program, so you should answer the questions in order. int a[ ] = {5, 6, 7, 8}; int &b = a[0]; int &c = b; c = 0; cout << a[0] << "," << b << "," << c << endl; \\ (A) _____________________________________ int d = a[1]; int * e = & a[2]; d = 0; * e = 0; cout << a[1] << "," << a[2] << "," << d << "," << *e << endl; \\ (B) _____________________________________ int * f = & a[3]; f = e; *f = 20; cout << a[2] << "," << a[3] << "," << *e << "," << *f << endl; \\ (C) _____________________________________ Problem #2file:///C|/Documents%20and%20Settings/Jason%20Raft...0-%20Fall%201996%20-%20Canny%20-%20Midterm%201.htm (1 of 5)1/27/2007 5:40:36 PM<!--CS61b, Midterm #1, Fall 1996)-->(10 points) Here is a for statement from a recent piece of CS61B code: int prod = 1; for (int i = 1; i<=n; i++) { prod *= i; // "Loop invariant" } Which of the following would be a suitable loop invariant to replace the " Loop invariant " in the comment above? (a) prod *= i; (b) prod = i! (i factorial) (c) i = i + 1 (d) i++ Your answer ____________________ (4 points) Now consider the following piece of code: int sum = 1; for (int i = 0; i < n; i++) { sum += sum; // "Loop invariant" } write down a loop invariant to replace the "Loop invariant" comment: Your answer __________________________ (6 points) Problem #3file:///C|/Documents%20and%20Settings/Jason%20Raft...0-%20Fall%201996%20-%20Canny%20-%20Midterm%201.htm (2 of 5)1/27/2007 5:40:36 PM<!--CS61b, Midterm #1, Fall 1996)--><!...
View Full Document

This note was uploaded on 05/17/2009 for the course CS 61B taught by Professor Canny during the Spring '01 term at University of California, Berkeley.

Page1 / 5

Computer Science 61B - Fall 1996 - Canny - Midterm 1 -...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online