Methods: Passing and Returning References - AP Computer Science A

  1. Which keyword is necessary to return an object from a method?
    return. Required for all method returns.
  2. What will 'return null;' do in a method returning a reference?
    Returns no object, just null. Explicitly returns no object reference.
  3. What occurs if a method returns null by default?
    No object is returned; it represents nothing. Null indicates no object reference exists.
  4. 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.
  5. State the method signature for a method returning an object of class MyClass.
    public MyClass methodName(). Method signature shows return type and parameters.
  6. What is the effect of returning 'this' in a method?
    Returns the current instance of the class. Enables method chaining and self-reference.
  7. What is the syntax to pass an object as a parameter in a method?
    methodName(ObjectType objectName). Parameter syntax includes type and variable name.
  8. What will 'return this;' do in a method of a class?
    Returns the current object reference. 'this' refers to the current object instance.
  9. How do you indicate a method returns no object?
    Use 'void' as the method's return type. Void indicates no return value expected.
  10. Which statement correctly passes an object to a method?
    methodName(object);. Object name without parentheses passes reference.
  11. Identify the correct method signature for returning a MyClass object.
    public MyClass methodName(). Return type matches the class name.
  12. How do you handle a null return value from a method?
    Check for null before using the return value. Prevents NullPointerException when accessing.
  13. What is the outcome of passing a null reference to a method?
    NullPointerException if dereferenced. Accessing null reference throws runtime exception.
  14. Which statement correctly returns an object created within a method?
    return new ObjectType();. Constructor creates and returns new object.
  15. 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.
  16. 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.
  17. Find and correct the error: 'return objectName()' for returning an object.
    Correct: 'return objectName;'. Parentheses invoke method; remove for variable return.
  18. Which option correctly returns an object of type String?
    return "Example";. String literals return String object references.
  19. When passing an object to a method, what is actually passed?
    The reference to the object. Memory address, not the entire object.
  20. How do you return an object reference from a method?
    Use the 'return' keyword followed by the object. Return statement sends object reference back.
  21. What does 'null' signify when returned by a method?
    The method does not return an actual object. Null represents absence of an object reference.
  22. 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.
  23. What is the default return value of a method that returns an object?
    null. Methods return null when no object is provided.
  24. When is a new object created during method execution?
    When using 'new' keyword inside the method. Constructor calls create new object instances.
  25. What does 'return null;' indicate in a method returning an object?
    No object is being returned. Explicit way to return no object.
  26. Identify the error: 'public void method() { return new MyClass(); }'
    Error: method has void return type. Void methods cannot return any value.
  27. 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.
  28. 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.
  29. What must match when a method returns an object?
    The return type and the object's type. Type compatibility ensures valid returns.
  30. 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.
  31. 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.
  32. Identify the error: 'public String getName() { return name }'
    Missing semicolon after 'name'. Missing semicolon terminates return statement.
  33. Identify the keyword used to return an object reference from a method.
    return. Basic keyword for returning any value or reference.
  34. 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.
  35. What is the outcome if you pass a null object to a method?
    Risk of NullPointerException if dereferenced. Null references cause exceptions when used.
  36. What is the effect of modifying an object within a method?
    Changes persist outside the method. Reference passing means original object is modified.

Study these flashcards