Structured Programming in C++

Prof. Gregory Safko

Camden County College

Review: Final Exam


 

General Knowledge

  1. Although the exam will focus on material learned since the previous exam (Exam 2), you should still be familiar with the concepts you learned in Chapters 1 – 7.
  2. Understand the additional concepts of Chapter 8 (Streams and Files), and Chapter 9 (Data Structures: enums and structs), (Sections 9.1, 9.2, 9.7 and 9.8).
  3. Know and understand how Project 2 (Pig Latin) works.
  4. Know and understand the questions and answers asked in Exam 1 and Exam 2.
  5. Understand and know how to use local and global variables.
  6. Know how to pass-by-value and pass-by-reference in functions.
  7. Understand the concept of prototyping, and overloaded functions.
  8. Understand how getline( ) and gets( ) work.
  9. Understand the concept of the Null Terminator in character arrays.
  10. Know how to declare variables used to manage external files.
  11. Know how to open, read from, write to, check for end-of-file, and close external files.
  12. Understand the concept of endl and eof in external files.
  13. Know the difference between const and #define
  14. Understand and know how to used the character utilities found in cctype.h, namely tolower(), toupper(), isalpha() isupper(), islower(), and isdigit().
  15. Know how to write and use enums
  16. Know how to create and populate an array.
  17. Know how to create an integer array with 0’s in it. 
  18. Know how to detect the end of a character array.
  19. Know how to read data from an array.
  20. Know what happens when you read or write data to an array, and you go out-of-bounds.

 

Sample Multiple Choice

 

1. To test if the first character of the string flag is the letter S, use:

a.  flag.at(0) = 'S';

b.  flag.at(0) == 'S';

c.  flag.at(0) = "S"; 

d.  flag.at(0) == "S";

e.  flag.at(1) = 'S';

f.  flag.at(1) == 'S';

g.  flag.at(1) = "S"; 

h.  flag.at(1) == "S";  

 

 

2. If string strItem = "Handle";, what character would be returned for strItem.At(2) ?

  1. a
  2. n
  3. H
  4. D
  5. None of these

 

3. Which object type do you use for input file streams?

 

  1. instream
  2. inputstream
  3. ifstream
  4. filestream

 

4. Assume you have string q1 = "Josh";. What is the output for cout<<q1.at(3);?

 

a.      Jos

  1. s
  2. h
  3. None of the above

 

5. Which #include library is needed to output to a file?

 

a.      fstream

b.      ostream

c.      fstream

d.      costream

 

 

6. The correct location in a program for an array declaration is (choose the best answer):

 

a.      Before the using namespace std; statement but after the #include …statement(s)

b.      Before #include …statements

c.      At the beginning of the main() program section

d.      Any of the above

 

Sample Free Response:

 

1.      When obtaining the last character of string name; , why do you use name.length()-1 and not name.length() ?

2.      What is wrong with this piece of code?

 

ifstream myStream;

ifstream.Open("myfile.txt");

 

3.      Explain why 1 + rand() % 6 returns a number between 1 and 6.

4.      What is wrong with the following program

 

int main()

{

    char A = 'A';

          string B = "Swan Lake";

char C = " C " ;

 

cout << " The Char A = " << A << endl;

cout << " The string B is = " << B << endl;

cout << " The char C = " << C << endl;

 

return 0;

}

 

Sample Programming

 

Know how to modify your Pig Latin program to create other word permutations:

 

1. Write makeSheepLatin(word), which will follow the same rules as Pig Latin, but will add “abaa” to the ends of words. If the word ends in b, just ad “aa” to the end

 

Examples:

 

makeSheepLatin("Hello") returns ellohabaa

makeSheepLatin("bubble") returns ubblebaa

 

2. Write makeCowLatin(word), which will follow the same rules as Pig Latin, but will add “omoo” to the ends of words. If the word ends in m, just ad “oo” to the end

 

Examples:

 

makeCowLatin("Hello") returns ellohomoo

makeCowLatin("money") returns oneymoo

 

 

 

3. Write a program that will ask a user for a string and print it out backwards without any vowels.

4. Write the correct construct for an if statement to test for the occurrence of the EOF (End Of File) in an input stream named in_stream:

5. Write a piece of code that opens a file “inputfile.txt” and then closes it immediately.

6. Write a function that takes an input character array of size n and returns true if an ASCII dot (period) is found in the array, and false otherwise.  The inputs are the array size n and the array inputchars:

 

 

 

 

 

 

 

 

 


 

 

Solutions

Multiple Choice

 

1.      B

2.      B

3.      C

4.      C

5.      C

6.      C

 

 

Free Response

1.      Because the indexing starts at zero, not one. Therefore, the string goes from 0 to length()-1, not 1 to length()

2.      open( ) is not supposed to be capitalized

3.      Rand grabs a number from the computer’s internal clock. This number is then “modded” by six to give 0,1,2,3,4,5 as possible answers. Adding one to those results gives an answers between 1 and 6 inclusive

4.       It’s an illegal use of the data type declaration, it should be char C = ' c ';

 

 

 

Selected Coding Solutions (Note: All of these are possible solutions)

3.

char temp[80];
string line;
string noVowels = "";
char nextLetter;

cout<<"Enter a string: ";
gets(temp);
line = temp;

 

for (int letter = line.length() - 1; letter >= 0;letter--)

{

    nextLetter = line.at(letter);

    switch(tolower(nextLetter))

  {

          case 'a': case 'e': case 'i': case 'o': case 'u':

              break;

          default:

              noVowels += nextLetter;

     }

}

cout << noVowels << endl;

 

 

4.

if( in_stream.eof() )

    {

       …

       statements executed if predicate is true

       …

    }

 

5.

ifstream myStream;

myStream.open("inputfile.txt");

myStream.close();

 

6.

// Function Code

     bool dot_chkr(int n, char inputchars[])

     {

          int i = 0;

          while (i < n)

          {

              if (inputchars[i] == '.')

                   return true;

              i++;

          }

          return false;

  }