AP Computer Science a Flashcards: Methods How To Write Them

Study Methods How To Write Them in AP Computer Science a with focused flashcards that help you recognize the idea, recall the key rule, and apply it in practice-style prompts.

AP Computer Science a

Methods How To Write Them

0 mastered0 still learning

0% Complete

QUESTION
1/ 73

Which option correctly calls a method named 'display' with an int parameter?

Tap card or press Space to flip

ANSWER

display(5). Proper method call syntax with parentheses and argument.

How well did you know it?

Card 1 / 73

What this deck covers

This deck focuses on Methods How To Write Them, giving you a quick way to review the definitions, rules, and examples that matter most for AP Computer Science a.

How to use these flashcards

Work through these flashcards in short sessions. Try to answer each prompt before flipping the card, then revisit any cards you miss until the explanation feels automatic.

All flashcards

Flashcard 1: Which option correctly calls a method named 'display' with an int parameter?

Answer: display(5). Proper method call syntax with parentheses and argument.

Flashcard 2: How do you call a method named 'compute' with no parameters?

Answer: compute(). Parentheses are required even when no parameters are passed.

Flashcard 3: Identify the error: 'public String greet() { return Hello; }'

Answer: Use double quotes: 'return "Hello";'. String values must be enclosed in double quotes.

Flashcard 4: What is the purpose of a constructor?

Answer: To initialize a new object of a class. Special method called when creating new class instances.

Flashcard 5: Identify the error: 'public void print() { System.out.println('Hello'); }'

Answer: Use double quotes: "Hello". String literals require double quotes, not single quotes.

Flashcard 6: What is the purpose of a method signature?

Answer: To uniquely identify a method with its name and parameters. Distinguishes methods by combining name with parameter types.

Flashcard 7: What is the difference between parameters and arguments?

Answer: Parameters are in the method declaration, arguments are in the call. Parameters define method interface; arguments are actual values passed.

Flashcard 8: What is the 'main' method in Java?

Answer: The entry point of a Java application: 'public static void main(String[] args)'. Special method where program execution begins in Java applications.

Flashcard 9: How can a method in Java be overloaded?

Answer: By changing the number or type of parameters. Different parameter signatures enable method overloading.

Flashcard 10: What is a method's access modifier?

Answer: Specifies the visibility of the method to other classes. Controls which classes can access and use the method.

Flashcard 11: How do you indicate that a method can throw an exception?

Answer: Use the 'throws' keyword in the method declaration. Declares potential exceptions that may occur during execution.

Flashcard 12: Which keyword allows a method to be used without a class instance?

Answer: static. Enables calling methods without instantiating the class.

Flashcard 13: Identify the error in this method header: 'public void myMethod(int x, y)'

Answer: Each parameter must have a type: 'int x, int y'. Parameter 'y' is missing its data type declaration.

Flashcard 14: What is the difference between parameters and arguments?

Answer: Parameters are in the method declaration, arguments are in the call. Parameters define method interface; arguments are actual values passed.

Flashcard 15: What is the purpose of an abstract method?

Answer: To be implemented by subclasses in an abstract class. Forces subclasses to provide concrete implementation.

Flashcard 16: Identify the error in this method: 'public static void main()'

Answer: Add parameter: '(String[] args)'. Main method requires String array parameter for command-line arguments.

Flashcard 17: How can a method in Java be overloaded?

Answer: By changing the number or type of parameters. Different parameter signatures enable method overloading.

Flashcard 18: Identify the error: 'public static void main(String args[])'

Answer: Use 'String[] args' for the parameter. The main method requires proper array syntax for command-line arguments.

Flashcard 19: What is the return type of a constructor?

Answer: Constructors do not have a return type. Constructors initialize objects and don't return values explicitly.

Flashcard 20: How do you indicate that a method can throw an exception?

Answer: Use the 'throws' keyword in the method declaration. Declares potential exceptions that may occur during execution.

Flashcard 21: Identify the error: 'public int add(int a, int b) return a + b;'

Answer: Add curly braces: '{ return a + b; }'. Method body requires curly braces to contain executable code.

Flashcard 22: What is the purpose of a method signature?

Answer: To uniquely identify a method with its name and parameters. Distinguishes methods by combining name with parameter types.

Flashcard 23: What is a method's return type?

Answer: Specifies the type of value the method returns. Declares what kind of data the method will return.

Flashcard 24: What is a recursive method?

Answer: A method that calls itself. Solves problems by breaking them into smaller, similar subproblems.

Flashcard 25: Identify the error: 'public void print() { System.out.println('Hello'); }'

Answer: Use double quotes: "Hello". String literals require double quotes, not single quotes.

Flashcard 26: How do you call a method named 'compute' with no parameters?

Answer: compute(). Parentheses are required even when no parameters are passed.

Flashcard 27: What is the purpose of a method header?

Answer: To declare the method's access, return type, and parameters. Defines method signature including visibility and parameter information.

Flashcard 28: Identify the error in this method header: 'public void myMethod(int x, y)'

Answer: Each parameter must have a type: 'int x, int y'. Parameter 'y' is missing its data type declaration.

Flashcard 29: What is the purpose of a method's 'body'?

Answer: Contains the block of code that executes when the method is called. Defines the executable statements that run when method is invoked.

Flashcard 30: What is the purpose of a method's 'body'?

Answer: Contains the block of code that executes when the method is called. Defines the executable statements that run when method is invoked.

Flashcard 31: What must match between a method call and its definition?

Answer: The number and types of parameters. Ensures proper method invocation with correct argument types.

Flashcard 32: Identify the error: 'void calculate() { return 5; }'

Answer: Remove 'return 5;' since return type is void. Void methods cannot return values, only perform actions.

Flashcard 33: What is method overloading?

Answer: Defining multiple methods with the same name but different parameters. Enables multiple methods with same name but different parameter lists.

Flashcard 34: Identify the error in this method: 'public static void main()'

Answer: Add parameter: '(String[] args)'. Main method requires String array parameter for command-line arguments.

Flashcard 35: Why would you use a method with a void return type?

Answer: For methods that perform actions but do not return values. Suitable for operations like printing or modifying object state.

Flashcard 36: What keyword is used to return a value from a method?

Answer: return. Exits the method and sends a value back to the caller.

Flashcard 37: Find and correct the error: 'public int add(a, b) { return a + b; }'

Answer: Specify parameter types: 'int a, int b'. Parameters must include data types for compilation.

Flashcard 38: How do you document a method in Java?

Answer: Use Javadoc comments: '/** ... */'. Standard documentation format for generating API documentation.

Flashcard 39: What is the purpose of the 'this' keyword in a method?

Answer: To refer to the current object instance. Distinguishes instance variables from parameters or local variables.

Flashcard 40: What is the purpose of the 'return' statement?

Answer: To exit a method and optionally return a value. Controls method flow and passes data back to caller.

Flashcard 41: What is a parameter in the context of a method?

Answer: A variable listed in the method's declaration. Receives values when the method is called.

Flashcard 42: What is the return type of a constructor?

Answer: Constructors do not have a return type. Constructors initialize objects and don't return values explicitly.

Flashcard 43: What is the syntax for defining a method in Java?

Answer: accessModifier returnType methodName(parameters) { body }. Standard Java method structure with access modifier, return type, name, and parameters.

Flashcard 44: How do you document a method in Java?

Answer: Use Javadoc comments: '/** ... */'. Standard documentation format for generating API documentation.

Flashcard 45: Identify the error: 'void calculate() { return 5; }'

Answer: Remove 'return 5;' since return type is void. Void methods cannot return values, only perform actions.

Flashcard 46: Why would you use a method with a void return type?

Answer: For methods that perform actions but do not return values. Suitable for operations like printing or modifying object state.

Flashcard 47: What is method overriding?

Answer: Providing a new implementation for an inherited method. Subclass provides specific implementation for inherited abstract method.

Flashcard 48: What is the purpose of the 'this' keyword in a method?

Answer: To refer to the current object instance. Distinguishes instance variables from parameters or local variables.

Flashcard 49: Which keyword is used to define a method that cannot be overridden?

Answer: final. Prevents subclasses from overriding the method implementation.

Flashcard 50: What does 'static' mean when used in a method declaration?

Answer: The method belongs to the class, not instances of the class. Can be called using the class name without creating objects.

Flashcard 51: What is a parameter in the context of a method?

Answer: A variable listed in the method's declaration. Receives values when the method is called.

Flashcard 52: What is the syntax for defining a method in Java?

Answer: accessModifier returnType methodName(parameters) { body }. Standard Java method structure with access modifier, return type, name, and parameters.

Flashcard 53: What does a method's parameter list contain?

Answer: Types and names of variables passed to the method. Defines what data the method accepts as input.

Flashcard 54: What must match between a method call and its definition?

Answer: The number and types of parameters. Ensures proper method invocation with correct argument types.

Flashcard 55: Identify the error: 'public int add(int a, int b) return a + b;'

Answer: Add curly braces: '{ return a + b; }'. Method body requires curly braces to contain executable code.

Flashcard 56: Identify the error: 'public String greet() { return Hello; }'

Answer: Use double quotes: 'return "Hello";'. String values must be enclosed in double quotes.

Flashcard 57: What is the 'main' method in Java?

Answer: The entry point of a Java application: 'public static void main(String[] args)'. Special method where program execution begins in Java applications.

Flashcard 58: What is the effect of method overloading in Java?

Answer: Allows different implementations for different parameter lists. Provides flexibility by supporting various parameter combinations.

Flashcard 59: What is a recursive method?

Answer: A method that calls itself. Solves problems by breaking them into smaller, similar subproblems.

Flashcard 60: Which option correctly calls a method named 'display' with an int parameter?

Answer: display(5). Proper method call syntax with parentheses and argument.

Flashcard 61: What is the purpose of an abstract method?

Answer: To be implemented by subclasses in an abstract class. Forces subclasses to provide concrete implementation.

Flashcard 62: What is a method's return type?

Answer: Specifies the type of value the method returns. Declares what kind of data the method will return.

Flashcard 63: What is the purpose of a method header?

Answer: To declare the method's access, return type, and parameters. Defines method signature including visibility and parameter information.

Flashcard 64: What keyword is used to return a value from a method?

Answer: return. Exits the method and sends a value back to the caller.

Flashcard 65: Find and correct the error: 'public int add(a, b) { return a + b; }'

Answer: Specify parameter types: 'int a, int b'. Parameters must include data types for compilation.

Flashcard 66: Which keyword allows a method to be used without a class instance?

Answer: static. Enables calling methods without instantiating the class.

Flashcard 67: What is a method's access modifier?

Answer: Specifies the visibility of the method to other classes. Controls which classes can access and use the method.

Flashcard 68: What is the purpose of the 'return' statement?

Answer: To exit a method and optionally return a value. Controls method flow and passes data back to caller.

Flashcard 69: What is the effect of method overloading in Java?

Answer: Allows different implementations for different parameter lists. Provides flexibility by supporting various parameter combinations.

Flashcard 70: What does a method's parameter list contain?

Answer: Types and names of variables passed to the method. Defines what data the method accepts as input.

Flashcard 71: Identify the error: 'public static void main(String args[])'

Answer: Use 'String[] args' for the parameter. The main method requires proper array syntax for command-line arguments.

Flashcard 72: What does 'static' mean when used in a method declaration?

Answer: The method belongs to the class, not instances of the class. Can be called using the class name without creating objects.

Flashcard 73: Which keyword is used to define a method that cannot be overridden?

Answer: final. Prevents subclasses from overriding the method implementation.