Methods: How to Write Them - AP Computer Science A
- Which option correctly calls a method named 'display' with an int parameter?
- display(5). Proper method call syntax with parentheses and argument.
- How do you call a method named 'compute' with no parameters?
- compute(). Parentheses are required even when no parameters are passed.
- What is the purpose of a constructor?
- To initialize a new object of a class. Special method called when creating new class instances.
- Identify the error: 'public void print() { System.out.println('Hello'); }'
- Use double quotes: "Hello". String literals require double quotes, not single quotes.
- 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.
- 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.
- Which keyword allows a method to be used without a class instance?
- static. Enables calling methods without instantiating the class.
- 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.
- 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.
- How can a method in Java be overloaded?
- By changing the number or type of parameters. Different parameter signatures enable method overloading.
- 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.
- What is the return type of a constructor?
- Constructors do not have a return type. Constructors initialize objects and don't return values explicitly.
- 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.
- What is a method's return type?
- Specifies the type of value the method returns. Declares what kind of data the method will return.
- What is a recursive method?
- A method that calls itself. Solves problems by breaking them into smaller, similar subproblems.
- 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.
- 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.
- What must match between a method call and its definition?
- The number and types of parameters. Ensures proper method invocation with correct argument types.
- Identify the error: 'void calculate() { return 5; }'
- Remove 'return 5;' since return type is void. Void methods cannot return values, only perform actions.
- What is method overloading?
- Defining multiple methods with the same name but different parameters. Enables multiple methods with same name but different parameter lists.
- Identify the error in this method: 'public static void main()'
- Add parameter: '(String[] args)'. Main method requires String array parameter for command-line arguments.
- 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.
- 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.
- How do you document a method in Java?
- Use Javadoc comments: '/** ... */'. Standard documentation format for generating API documentation.
- 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.
- 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.
- What is method overriding?
- Providing a new implementation for an inherited method. Subclass provides specific implementation for inherited abstract method.
- 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.
- Which keyword is used to define a method that cannot be overridden?
- final. Prevents subclasses from overriding the method implementation.
- 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.
- Identify the error: 'public String greet() { return Hello; }'
- Use double quotes: 'return "Hello";'. String values must be enclosed in double quotes.
- 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.
- What is the effect of method overloading in Java?
- Allows different implementations for different parameter lists. Provides flexibility by supporting various parameter combinations.
- What is the purpose of an abstract method?
- To be implemented by subclasses in an abstract class. Forces subclasses to provide concrete implementation.
- What keyword is used to return a value from a method?
- return. Exits the method and sends a value back to the caller.
- 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.
- 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.
- 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.