Module 5: Methods Flashcards

(159 cards)

1
Q

What is a method?

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

What are the benefits of using methods?

A

Define reusable code, organize and simplify coding, make code easy to maintain

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

What is the syntax for defining a method?

A

modifier returnValueType methodName(list of parameters) { // Method body; }

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

What are the two parts of a method?

A

Method header and method body

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

What is a method header?

A

Specifies the modifiers, return value type, method name, and parameters of the method

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

What is a method body?

A

Contains a collection of statements that implement the method

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

What is a formal parameter?

A

A variable defined in the method header (also called parameter)

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

What is an actual parameter?

A

The value passed to a parameter when invoking a method (also called argument)

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

What is a method signature?

A

The method name and the parameter list together

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

What is a value-returning method?

A

A method that returns a value

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

What is a void method?

A

A method that performs operations without returning a value

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

What keyword is used for void methods?

A

void

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

What statement is required in value-returning methods?

A

A return statement

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

What is the syntax of a return statement?

A

return value; or just return; for void methods

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

Can a void method have a return statement?

A

Yes, but it’s optional and uses syntax: return;

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

What is the purpose of return in a void method?

A

To terminate the method and return to the caller

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

What modifier is used for all methods in Chapter 6?

A

static

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

What are examples of value-returning methods?

A

max, Math.pow, Math.random

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

What are examples of void methods?

A

System.out.println, System.exit, printGrade

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

What is a caller?

A

The program that calls/invokes a method

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

How do you invoke a value-returning method?

A

As part of an expression: int larger = max(3, 4);

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

How do you invoke a void method?

A

As a statement: System.out.println(‘Hello’);

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

Can a value-returning method be invoked as a statement?

A

Yes, but the return value is ignored (not common practice)

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

What is parameter order association?

A

Arguments must be provided in the same order as parameters in the method signature

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does it mean for arguments to match parameters?
They must match in order, number, and compatible type
26
What is a compatible type?
A type that can be passed without explicit casting, like int to double
27
What is pass-by-value?
The value of the argument is passed to the parameter, not the variable itself
28
Does changing a parameter affect the original argument?
No, the parameter has its own memory space
29
What is an activation record?
Stores parameters and variables for a method during execution
30
What is a call stack?
Memory area that stores activation records (also called execution stack or runtime stack)
31
How does a call stack work?
Last-in, first-out: the most recently called method's record is removed first
32
When is an activation record created?
When a method is invoked
33
When is an activation record removed?
When a method finishes and returns to its caller
34
If method m1 calls m2, and m2 calls m3, what order are records removed?
m3, then m2, then m1
35
Can you swap two variables using a method with pass-by-value?
No, changes to parameters don't affect original arguments
36
Does it matter if parameter and argument have the same name?
No, the parameter has its own memory space regardless of name
37
What happens if you don't declare each parameter separately?
Compilation error: max(int num1, num2) is wrong
38
What is the difference between define and declare?
Define: what the item is; Declare: allocate memory for the item
39
What error occurs if a value-returning method has no return?
Compilation error
40
What is wrong with: public static int sign(int n) { if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return -1; }
Compiler thinks method might not return a value (should remove 'if (n < 0)')
41
What is the return type of the main method?
void
42
What is the main method header?
public static void main(String[] args)
43
What does String[] args mean in main method?
The parameter is an array of String
44
Can you write a return statement like: return x + y; in a void method?
No, void methods cannot return a value (causes syntax error)
45
What are method header examples for different purposes?
computeCommission, displayCalendar, sqrt, isEven, displayMessage, monthlyPayment, toUpperCase
46
What is method overloading?
Defining multiple methods with the same name but different parameter lists
47
How does Java distinguish overloaded methods?
By the method signature (name + parameter list)
48
What makes a method signature unique for overloading?
Different number, types, or order of parameters
49
Can you overload methods with different return types only?
No, the parameter list must be different
50
What is an ambiguous invocation?
When the compiler cannot determine which overloaded method to call
51
When does ambiguous invocation occur?
When two methods can both match the call with type conversions
52
What is an example of ambiguous invocation?
max(1, 2) when you have max(int, double) and max(double, int)
53
What is the scope of a variable?
The part of the program where the variable can be referenced
54
What is a local variable?
A variable defined inside a method
55
What is the scope of a local variable?
From its declaration to the end of the block containing it
56
Can you declare the same variable twice in a method?
No, each variable must be declared only once in its scope
57
Can two different methods have variables with the same name?
Yes, they have different scopes
58
Can you reference a local variable before it's declared?
No, causes compilation error
59
Can you have variables with the same name in nested blocks?
No, causes compilation error
60
What is method abstraction?
Separating the use of a method from its implementation
61
What is information hiding?
Encapsulating implementation details in a method, hidden from the client
62
What is encapsulation?
Hiding implementation details from the client who invokes the method
63
Why is method abstraction useful?
Client can use method without knowing how it's implemented
64
What is the divide-and-conquer strategy?
Decomposing a large program into subproblems (also called stepwise refinement)
65
What is stepwise refinement?
Breaking a problem into smaller, manageable subproblems
66
What is a structure chart?
Visual diagram showing the hierarchical relationship of subproblems
67
What is top-down design?
Breaking problems from top to bottom into smaller subproblems
68
What is a stub?
A simple but incomplete version of a method used during development
69
What is the purpose of stubs?
Enable quick building of program framework while details are being implemented
70
What is the top-down approach?
Implementing methods from top to bottom using stubs for incomplete methods
71
What is the bottom-up approach?
Implementing methods from bottom to top with test programs
72
What is a driver?
A test program written to test a method in bottom-up approach
73
Can top-down and bottom-up approaches be used together?
Yes, both approaches can be combined
74
What is incremental development?
Implementing and testing methods one at a time
75
What are the benefits of incremental development?
Helps isolate errors and makes debugging easier
76
What is the benefit of stepwise refinement for program simplicity?
Breaks long programs into smaller methods, easier to read and understand
77
How does stepwise refinement promote code reuse?
Methods can be defined once and invoked multiple times
78
How does stepwise refinement help with debugging?
Each method can be developed, debugged, and tested individually
79
How does stepwise refinement facilitate teamwork?
Subproblems can be assigned to different programmers
80
In the PrintCalendar example, what is the main problem?
Print the calendar for a given month and year
81
What are the two main subproblems in PrintCalendar?
Read input and print month
82
What are the subproblems of printMonth?
printMonthTitle and printMonthBody
83
What does getMonthName do?
Returns the month name (e.g., 'January') from numeric month (e.g., 1)
84
What does getStartDay do?
Returns which day of the week is the first day of the month
85
What does getNumberOfDaysInMonth do?
Returns how many days are in the month
86
What does getTotalNumberOfDays do?
Computes total days between January 1, 1800 and the first date of calendar month
87
What does isLeapYear do?
Determines if a year is a leap year
88
What is the formula for leap year?
year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
89
What was the start day for January 1, 1800?
Wednesday (3)
90
How do you calculate start day for any month?
(totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7
91
How many days do January, March, May, July, August, October, December have?
31 days
92
How many days do April, June, September, November have?
30 days
93
How many days does February have in regular year?
28 days
94
How many days does February have in leap year?
29 days
95
How many days are in a regular year?
365 days
96
How many days are in a leap year?
366 days
97
What is the purpose of padding space in printMonthBody?
To align the first day of month with correct day of week
98
What does (i + startDay) % 7 == 0 check?
Whether to start a new line (end of week)
99
What does printf('%4d', i) do in calendar printing?
Prints integer in field width of 4, right-aligned
100
Should the PrintCalendar program validate input?
Yes, should check month is 1-12 and year is valid
101
Can PrintCalendar be modified for whole year?
Yes, easily modified to print entire year's calendar
102
What is a predefined method?
A method already defined in Java library (System.out.println, Math.pow)
103
What methods are in the Math class?
pow, sqrt, sin, cos, tan, exp, log, min, max, abs, random, etc.
104
What does Math.random() return?
A random double value between 0.0 and 1.0 (excluding 1.0)
105
How do you generate random int from 0 to 9?
(int)(Math.random() * 10)
106
How do you generate random int from 50 to 99?
50 + (int)(Math.random() * 50)
107
What does Math.pow(2, 3) return?
8.0 (2 raised to power 3)
108
What does Math.abs(-5) return?
5 (absolute value)
109
What does Math.max(5, 10) return?
10 (maximum of two numbers)
110
What does Math.min(5, 10) return?
5 (minimum of two numbers)
111
Can methods have no parameters?
Yes, for example Math.random() has no parameters
112
Are parentheses required when calling parameterless methods?
Yes, you must use () even if no parameters
113
What happens if you forget parentheses on method call?
Compilation error
114
Can a method call another method?
Yes, methods can call other methods
115
Can a method call itself?
Yes, this is called recursion (covered in later chapters)
116
What is the purpose of the return statement's value?
It specifies what value the method returns to its caller
117
What happens when return statement is executed?
The method terminates and returns control to the caller
118
Can you have multiple return statements in a method?
Yes, but only one will execute per method call
119
What is an example of multiple returns?
if-else chains where each branch returns different value
120
Should you have unreachable code after return?
No, causes compilation error
121
What is the modifier public used for?
Makes the method accessible from other classes
122
What is the modifier static used for?
Makes the method belong to the class rather than instances (objects)
123
Why use static modifier in Chapter 6?
Required to call methods from static main method (explained in Chapter 9)
124
Can you modify parameter values inside a method?
Yes, but it doesn't affect the original argument
125
What is method decomposition?
Breaking down a complex method into simpler helper methods
126
What is a helper method?
A method that assists another method by performing a subtask
127
What is code reusability?
Writing code once and using it multiple times
128
How do methods promote code reusability?
Can be called multiple times from different places in program
129
What is the first step in solving problems with methods?
Use method abstraction to isolate details from design
130
When should you start coding details?
After understanding the overall structure and design
131
What is the purpose of a method signature in overloading?
Allows multiple methods with same name but different parameters
132
Can you overload with (int x) and (int y)?
No, parameter names don't matter, only types
133
Can you overload with (int x) and (double x)?
Yes, different parameter types
134
Can you overload with (int x, double y) and (double x, int y)?
Yes, different parameter order
135
What type of error is ambiguous invocation?
Compilation error
136
How do you avoid ambiguous invocation?
Design method signatures carefully to avoid multiple matches
137
What happens during method invocation?
Control transfers to method, parameters receive argument values
138
What happens when method completes?
Control returns to caller at the point after the method call
139
Where does execution continue after method returns?
At the statement immediately following the method call
140
Can main method call other methods in same class?
Yes, very common pattern
141
Can main method call methods in other classes?
Yes, using ClassName.methodName() if methods are static
142
What is the syntax to call a method in another class?
ClassName.methodName(arguments)
143
What is TestMax.max(5, 2)?
Calling max method from TestMax class
144
Is Java case-sensitive for method names?
Yes, max and Max are different methods
145
What is the naming convention for methods?
Use lowercase for first word, capitalize first letter of subsequent words (camelCase)
146
What are examples of good method names?
getArea, computeTax, displayResult, isValid
147
Should method names be verbs?
Yes, methods perform actions so should use verb phrases
148
What should method documentation include?
Purpose of method, description of parameters, description of return value
149
What is the benefit of documenting methods?
Makes code easier to understand and maintain
150
What happens if you pass wrong number of arguments?
Compilation error
151
What happens if you pass wrong type of arguments?
Compilation error if types are incompatible
152
Can you pass expressions as arguments?
Yes, expressions are evaluated first then value is passed
153
What is max(3 + 2, 5 * 2)?
Valid: evaluates to max(5, 10)
154
Can constants be passed as arguments?
Yes, any value can be passed
155
Can variables be passed as arguments?
Yes, the variable's value is passed
156
Can method calls be passed as arguments?
Yes, for example max(min(1,2), 3)
157
What is the evaluation order of nested method calls?
Inner methods are evaluated first
158
In max(min(1,2), sqrt(4)), what order are methods called?
min is called first, then sqrt, then max
159
What is the purpose of breaking problems into methods?
Makes programs more modular, maintainable, and easier to test