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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
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 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.
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.