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.charAt(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 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
1. Know how to modify your Palindrome Checker to check for numeric palindromes. Some examples of valid numeric palindromes are:
12321
45.654
+1454.1
-99
Some invalid numeric palindromes are:
900
12.2
0111
0111
2. Know how to modify your Palindrome Checker to check for character palindromes. Some examples of valid character palindromes are:
#$$#
-99-
@@@…@@@
1234#4321
abccba
{[<()>]}
Some invalid character palindromes are:
1.221 (this is a valid numeric palindrome, but not a valid character palindrome)
Hannah (this is a valid Project 2 palindrome, but not a valid character palindrome, since ‘H” doesn’t match ‘h’)
never even (this is a valid palindrome, but the space between the two words invalidates the checking)
{[<()>]}() (this is a valid series of balanced parenthesis, but not a valid character palindrome)
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 harry:
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.
string line;
string noVowels = "";
char nextLetter;
cout<<"Enter a string: ";
getline(cin,line,'\n');
for(int letter=0;letter<line.length();letter++)
{
switch(tolower(nextLetter))
{
case 'a': case 'e': case 'i': case 'o': case 'u':
break;
default:
noVowels += nextLetter;
}
}
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 harry[])
{
int i = 0;
while (i < n)
{
if (harry[i] == '.')
return true;
i++;
}
return false;
}