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), 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?

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

 

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

 

  1. If you use the char tolower(char) function prototype, what standard library is required?
  2. 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 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

 

  1. cctype
  2. It is the wrong data type. (string vs. character). Correct it by replacing the double-quotes with single-quotes.
  3. 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)
  4. You cannot modify a constant. a++ attempts to modify const a, which is an error
  5. 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
  6. ! (exclamation point)
  7. ‘A’ – represents a character constant ; “A” – represents a string constant

 

 

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;