Profanity is the one language that all programmers understand.
-- Anon.
Define “class.”
Where are the computers that are to be used for homework for this course?
What compiler are we using for this course?
The first and most important thing of all, at least for writers today, is to strip language clean, to lay it bare down to the bone.
— Ernest Hemingway
Define “Machine Language.”
Define “Assembly Language.”
Define “source code.”
Define “object code.”
Define “library.”
Define “linker.”
Define “executable program.”
How is assembly language different from machine language?
What does a compiler do?
How is a compiler different from an assembler?
What is the extension for source files on our machine?
What type of program is used to create “source code.”
What is the extension for object files on our machine?
What type of programs are used to produce object files?
What is the extension for the executable programs?
What type of files go into creating an executable program?
What type of program is used to create an executable program?
How do you access the on-line help system for your compiler?
There is no programming language, no matter how structured, that will prevent programmers from writing bad programs.
- L. Flon
It is the nobility of their style which will make our writers of 1840 unreadable forty years from now.
- Stendhal
Why are comments required in a program?
Why must you write comments before or while you write the program, never after?
Which
is better: 1) The underscore method of writing names, such as:
this_is_a_var
, or 2) The
upper/lower case method: ThisIsATest
?
A journey of a thousand miles must begin with a single step.
— Lao-zi
If carpenters made buildings the way programmers make programs, the first woodpecker to come along would destroy all of civilization.
— Anon.
Name and define the three elements of a program.
Why are comments required in a program?
What
is the purpose of the return(0);
near the end of each program?
What punctuation character signals the end of each statement?
What statement to you use to print something to the screen?
What are the five simple C++ operators?
Evaluate the following C++ expressions:
5 + 3 / 2 + 1
(5 + 3) / ( 2 + 1)
8 % (3 * 4) + 8 / 3 - 9
8 / (3 * 4 + 8 / 3 - 9)
4 + 9 * 6 - 8
(11 % 7) / 3
What will the following statements print? (Assume i=5 and j=7.)
std::cout
<< i << '\n';
std::cout
<< "i" << '\n';
std::cout
<< i / j << '\n';
std::cout
<< "i=" << i;
std::cout
<< "i=" << i << '\n';
Turn
the equation
½ d t2
into
a C++ statement.
What does it mean when you see the message “Warning: Null effect”?
Define “variable.”
Define “variable declaration.”
What are the three purposes for a variable declaration?
The FORTRAN language (among others) would automatically declare a variable the first time it was used. Variables that began with A-H,O-Z where automatically declared float and variables that begin with I-M were automatically declared int. Discuss the advantages and disadvantages of this “feature.” (Note: FORTRAN was invented before lowercase was widely used in computes, so all variables were written in uppercase only.)
Define
std::cout
and use it in a C++ statement.
Define “assignment statement” and give an example.
What is the difference between a real and an integer variable?
Give an example of an integer division statement and a floating point division statement?
The same operator ‘/’ is used for both floating point and integer divides. How does the compiler know which one to use?
A thermos keeps hot things hot and cold things cold. How does it know which one to do?
The standard character set, ASCII, handles 128 characters. Only 95 of these can be typed in using the keyboard. What does a C++ programmer use to specify the none printing characters?
Define “character variable” and show how a character variable can be declared and used.
What are the possible value of a boolean (bool) variable?
The answers to these may require resources outside the scope of this book.
What
is the value of 4.5 % 3.2
?
Why are real numbers called floating point numbers?
What “floats” in a floating point number?
What is the biggest integer variable that you can have on your machine?
What is the biggest floating point variable that you can have on your machine?
Why are integer variables exact and floating point variables inexact?
That mysterious independent variable of political calculations, Public Opinion.
— Thomas Henry Huxley
What is the difference between an array and a simple variable?
What is the number of the last element of the following array?
int test[5];
What's the header file that's used to bring in the data definitions for C++ strings?
#include <string>
How do you concatenate two C++ style string?
What is the difference between a C style string and an array of characters?
Why
must we use std::strcpy
to assign one C style string to another?
We define a character array whose job is to hold a string. What is the difference between the size of the array and the length of a C style string?
Can the size of any array change during the execution of a program?
Can the length of a string variable change during the execution of a program?
What
happens if you try to read a C++ style string using std::cin
and the >>
operator? (Try it!) What about the C style strings?
How many elements are allocated for the multiple dimension array:
int four_dimensions[25][86][48][96];
Bonus question: Why will you probably never see a declaration like this in a real program?
Define the following:
long int
short int
unsigned
signed
float
double
register
auto
volatile
const
reference
extern
Why must you use the notation
static_cast<
int>(very_short)
to write a very short number (a.k.a. a character variable)? What
would happen if you wrote it out without the int
wrapper?
Define side effect.
Why are side effects bad things?
Once a decision was made, I did not worry about it afterward.
— Harry Truman
Define the following:
Branching Statements
Looping Statements
if statement
else clause
relational operator
logical operator
while statement
Fibonacci number
break statement
It’s just a simple matter of programming.
— Any boss who has never written a program.
Review Questions
Describe the various steps of the programming process:
a. “Requirements”
b. “Specification”
c. “Code Design”
d. “Coding”
e. “Testing”
f. “Debugging”
g. “Release”
h. “Maintenance”
i. “Revision and updating”
Why do we put each program in its own directory?
What is “Fast Prototyping?”
What is the make utility and how is it used?
Why is a “Test Plan” necessary?
What is “Electronic Archeology?”
What tools are available to aid you in going through some one else’s code?
What
is grep and how would you use it to find all
references to the variable total_count
?
Grammar, which knows how to control even kings...
— Molière
Why do C++ programs count to five by saying “0, 1, 2, 3, 4?” Why should you learn to count that way?
Write a for statement to zero the following array:
data[10];
Why
do we end each case with break or //
Fall through
?
Why do we always put a break at the end of each switch?
What will continue do inside of a while?
What will continue do inside of a for?
What will continue do inside of a switch? (This is a trick question.)
What will break do inside of a while?
What will break do inside of a for?
What will break do inside of a switch? (This is not a trick question.)
Discuss the pros and cons of putting the default statement at the end of every switch. Is there a case where putting the default statement somewhere else might be useful?
But in the gross and scope of my opinion
This bodes some strange eruption to our state.
— Shakespeare Hamlet, Act I, Scene I
Define “Variable Scope.”
Define “Variable Storage Class.”
Why are hidden variables a bad idea?
What is the scope of a function’s parameters?
What is the class of a function’s parameters?
The keyword static changes a local variable’s storage class from __________ to __________.
Why are all global variables permanent?
When is the following variable created, initialized, and destroyed?
int
funct(void) {
int
var = 0 // The variable in question
//
......
When is the following variable created, initialized, and destroyed?
int
funct(void) {
static
int var = 0 // The variable in question
//
......
When is the following variable created, initialized, and destroyed?
int
var = 0 // The variable in question
int
funct(void) {
//
......
Define namespace.
What is the name of the namespace we've already used in this book for cin and cout?
Define “reference.”
What is binding and when does it occur?
What is the difference, if any, between the type of values returned by the following two functions:
int
func1(void)
const
int funct2(void)
What is the difference, if any, between the type of values returned by the following two functions:
int
&fun1(void)
const
int &fun2(void)
Which parameters in the following function can be modified inside the function:
void
fun(int one, const int two, int &three, const int &four);
In the previous question, which parameters, when changed, will result in changes made to the caller’s variables?
Which parameters in the following function can be modified inside the function:
void
fun(int one[], const int two[]);
In the previous question, which parameters, when changed, will result in changes made to the caller’s variables?
Define “Dangling Reference.”
Why are dangling references a bad thing?
Can we overload the square function using the following two function definitions?
int
square(int value);
float
square(float value);
Can we overload the square function using the following two function definitions?
int
square(int value);
float
square(int value);
Define “default parameters.”
Why must default parameters occur at the end of a parameter list?
What does the keyword inline do?
What programming technique was used to solve the “calculator problem” of Chapter 7, The Programming Process?
Define “Structured Programming.”
Describe “Top down programming.”
Describe “Bottom up programming.”
Define “recursion.”
What are the two rules you must follow to make a recursive function?
The speech of man is like embroidered tapestries, since like them this has to be extended in order to display its patterns, but when it is rolled up it conceals and distorts them.
— Themistocles
Define
#define
.
What is the difference between a simple #define macro and a const declaration?
When would you use conditional compilation?
Would you ever use conditional compilation for something other than debugging?
What is the difference between a parameterized macro and a simple macro?
What is the difference between a macro and a normal function?
What is the difference between a macro and an inline function?
Can the C++ preprocessor be run on things other than C++ code? If so what?
Why do we not want to use macros to define things like:
#define BEGIN {
#define END }
To be or not to be, that is the question
— Shakespeare on boolean algebra
Describe the difference between bitwise and (&) and logical and (&&). Give an example of each.
Describe the difference between bitwise or (|) and logical or (||).
If you didn’t have an exclusive or operator (^) how would you create one out of the other operators?
How would you test to see if any one of a set of bits is on? (This can be done without using the logical and (&&) operation.)
How
would we rewrite the set_bit
function if we used an array of short int instead of an array
of char.
Total grandeur of a total edifice,
Chosen by an inquisitor of structures.
— Wallace Stevens
Define Structure.
Define Union.
How can we tell what type of data is currently stored in a union?
Define typedef.
Which is better typedef or #define? Why?
Define enum type.
List two reasons that enum types are better than defining a series of constants.
Define “bit field.”
What are the advantages and disadvantages of using bit fields.
She thinks that even up in heaven
Her class lies late and snores,
— Cyril Connolly
Define “class”
Define the “big four” member functions.
What are the hidden functions called in the following code:
void
use_stack(stack &local_stack)
{
local_stack.push(9);
local_stack.push(10);
}
Why can you overload the constructor and not overload the destructor?
Which of the big four are generated automatically?
How do you prevent each of them from being generated automatically if you don’t explicitly define one.?
This method is, to define as the number of a class the class of all classes similar to the given class.
— Bertand Russell
Principles of Mathematics part II, chapter 11, section iii, 1903
Review Questions
Define “friend.”
Describe an example when you would use a “friend” function.
Define “constant member functions.”
Define “constant member variable.”
How do you initialize the constant member variables of a class?
Define “static” member variables.
Why are you able to access static member variables using the class::var syntax you cannot use the same syntax for ordinary member variables?
Define “static member function.”
Why are static member functions prohibited from accessing ordinary member variables?
Why can they access static member variables?
The choice of a point of view is the initial act of culture.
— Ortega y Gasset
Define “pointer.”
How is a pointer different from a thing?
How many pointers can point to a single thing?
What happens if you assign a pointer NULL and then try and dereference it?
Are pointers more efficient than arrays?
Are pointers more risky to use than arrays?
Which of the preceding two questions is the more important?
I the heir of all the ages, in the foremost files of time.
— Tennyson
There are three different I/O packages described in this chapter: C++ streams, raw I/O, and C standard I/O. Describe the advantages and disadvantages of each.
Define
std::cin
,
std::cout
,
and std::cerr
.
Define
std::ifstream
.
Define
std::ofstream
.
How can we tell if a write worked?
How can we tell if a file exists?
What’s
the difference between std::getline
and the >> string
operation?
What
does sizeof
do?
Write a C++ program fragment to write out a variable containing the value “3” as “0003”.
Why are graphic images almost always stored as binary files?
One type of image that’s stored as an ASCII file is an X icon. Why is it ASCII?
What’s the difference between 0 and ‘0’?
What type of end of line is used on your machine?
Someone took a text file from DOS and moved it to UNIX. When they tried to edit it a lot of control-M’s (^M) appeared at the end of each line. Why?
Why is buffering a good idea?
When is it not a good idea?
Why
is the C style std::printf
more risky to use than the C++ style std::cout
?
Bloody instructions which, being learned, return to plague the inventor.
— Shakespeare on debugging.
Describe useful debugging aides that might be built into a program.
Describe your hardest bug and what you did to fix it.
What happens when you try to divide by 0?
What happens when you attempt to dereference the NULL pointer?
What happens when you modify the data pointed to by the NULL pointer?
What happens when you write a program that does infinite recursion?
Define std::flush
..
Define “Confessional Method of Debugging.”
Define “Loop ordering.”
Define “Reduction in Strength.”
Write a program that divides an integer variable by 10 a thousand times and time how long it takes. How long does it take to divide the variable by 16?
Overloaded, undermanned, ment to flounder, we
Euchred God Almighty’s storm, bluffed the Eternal Sea!
— Kipling
Define “overloading.”
Define “operator overloading.”
What’s the difference between x++ and ++x?
How do you tell them apart when defining the operator functions?
What is casting?
Why should you not overload the casting operators? What should you use instead? Why?
1 is equal to 2 for sufficiently large values of 1.
— Anonymous.
.
Define “Overflow.”
Can you have overflow with integer calculations?
Define “Underflow.”
Can you have underflow in integer calculations?
What’s the biggest floating point number that can be represented on your machine.
On your pocket calculator what does 1-(1/3)-(1/3)-(1/3) equal?
Why do you never want to use floating point for money?
A race that binds
Its body in chains and calls them Liberty,
And calls each fresh link progress.
— Robert Buchanan
Review Questions
Define “Linked List.”
What would you use a linked list for?
Define “Double-linked List.”
What can a double-linked list be used for that a linked list cannot.
Define “binary tree.”
What are trees good for?
How would you delete an element from a tree?
The ruling ideas of each age have ever been the ideas of its ruling class.
— Karl Marx
Manifesto of the Communist Party
Define “derived class.”
Define “base class.”
Can a class be both derived and base?
Define “virtual function.”
Define “pure virtual function.”
Describe how to implement virtual functions using function pointers. (This is what C++ does internally.)
Define “abstract class.”
Why can’t you use an abstract class to define a variable?
How glorious it is — and also how painful — to be an exception.
— Alfred De Musset
Define “exception.”
Define “try.”
Define “catch.”
Define “throw.”
Many hands make light work.
— John Heywood
Define “header file.”
What should go in a header file? Why?
What should not go in a header file? Why?
Define “extern.”
Define “static.” (Warning: It’s used for a lot of things.)
Define “infinite array.”
Thou cunning’st pattern of excelling nature,.
— Shakesphere, Othello Act V
Define “template.”
When does a template cause code to be generated?
What is a function specialization?
What is a class specialization?
Define "export"?
Name a compiler which implements the "export" directive correctly.
Goodness and evil never share the same road, just as ice and charcoal never share the same container.
— Chinese proverb
Define “STL Container.”
What are the differences between a STL vector and a C++ array?
How do you check to see if an iterator has reached the end of a container?
What's stored in a map?
If carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization
— Traditional computer proverb
Review Questions
Name and define three design goals that should be a part of any program's design.
What are other design factors that might be considered.
MS/DOS was designed for a 640K PC with 360K floppies as it's primary storage device. Did the design of the operating system make it easy to expand the system for today's modern computers?
What makes a good module?
What is information hiding?
Why are global variables considered bad?
Are all global variables bad?
One of the problems with C++ is that a class must expose implementation information to modules using that class. Where is this implementation information located?
For there isn’t a job on the top of the earth the beggar don’t know, nor do.
— Kipling
Describe the programming process used to create the program created in this chapter. Explain what worked and what didn’t. If something didn’t work, what can be done to fix the problem?
No distinction so little excites envy as that which is
derived from ancestors by a long descent.
— François De Saliganc De La Mothe Fénelon
Define “K&R Style Functions.”
Define
malloc
.
Define
free
.
Why
should you never use malloc
on a class?
Why
should you never use free
on a class?
How do you initialize a structure to be all zeros? How do you initialize a class to be all zeros?
Define
setjmp
and longjmp
.
What are the dangers associated with using these functions?
Second thoughts are ever wiser.
— Euripides
New nobility is but the act of power, but ancient nobility is the act of time.
— Francis Bacon
Why are comments required in a program?
Why must you write comments before or while you write the program, never after?
Which
is better: 1) The underscore method of writing names example:
this_is_a_var
,
or the upper/lower case method: ThisIsATest
?
(Note this is a religious issue and has no right answer.)
Define
std::
cout
and use it in a C++ statement.
Define the following:
long int |
short int |
unsigned |
signed |
float |
double |
register |
auto |
volatile |
const |
reference |
extern |
Why
must you use the notation int(very_short)
to write a very short number (aka. a character variable)? What would
happen if you wrote it out without the int
wrapper?
Define side effect.
Why are side effects bad things.
When is the following variable created, initialized and destroyed?
int
funct(void) {
int
var = 0 // The variable in question
//
......
When is the following variable created, initialized and destroyed?
int
funct(void) {
static
int var = 0 // The variable in question
//
......
When is the following variable created, initialized and destroyed?
int
var = 0 // The variable in question
int
funct(void) {
//
......
Define “reference”?
What is binding and when does it occur?
What is the difference, if any, between the type of values returned by the following two functions:
int
func1(void)
const
int funct2(void)
What is the difference, if any, between the type of values returned by the following two functions:
int
&fun1(void)
const
int &fun2(void)
Which parameters in the following function can be modified inside the function:
void
fun(int one, const int two, int &three, const int &four);
In the previous question, which parameters when changed will result in changes made to the caller’s variables?
Which parameters in the following function can be modified inside the function:
void
fun(int one[], const int two[]);
In the previous question, which parameters when changed will result in changes made to the caller’s variables?
Define “Dangling Reference.”
Why are dangling references a bad thing?
Can we overload the square function using the following two function definitions?
int
square(int value);
float
square(float value);
Can we overload the square function using the following two function definitions?
int
square(int value);
float
square(int value);
Define “default parameters.”
What does the keyword inline do?
What programming technique was used to solve the “calculator problem” of Chapter 7, The Programming Process.