Structured Programming in C++
Prof. Gregory Safko

Camden County College

Review: Exam 2


 

General Knowledge

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) & 7 (Simple Data Types) 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 how to include external files in a workspace (Such as other.cpp)

6.   Know how to use #include to include externals files in a program (e.g. #include "other.cpp")

7.   Know the difference between an iterative function and a recursive function, and how to immediately recognize the difference.

8.   Know the two ways to declare data as a constant (const and #define)

9.   Know how to use and interpret an ASCII table

10. Know how to use the char data type. Know how to extract both characters and integers from a char.

11. Know how to use the functions found in <cctype> (tolower, toupper, islower, isupper, ispunct, isdigit, etc.)

12. Know how to declare enums, and how to use them in code.

13. Know how to declare, populate, and traverse an array.

14. Know how to write and prototype overloaded functions

15. Know what a predicate is, and how it is used.

 

 

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 do 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 value is associated with wednesday in the following expression?

         enum dayofweek (monday, tuesday, wednesday, thursday, friday, saturday, sunday);

A.     2

B.     Wednesday

C.    3

D.    ‘Wednesday’

 

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

 

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

 

8.      What is another way to write !(boolean1 && boolean2) ?

A.     (!boolean1 && !boolean2)

B.     !(boolean1 || boolean2)

C.    (!boolean1 || !boolean2)

D.    !boolean1 || boolean2

 

9.      Which of the following declarations will cause a compiler error?

    1. char w = 'A';
    2. char x = "B";
    3. char y = '5';
    4. char z = 68;

 

10.  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

 

  1. If you use the char tolower(char) function prototype, what standard library is required?
  2. What is the difference between an iterative and a recursive function?
  3. What is wrong with this piece of code and how can you correct it?

char myA = "a";

  1. Why won't this code compile? Why is it an infinite loop?
       for (int i = 0,i <= x)
       {
           cout<<"hello";
       }

 

  1. What is wrong with this piece of code?

 

const int a = 10;

a++;

 

  1. What is wrong with this piece of code?

int lottery (3);

lottery (1) = 12;

lottery  (2)  = 20;

lottery (3)  =  28;

 

  1. To form the complement (or opposite or negation) of a predicate, we use the ____________.

 

  1. What is the difference between ‘A’ and “A”

 

 

Sample Coding

 

1.      Write a function that tests a character value for a number or a letter only (either letter case) and returns true if a letter or number.  All other ASCII characters 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 a function 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.      A

6.      B

7.      E (It is an indication of a switch statement)

8.      C

 

 

Sample Free Response/Debugging Answers

 

  1. cctype
  2. An iterative function finds the solution via iteration (a loop structure), where a recursive function finds the solution via calling itself recursively
  3. It is the wrong data type. (string vs. character). Correct it by replacing the double-quotes with single-quotes.
  4. Use a "," instead of ";" to separate clauses in the for loop. It is infinite since it is missing an Increment/Decrement section. It also has the wrong comparison operator (use >= instead)
  5. You cannot modify a constant. a++ attempts to modify const a, which is an error
  6. The first line is fine, but the other lines are acting as if the variable lottery was an array. Correct it by using [ ] instead of ( ) around the indices
  7. ! (exclamation point)
  8. ‘A’ – represents a character constant ; “A” – represents a string constant

 

 

Sample Coding Solutions

 

1.

 

bool testChar(char c)

{

       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;

for (int posChar =0; posChar < inputWord , inputWord.length(); posChar++)

cout << inputWord.at(2) << endl;

cout << inputWord.length() << endl;