Methods: Passing and Returning References - AP Computer Science A
Card 1 of 30
Which keyword is necessary to return an object from a method?
Which keyword is necessary to return an object from a method?
Tap to reveal answer
return. Required for all method returns.
return. Required for all method returns.
← Didn't Know|Knew It →
Find and correct the error: 'return objectName()' for returning an object.
Find and correct the error: 'return objectName()' for returning an object.
Tap to reveal answer
Correct: 'return objectName;'. Parentheses invoke method; remove for variable return.
Correct: 'return objectName;'. Parentheses invoke method; remove for variable return.
← Didn't Know|Knew It →
Identify the correct method signature for returning a MyClass object.
Identify the correct method signature for returning a MyClass object.
Tap to reveal answer
public MyClass methodName(). Return type matches the class name.
public MyClass methodName(). Return type matches the class name.
← Didn't Know|Knew It →
What is the outcome if you pass a null object to a method?
What is the outcome if you pass a null object to a method?
Tap to reveal answer
Risk of NullPointerException if dereferenced. Null references cause exceptions when used.
Risk of NullPointerException if dereferenced. Null references cause exceptions when used.
← Didn't Know|Knew It →
Identify the keyword used to return an object reference from a method.
Identify the keyword used to return an object reference from a method.
Tap to reveal answer
return. Basic keyword for returning any value or reference.
return. Basic keyword for returning any value or reference.
← Didn't Know|Knew It →
What happens when you pass a reference of an object to a method?
What happens when you pass a reference of an object to a method?
Tap to reveal answer
The method can modify the object's state. Reference allows access to the original object.
The method can modify the object's state. Reference allows access to the original object.
← Didn't Know|Knew It →
What is the default return value of a method that returns an object?
What is the default return value of a method that returns an object?
Tap to reveal answer
null. Methods return null when no object is provided.
null. Methods return null when no object is provided.
← Didn't Know|Knew It →
What is the syntax to pass an object as a parameter in a method?
What is the syntax to pass an object as a parameter in a method?
Tap to reveal answer
methodName(ObjectType objectName). Parameter syntax includes type and variable name.
methodName(ObjectType objectName). Parameter syntax includes type and variable name.
← Didn't Know|Knew It →
What is the effect of modifying an object within a method?
What is the effect of modifying an object within a method?
Tap to reveal answer
Changes persist outside the method. Reference passing means original object is modified.
Changes persist outside the method. Reference passing means original object is modified.
← Didn't Know|Knew It →
What type of method can return an object reference in Java?
What type of method can return an object reference in Java?
Tap to reveal answer
Any method with a non-void return type. Non-void methods can return object references.
Any method with a non-void return type. Non-void methods can return object references.
← Didn't Know|Knew It →
State the method signature for a method returning an object of class MyClass.
State the method signature for a method returning an object of class MyClass.
Tap to reveal answer
public MyClass methodName(). Method signature shows return type and parameters.
public MyClass methodName(). Method signature shows return type and parameters.
← Didn't Know|Knew It →
When is a new object created during method execution?
When is a new object created during method execution?
Tap to reveal answer
When using 'new' keyword inside the method. Constructor calls create new object instances.
When using 'new' keyword inside the method. Constructor calls create new object instances.
← Didn't Know|Knew It →
What does 'null' signify when returned by a method?
What does 'null' signify when returned by a method?
Tap to reveal answer
The method does not return an actual object. Null represents absence of an object reference.
The method does not return an actual object. Null represents absence of an object reference.
← Didn't Know|Knew It →
Find and correct the error: 'return objectName()' for returning an object.
Find and correct the error: 'return objectName()' for returning an object.
Tap to reveal answer
Correct: 'return objectName;'. Parentheses invoke method; remove for variable return.
Correct: 'return objectName;'. Parentheses invoke method; remove for variable return.
← Didn't Know|Knew It →
What is the outcome of passing a null reference to a method?
What is the outcome of passing a null reference to a method?
Tap to reveal answer
NullPointerException if dereferenced. Accessing null reference throws runtime exception.
NullPointerException if dereferenced. Accessing null reference throws runtime exception.
← Didn't Know|Knew It →
How do you return an object reference from a method?
How do you return an object reference from a method?
Tap to reveal answer
Use the 'return' keyword followed by the object. Return statement sends object reference back.
Use the 'return' keyword followed by the object. Return statement sends object reference back.
← Didn't Know|Knew It →
Identify the result of modifying a primitive data type passed to a method.
Identify the result of modifying a primitive data type passed to a method.
Tap to reveal answer
Modification does not persist outside. Primitives are passed by value, not reference.
Modification does not persist outside. Primitives are passed by value, not reference.
← Didn't Know|Knew It →
What will 'return this;' do in a method of a class?
What will 'return this;' do in a method of a class?
Tap to reveal answer
Returns the current object reference. 'this' refers to the current object instance.
Returns the current object reference. 'this' refers to the current object instance.
← Didn't Know|Knew It →
Which option correctly returns an object of type String?
Which option correctly returns an object of type String?
Tap to reveal answer
return "Example";. String literals return String object references.
return "Example";. String literals return String object references.
← Didn't Know|Knew It →
Identify the error: 'public void method() { return new MyClass(); }'
Identify the error: 'public void method() { return new MyClass(); }'
Tap to reveal answer
Error: method has void return type. Void methods cannot return any value.
Error: method has void return type. Void methods cannot return any value.
← Didn't Know|Knew It →
Which statement correctly passes an object to a method?
Which statement correctly passes an object to a method?
Tap to reveal answer
methodName(object);. Object name without parentheses passes reference.
methodName(object);. Object name without parentheses passes reference.
← Didn't Know|Knew It →
How do you define a method that accepts an object as an argument?
How do you define a method that accepts an object as an argument?
Tap to reveal answer
Use the object's class as parameter type. Parameter type must match the object's class.
Use the object's class as parameter type. Parameter type must match the object's class.
← Didn't Know|Knew It →
What occurs if a method returns null by default?
What occurs if a method returns null by default?
Tap to reveal answer
No object is returned; it represents nothing. Null indicates no object reference exists.
No object is returned; it represents nothing. Null indicates no object reference exists.
← Didn't Know|Knew It →
Which statement correctly returns an object created within a method?
Which statement correctly returns an object created within a method?
Tap to reveal answer
return new ObjectType();. Constructor creates and returns new object.
return new ObjectType();. Constructor creates and returns new object.
← Didn't Know|Knew It →
Identify the error: 'public String getName() { return name }'
Identify the error: 'public String getName() { return name }'
Tap to reveal answer
Missing semicolon after 'name'. Missing semicolon terminates return statement.
Missing semicolon after 'name'. Missing semicolon terminates return statement.
← Didn't Know|Knew It →
What happens if you modify an object's field inside a method?
What happens if you modify an object's field inside a method?
Tap to reveal answer
The modification persists outside the method. Object state changes persist after method ends.
The modification persists outside the method. Object state changes persist after method ends.
← Didn't Know|Knew It →
How do you indicate a method returns no object?
How do you indicate a method returns no object?
Tap to reveal answer
Use 'void' as the method's return type. Void indicates no return value expected.
Use 'void' as the method's return type. Void indicates no return value expected.
← Didn't Know|Knew It →
What must match when a method returns an object?
What must match when a method returns an object?
Tap to reveal answer
The return type and the object's type. Type compatibility ensures valid returns.
The return type and the object's type. Type compatibility ensures valid returns.
← Didn't Know|Knew It →
What is the effect of returning 'this' in a method?
What is the effect of returning 'this' in a method?
Tap to reveal answer
Returns the current instance of the class. Enables method chaining and self-reference.
Returns the current instance of the class. Enables method chaining and self-reference.
← Didn't Know|Knew It →
When passing an object to a method, what is actually passed?
When passing an object to a method, what is actually passed?
Tap to reveal answer
The reference to the object. Memory address, not the entire object.
The reference to the object. Memory address, not the entire object.
← Didn't Know|Knew It →