VARIABLE DECLARATIONS and ASSIGNMENTS
-
Declare and initialize in a single C++ statement a double variable that
will have the value 3.14.
-
Write one line of C++ code that adds one to the integer variable iCounter.
-
Declare a float constant named RATE that has a value of 2.75.
-
Declare and initialize in a single C++ statement a floating point variable
named fVal that has a value of 27.5.
-
Declare and initialize in a single C++ statement a character variable that
will have the value 'R'.
-
Declare and initialize in a single C++ statement a boolean variable that
will have the value false.
-
Declare and initialize in a single C++ statement a long integer variable
that will have the value 154332.
-
Declare and initialize in a single C++ statement a short integer variable
that will have the value 2.
-
Declare and initialize in a single C++ statement a string variable(STL
string class) that will have the value "Fred".
-
Declare an integer constant named MAX that has a value of 1000.
-
Declare a double constant named BIGMAX that has a value of 1000.01.
-
Declare a short constant named SMALLFRY that has a value of 0.
OPERATORS
&&, ||, !, <, >, =, <=, >=, ==, +, -, *, /, %, ++, --
-
Place parentheses in the following expression that correctly describe the
order in which the operations would be performed by default.
6 + 7 * 3 - 4 / 2
-
Suppose we have two integers iVal1 = 10 and iVal2 = 4. What is the output
for each of the following lines of C++ code?
-
cout << float(iVal1/iVal2);
-
cout << iVal1 % iVal2;
-
cout << float(iVal1)/iVal2;
-
Given the code below, write the output.
bool bA = true;
bool bB = false;
if(bA || bB)
cout << "Hi";
else
cout << "Bye";
-
Given the code below, write the output.
bool bA = true;
bool bB = false;
if(bA && bB)
cout << "Hi";
else
cout << "Bye";
-
Given the code below, write the output.
bool bA = true;
bool bB = false;
if(bA == bB)
cout << "Hi";
else
cout << "Bye";
-
Given the code below, write the output.
bool bA = true;
bool bB = false;
if(bA = bB)
cout << "Hi";
else
cout << "Bye";
-
Given the code below, write the output.
int iVal1 10;
int iVal2 = 3;
if(!(iVal1 > iVal2))
cout << "Hi";
else
cout << "Bye";
-
Given the equation y = ax2 + bx + c, which of the following
is the correct C++ assignment statement for this equation?
-
y = a*x*x*(b*x+c)
-
y = a*(x*x)+b*x+c
-
y = (a*x)*x*x+b*(x+c)
-
y = a*(x*2)+b*x+c
-
None of these
-
Given the equation y = ax2+bx/c , which of the following
is the correct C++ assignment statement for this equation?
-
y = (a * x * x + b * x) / c
-
y = (a * x * x * x + b * x) / c
-
y = (a * x * 2 + b * x) / c
-
y = a * x * x + b * x / c
-
None of these
-
Given the following code, what is the value of siVal1 after the code has
executed?
short siVal1 = 15;
short siVal2 = 5;
siVal1 = siVal1 - siVal2;
-
Given the code below, what is the output?
int iA = 6;
if(iA = 5)
cout << "Hello";
cout << " World";
-
Given the code below, what is the value of iN after it is executed?
int iN = 6;
iN++;
-
Given the code below, what is the value of fNum after it is executed?
float fNum = 2.0;
double dNum = 5.0;
dNum = dNum * fNum;
I/O
cin, cout
-
Write the output produced by the following code segment.
int iX = 10, iY = 12, iZ = 4;
cout << iX << iZ << iY << endl;
cout << iX;
cout << iY << "\n";
cout << iZ;
-
Write the output produced by the following code segment.
short siNum = 5;
cout << "siNum = " << siNum;
PRE PROCESSOR
Basic #include stuff
-
What does the following statement do when placed at the beginning of a
.cpp or .h file?
#include <iostream.h>
-
What does the following statement do when placed at the beginning of a
.cpp or .h file?
#include <fstream.h>
-
Write a user include statement to include a file named foo.h.
DECISIONS
if, if else, multiway if else
- Modify the code given below adding curly brackets so that it will produce
the given output when iX is 0 and iY is 20. Do not move, add or delete any
of the statements.
Code |
|
Output |
if(iX<10)
if(iY>10)
cout << "***\n";
else
cout << "###\n";
cout << "$$$\n";
|
|
***
###
$$$
|
-
Given an integer variable iX, write a multi-way if else that will
behave in the following way. If iX is greater than or equal to 10,
"High" will be output. If iX is greater than 0 and less than 10,
"Medium" will be output. If iX is less than or equal to 0, "Low"
will be output.
-
Write an if else statement that will output to the screen "Hi" if
the short integer siValue is greater than or equal to five, and will print
out "Bye" otherwise.
-
Write an if else statement that will check the value of a variable
ldTest.
If ldTest is less than zero, a message will print to the screen
stating that ldTest is less than zero. If ldTest is not less
than zero, a message will print to the screen that reads ldTest = xxx,
where xxx is the value of ldTest.
-
Write the output produced by the following code segment.
int iA = 5, iB = 3;
if(iA < iB)
cout << "Hello" << endl;
cout << "GoodBye" << endl;
-
Write the output produced by the following code segment.
int iMark = 94, iTime = 82;
cout << iTime << endl;
iTime = iTime + 8;
cout << iTime << endl;
if(iTime >= iMark)
{
iTime++;
cout << iTime << endl;
}
else
{
iTime= iMark-iTime;
cout << iTime << endl;
}
cout << "Time: " << iTime << endl;
LOOPS
while, do while, for
-
Given the code below, write the output.
int ii;
for(ii = 1; ii < 15; ii = ii + 3)
{
cout << ii << ", ";
}
cout << endl << "The end!";
-
Write a for loop that sums the odd integers from 1 to 15 inclusive.
This value should be stored in an integer variable name iVal.
-
Write a for loop that sums every third integer from 2 to 32 (2,
5, 8, 11...), inclusive, into an accumulator variable that has an initial
value of zero.
-
What is the primary difference between a while loop and a do
while loop?
-
Write the output produced by the following code segment.
int iX = 4;
int iCount = 0;
while(iCount <= 3)
{
iX = 2 * iX;
cout << iX << endl;
iCount++;
}
cout << iCount;
-
Write the output produced by the following code segment.
int ii;
for(ii = 3; ii <= 9; ii = ii + 2)
cout << ii << endl;
-
Write a for loop that will output the numbers 6, 5, 4, 3, 2, 1,
in that order, one per line.
-
Write an equiValent for loop for the following while loop.
int iValue = 6;
while(iValue < 33)
{
cout << iValue << endl;
iValue = iValue + 3;
}
FUNCTIONS
prototypes, definitions, value args, reference args, returns, calls
-
Assume we want to find the square of the integers between 1 and 10 and
print. Fill in the blanks of the C++ program below to make the code function
as prescribed.
#include
int Square(int iNum1);
int main()
{
for(int iNum2 = 1; iNum2 <= ______; iNum2++)
{
cout << Square(iNum2) << endl;
}
return 0;
}
int Square(int iNum3)
{
return ______ * iNum3;
}
-
After the function MyFunction below is exectued, what value is returned
from this function? (assume iN = 4)
int MyFunction(int iN)
{
int iP = 1;
for(int iO = 2; iO <= iN; iO++)
{
iP = iP * iO;
}
return iP;
}
-
What is the difference between pass by value and pass by reference?
-
Write a function prototype that has the following characteristics:
-
The function is named FooBar
-
The function returns a character
-
The function takes as an argument a constant reference integer.
-
Write a function prototype that has the following characteristics:
-
The function is named MyFunction
-
The function does not return anything
-
The function takes as an argument an array of integers
-
The function takes as an argument an integer that specifies the number
of elements in the array
-
What is the one function that every C++ program must have?
-
Define pre and post conditions of a function, and state the relationship
between them.
-
What is the value of iy after the code below is exectued?
int ix = 15, iw = 20;
int iy = Min(ix, iw);
int Min(int ia, int ib)
{
int ireturn;
if(ia < ib)
ireturn = ia;
else
ireturn = ib;
return ireturn;
}
ARRAYS 1-D
declaration, traversal, char array(null terminated)
-
Declare and initialize an array of 5 integers such that each array value
is equal to its index.
-
Given the code below, what character is stored in the element szFred[7]?
char szFred[] = "program";
-
Add on to the code given below a statement that will copy the string "foobar"
into the character array szWord. Assume the appropriate #include
statments are present.
char szWord[10];
-
Given the code below, how many elements are in the array szBob?
char szBob[] = "foo";
-
Write the output produced by the following code segment.
int iItem3 = 7;
int iItem[5] = {0};
cout << iItem3 << endl;
cout << iItem[3];
-
Declare a one dimensional array named rgfArr of type float that
contains 20 elements.
-
Write the output produced by the following code segment.
int rgiBob[5] = {9, 8, 7, 6, 5};
int iIndex = 1;
int iX = rgiBob[iIndex + 2];
int iY = rgiBob[iIndex] + 2;
cout << iX << " " << iY;
-
Add on to the code given below a for loop that will assign the value
7.5 to all the elements of the array.
float rgfTemp[5];
-
The Box below represents the array rgi. Fill in the box with the values
that would be there after the code is executed.
int ii;
int rgi[] = {15, 25, 10, 30, 40};
for(ii = 0; ii < 4; ii++)
{
rgi[ii] = rgi[ii+1];
}
CLASSES
constructors, public, private, member variables, member functions
-
Where in a program can the programmer directly access private members of
a class named CBox?
-
Anywhere
-
The main function
-
Any function
-
Member functions of any class
-
None of these
-
Write a structure definition that contains an integer variable named iVal,
and a floating point variable named fNum.
-
Given the code below, write a line of C++ code that assigns the value 50
to the variable m_siStars of the object MyFlag.
struct SFlag
{
short m_siStars;
short m_siStripes;
};
int main()
{
SFlag MyFlag;
return 0;
}
-
Given the class definition below, write a constructor that will assign
the value 5 to the integer, and 'z' to the character.
class Stuff
{
public:
Stuff();
private:
char m_cLabel;
int m_iSize;
};
-
Given the class definition below, write the GetSize function that will
return the value stored in the variable m_iSize to the caller.
class CBox
{
public:
CBox();
int GetSize();
private:
int m_iSize;
};
-
Given the class definition below, write the SetSize function that will
take one integer argument, and assign the value stored in the argument
to m_iSize.
class CBox
{
public:
CBox();
void SetSize(int);
private:
int m_iSize;
};
FILE I/O
ifstream, ofstream
-
Given the code below, write a line of C++ code that will read a number
from the data file and store the number in fValue.
float fValue;
ifstream ifile;
ifile.open("input.dat");
-
Add on the the code given below the code necessary to open up a file named
output.dat.
ofstream ofOut;
-
Suppose that input stream ifInput is connected to the file InputData.dat.
Write one line of code that will close the file.
-
Write a program segment that does all of the following.
-
Declares an input file stream name if_input
-
Opens a file named field.dat for use with if_input
-
Closes the file