Methods: How to Write Them - AP Computer Science A

  1. Which option correctly calls a method named 'display' with an int parameter?
    display(5). Proper method call syntax with parentheses and argument.
  2. How do you call a method named 'compute' with no parameters?
    compute(). Parentheses are required even when no parameters are passed.
  3. What is the purpose of a constructor?
    To initialize a new object of a class. Special method called when creating new class instances.
  4. Identify the error: 'public void print() { System.out.println('Hello'); }'
    Use double quotes: "Hello". String literals require double quotes, not single quotes.
  5. What is the purpose of a method signature?
    To uniquely identify a method with its name and parameters. Distinguishes methods by combining name with parameter types.
  6. How do you indicate that a method can throw an exception?
    Use the 'throws' keyword in the method declaration. Declares potential exceptions that may occur during execution.
  7. Which keyword allows a method to be used without a class instance?
    static. Enables calling methods without instantiating the class.
  8. Identify the error in this method header: 'public void myMethod(int x, y)'
    Each parameter must have a type: 'int x, int y'. Parameter 'y' is missing its data type declaration.
  9. What is the difference between parameters and arguments?
    Parameters are in the method declaration, arguments are in the call. Parameters define method interface; arguments are actual values passed.
  10. How can a method in Java be overloaded?
    By changing the number or type of parameters. Different parameter signatures enable method overloading.
  11. Identify the error: 'public static void main(String args[])'
    Use 'String[] args' for the parameter. The main method requires proper array syntax for command-line arguments.
  12. What is the return type of a constructor?
    Constructors do not have a return type. Constructors initialize objects and don't return values explicitly.
  13. Identify the error: 'public int add(int a, int b) return a + b;'
    Add curly braces: '{ return a + b; }'. Method body requires curly braces to contain executable code.
  14. What is a method's return type?
    Specifies the type of value the method returns. Declares what kind of data the method will return.
  15. What is a recursive method?
    A method that calls itself. Solves problems by breaking them into smaller, similar subproblems.
  16. What is the purpose of a method header?
    To declare the method's access, return type, and parameters. Defines method signature including visibility and parameter information.
  17. What is the purpose of a method's 'body'?
    Contains the block of code that executes when the method is called. Defines the executable statements that run when method is invoked.
  18. What must match between a method call and its definition?
    The number and types of parameters. Ensures proper method invocation with correct argument types.
  19. Identify the error: 'void calculate() { return 5; }'
    Remove 'return 5;' since return type is void. Void methods cannot return values, only perform actions.
  20. What is method overloading?
    Defining multiple methods with the same name but different parameters. Enables multiple methods with same name but different parameter lists.
  21. Identify the error in this method: 'public static void main()'
    Add parameter: '(String[] args)'. Main method requires String array parameter for command-line arguments.
  22. Why would you use a method with a void return type?
    For methods that perform actions but do not return values. Suitable for operations like printing or modifying object state.
  23. Find and correct the error: 'public int add(a, b) { return a + b; }'
    Specify parameter types: 'int a, int b'. Parameters must include data types for compilation.
  24. How do you document a method in Java?
    Use Javadoc comments: '/** ... */'. Standard documentation format for generating API documentation.
  25. What is the purpose of the 'return' statement?
    To exit a method and optionally return a value. Controls method flow and passes data back to caller.
  26. What is a parameter in the context of a method?
    A variable listed in the method's declaration. Receives values when the method is called.
  27. What is method overriding?
    Providing a new implementation for an inherited method. Subclass provides specific implementation for inherited abstract method.
  28. What is the purpose of the 'this' keyword in a method?
    To refer to the current object instance. Distinguishes instance variables from parameters or local variables.
  29. Which keyword is used to define a method that cannot be overridden?
    final. Prevents subclasses from overriding the method implementation.
  30. What is the syntax for defining a method in Java?
    accessModifier returnType methodName(parameters) { body }. Standard Java method structure with access modifier, return type, name, and parameters.
  31. Identify the error: 'public String greet() { return Hello; }'
    Use double quotes: 'return "Hello";'. String values must be enclosed in double quotes.
  32. What is the 'main' method in Java?
    The entry point of a Java application: 'public static void main(String[] args)'. Special method where program execution begins in Java applications.
  33. What is the effect of method overloading in Java?
    Allows different implementations for different parameter lists. Provides flexibility by supporting various parameter combinations.
  34. What is the purpose of an abstract method?
    To be implemented by subclasses in an abstract class. Forces subclasses to provide concrete implementation.
  35. What keyword is used to return a value from a method?
    return. Exits the method and sends a value back to the caller.
  36. What is a method's access modifier?
    Specifies the visibility of the method to other classes. Controls which classes can access and use the method.
  37. What does a method's parameter list contain?
    Types and names of variables passed to the method. Defines what data the method accepts as input.
  38. What does 'static' mean when used in a method declaration?
    The method belongs to the class, not instances of the class. Can be called using the class name without creating objects.

Study these flashcards