DOC PREVIEW
Stanford CS 106A - Parameter Problem Set

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Eric Roberts Handout #24CS 106A January 22, 2010Parameter Problem SetDue date: Friday, January 29, 5:00P.M. (There are no late days on problem sets.)This handout is the first of two problem sets in CS 106A, which are designed to makesure that you understand some of the important concepts that do not always come acrossin the assignments. The problem sets are pencil-and-paper exercises rather thanprogramming problems, although you are free to use the computer to check your work.Problems of this sort, however, will definitely appear on the midterm and final exam, soit is important to make sure that you can solve them without electronic assistance.Special note: The best entries in each of the short answer questions will qualify fortickets in the final drawing for a special grand prize.1. True/False questionsFor each of the following statements below, indicate whether it is true or false in Java.None of these are in any sense “trick questions” for which the answer depends onthinking up some special case. If you understand what’s going on with methods, theanswers will be entirely straightforward.1a) The value of a local variable named i has no direct relationshipwith that of a variable named i in its caller. 1b) The value of a parameter named x has no direct relationship withthat of a variable named x in its caller. 1c) Assigning a new value to a parameter changes the value of thecorresponding variable in the caller’s argument list. 1d) Local variables of type int are automatically initialized to 0 whena method is called. 1e) Predicate methods always return a value of the primitive typeboolean. 2. Short answerMany beginning students find it hard to understand the relationships among variables inthe different methods that make up a program. Often, students tell me that it would beeasier if a particular variable name, such as total or i, always contained the same valuein any part of the program in which that variable appeared. In your own words, describewhat would be wrong with that approach?Answer to question 2– 2 –3. Short answerSuppose that you wanted to explain the concept of arguments and parameters to a sibling(or, more likely, a parent) with no background in programming at all. What everyday,real-world example would you use to make it clear why a procedure (or any generalizednotion of a set of steps for carrying out a particular task) might want to include someform of parameterization?Answer to question 34. Short answerIn a few sentences, describe the difference between local variables and instance variables.Make sure that your answer includes any differences in (a) how those variables aredeclared and (b) how long the values of those variables persist.Answer to question 45. Tracing method executionFor each of programs on the two pages that follow, show what output is produced by theprogram when it runs. Each of these problems is taken from a past midterm exam.– 3 –5a)/* * File: Mystery.java * ------------------ * This program doesn't do anything useful and exists only to test * your understanding of method calls and parameter passing. */import acm.program.*;public class Mystery extends ConsoleProgram {public void run() {ghost(13);}private void ghost(int x) {int y = 0;for (int i = 1; i < x; i *= 2) {y = witch(y, skeleton(x, i));}println("ghost: x = " + x + ", y = " + y);}private int witch(int x, int y) {x = 10 * x + y;println("witch: x = " + x + ", y = " + y);return x;}private int skeleton(int x, int y) {return x / y % 2;}}Answer to question 5a– 4 –5b)/* * File: Hogwarts.java * ------------------- * This program doesn't do anything useful and exists only to test * your understanding of method calls and parameter passing. */import acm.program.*;public class Hogwarts extends ConsoleProgram {public void run() {bludger(2001);}private void bludger(int y) {int x = y / 1000;int z = (x + y);x = quaffle(z, y);println("bludger: x = " + x + ", y = " + y + ", z = " + z);}private int quaffle(int x, int y) {int z = snitch(x + y, y);y /= z;println("quaffle: x = " + x + ", y = " + y + ", z = " + z);return z;}private int snitch(int x, int y) {y = x / (x % 10);println("snitch: x = " + x + ", y = " + y);return y;}}Answer to question


View Full Document

Stanford CS 106A - Parameter Problem Set

Download Parameter Problem Set
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Parameter Problem Set and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Parameter Problem Set 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?