Anatomy of a Class and Constructors
Free AP Computer Science A multiple-choice practice for topics 3.3–3.4, Unit 3 (Class Creation). Answer five random questions, check your work, and review the essential knowledge behind each one.
Unit 3: Class Creation. Topics 3.3–3.4. This set has 60 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.
Topics assessed
- 3.3 Anatomy of a Class
- 3.4 Constructors
Practice questions
Loading questions…
Essential knowledge for this set
These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 60 questions in this set assess, grouped by topic and learning objective. The table under each question links here.
Topic 3.3: Anatomy of a Class
3.3.A — Develop code to designate access and visibility constraints to classes, data, constructors, and methods.
- 3.3.A.1 Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes. The keywords public and private affect the access of classes, data, constructors, and methods. The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.
- 3.3.A.2 In this course, classes are always designated public and are declared with the keyword class.
- 3.3.A.3 In this course, constructors are always designated public.
- 3.3.A.4 Instance variables belong to the object, and each object has its own copy of the variable.
- 3.3.A.5 Access to attributes should be kept internal to the class in order to accomplish encapsulation. Therefore, it is good programming practice to designate the instance variables for these attributes as private unless the class specification states otherwise.
- 3.3.A.6 Access to behaviors can be internal or external to the class. Methods designated as public can be accessed internally or externally to a class, whereas methods designated as private can only be accessed internally to the class.
Topic 3.4: Constructors
3.4.A — Develop code to declare instance variables for the attributes to be initialized in the body of the constructors of a class.
- 3.4.A.1 An object’s state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This defines a has-a relationship between the object and its instance variables.
- 3.4.A.2 A constructor is used to set the initial state of an object, which should include initial values for all instance variables. When a constructor is called, memory is allocated for the object and the associated object reference is returned. Constructor parameters, if specified, provide data to initialize instance variables.
- 3.4.A.3 When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable does not hold a reference to the original object, and methods are prevented from modifying the state of the original object.
- 3.4.A.4 When no constructor is written, Java provides a no-parameter constructor, and the instance variables are set to default values according to the data type of the attribute. This constructor is called the default constructor.
- 3.4.A.5 The default value for an attribute of type int is 0. The default value of an attribute of type double is 0.0. The default value of an attribute of type boolean is false. The default value of a reference type is null.