Calling Instance Methods - AP Computer Science A

  1. What keyword is used to refer to the current object in an instance method?
    this. References the current object instance within methods.
  2. What is an instance method in Java?
    A method that operates on an instance of a class. Operates on specific object instances and can access instance variables.
  3. What is the access level of a method if no modifier is provided?
    Package-private (default). No modifier means package-private visibility within same package.
  4. What does 'public' signify in a method declaration?
    The method is accessible from any other class. Highest visibility level allowing access from any class.
  5. What happens if a method's return type is void and it returns a value?
    A compile-time error occurs. Void methods cannot return values; causes compilation failure.
  6. How do you call a method 'update' on 'obj' with 'newValue'?
    obj.update(newValue);. Standard method call with single argument parameter.
  7. Identify the error: 'object.method();' when 'method' requires an argument.
    Provide an argument: 'object.method(arg);'. Missing argument causes compile error; provide required parameter.
  8. Can 'this' keyword be used in static methods?
    No, 'this' is not available in static methods. 'this' refers to instance; static methods have no instance.
  9. What does 'return' do in a method?
    Exits the method and optionally returns a value. Terminates method execution and sends result to caller.
  10. When is an instance method appropriate over a static method?
    When the method needs to access instance fields. Instance methods can access and modify object state.
  11. How do you call the method 'add' with integer arguments 4 and 5 on 'calc'?
    calc.add(4, 5);. Pass arguments in parentheses matching parameter types.
  12. Identify the error: 'obj.method(1, 2)' when 'method' is protected.
    Cannot access from outside package or subclass. Protected access requires package membership or inheritance relationship.
  13. What does 'protected' mean for a method's access level?
    Accessible within package and subclasses. Visible to package members and subclasses only.
  14. What does the return type 'void' signify in a method declaration?
    The method does not return a value. Method completes without returning any data to caller.
  15. What distinguishes an instance method from a static method?
    Instance methods require an object to be called; static do not. Static methods belong to class; instance methods belong to objects.
  16. What is an overloaded method?
    A method with the same name but different parameters. Same name but different parameter list creates overload.
  17. What must match when calling an instance method with arguments?
    The number, type, and order of parameters. Method signature must exactly match call arguments.
  18. How do you call method 'run' with 'speed' argument on object 'vehicle'?
    vehicle.run(speed);. Argument passes data to method parameter.
  19. What is required for method overloading?
    Different parameter lists. Overloading requires distinct parameter signatures for each version.
  20. What does overloading a method allow?
    Multiple methods with the same name but different parameters. Provides multiple method implementations for different parameter types.
  21. Can an instance method be called without an object in Java?
    No, it requires an object. Instance methods need object context to access instance data.
  22. How do you call a method 'print' with no arguments on 'printer'?
    printer.print();. Empty parentheses indicate no parameters required.
  23. What is the default return type if none is specified?
    There is no default; return type must be specified. Java requires explicit return type specification for all methods.
  24. How do you call a method 'display' of class 'Screen' on an instance 'screenObj'?
    screenObj.display();. Standard object.method() syntax for instance method calls.
  25. What is method chaining?
    Calling multiple methods on the same object in a single statement. Links method calls together for fluent programming style.
  26. How do you call method 'display' with 'message' on object 'screen'?
    screen.display(message);. Passes string argument to display method parameter.
  27. How do you call an instance method named 'calculate' on an object 'obj'?
    obj.calculate();. Use dot notation: objectName.methodName();
  28. What is the syntax to define an instance method in Java?
    accessModifier returnType methodName(parameters) { body }. Standard format for declaring methods with optional parameters.
  29. What happens if a method is called with incorrect argument types?
    A compile-time error occurs. Type mismatch prevents compilation; arguments must match parameters.
  30. What is the purpose of method overloading?
    To define methods with the same name but different parameters. Enables multiple method versions with different parameter signatures.
  31. Identify the error: 'obj.method(4, 5)' when 'method' takes no parameters.
    Remove the arguments: 'obj.method();'. Too many arguments causes compile error; remove extras.
  32. Identify the return type: 'public int getValue() { return x; }'
    int. Return type appears before method name in declaration.
  33. When is the 'this' keyword used?
    To refer to the current instance of a class. Disambiguates between instance variables and parameters with same name.
  34. How do you refer to the current class's method from another method?
    Use 'this.methodName()'. Optional 'this.' prefix clarifies current object method call.
  35. What is required to call an instance method?
    An instance of the class. Instance methods must be called on specific object instances.
  36. Why must a void method not have a return statement with a value?
    It causes a compile-time error. Void methods cannot return values; only execution control.
  37. What is the effect of calling a method with no return statement?
    If non-void, causes a compile-time error. Non-void methods must return value; missing return fails compilation.
  38. Identify the error in 'obj.calculate()' when 'calculate' is private.
    Cannot access private method from outside class. Private methods only accessible within same class.
  39. What is the role of parameters in an instance method?
    They allow input to be passed to the method. Parameters accept data from caller for method processing.
  40. What is the correct way to call a method 'compute' on instance 'obj'?
    obj.compute();. Standard dot notation for calling methods on objects.

Study these flashcards