You are on page 1of 9

C++ Questions

Page 1 of 9

VARIABLE DECLARATIONS and ASSIGNMENTS 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 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 &&, ||, !, <, >, =, <=, >=, ==, +, -, *, /, %, ++, -1. 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

2. Suppose we have two integers iVal1 = 10 and iVal2 = 4. What is the output for each of the following lines of C++ code? A. cout << float(iVal1/iVal2); B. cout << iVal1 % iVal2; C. cout << float(iVal1)/iVal2; 3. Given the code below, write the output.
bool bA = true; bool bB = false; if(bA || bB) cout << "Hi"; else cout << "Bye";

4. Given the code below, write the output.


bool bA = true; bool bB = false; if(bA && bB) cout << "Hi"; else cout << "Bye";

5. Given the code below, write the output.

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 2 of 9

bool bA = true; bool bB = false; if(bA == bB) cout << "Hi"; else cout << "Bye";

6. Given the code below, write the output.


bool bA bool bB if(bA = cout else cout = true; = false; bB) << "Hi"; << "Bye";

7. Given the code below, write the output.


int iVal1 10; int iVal2 = 3; if(!(iVal1 > iVal2)) cout << "Hi"; else cout << "Bye";

8. Given the equation y = ax2 + bx + c, which of the following is the correct C++ assignment statement for this equation? A. y = a*x*x*(b*x+c) B. y = a*(x*x)+b*x+c C. y = (a*x)*x*x+b*(x+c) D. y = a*(x*2)+b*x+c E. None of these 9. Given the equation y = ax2+bx/c , which of the following is the correct C++ assignment statement for this equation? A. y = (a * x * x + b * x) / c B. y = (a * x * x * x + b * x) / c C. y = (a * x * 2 + b * x) / c D. y = a * x * x + b * x / c E. None of these 10. Given the following code, what is the value of siVal1 after the code has executed?
short siVal1 = 15; short siVal2 = 5; siVal1 = siVal1 - siVal2;

11. Given the code below, what is the output?


int iA = 6; if(iA = 5) cout << "Hello"; cout << " World";

12. Given the code below, what is the value of iN after it is executed?

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 3 of 9

int iN = 6; iN++;

13. 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 1. 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;

2. Write the output produced by the following code segment.


short siNum = 5; cout << "siNum = " << siNum;

PRE PROCESSOR Basic #include stuff 1. What does the following statement do when placed at the beginning of a .cpp or .h file?
#include <iostream.h>

2. What does the following statement do when placed at the beginning of a .cpp or .h file?
#include <fstream.h>

3. Write a user include statement to include a file named foo.h. DECISIONS if, if else, multiway if else 1. 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
if(iX<10) if(iY>10) cout << "***\n"; else

Output
*** ### $$$

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 4 of 9

cout << "###\n"; cout << "$$$\n";

2. 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. 3. 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. 4. 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. 5. Write the output produced by the following code segment.
int iA = 5, iB = 3; if(iA < iB) cout << "Hello" << endl; cout << "GoodBye" << endl;

6. 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 1. Given the code below, write the output.
int ii; for(ii = 1; ii < 15; ii = ii + 3) { cout << ii << ", "; } cout << endl << "The end!";

2. 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. 3. 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. 4. What is the primary difference between a while loop and a do while loop?

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 5 of 9

5. 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;

6. Write the output produced by the following code segment.


int ii; for(ii = 3; ii <= 9; ii = ii + 2) cout << ii << endl;

7. Write a for loop that will output the numbers 6, 5, 4, 3, 2, 1, in that order, one per line. 8. 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 1. 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; }

2. After the function MyFunction below is exectued, what value is returned from this function? (assume iN = 4)

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 6 of 9

int MyFunction(int iN) { int iP = 1; for(int iO = 2; iO <= iN; iO++) { iP = iP * iO; } return iP; }

3. What is the difference between pass by value and pass by reference? 4. Write a function prototype that has the following characteristics: 1. The function is named FooBar 2. The function returns a character 3. The function takes as an argument a constant reference integer. 5. Write a function prototype that has the following characteristics: 1. The function is named MyFunction 2. The function does not return anything 3. The function takes as an argument an array of integers 4. The function takes as an argument an integer that specifies the number of elements in the array 6. What is the one function that every C++ program must have? 7. Define pre and post conditions of a function, and state the relationship between them. 8. 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) 1. Declare and initialize an array of 5 integers such that each array value is equal to its index. 2. Given the code below, what character is stored in the element szFred[7]?
char szFred[] = "program";

3. 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];

4. Given the code below, how many elements are in the array szBob?

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 7 of 9

char szBob[] = "foo";

5. Write the output produced by the following code segment.


int iItem3 = 7; int iItem[5] = {0}; cout << iItem3 << endl; cout << iItem[3];

6. Declare a one dimensional array named rgfArr of type float that contains 20 elements. 7. 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;

8. 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];

9. 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 1. Where in a program can the programmer directly access private members of a class named CBox? A. Anywhere B. The main function C. Any function D. Member functions of any class E. None of these 2. Write a structure definition that contains an integer variable named iVal, and a floating point variable named fNum. 3. 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

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 8 of 9

{ short m_siStars; short m_siStripes; }; int main() { SFlag MyFlag; return 0; }

4. 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; };

5. 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; };

6. 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 1. 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;

file://D:\resumes\C++%20Questions.htm

6/6/2003

C++ Questions

Page 9 of 9

ifile.open("input.dat");

2. Add on the the code given below the code necessary to open up a file named output.dat.
ofstream ofOut;

3. Suppose that input stream ifInput is connected to the file InputData.dat. Write one line of code that will close the file. 4. Write a program segment that does all of the following. 1. Declares an input file stream name if_input 2. Opens a file named field.dat for use with if_input 3. Closes the file

file://D:\resumes\C++%20Questions.htm

6/6/2003

You might also like