Method Flashcards

1
Q

What is the purpose of a method in programming?

A

A method is a block of code that performs a specific action when called. Methods improve code readability, allow for code reuse, and help break down complex programs into manageable sections.

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

What is a call stack, and why is it important?

A

A call stack manages method execution and variable scope. Each method operates within its own scope, and the call stack ensures that the flow of execution is correctly handled. In a multithreaded environment, each thread has its own call stack, enabling concurrent execution of methods.

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

What are the key components of a method?

A

Method Name: A verb or verb phrase describing what the method does.

Method Body: The code that implements the method’s functionality, enclosed in curly braces {}.

Return Type: Specifies the type of value returned by the method (e.g., void, int, String).

Return Statement: Used to exit a method and return a value to the caller.

Parameters: Variables that are passed into the method to perform actions on.

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

What are the different types of access modifiers in methods?

A

public: The method is accessible from any class.

private: The method is only accessible within the class it is declared in.

protected: The method is accessible within the same package and from subclasses in other packages.

default (no keyword): The method is accessible only within the same package.

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

What is the difference between passing primitive types and objects as parameters in Java?

A

Primitive types: The actual value is passed to the method. Changes made to the parameter do not affect the original variable.

Objects: The reference (memory address) to the object is passed, meaning changes to the object’s state inside the method will affect the original object outside the method.

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

What is variable scope, and why is it important?

A

Variable scope defines the visibility and lifetime of a variable. It ensures that variables are only accessible in the parts of code where they are needed, helping to write cleaner and error-free code.

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

What is the difference between void and non-void methods?

A

Void method: Does not return any value. It performs an action (e.g., printing a message).

Non-void method: Returns a value of the specified return type (e.g., an int representing the sum of two numbers).

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

What is method overloading in Java?

A

Method overloading allows multiple methods with the same name but different signatures. This can vary based on the number or type of parameters. Overloading is a form of compile-time polymorphism.

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

What is recursion, and what is its importance?

A

Recursion is when a method calls itself to solve smaller subproblems. It is useful for problems that can be divided into smaller, similar problems. A base case is essential to prevent infinite recursion and potential stack overflow errors.

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

What are the key principles for writing clean, effective methods?

A

DRY (Don’t Repeat Yourself): Avoid redundant code by reusing existing methods.

Single Responsibility: Each method should solve one specific problem.

Break down larger functions: Split big methods into smaller, more manageable ones.

Use descriptive method names: Choose names that clearly describe the method’s purpose.

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

What is the significance of using a return statement in a method?

A

The return statement is used to exit a method and send back a value to the caller. The type of value returned must match the method’s declared return type.

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