Writing Methods and Passing References
Free AP Computer Science A multiple-choice practice for topics 3.5–3.6, 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.5–3.6. This set has 59 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.5 Methods: How to Write Them
- 3.6 Methods: Passing and Returning References of an Object
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 59 questions in this set assess, grouped by topic and learning objective. The table under each question links here.
Topic 3.5: Methods: How to Write Them
3.5.A — Develop code to define behaviors of an object through methods written in a class using primitive values and determine the result of calling these methods.
- 3.5.A.1 A void method does not return a value. Its header contains the keyword void before the method name.
- 3.5.A.2 A non-void method returns a single value. Its header includes the return type in place of the keyword void.
- 3.5.A.3 In non-void methods, a return expression compatible with the return type is evaluated, and the value is returned. This is referred to as return by value.
- 3.5.A.4 The return keyword is used to return the flow of control to the point where the method or constructor was called. Any code that is sequentially after a return statement will never be executed. Executing a return statement inside a selection or iteration statement will halt the statement and exit the method or constructor.
- 3.5.A.5 An accessor method allows objects of other classes to obtain a copy of the value of instance variables or class variables. An accessor method is a non-void method.
- 3.5.A.6 A mutator (modifier) method is a method that changes the values of the instance variables or class variables. A mutator method is often a void method.
- 3.5.A.7 Methods with parameters receive values through those parameters and use those values in accomplishing the method’s task.
- 3.5.A.8 When an argument is a primitive value, the parameter is initialized with a copy of that value. Changes to the parameter have no effect on the corresponding argument.
Topic 3.6: Methods: Passing and Returning References of an Object
3.6.A — Develop code to define behaviors of an object through methods written in a class using object references and determine the result of calling these methods.
- 3.6.A.1 When an argument is an object reference, the parameter is initialized with a copy of that reference; it does not create a new independent copy of the object. If the parameter refers to a mutable object, the method or constructor can use this reference to alter the state of the object. It is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.
- 3.6.A.2 When the return expression evaluates to an object reference, the reference is returned, not a reference to a new copy of the object.
- 3.6.A.3 Methods cannot access the private data and methods of a parameter that holds a reference to an object unless the parameter is the same type as the method’s enclosing class.