C++ Test Question

Sharky Forums


Results 1 to 3 of 3

Thread: C++ Test Question

  1. #1
    Sushi
    Join Date
    Feb 2002
    Posts
    1

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


  2. #2
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Post

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

  3. #3
    Grouchy Tech Recon's Avatar
    Join Date
    Jun 2001
    Posts
    4,963

    Post

    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
  •