Structured Programming in C++
Prof. Gregory Safko
Camden County College
1. Know the material from Exam 1 (since learning the language is cumulative, you should be comfortable with using all of the previously learned concepts)
2. Know the material from Chapter 6 (Modular Programming), Chapter 7 (Simple Data Types) and Chapter 8 (Streams and Files) in the book
3. Know the definition of: Top-Down Design, Abstraction, Stubs, and Drivers
4. Know the unary, binary, and ternary operators of C++ (what is the only ternary operator in C++?)
5. Know the two ways to declare data as a constant (const and #define)
6. Know how to use and interpret an ASCII table
7. Know how to use the char data type. Know how to extract both characters and integers from a char.
8. Know how to use the functions found in <cctype> (tolower, toupper, islower, isupper, ispunct, isdigit, etc.)
9. Know how to write and prototype overloaded functions
10. Know the difference between passing by value and passing by reference.
Sample Multiple Choice
1. A function reference parameter is normally used for parameter passing of: (choose best single answer):
A. Input parameters only.
B. Outputs parameters only.
C. Input or output parameters.
D. Output or input /output parameters
2. Which one of these is not true
A. ">" is the opposite of ">="
B. "<" is the opposite of ">="
C. "==" is the opposite of "!="
D. && is the opposite of ||
3. Which of the following is the correct way to perform a precompiled define:
A. #define value result
B. #define value = result
C. <define value = result>
D. define value = result;
4. A function declared as _______ can still return results to the calling function by using reference parameters.
A. int
B. sum
C. bool
D. void
5. What does the #define statement allow you to do?
A. Define the ASCII code of a variable
B. Replace a character with a constant variable
C. Declare a variable
D. Define p
6. The statement case 'A': is an indication of a(n):
A. if/else statement
B. do/while statement
C. do statement
D. case statement
E. None of the above
7. What is another way to write !(boolean1 && boolean2) ?
A. (!boolean1 && !boolean2)
B. !(boolean1 || boolean2)
C. (!boolean1 || !boolean2)
D. !boolean1 || boolean2
8. Which of the following declarations will cause a compiler error?
9. Pass-by-value is also known as:
A. Pass-by-reference
B. Call-by-reference
C. Call-by-value
D. Cast-by-value
E. None of these
Sample Free Response/Debugging
char myA = "a";
const int a = 10;
a++;
int lottery (3);
lottery(1) = 12;
lottery(2) = 20;
lottery(3) = 28;
Sample Coding
1. Write a function that tests a character value for a number or an uppercase letter only, and returns true only if it is an uppercase letter or number. All other ASCII characters (including lowercase letters) must return false.
2. Write a piece of code which identifies whether the first character of a string is capital letter. Assume the library cctype is included and the string has already been validated to have at least one character. If the first character is not a letter, return false.
3. Write code that will print out the second character and the number of characters of an input word. There are no restrictions on the length of the input word.
Sample Multiple Choice Answers
1. A
2. A
3. A
4. D
5. B
6. E (It is an indication of a switch statement)
7. C
8. B
9. A
Sample Free Response/Debugging Answers
Sample Coding Solutions
1.
bool testChar(char c)
{
if(islower(c))
return false; // it cannot be lowercase, so kick it out now
if(isdigit(c) || isalpha(c) )
return true;
return false;
}
2.
bool IsFirstLetterCapital(string inString)
{
char firstLetter = inString[0];
if (isalpha(firstLetter) == false)
{
return false;
}
if (isupper(firstLetter))
{
return true;
}
return false;
}
3.
string inputWord;
cout << Enter the Word: ;
cin >> inputWord;
cout << inputWord.at(1) << endl;
cout << inputWord.length() << endl;