CS401 - Self-Check Quiz #2

Give yourself about 30 minutes to do these problems.  Try not to look at your notes or the book until you're done.  Solutions will be made available online near the end of the week.
 

Question 1:  True/False

Circle T or F for the following statements:
  1. Value parameters are "safer" to use than reference parameters.
  2. Structs allow you to define a new type.
  3. The "return" value of a function is what it prints on the screen.
  4. Default parameters must appear at the beginning of a function definition parameter list.
  5. You are not allowed to use the same parameter name twice in a program with multiple functions.

Question 2:  Output 

Do the following problems from the book:
  • 7.1 (p.351-2)
  • 7.3 (p.353)
Determine the output of the following program:
 
#include <iostream>
#include <string>
using namespace std;

bool shrinkable(int val);
void shrink(int& val);
void unshrink(int& val);
void output(int m, int n, int p);

int a=2, b=8, c=0;

int main() {
  int c=28;
  while (shrinkable(c)) {
    int a;
    a = 4;
    shrink(c);
    if (shrinkable(b)) {
      shrink(b);
      unshrink(c);
    }
    output(a,b,c);
  }
  output(a,b,c);
}

bool shrinkable(int val) {
  return (!(val%a));
}

void shrink(int& val) {
  c++;
  val = val / a;
}

void unshrink(int& val) {
  val = val * a;
}

void output(int m, int n, int p) {
  cout << m << " " << n << " " << p << " " << c << endl;
}

Question 3:  Short Answers

  1. Describe the difference between pass-by-reference and pass-by-value.  Give one example where you would have to have PBR because PBV is not sufficient.
  2. Describe the concept of a stream and how C++ treats them.
  3. Show how a for loop can be rewritten as a while loop.  Keep it as general as possible.
  4. How is recursion similar to looping?  How is it different? (one similarity and one difference is sufficient)
  5. What is function overloading?  Give an example and show a few calls to your function.

Question 4:  Write C++ code

  1. Write a prototype for a void, parameterless function called "displayMenu".
  2. Problems 5.10, 5.11, and 5.12 in the book.
  3. Write a struct declaration for a book (pick about 3 or 4 fields).  Declare a few book variables and give the fields values.
  4. Write a function that takes in three integer parameters and returns the largest one.  
  5. Write a void function with four integer parameters such that:
    1. The first two parameters are input, representing the low and high ends of a range.
    2. The third parameter should be assigned the difference between those low and high values.
    3. The fourth parameter should get the sum of all numbers between the low and high values (inclusive).
  6. Write a function that takes in an output stream as an argument and three integers, then sends those three integers to the specified stream so that they are in a vertical column, right justified.  You may assume the integers will not exceed 4 digits in width, and there is no need to insert commas.
  7.  Problem 6.51 in the book.
Last Updated: 7/2/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin