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

How are non-void methods used in mathematical calculations?

To perform calculations and return the result (e.g., calculating the area of a circle).

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

All Flashcards

How are non-void methods used in mathematical calculations?

To perform calculations and return the result (e.g., calculating the area of a circle).

How are non-void methods used in data validation?

To check if data meets certain criteria and return a boolean value (e.g., checking if a number is within a specific range).

How are non-void methods used in string manipulation?

To create or modify strings and return the result (e.g., reversing a string or creating a greeting).

How are non-void methods used in object creation?

To create new objects and return a reference to the object (e.g., creating an ArrayList).

How are non-void methods used in game development?

To calculate scores, determine game states (win/lose), and manipulate game objects.

How are non-void methods used in financial applications?

To calculate interest rates, loan payments, and investment returns.

How are non-void methods used in scientific simulations?

To perform complex calculations and return simulation results.

How are non-void methods used in web development?

To process user input, generate dynamic content, and interact with databases.

How are non-void methods used in mobile app development?

To handle user interactions, manage data, and perform background tasks.

How are non-void methods used in AI and machine learning?

To implement algorithms, calculate predictions, and evaluate model performance.

What is a non-void method?

A method that returns a value of a specific data type.

What is a return type?

The data type of the value returned by a non-void method.

What is the purpose of the return statement?

To send a value back to the caller and terminate the method's execution.

What is a parameter list?

Input values the method needs to do its job (optional).

What is the significance of the static keyword in a method header?

Indicates the method belongs to the class itself, not an object.

What is the purpose of access modifiers like public?

Specifies the accessibility of the method from other parts of the code.

What is unreachable code?

Code that will never execute because it is placed after a return statement.

Define a boolean method.

A method that returns a boolean value (true or false).

What is a String method?

A method that returns a String value.

What is a reference type?

A data type that refers to an object (e.g., String, ArrayList).

What does the following code output?

java
public class Test {
    public static int getValue() {
        return 10;
    }
    public static void main(String[] args) {
        System.out.println(getValue());
    }
}

10

Identify the error in the following code:

java
public class Test {
    public static int getValue() {
        System.out.println("Returning 10");
    }
}

Missing return statement. A non-void method must return a value.

What does the following code output?

java
public class Test {
    public static String getGreeting(String name) {
        return "Hello, " + name + "!";
    }
    public static void main(String[] args) {
        System.out.println(getGreeting("World"));
    }
}

Hello, World!

Identify the error in the following code:

java
public class Test {
    public static int calculateSum(int a, int b) {
        return a + b;
        System.out.println("Sum calculated");
    }
}

Unreachable code. The System.out.println statement is after the return statement and will never execute.

What does the following code output?

java
public class Test {
    public static boolean isPositive(int num) {
        return num > 0;
    }
    public static void main(String[] args) {
        System.out.println(isPositive(5));
        System.out.println(isPositive(-3));
    }
}

true false

Identify the error in the following code:

java
public class Test {
    public static String getMessage(int code) {
        if (code == 1) {
            return "Success";
        }
    }
}

Missing return statement for cases where code is not 1. The method must always return a String.

What does the following code output?

java
public class Test {
    public static double calculateArea(double radius) {
        return Math.PI * radius * radius;
    }
    public static void main(String[] args) {
        System.out.println(calculateArea(2.0));
    }
}

12.566370614359172

Identify the error in the following code:

java
public class Test {
    public static int square(double num) {
        return num * num;
    }
}

Mismatched return type. The method is declared to return an int, but num * num (where num is a double) results in a double. Needs casting or to change the return type to double.

What does the following code output?

java
public class Test {
    public static String concatenate(String str1, String str2) {
        return str1.concat(str2);
    }
    public static void main(String[] args) {
        System.out.println(concatenate("Hello", "World"));
    }
}

HelloWorld

Identify the error in the following code:

java
public class Test {
    public static boolean checkRange(int num) {
        if (num > 0 && num < 10) {
            return;
        }
    }
}

Missing return value. If the condition is false, the method does not return a boolean value.