chapter 6 but shorter Flashcards
What is a method in Java?
A collection of statements grouped together to perform an operation.
Why use methods?
✅ Improves code readability
✅ Supports modular programming
✅ Reduces redundancy by reusing code
What is a static method?
A method that belongs to a class rather than an object (e.g., Math.sqrt(x)
).
What are the components of a method definition?
✅ Modifiers (e.g., public
, static
)
✅ Return type (e.g., void
, int
)
✅ Method name
✅ Parameter list (optional)
✅ Method body
Example of a method that returns a value?
```java
public static int square(int num) {
return num * num;
}
~~~
✅ This method takes an integer and returns its square.
Example of a void
method?
```java
public static void printMessage() {
System.out.println(“Hello, Java!”);
}
~~~
✅ This method does not return a value.
How do you call a method in Java?
By using its name and providing necessary arguments.
Example of calling a method that returns a value?
```java
int result = square(5); // result = 25
~~~
✅ Stores the returned value in result
.
Example of calling a void
method?
```java
printMessage(); // Outputs: Hello, Java!
~~~
✅ Calls the method, but does not return a value.
What is the difference between an argument and a parameter?
✅ Parameter → A variable inside the method definition.
✅ Argument → The actual value passed when calling the method.
Example of passing arguments?
```java
public static int add(int a, int b) {
return a + b;
}
System.out.println(add(3, 4)); // Output: 7
~~~
✅ 3
and 4
are arguments, a
and b
are parameters.
What is the purpose of the return
statement?
It terminates the method and sends a value back to the caller.
Can a method return more than one value?
No, but it can return an array or object to store multiple values.
What is method overloading?
Defining multiple methods with the same name but different parameters.
Example of method overloading?
```java
public static int multiply(int a, int b) {
return a * b;
}
public static double multiply(double a, double b) {
return a * b;
}
~~~
✅ The compiler chooses the correct method based on the argument types.
What is the scope of a local variable in a method?
A local variable exists only within the method where it is declared.
Example of variable scope?
```java
public static void example() {
int x = 10; // x exists only inside this method
}
System.out.println(x); // ERROR: x is out of scope!
~~~
✅ x
cannot be accessed outside example()
.
What happens when a method is called in relation to the stack frame?
✅ Java creates a new stack frame for the method’s variables.
✅ The method executes, then removes the stack frame when done.
Why is the call stack important?
It manages method execution and helps track return values.
Can a method contain loops and conditionals?
Yes, a method can have:
✅ Loops (for
, while
)
✅ Conditionals (if-else
)
Example of a method with a loop?
```java
public static void countDown(int n) {
while (n > 0) {
System.out.println(n);
n–;
}
}
~~~
✅ This method prints numbers from n
to 1.
How are arguments passed to methods in Java?
Primitive types (e.g., int
, double
) are passed by value.
Example of pass-by-value?
```java
public static void changeValue(int num) {
num = 10;
}
int x = 5;
changeValue(x);
System.out.println(x); // Output: 5
~~~
✅ x
is not changed because Java passes a copy of the value.
What happens when an array is passed to a method?
The reference is passed, so the original array can be modified.
Example of passing an array?
```java
public static void modifyArray(int[] arr) {
arr[0] = 99; // Modifies the original array
}
int[] nums = {1, 2, 3};
modifyArray(nums);
System.out.println(nums[0]); // Output: 99
~~~
✅ The actual array is modified because arrays are passed by reference.
What is method abstraction?
Hiding method details while exposing only the necessary parts.
Example of method abstraction?
```java
public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}
~~~
✅ The user doesn’t need to know how the area is calculated.
Why should we validate input parameters?
✅ Prevents bugs and incorrect calculations.
✅ Avoids unexpected errors during execution.
Example of error checking?
```java
public static void setAge(int age) {
if (age < 0) {
System.out.println(“Invalid age!”);
return;
}
System.out.println(“Valid age: “ + age);
}
~~~
✅ This method ensures negative values are not accepted.