Quiz — Fundamentals
This quiz focuses on the aspects of the C++ language that are conceptually similar to those in other procedural languages: variable declaration, expression evaluation, functions, input/output, and flow-of-control constructs. It also introduces the class construct, the basis of object-oriented programming in C++.
A lot of detail is covered in this segment. Many students prefer to work on the "Simple looping and testing" and "Flow of control" programming assignments prior to taking this quiz, in order to provide themselves with more of the big picture in which all the details fit.
Readings
A Computer Science Tapestry, chapters 1 through 7 except sections 6.3.5, 6.3.7, 6.5, and 7.4 (these sections cover files and structs, which appear on the next quiz).
C++ Program Design, chapters 1 through 8 except sections 5.6 and 5.8 (these sections cover files, which appear on the next quiz, and character classification functions, which aren't covered in CS 9F).
Computing Fundamentals with C++, chapters 1 through 8.
The section of this document titled "Comparing C++ and non-C-based programming languages". Students who have programming experience in C should also read "C constructs to avoid" in this study guide. The forbidden constructs are forbidden in quiz solutions as well as programs.
Exercises
All exercises in chapters 2 through 7 in A Computer Science Tapestry, except for exercises involving files and structs, will be useful.
All exercises in chapters 2 through 8 of C++ Program Design except for exercises involving enums, files, or the iomanip, cmath, or ctype libraries will be useful. You need not know the details of the SimpleWindow class library. You should, however, be able to work with code based on a class whose definition is provided on the quiz.
All exercises in chapters 2 through 8 of Computing Fundamentals with C++except for exercises involving the cmath library, including self-check exercises but not including programming projects, will be useful.
Sample quiz
- Explain what function overloading is, and how it's useful.
- Write an input statement that reads values from cin into variables cmd and arg, and a single output statement that prints those values, labeled appropriately, on two separate lines.
- Rewrite the following code segment as a switch statement.
if (n == 1) { total = total + 2; } else if (n == 2) { total = total + 27; } else if (n == 3) { total = total + 314; } else { total = total - 6; }n = 0; for (int k=1; k<5; k=k+2) { n++; }
- Fill in the blank in the definition of the function InRange given below. InRange should return true if its integer argument is between 4 and 27, inclusive.
bool InRange (int n) { return _________________________________ ; }
- Complete the heading for function F so that, on return from the call to F in main, the variable n1 contains the value 3 and the variable n2 contains the value 7.
void F ( _________________________________ ) { k1++; k2 = k2+2; } int main () { int n1=3, n2=5; F(n2,n1); }
- List the contents of the fractions.h file for a Fraction class whose member functions include a constructor, Numerator and Denominator access function, and a Sum function that returns the result of adding the value stored in the Fraction object to the argument value. Assume that the private data for a fraction consists of two integers called myNumer and myDenom.
- Define the Sum function that would appear in the fractions.cpp file for the Fraction class described in question 7.
Solutions to sample quiz
- Function overloading refers to the use of the same name for two different functions, for example, use of the name "Print" as a member function of two different classes. With function overloading, one can give the same name to functions that perform the same action, regardless of what types of data are involved; otherwise, one would have to make up artificially different names such as "PrintDate" and "PrintTime".
cin >> cmd >> arg; cout << "cmd = " << cmd << endl << "arg = " << arg << endl;switch (n) { case 1: total = total + 2; break; case 2: total = total + 27; break; case 3: total = total + 314; break; default: total = total - 7; break; }The last break (the one in the default case) is optional.
- n contains 2; it was incremented once for k having the value 1, and once more for k having the value 3.
- There are several possible answers. Here is one:
return n>=4 && n<=27;void F (int &k2, int k1) ...class Fraction { public: Fraction (int numer, int denom); int Numerator (); int Denominator (); Fraction Sum (Fraction x); private: int myNumer; int myDenom; };Fraction Fraction::Sum (Fraction x) { return Fraction (myNumer*x.myDenom + myDenom*x.myNumer, myDenom*x.myDenom); }