AP Computer Science A : Recognizing Class Hierarchy

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #2 : Program Design

What is the value of the string kitchen after the following code is run?

  1. class home
  2. {
  3. public:
  4. home(string);
  5. void searchhome();
  6. int buyhome();
  7. private:
  8. string kitchen();
  9. };
  10. home::home(string c)
  11. {
  12. kitchen=c;
  13. }
  14. int main()
  15. {
  16. str=’big’;
  17. home(str);
  18. }
Possible Answers:

str

'small'

'big'

c

void

Correct answer:

'big'

Explanation:

The constructor here in line 4 of the class definition is where it gets tricky. In the initialization of the constructor, we note that the input is a string.

Going down to line 10, to where the constructor function is defined, we see that a constructor with an input of c, which is defined as a string, will set the value of kitchen to c.

Finally, going down to our main code, we see that the value of the constructor in main is 'big', defined in str.

So kitchen='big'.

Learning Tools by Varsity Tutors