chapter 6 but shorter Flashcards

(19 cards)

1
Q

What is a method in Java?

A

A collection of statements grouped together to perform an operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why use methods?

A

✅ Improves code readability
✅ Supports modular programming
Reduces redundancy by reusing code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a static method?

A

A method that belongs to a class rather than an object (e.g., Math.sqrt(x)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the components of a method definition?

A

Modifiers (e.g., public, static)
Return type (e.g., void, int)
Method name
Parameter list (optional)
Method body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you call a method in Java?

A

By using its name and providing necessary arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between an argument and a parameter?

A

Parameter → A variable inside the method definition.
Argument → The actual value passed when calling the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the return statement?

A

It terminates the method and sends a value back to the caller.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can a method return more than one value?

A

No, but it can return an array or object to store multiple values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is method overloading?

A

Defining multiple methods with the same name but different parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the scope of a local variable in a method?

A

A local variable exists only within the method where it is declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Example of variable scope?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens when a method is called in relation to the stack frame?

A

✅ Java creates a new stack frame for the method’s variables.
✅ The method executes, then removes the stack frame when done.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why is the call stack important?

A

It manages method execution and helps track return values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can a method contain loops and conditionals?

A

Yes, a method can have:
Loops (for, while)
Conditionals (if-else)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are arguments passed to methods in Java?

A

Primitive types (e.g., int, double) are passed by value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens when an array is passed to a method?

A

The reference is passed, so the original array can be modified.

17
Q

Example of passing an array?

A

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

18
Q

What is method abstraction?

A

Hiding method details while exposing only the necessary parts.

19
Q

Why should we validate input parameters?

A

✅ Prevents bugs and incorrect calculations.
✅ Avoids unexpected errors during execution.