C++ 1 Final Study Questions Name__________________
1. What is the assignment operator in C++?
A. =
2. What function is used to generate random numbers?
A. rand()
3. List the arithmetic operators in C++:
A. + - * / % ++ -- (and some others we haven't learned yet)
4. What are the three loops in C++? When would you use each type?
A. use a for loop if you know how many times the loop should repeat
Use a while loop if the loop may never run, a do-while if the loop should run at least once.
5. What is wrong with the following line of code?
cout<<"Looks Ok to me"<
A. the E in endl is capitalized. C++ is "case sensitive", C++ "thinks" that e and E are two entirely different letters.
6. Or this one?
int Main()
A. the M in main is capitalized.
7. List the logical operators in C++:
A. && (and) || (or) ! (not)
8. List the relational operators in C++:
A. > >= < <= == !=
9. What is the output of the following block of code?
for (int n1=1; n1<=3;n1++)
for (char c1='a';c1<='b';c1++)
cout<<n1<<c1<<endl;
A.
1a
1b
2a
2b
3a
3b
10. What are some common C++ data types?
A. char int double string are the ones we've used in class (there are others).
11. Write a function header, function prototype and function call for the following function:
void Mystery(int &nNum, float fSum)
{
nNum = nNum * fSum;
}
A. header: void Mystery(int &nNum, float fSum)
prototype void Mystery(int &nNum, float fSum);
call: Mystery(5, 2.7);
12. What ends every statement in C++?
A. a semi-colon (;)
13. What are some common keywords in C++?
(keywords are words with a special meaning that can not be used for variable names, function names, etc.)
A. int, char, float, main, for, if, else, switch, case, default, void and many others
14. What does "every array in C++ is zero-based" mean? Give an example.
A. The first element is at position zero.
15. What are two ways to make comments in C++?
// for single line comments
and /* */ for comments that take up many lines
16. What is the difference between an initialization and a declaration?
A. a declaration makes a "mailbox" where you can place a number, e.g. int nNum;
an initializtion puts a value in the "mailbox" for the FIRST time. E.g. nNum = 5;
17. What is the basic scope rule?
A. The scope (think "neighborhood") or a variable begins with its declaration and ends with the closing curly brace of the block in which the variable was declared.
18. When do you use single quotes and when do you use double quotes?
A. Single quotes are used with single characters (char). Double quotes are used with strings.
19. What is 5%3?
A. 2
20. Write a statement that stores the result of nDogs modulus nDogsPerDogHouse in nColdDogs.
A. nColdDogs = nDogs%nDogsPerDogHouse;
21. What will the value of nFirstNumber be after the following statement is executed?
nFirstNumber = (3 + 4) / (2 + 5) * 2 - 3;
A. -1
22. Write a statement to output the value stored in nBoats:
A. cout<<nBoats;
23. What is the exclamation point (!) used for in C++?
A. It means "not" or "opposite"
24. How does the double equal sign (==) differ from the single equal sign (=)? give an example of when you would use each.
A. the single equals sign MAKES a variable equal to a value. The double equals sign asks the question "Is is true that these two things are equal?"
25. Make a "Truth Table" for the logical operator "and".
&& T F
T T F
F F F
26. Fill in the blanks in the following block of code so that it displays the sum of all numbers from 1 to nNum.
int nSum = 0;
int nInt = nNum;
do
{
nSum = nSum + nInt;
___ nInt-- ___;
} while (nInt != 0);
cout<<____ nSum ______<<endl;