Structured Programming in C++
Prof. Gregory Safko
Camden County College
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) ?
3. Which object type do you use for input file streams?
4. Assume you have string q1 = "Josh";. What is the output for cout<<q1.at(3);?
a. Jos
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;
}