Click to See Complete Forum and Search --> : C++ Test Question


Alpha_Pal
02-01-2002, 01:52 PM
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.

biosx
02-01-2002, 02:23 PM
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 ##

Recon
02-01-2002, 04:28 PM
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 http://www.sharkyforums.com/ubb/wink.gif


------------------
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.