chapter 6 but shorter Flashcards

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

Example of a method that returns a value?

A

```java
public static int square(int num) {
return num * num;
}
~~~
✅ This method takes an integer and returns its square.

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

Example of a void method?

A

```java
public static void printMessage() {
System.out.println(“Hello, Java!”);
}
~~~
✅ This method does not return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
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
8
Q

Example of calling a method that returns a value?

A

```java
int result = square(5); // result = 25
~~~
✅ Stores the returned value in result.

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

Example of calling a void method?

A

```java
printMessage(); // Outputs: Hello, Java!
~~~
✅ Calls the method, but does not return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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
11
Q

Example of passing arguments?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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
13
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
14
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
15
Q

Example of method overloading?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
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.

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

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

19
Q

Why is the call stack important?

A

It manages method execution and helps track return values.

20
Q

Can a method contain loops and conditionals?

A

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

21
Q

Example of a method with a loop?

A

```java
public static void countDown(int n) {
while (n > 0) {
System.out.println(n);
n–;
}
}
~~~
✅ This method prints numbers from n to 1.

22
Q

How are arguments passed to methods in Java?

A

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

23
Q

Example of pass-by-value?

A

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

24
Q

What happens when an array is passed to a method?

A

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

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

26
Q

What is method abstraction?

A

Hiding method details while exposing only the necessary parts.

27
Q

Example of method abstraction?

A

```java
public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}
~~~
✅ The user doesn’t need to know how the area is calculated.

28
Q

Why should we validate input parameters?

A

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

29
Q

Example of error checking?

A

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