professor-curious-logo
professor-curious-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What is a method?

A block of code that performs a specific task.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What is a method?

A block of code that performs a specific task.

What is a void method?

A method that performs an action but does not return a value.

What is a non-void method?

A method that performs an action and returns a value.

What is a static method?

A method that belongs to the class itself, not to any specific object.

What is a non-static method?

A method that operates on a specific object.

What is a method signature?

The method name and the parameter list (types and names of inputs).

Define 'parameter'.

Input values that a method can accept to modify its behavior.

What does 'dot notation' refer to?

The way to call methods using the class/object name, a dot, and the method name.

What is the purpose of methods?

To organize code into reusable blocks, making programs more modular and easier to manage.

What is the behavior of a method?

The actions an object can perform.

How are methods used in real-world software development?

To break down complex tasks into smaller, manageable, and reusable components, improving code organization and maintainability.

Give an example of how static methods are used.

Utility functions like Math.random() or helper methods in a class that don't depend on object state.

How are void methods used in GUI applications?

To handle events like button clicks or menu selections, performing actions without returning a value.

How are non-void methods used in data processing?

To perform calculations or transformations on data and return the result, such as calculating the average of a list of numbers.

How are methods used in game development?

To define the actions and behaviors of game objects, such as moving a character or firing a weapon.

How are methods used in web development?

To handle user input, process data, and generate dynamic content on web pages.

How are methods used in mobile app development?

To implement the functionality of app features, such as displaying data, handling user interactions, and managing device resources.

How are methods used in database management?

To perform operations on database records, such as inserting, updating, deleting, and querying data.

How are methods used in scientific computing?

To implement mathematical algorithms and simulations, such as solving equations or modeling physical phenomena.

How are methods used in machine learning?

To implement machine learning algorithms, such as training models, making predictions, and evaluating performance.

Identify the error:

java
public class Example {
 public void myMethod(int a) {
 System.out.println(a);
 }
 public static void main(String[] args) {
 myMethod(5);
 }
}

myMethod is not static and cannot be called directly from the static main method. An object of the Example class must be created first.

What is the output?

java
public class Test {
 public static void printMessage() {
 System.out.println("Hello");
 }
 public static void main(String[] args) {
 Test.printMessage();
 }
}

Hello

Complete the code to call the setName method on myDog:

java
Dog myDog = new Dog();
// Complete the line below
__________;

myDog.setName("Buddy");

Identify the error:

java
public class MathUtils {
 public int add(int a, int b) {
 return a + b;
 }
 public static void main(String[] args) {
 System.out.println(add(5, 3));
 }
}

The add method is not static and cannot be called directly from the static main method without creating an object of the MathUtils class.

What is the output?

java
public class Counter {
 public int count = 0;
 public void increment() {
 count++;
 }
 public static void main(String[] args) {
 Counter c = new Counter();
 c.increment();
 System.out.println(c.count);
 }
}

1

Complete the code to call the static method incrementMinimumWage from the class Salary:

java
// Complete the line below
__________;

Salary.incrementMinimumWage();

Identify the error:

java
public class Printer {
 public void print(String message) {
 System.out.println(message);
 }
 public static void main(String[] args) {
 String msg = "Hello, world!";
 Printer.print(msg);
 }
}

The print method is not static and cannot be called directly using the class name. An object of the Printer class must be created first.

What is the output?

java
public class Greeter {
 public void greet(String name) {
 System.out.println("Hello, " + name + "!");
 }
 public static void main(String[] args) {
 Greeter g = new Greeter();
 g.greet("Alice");
 }
}

Hello, Alice!

Complete the following code to create an object of type Car called myCar and then call the method setMake with the argument "Toyota".

java
public class Car {
 String make;
 public void setMake(String newMake) {
 make = newMake;
 }
 public static void main(String[] args) {
 // Complete the code below
 }
}
java
Car myCar = new Car();
myCar.setMake("Toyota");

What is the output?

java
public class Calculator {
 public int square(int num) {
 return num * num;
 }
 public static void main(String[] args) {
 Calculator calc = new Calculator();
 System.out.println(calc.square(5));
 }
}

25