Chapter 5 Flashcards

1
Q

What is the purpose of using methods in Java?

A

Methods break a complex problem into manageable pieces (divide and conquer) and allow code reuse, making programs simpler and more organized.

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

What is a void method in Java?

A

A void method performs a task and terminates without returning any value.

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

What is a value-returning method in Java?

A

A value-returning method performs a task and sends a value back to the code that called it.

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

What components make up a method definition in Java?

A

A method definition consists of a header (including method modifiers, return type, method name, and parameters) and a body (the statements executed when the method is called).

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

What does the public method modifier mean in Java?

A

The public modifier makes the method accessible from outside the class.

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

What does the static method modifier mean in Java?

A

The static modifier means the method belongs to the class, not a specific instance of the class.

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

What is the purpose of documentation comments (/** … */) in methods?

A

Documentation comments explain the method’s purpose and can include @param tags to describe parameters.

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

What is the difference between arguments and parameters in Java methods?

A

Arguments are values passed to a method during the call, while parameters are variables that hold the argument values inside the method.

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

What is the significance of widening and narrowing conversions when passing arguments to methods?

A

Widening conversions (e.g., int to double) are handled automatically, but narrowing conversions (e.g., double to int) cause a compiler error unless explicitly cast.

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

How are primitive data types passed to methods in Java?

A

Primitive data types are passed by value, meaning only a copy of the data is passed to the method.

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

How are objects passed to methods in Java?

A

Objects are passed by reference, meaning the reference to the object is passed, but the object itself is not directly modified.

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

What are local variables in Java?

A

Local variables are declared inside a method, are not accessible outside it, and are destroyed when the method ends. They must be initialized before use.

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

How do you validate arguments in Java methods?

A

Use boolean methods to test if arguments meet specific conditions and return true or false.

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

What is functional decomposition in programming?

A

Functional decomposition is the process of breaking a large, complex problem into smaller, manageable pieces (methods).

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

What does the throws clause in a method header signify in Java?

A

The throws clause indicates that the method might throw an exception (e.g., IOException), and the caller must handle or declare it.

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

What is the difference between throws and try-catch blocks in Java?

A

throws declares that a method might throw an exception, while a try-catch block is used to handle exceptions within a method.