The return keyword is used in functions/methods to send back a value to the caller. It terminates the execution of a function and passes control back to where it was called from.
Imagine going to a vending machine and inserting money to buy a snack. Once you make your selection, the machine returns your chosen snack (value) and ends its operation, allowing you to continue with whatever you were doing before.
Void method/function: A void method or function does not return any value. It performs some actions but doesn't send anything back.
Return type: The return type specifies what type of value will be returned by a function/method.
Method call/invocation: A method call refers to using/calling a specific method in your program, which may include passing arguments and receiving returned values.
What is the output of the following code? public class Calculator { public int add(int x, int y) { return x + y; } public int multiply(int x, int y) { return x * y; } public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 6) + calc.multiply(2, 3)); } }
Consider the following class definition: public class Student { private String name; private int age; private String major; public Student(String studentName, int studentAge, String studentMajor) { name = studentName; age = studentAge; major = studentMajor; } public String getName() { return name; } public int getAge() { return age; } public String getMajor() { return major; } } Assume that the following code segment appears in a class other than Student: Student student = new Student("Alice", 19, "Computer Science"); String name = student.getName(); int age = student.getAge(); String major = student.getMajor(); System.out.println(name + " is " + age + " years old and majoring in " + major + "."); What is printed as a result of executing the code segment?
Consider the following method: public static int powerOfTwo(int n) { if (n == 0) { return 1; } else { return 2 * powerOfTwo(n - 1); } } Assume that the following code segment appears in a class: int result = powerOfTwo(3); System.out.println("2 raised to the power of 3: " + result); What is printed as a result of executing the code segment?
Consider the following method: public static boolean isEven(int number) { return number % 2 == 0; } Assume that the following code segment appears in a class: boolean result = isEven(7); System.out.println("Is 7 an even number? " + result); What is printed as a result of executing the code segment?
Why does the following code not compile? public class Student { private int gpa; public getGPA() { return gpa; } // Constructor not shown }
What does the following code do? public static int codeBlock(int[] array) { int x = 1; for (int y: array) { x *= y; } return x; }
What does the following code do? public static int[] codeBlock(int[] array) { int firstItem = array[0] for (int i = 0; i < array.length - 1; i++) { array[i] = array[i+1]; } array[array.length - 1] = firstItem; return array; }
What does the following code do? public static int[] codeBlock(ArrayList<Integer> arrayList) { int[] newArray = new int[arrayList.size()]; for (int i = 0; i < arrayList.size(); i++) { newArray[i] = arrayList.get(i); } return newArray; }
What is returned by the following code if we pass in the array [65, 78, 29, 71]? public static int codeBlock(int[] array) { int a = 0; int c = 0; for (int i = 0; i < array.length - 1; i++) { int b = 1; for (int j = i + 1; j < array.length; j++) { if (array[j] == array[i]) { b++; } } if (b > c) { a = array[i]; c = b; } } return a; }
What is returned by the following code if [3, 1, 10, 8] is passed in as the argument? public static int maximum(int[] array) { int maxValue = array[0]; for (int number: array) { if (number < maxValue) { maxValue = number; } } return maxValue; }
What is returned by the following code if the array [16, 3, 5, 4] is passed in? public static int codeBlock(int[] array) { int x = 0; for (int y: array) { x += y; } return (double) sum / (array.length); }
If we call the following method on the array [10, 9, 3, 12, 5], what is returned? public static int[] codeBlock(int[] array) { int[] newArray = new int[array.length]; for (int i = 0; i < array.length; i++) { newArray[i] = array[array.length - i - 1]; } return newArray; }
What does the following method return for the array [16, 7, 20, 35, 36, 7]? public static boolean codeBlock(int[] array) { for (int i: array) { if (15 <= i <= 30) { return false; } } return true; }
After calling the following method on the array [210, 303, 198, 762, 135], what is returned? public static int codeBlock(int[] array) { int x = array.length; for (int y: array) { if (y % 2 == 0) { x--; } } return x; }
What does the following code do? public static boolean codeBlock(ArrayList<Integer> array) { for (int i = 0; i < array.size() - 1; i++) { for (int j = i + 1; j < array.size(); j++) { if (array.get(j) == array.get(i)) { return true; } } } return false; }
What does the following code do? public static boolean codeBlock(ArrayList<Integer> array) { for (int i: array) { if (i % 3 == 1 || i % 3 == 2) { return false; } } return true; }
What is returned by the following code if the passed in ArrayList is [6, 5, 5, 2, 8]? public static int codeBlock(ArrayList<Integer> array) { int x = 0; int y = 0; for (int i = 0; i < array.size() - 1; i++) { int z = 1; for (int j = i + 1; j < array.size(); j++) { if (array.get(j) == array.get(i)) { z++; } } if (z > y) { x = array.get(i); y = z; } } return x; }
What is returned by the following code if the ArrayList passed in as the argument is [7, 10, 3, 5, 1]? public static int minimum(ArrayList<Integer> array) { int minValue = 0; for (int number: array) { if (number < minValue) { minValue = number; } } return minValue; }
What does the following code return for the ArrayList [6, 10, 25, 15, 13, 10]? public static boolean codeBlock(ArrayList<Integer> array) { for (int number: array) { if (number < 5) { return false; } } return true; }
Study guides for the entire semester
200k practice questions
Glossary of 50k key terms - memorize important vocab
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.