|
-
C++ Test Question
This question was on a AP Computer Science test recently:
The letters ABCDEF# will be entered in that order.
How many times will the function Mystery() be called while processing the above data?
void Mystery()
{
cin >> inChar;
if(inChar != '#')
Mystery();
cout << inChar;
}
a)6
b)7
c)8
d)9
Please respond...there has been much debate over this question in my class.
-
Reef Shark
6 if you don't count the initial call to the function. 7 if you do.
Initial Call to mystery()
- Letter 'A' is read in
- Letter 'A' is tested to true
- mystery() is called (1)
Second Call to mystery() (1st recursive)
- Letter 'B' is read in
- Letter 'B' is tested to true
- mystery() is called (2)
Third Call to mystery (2nd recursive)
- Letter 'C' is read in
- Letter 'C' is tested to true
- mystery is called (3)
--::SNIP::--
Sixth Call to mystery (5th recursive)
- Letter 'F' is read in
- Letter 'F' is tested to true
- mystery() is called (6)
Seventh Call to mystery (6th recursive)
- Character '#' is read in
- Character '#' is tested to false
- Character '#' is outputted to STDOUT
Then the functions go back down printing, etc. etc.
So like I said, 6 if you are just asking how many times it calls itself _within_ itself (recursively). 7 if you are counting the intial call to mystery to start the recursion.
I'm am 90% sure
------------------
## root is the greed of all evil ##
## root is the greed of all evil ##
-
Grouchy Tech
Yep crappy question, depends on what you count!
I had this one a couple years ago,
Write all the operators, which cant be overloaded?
now most people took the comma as seperating two different statements (myself included) so went off trying to remeber all the damn operators 
------------------
Where there's a whip <crack> thers a way.
A crack on the back says were gonna fight were gonna march all day and night and more cuz we are the slaves of the dark lord's war.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|