Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key


Play button


Play button




Click to flip

100 Cards in this Set

  • Front
  • Back
C++ not only supports OOP but also supports other programming styles.
Before a variable in C++ is used, it must be
The person responsible for the initial development of C++ was
Bjarne Strousrup

Bjarne Stroustrup initially developed C++ at AT&T Bell Labs in the 1980s.
A l-value
is assigned a value
OOP is an acronym that means Object Oriented Programming.
Which of the following does the C++ language not support?
automatic garbage collection
In C++ the variables Alpha, ALPHA, and AlphA are the same identifier.
The namespace facility is a tool that assists in the study of genealogy.

The namespace facility helps prevent the libraries from "preempting all the good names," and allows us to use names we want whether the library has used them.
Which of the following types are not built into the C++ language?

There is no "real" type in C++.
In C++ the compiler will infer the type intended for a variable from the context in which the variable occurs.

C++ requires every identifier to be declared prior to use. The type is specified in the declaration.
The if, while, and for statements control only one statement.

The one statement may be a block (statements that are enclosed with curly braces { }) or a simple statement.
Suppose we have these declarations,

int x = -1, y = 0, z = 1;

This Boolean expression is correct and it does what the programmer intends:

x < y < z

Unfortunately, the expression compiles without error and runs. The < operator associates (groups) left to right, so the expression evaluates as

(x < y) < z

The left hand expression evaluates to true, which, if compared to a numeric type, converts to 1. When compared to 1, this is false. What the programmer intends, expressed as mathematics might, is -1 < 0< 1, a result that is clearly true.
The value of count is 0; limit is 10. Evaluate:

(count == 0)&&(limit < 20)
What is the value of the bool valued expression, 1 < x < 10? Does the value of this depend on the value of x? Explain and give the expression that the programmer probably meant.
The value is always true, and the value does not depend on x.

This expression is always true. The value does not depend on x. This is the mathematicians' shorthand for what the programmer probably meant:

(1 < x)&&(x < 10).

The < operator associates (groups) left to right, so the expression evaluates as (1 < x) < 10. The expression (1 < x) evaluates to either false or true, regardless of x. These bool values convert to int values 0 or 1 when compared to int value 10. The expression evaluates to true for all values of x.
In a switch statement, when a break statement is encountered, an immediate transfer of control is made to
the statement beyond the end of the switch statement.
Which of the following determines the operator that is processed prior to another operator?
operator precedence
A switch statement must have
none of the above
You want to determine whether time has run out. The following code correctly implements this:

!time > limit

The expression always evaluates to false. This cannot be what the programmer intended. The compiler doesn't catch the problem because the code is legal, correct C++. Corrected code is

!(time > limit)

Code execution proceeds as follows: The operator ! takes a bool argument. It returns the opposite bool value. The value of time is converted to a bool. The value of time is certainly nonzero, hence !time is !true, i.e., false. The > compares this result with a numeric value, limit, (an int or perhaps some kind of floating point). The value on the left (false) is converted to a 0 value of that type. The value of limit is unlikely to be a negative number and we are concerned about time running out, so it is unlikely that time is zero. Consequently, the inequality becomes 0>limit, where limit is nonzero. This is false.
Given the declaration

int x = 0;

the following expression causes a divide by zero error:

(x !=0) || (2/x < 1);

The || operator uses short-circuit evaluation. The first member of this expression is true; the truth value of the complete expression can be determined from this; consequently, the second expression is not evaluated. There is no divide-by-zero error.
Which of the following loop statements is guaranteed to iterate the body of the loop at least once?
do body while(control);
It is legal to replace the prototype

double totalCost(int numberParam, double priceParam);

with the more terse, alternate form

double totalCost(int, double);

The alternate form applies only to function declarations. You will see this form in manuals. It is possible to omit variable names that will not be used in function definitions, but the text does not do this.
What do the calls to exit(...) do? When exit(0) and exit(1) are called, what receives these arguments and what is done with them?
The exit( ) function stops the program. The argument is passed to the operating system which uses it for an error code.

The other answers are foils, with nothing to recommend them except that they might sound good to some students who have not adequately prepared.
Every programmer must know all the details of what that programmer's team mates are doing in their projects to do the work assigned. Why or why not?

Only the client programmer needs to know the use to which a function is put. Only the author of the function needs to know the internals. The only thing these two share is the contract or specification of the function.
Code after a return or a call to the exit function is executed will not be executed.

Control returns to the caller if return is executed out of a called function, and returns to the operating system if return is executed out of main. If exit is executed anywhere, the program is terminated and control is returned to the operating system.
A call to a C++ function is
the name of the function followed by exactly the number of arguments as there are parameters in the definition.
Procedural abstraction involves information hiding in that only the "contract" between the programmer using the function (the client) and author of a function is known to either.

Under procedural abstraction, the author (or programmer) of a given function should know about how the application programmer (or the client) will use the function in the "contract." The contract is what the function is to do, and the conditions set on the parameters to guarantee that the function can do IT. The author need not know how the function is to be used by the client programmer. The client programmer only needs to know what the function does, the requirements on the parameters, but not how the function carries out the task. This way each can do his job independently of the other.
A void function can have
as many arguments as the programmer wishes.

The other answers are foils. This is the only correct answer, as many as the programmer wishes.
A sequence of calls to the library function rand() generates mathematically correct random number sequences.

The sequence is called pseudorandom because it appears to be random. The results are good enough to obtain usable answers with computer simulation.
Which of the following code fragments give(s) a random double value between 2 and 5 inclusive? You can assume that the random number seed has been set and any needed definitions and initializations have been made.
((RAND_MAX-rand())/static_cast<double>(RAND_MAX))*3 + 2

Divide and multiply associate left to right, so the expression


gives a double between 0.0 and 1.0. Multiplying by 3 scales this to 0.0 to 3.0, adding 2 gives 2.0 to 5.0 as required, without overflow.
Which of the following are not names of a C++ library function?

The closest library function name to random is rand, which returns pseudorandom numbers, or perhaps srand, which seeds the rand random number generator.
There is only one kind of parameter passing in C++, namely call-by-value. Explain.

There is also call-by-reference. Call-by-value makes a copy of the argument then uses that, hence does not make changes to the argument. Call-by-reference effectively passes the argument, hence can make changes to the argument.
A call-by-value parameter may pass data only into a function.

The value parameter is essentially a local variable initialized to the value of the argument at the time of a call.
Given the following function, and the main function calling it, which, if any, of the choices below is the output of the following code? What does this function do?

#include <iostream>
using namespace std;

void func ( int& x, int & y)
int t = x;
x = y;
y = t;
int main()
int u = 3;
v = 4;
// ...
cout << u << " " << v << endl;
func ( u, v )
cout << u << " " << v << endl;
// ...
3 4 4 3

The code swaps the values stored in the caller's arguments.
A program should have all of its functions written before testing can begin.

Functions that call other functions can be tested independently of the called functions by writing stub functions. Stub functions are very simple functions that do nothing much more than sending just enough data to the caller to allow testing.
Functions that call other functions can be tested independently of the called functions.

Functions that call other functions can be tested independently of the called functions by writing stub functions. Stub functions are substitute functions that are as simple as possible yet send data enough back to the caller to allow testing of the caller.
The call-by-reference mechanism is specified in the function declaration and definition, using a $ between the type and the parameter.

The character is an ampersand, &, not a $. It is placed between the type and the parameter to indicate a call-by-reference parameter.
A call-by-reference parameter may pass data only out of a function.

The reference parameter is an alias for the caller's argument. The mechanism is to pass the address of the argument which is used where the parameter occurs. Hence, a reference parameter makes available to the function the initial and ongoing values of the caller's argument as well as providing a mechanism for the function to change the caller's argument.
The position of the ampersand in the function header is of no importance to the proper compiling and execution of code that uses call-by-reference parameters.

It makes no difference whether the ampersand appears next to the type or the variable.
Call-by-reference is restricted to void functions.

There is no connection between the return mechanism and the parameter passing mechanism.
Consider the following function and code segment.

void One( int first, int & second )
first = 17;
second = first + 1;
int main()
// other code ...
int j = 4;
int k = 3;
One(j, k);
// other code ...

After the call to One(j, k); what are the values of j and k? Why?
j == 4, k == 18

The first parameter is called by value, so the parameter first is assigned 4 by the call. The parameter first is immediately assigned in the function, but the first argument is not changed, hence, j==4. The second parameter is call-by-reference, so when first + 1 is computed to be 18, and is assigned to second, second is changed, and the corresponding argument, k is changed, hence, k==18.
No special syntax is necessary to define a function that uses a structure parameter. (This is unlike using an array parameter.)

Once the structure definition has been made, a structure tag is a type that may be used in any way that a built-in type such as int or double can be used. For example, given the structure definition

struct S {/*…*/};

here is a definition of the void function func that has an S structure parameter:

void func(S y);

This is unlike using an array parameter, which requires square brackets, as well as an additional size parameter, such as

void bar(Y[] x, int size);
There is no aggregate initialization available for structure variables. You must declare structure variables then use assignment to initialize the members.

A struct can be initialized by using a sequenced of initializers very much like arrays:

struct A { int a; int b; double d;}; A x = { 1, 2, 3.0};
Structure definitions are usually global (defined outside any functions).

A structure definition is a type that, like const definitions, we usually want to be available everywhere in a program that follows the structure definition.
Consider these hierarchical structures:

struct Date
int year;
struct Person
Date birthDay;
//other members
Person Bill;

The year of Bill's birthday may be accessed as Bill.year;

To access the year of Bill's birthday one must specify the Person's Date structure variable, as Bill.birthDay.year.
The following definition of the structure variables of type myStruct s and t could be made using several definitions using the structure tag.

struct myStruct
int i;
double d;
} s, t;

This possibility is why the semicolon is necessary at the end of a structure definition. Here is how the definitions may be rewritten.

struct myStruct
int i;
double d;
myStruct s;
myStruct t;
A structure member is accessed using the index operator [ ], with the member name as index.

Access to structure members is made by giving the structure tag, followed by a dot followed by the member name.
A C++ structure, or struct, like the C++ array, is a homogeneous data structure (i.e., all data is of the same type).

The struct provides a mechanism to define a collection of values that may be of several different types.
A class is a type similar to a structure type that normally has member functions as well as member variables.

We have treated the structure as a data members-only aggregate, as C does, and we treat the class as having function members and data members.
A structure variable can be defined directly in the same statement that defines a structure definition.

The variables s and t are structure variables of structure type myStruct under the following definitions:

struct myStruct
int i;
double d;
} s, t;
A structure can have a member whose type is another structure.

Structures collect, name, and give a type to related data. A subcollection of data from a structure may be so closely related that we want to make a struct of the subcollection. Example: A struct containing personal data may contain a birth date of struct type, in turn composed of day, month, and year.
With arrays, indices start at any number the programmer chooses to indicate in the definition.

Index values start at 0, not any other number, and run to one less than the declared size.
If we want to find the median of 100 scores, we need 100 separate variables to hold the data.

We can (much more easily) use an array of 100 of the type of the scores to find the median.
Which of the following are correct? Why are the others incorrect? When a function having an array formal parameter is called, the formal array parameter
is passed the address of the argument, and the function needs further information about the array size to safely use an array parameter.
If you need an array with more than one index, you can use a multidimensional array, which is actually an array of arrays. In the explanation, declare an array of doubles with 2 rows and 5 columns.

Perhaps the easiest way to see this is to think of a two dimensional array as an array of lines on a page. The requested declarations is

double array[2][5];

The first index is the row, or line number, and the second index is the column position within that row.
Consider the array declaration, int x[20]; There is no memory allocated for x[20].

There is memory allocated for x[0] through x[19] but not for x[20]
In a sorting an array, the items in the array are rearranged so that for all j and k, if j < k, then array[j]<=array[k]

This is sorting into increasing order. This ignores sorting into decreasing order. Care should be taken not to compare elements with index values out of the range 0 to declared_size-1
C++ arrays check for out-of-range index values.

C++ array access is directly to memory through addresses, and is designed to be as efficient as possible. The C++ Standard does not require a mechanism to detect out-of-range index values. Finally, no version of C++ this writer is aware of provides this feature.
Given the array declaration int a[20]; the first element is written as:
When using an array, it is perfectly legal to access indexed variables with index values less than 0 or greater than or equal to the declared size of the array.

These index values are out of range, or simply illegal. The compiler cannot catch this. Unless you access protected memory doing this, the C++ runtime system will not catch this mistake either. The programmer has the obligation of detecting illegal index values or assuring there will be none.
Given the two C++ array declarations: int a[10], b[10]; You can successfully compute one array, say a, then assign b to a: a = b;

You cannot assign arrays in C++. Some compilers allow this, but the Standard says this is illegal, and most compilers forbid it. To do this you need to write a loop or otherwise copy individual indexed variables.
A class member that is to be shared among all objects of a class is called
a static member.

Static function members do not need a calling object. They provide the usefulness of global data members without the abuses that are usually associated with global members in part because the static data member is local to the class. A static member variable must be initialized outside the class (by the class author). A static member function does not use data from any object, so in that sense it is shared by all members. Static member functions act as global functions for the class without affecting the rest of the program. Like real global functions, they do not have access to data in the any object. The association of a static member function with the class is explicit and obvious, but for real global functions this is not true.
You can write a class that is useful with all its constructors in the private section.

Apart from friend of the class (something you will see later in this course) a class with all private constructors cannot create an object of the class. It seems useless to have a class for which you can have no objects.
A constructor
can do anything any other method can do.
usually initializes all, or most, member variables.
A constructor is a special kind of member function. It is automatically called when an object of that class is declared.

The purpose of a constructor is to automatically do any needed initialization.
A constructor is like a function. It can return any type value needed.

A constructor may not return any value, not even void. There can be no return type in front of the name.
A constructor can be called implicitly or explicitly. (Implicitly means the compiler did it for you.) (In the interest of uniformity in answers, please use class A; for your examples.)

A constructor can be implicitly called with the declaration A x;. The default constructor called explicitly with syntax is A x = A();
Which of the following are correct?
A class can have only one default constructor.
Concerning nested classes, which of the following are true?
You can define a class within a class.
The inner class is within the scope of the outer class.
Qualification by the outer class name with the scope resolution operator is necessary to use the inner class outside the outer class.
Any use of the keyword const is a promise to the compiler, and a request to the compiler to enforce the promise. (If true, what promises?)

We have seen several uses of const. A const call-by-reference parameter promises not to write code that could change the parameter, hence not to write code that could change the argument. A const identifier definition promises not to write code that could change the identifier. Finally, a const member function promises not to write code in the member function that could change the state of the calling object.
Which of the following are accurate comparisons between call-by-value and const call-by-reference?
Both protect against changing the caller's argument.
Call-by-value uses more memory than const call-by-reference in making the copy.
Call-by-value copies the argument whereas const call-by-reference does not.
We reproduce the class Money here, in part:

class Money
Money( );
Money(int theDollar, int theCents);
Money(int theDollars);
Money(double amount);
// other public members
int getCents( ) const;
int getDollars( ) const;
int dollars;
int cents;
// other private members

Note that * is not overloaded in the class, but operator+ is overloaded using an operator function with the following declaration:
Money const operator+(const Money& amt1, const Money& amt2)

The question is, given the declarations
Money baseAmount(100, 60); // $100.60
Money fullAmount;
which of the following operations are legal? Why or why not?
25 + BaseAmount;
BaseAmount + 25;
Consider this operator overloading for class Money (modified from Display 8.1 by omitting the const modifier on the return type):

Money operator+(const Money& amt1,const Money& amt2);

The following expression is legal. (If true, what could it mean, and where does the information that is assigned go?)

Money m1(17.99), m2(23.57) m3(15, 22);
(m1 + m2) = m3;

Though legal, this bit of code does not mean much. What actually happens is that C++ makes an anonymous Money object to hold the results of m1+m2. This object is assigned from m3, then discarded. The const, which we removed from the return type, would make this strange assignment illegal.
An overloaded operator= function must be a non-static class member.
You can change the behavior of + for the int type using operator overloading.

Overloading any operator requires that one or more of the operands be a class object. This prevents changing the behavior of operators on primitive types.
The rules for using operator [] are different from other operators. Which of the following statements are correct? Explain.
Operator [] may be overloaded only as a static member function.
When writing an overloaded function for operator [], the [] follows the keyword operator, followed by one index parameter declaration between parentheses.
A friend function has access only to the private members and member functions of the class (and all objects of that class) of which it is a friend.

A friend function, like every other function, has access to any public members of any class, the class of which it is a friend included. Friendship gives the function access to all public and private members.
One can guarantee left to right evaluation of the arguments to a comma expression overloading.

Invocations of overloaded operators are function calls. All the arguments of function calls are evaluated before the function is called, but there is no guarantee that any two compilers will generate code with left to right evaluation. It is better not to overload the comma operator.
When overloading an operator, you cannot change the number of arguments an operator takes.

The compiler recognizes the operators and expects them have the usual number of arguments.
Overloading a binary operator as a stand-alone function requires two arguments.

Consider a use of the operator _:

leftAObject + rightAObject;

This translates to

operator+(rightAObject, leftAObject);

requiring two arguments.
Operator overloading is essentially a function that uses a different syntax for invocation.
To read a character at a time, or to write a character at a time, declare a character variable ch and write this:

cin >> ch;

The problem with this code is that by default, cin>>ch discards whitespace. (This default behavior can be changed, but we have a different solution.) To read a character at a time, no matter what it is, even a space, use the member functions get. The get function is a member of cin's class, and has a type char call-by-reference parameter.
A C-string variable with a something in it is always filled with characters.

A C-string is a partially filled array. If a C-string is stored in a C-string variable, the null character marks the position one beyond the last used character position.
A C-string variable is an array, so it can be indexed to access individual character positions (indexed variables of type character).
The C-string library functions use the null terminator to decide when to stop processing.
The C-string library function strcmp compares two strings for equal length.

This library function compares the first characters of the two strings with each other. In strcmp(s1, s2) if the first character of s1 is less than the first character of s2, then strcmp returns a result > 0. The reverse yields a value < 0. If characters are equal, the successive characters are examined. If the end of the strings is reached at the same index, the strings are equal, and strcmp returns 0.
The header file that you must #include to have access to the Standard Library string class is

There is no .h extension on C++ header files, nor the ones that C++ has inherited from C and then converted. C++ uses the <cstring> header. The string.h include file is a header file used by C++ prior to the C++ Standard. It is still provided by some C++ compilers for backward compatibility but its use is discouraged. String.h is an outdated header for a proprietary string library.
C-string literals are written 'Hello'.

C-string literals are spelled with the so-called double quote: "Hello"
The preprocessor symbol NULL and the C-string terminator, the null character, share the value 0.

NULL is #defined in the preprocessor to be 0. The value of the null character is '\0', which is the character that is encoded 0.
If you want to examine an input value without actually removing it from the input stream, you can apply a member function of cin to the variable ch, which you are to assume is a char type variable. Which of these does this task?

cin.peek(ch) returns the next character from the input stream without removing it from the input stream.
It is legal to assign C-string variables.

C-string variables are arrays. It is illegal to assign arrays of any kind.
A function can return an array.

A function can return a pointer to an object whose lifetime is longer than the function's lifetime, which can be a dynamic array, but arrays may not be returned by a function. Specifically, the object must not be a local variable.
The declaration below declares three pointer variables of type pointer to double, that is, a pointer of type (double*)

double* p1, p2, p3;

This declares one pointer variable, p1, and twodouble variables, p2 and p3. The * binds more closely to the variable, not to the type. In spite of this, the usually style puts the asterisk against the type. To get three pointer variables, one must write

double *p1, *p2, *p3;

or better, declare the three pointer variables on separate lines:

double *p1;
double *p2;
double *p3;
The Big Three consists of which three from this list?
copy constructor
Dangling pointers present no problem in C++.

A dangling pointer occurs when several pointers point to the same chunk of allocated freestore memory. If one of the pointers has had the delete pointer applied to it, releasing the freestore memory, the other pointers are said to be dangling. Because the pointer that had delete applied to it most of the time isn't
changed by the delete operation (though this is not guaranteed by any means), a deleted pointer is frequently called a 'dangling pointer.'
Dynamic variables or dynamically allocated variables in C++ are created and destroyed according to the program's needs.

Dynamic variables are unaffected by function calls or function return. This is not true of local pointer variables that may point to dynamic variables. Consequently, in a poorly written program, a local pointer can go out of scope and the dynamic variable to which the pointer points may be lost.
When declaring several pointer variables, there must be one pointer declarator * for each pointer variable.

Consider this definition of p1, p2, and p3 as pointer to doubles:

double *p1, *p2, *p3;

There must be one * for each pointer variable defined.
The default copy constructor and default operator = provide deep copy.

The default copy constructor and operator = just copy the members. This is the definition of shallow copy.
You can get a pointer value to initialize a pointer variable from an object of an appropriate type with the "address-of" operator, &.

Every object has a value and an address. The &"address-of" operator extracts the address to make it available to initialize a pointer variable of appropriate type. Example: int x; int *ptrX = &x;
If a class is named MyClass, what must the destructor be named?

That is a language design decision that several languages that support classes made.
A pointer is a variable that holds the address of some other location in memory.

Computer memory is composed of addressable chunks of information called bytes. A pointer value is the memory address stored in a pointer variable.