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

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

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

All Flashcards

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.

Why must the return type in the method header match the type of the returned value?

To ensure type safety and prevent compile-time errors.

What happens if a non-void method does not have a return statement?

It causes a compile-time error.

Why is it important to trace code with non-void methods?

To understand the flow of execution and predict the returned value.

Explain the concept of returning a reference type.

Returning a reference type means returning the memory address of an object.

What is the significance of naming boolean methods with is?

It improves code readability and indicates that the method checks a condition.

Why are non-void methods useful?

They allow methods to perform calculations or retrieve values and pass them back for use elsewhere in the program.

What is the purpose of the return statement?

The return statement is crucial for returning a value and stopping the method's execution.

What is a common mistake to avoid when using non-void methods?

Mismatched return types, unreachable code, and forgetting to return.

What is the difference between void and non-void methods?

Void methods do not return a value, while non-void methods return a value of a specific data type.

How does the return statement affect the execution of the method?

The method stops executing after a return statement.

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).