AP Computer Science A : Designing Classes

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

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 : Designing Classes

Which of the following lines represents a data member?

1. class animal
2. {
3. public:
4. animal();
5. void fetch();
6. private:
7. char bone;
8. }

Possible Answers:

Line 7

Line 5

Line 3

Line 6

Line 1

Correct answer:

Line 7

Explanation:

Data members are variables created in a class, and are typically found under the private: label, as data members are typically kept local to that class. It can be distinguished from member functions because of its lack of parenthesis().

Example Question #2 : Designing Classes

Which of the following operations allow you to define the function func1() outside of the class definition?

1. class school
2. {
3. public:
4. school();
5. void func1();
6. private:
7. char class;
8. }

Possible Answers:

->

;

,

=

::

Correct answer:

::

Explanation:

The function template for func1() has been defined in the class definition, however the implementation of the function has not. In order to specify what the function does, you must say

classname::functionname()

{

}

Or, in our case,

school::function1()

{

//function definition goes in here

}

Example Question #3 : Object Oriented Program Design

Design a shell for a class named Dog that has 3 attributes:

  • Does the dog have a tail? 
  • Is the dog purebred?
  • A list of strings of definitive features for the dog
Possible Answers:

class Dog {

  boolean purebred;

  List features;

}

class Dog {

  boolean tail;

  boolean purebred;

  List features;

}

class Dog {

  boolean tail;

  boolean purebred;

}

class Dog {

 

}

Correct answer:

class Dog {

  boolean tail;

  boolean purebred;

  List features;

}

Explanation:

In the prompt, any yes or no questions should be addressed as booleans (i.e. tail, purebred). Any lists should have the type and name of the list. This was just a shell, so there should be no declarations of variables. 

Example Question #3 : Designing Classes

Design a class, SuperHero, that will be used for an existing application. The class should extend the parent class Man. The class should define clothes, skin color, hair color, eye color, good or evil, whether or not powers are had, and a list of personality traits. The class Man supports an implementation of the method doGood. Here is the method stub for doGood:

public String doGood(Boolean good);

Your class will need to support an implementation of doGood.

Choose the best answer. 

Possible Answers:

class SuperHero {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

 

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

 

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

Correct answer:

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

 

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

Explanation:

This is the correct answer because the class extends the super class Man. It also lists all of the attributes defined in the prompt. In addition to listing all of the attributes, the class also defines an implementation of the doGood method which the prompt states is defined by the parent. One of the answer choices simply defined the stub for the method. The prompt clearly states to support an implementation of the method. 

 

Learning Tools by Varsity Tutors

Incompatible Browser

Please upgrade or download one of the following browsers to use Instant Tutoring: