Thread: some AP test prep help

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    some AP test prep help

    I have a few questions i need help on. It's practice exams for the AP test.

    here they are --


    Consider the following function:
    bool Mystery(int k)
    {
    int n;
    bool flag=false;
    while (k>0)
    {
    cin>>n;
    flag=flag &&(n>=0);
    k--;
    }
    return flag;
    }

    Which of the following best describes what function Mystery does?
    A. Always returns true.
    B. Always returns false.
    C. Returns true if any of the k number it reads is positive.
    D. Returns true if any of the k numbers it reads is negative.
    E. Returns ture if the last of the k nubmers it reads is positive.


    i dont even get that one..... how can you cin something that the user doesn't input. and flag=flag && (n>=0)..... aren't those for "if" statements?





    A program is being written by a team of programmers. One programmer is implementing a class called Employee; another programmer is writing code that will use the Employee class. Which of the following aspects of the public member functions of the Employee class does not need to be known by both programmers?
    A. The functions' names.
    B. The functions' return types.
    C. What the functions do.
    D. How the functions are implemented.
    E. The numbers and types of the functions' parameters.





    Questions 26 and 27 refer to the following code segment (line numbers are included for reference):
    1. int x, sum;
    2. x = 1;
    3. sum = 1;
    4. cin >> x;
    5. while (x >= 0)
    6. {
    7. if (( x % 2) == 0 )
    8. sum += x;
    9. cin >> x;
    10. }
    11. cout << sum << endl

    The code segment given above was intended to read values until a negative value was read and then to print the sum of the even values read. However, the code does not work as intended. Which of the following best describes the error?
    A. Variable x is not initialized correctly.
    B. Variable sum is not initialized correctly.
    C. Variable x is used before being initialized.
    D. Variable sum is used before being initialized.
    E. The negative value intended to signal the end of input is included in the sum.





    thanks for any help!!

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    69

    i Hope this isn't a real test

    bool Mystery(int k) //int k will be how many times the while loop runs
    {
    int n; // this integer will be where the input function cin stores
    // what the use enters in
    bool flag=false; //sets it to false so if it it doesn't run it returns
    //false
    while (k>0) //as in the top line it'll run until k is equal to zero
    {
    cin>>n; //get number inputed from user
    flag=flag &&(n>=0); //check if it is positive then it makes it
    // true else it won't be the flag && (n>=0) it mean what
    //flag equals and if n is greater or equal to 0 it says true or false
    // and it combines both answers
    k--; //it decrements k so it has 1 less loop to run else it'll go
    //forever
    }
    return flag; //returns what flags is
    }



    HOme that was helpful.
    Well that's a commented version

    You can make your mind up on that one i think it's logical

    The answer for 2) is D most likely since you don't need to know the George was making the functions unless he got his cat to do it.

    For 3) It is D the sum is used before being initialized because usually in most cases it will definitely not be initialized to 0 it depends on what the computer did to it last and all that stuff.

    I hope this was helpful

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    69
    bool Mystery(int k) //int k will be how many times the while loop runs
    {
    int n; // this integer will be where the input function cin stores
    // what the use enters in
    bool flag=false; //sets it to false so if it it doesn't run it returns
    //false
    while (k>0) //as in the top line it'll run until k is equal to zero
    {
    cin>>n; //get number inputed from user
    flag=flag &&(n>=0); //check if it is positive then it makes it
    // true else it won't be the flag && (n>=0) it mean what
    //flag equals and if n is greater or equal to 0 it says true or false
    // and it combines both answers
    k--; //it decrements k so it has 1 less loop to run else it'll go
    //forever
    }
    return flag; //returns what flags is
    }



    Hope that was helpful.
    Well that's a commented version

    You can make your mind up on that one i think it's logical

    The answer for 2) is D most likely since you don't need to know the George was making the functions unless he got his cat to do it.

    For 3) It is D the sum is used before being initialized because usually in most cases it will definitely not be initialized to 0 it depends on what the computer did to it last and all that stuff.

    I hope this was helpful

  4. #4
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Question 1 - Answer B
    Code:
    truth table for && (logical AND)
    T AND T = T
    F AND F = F
    T AND F = F
    F AND T = F
    We look at the code and see that flag was initialized to false. There are only two options for n:
    1. n >= 0, which gives us F AND T
    or
    2. n < 0, which gives us F AND F
    so flag will always be false.


    Question 2 - Answer D
    The purpose of information hiding (OOP) is to hide the implementation from the user. That way implementation of the method can be changed without rewriting a program that uses it.


    Question 3 - Answer B
    Sum is initialized to 1, which means that adding the even numbers to it will always keep it odd (giving the wrong answer). Sums should be initialized to 0.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  2. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  3. Anyone Ever Taken Computer Science A AP Test?
    By adc85 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-04-2003, 02:33 PM
  4. C++ AP Test
    By CheesyMoo in forum C++ Programming
    Replies: 5
    Last Post: 02-02-2003, 07:28 PM
  5. us government ap test
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-15-2002, 11:24 AM