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