Using Objects in AP Computer Science A
Which of the following is the primary purpose of a non-void method?
To perform a task without returning any value.
To calculate or retrieve a value and return it.
To define a class.
To handle exceptions.
Which part of a non-void method header specifies the type of value that the method will return?
public
static
methodName
dataType
Consider the following method definition:
java
public int calculateSum(int a, int b) {
int sum = a + b;
}
What is the most likely error in this code?
The method should be declared as static
.
The method is missing a return
statement.
The variables a
and b
should be declared as double
.
The sum
variable is unnecessary.
What should be the return type of a method that calculates the area of a square, given the side length as a double
?
int
void
double
String
Complete the following method to correctly determine if a number is prime:
java
public static boolean isPrime(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0) {
// Not prime
}
}
// Prime
return _________;
}
number % 2 == 0
number == 1
true
false
What will be the output of the following code?
java
public class StringModifier {
public static String addExclamation(String str) {
return str + "!";
}
public static void main(String[] args) {
String message = "Hello";
String excitedMessage = addExclamation(message);
Sys...
Hello
Hello!
null
An error message
Consider the following method:
java
public static ArrayList<Integer> createList(int size) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
if (i % 2 == 0) {
list.add(i);
}
}
return list;
}
What will be returned if createList(5)
is cal...
[0, 1, 2, 3, 4]
[0, 2, 4]
[1, 3]
[]

How are we doing?
Give us your feedback and let us know how we can improve
Identify the error in the following code:
java
public class Example {
public int calculate(int x, int y) {
double result = x * y;
return result;
}
}
The method should be static
.
The variable result
should be an int
.
There is a mismatched return type.
The method should be void
.
What is the issue with the following code?
java
public class MathUtils {
public int square(int num) {
return num * num;
System.out.println("Calculation complete");
}
}
The method is not static
.
The return
statement should be at the end of the method.
The println
statement is unreachable.
The method should return a double
.
Why is it important to carefully read the method signature, especially the return type, when answering exam questions involving non-void methods?
To understand the method's purpose.
To determine the correct data type to return.
To identify potential errors related to mismatched types.
All of the above.