Methods Flashcards

1
Q

What is the difference between a method and a function? Is there one?

A

A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.

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

How do I designate a method’s inputs?

A

Put them in the parameters.

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

How do I designate a method’s outputs?

A

You just put them in the method signature instead of the word void.

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

Where can a method definition go in your source code? Can definitions be nested?

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

Where can a method call go in your source code? Can calls be nested?

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

How many things can a method return? (How many types?)

A

You can return only one value in Java. If needed you can return multiple values using array or an object. Methods can only return one type.

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

How do I return several things from a method?

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

What if I have nothing to return?

A

void keyword

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

How many return statements are allowed in a method?

A

You can have several, but it’s best to keep the code clean and clear.

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

What is the ideal length for a method?

A

5-10 lines

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

Can a method call itself?

A

Yes, when a method calls itself, it is called recursion.

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

What is the difference between a static and non-static method? Give an example of each.

A

Non-static methods have access to fields. Static methods don’t.

One way to think about it is that static methods are just independent methods with no context. They’re defined in a class, but they’re not part of an individual object.

Non-static methods can’t be executed independently. They’re part of an object. They have access to an object’s state.

Non-static methods are executed using an instance of an object. Static methods are executed using a class name.

Static method ex:
String.format
String.format(“Numbers: %s, %s, %s”, 1, 2, 3);

Non-static method ex: toUpperCase
String vegetable = “kale”;
vegetable.toUpperCase();

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

How do I indicate a method is non-static?

A

Omit the word static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Is this valid syntax?:
void doNothing() {
    return;
}
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is method overriding?

A

In addition to inheriting parent members, subclasses can completely replace a parent method. This is called overriding a method. To override, the method is re-implemented in the subclass

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

What is method overloading and when is it useful?

A

If methods have the same name, but different signatures, they are said to be overloaded.

Use it when you actually do need multiple methods with different parameters, but the methods do the same thing.

17
Q

Write a method that takes two integers and returns their average as an integer, rounded down.

Change it to return their average rounded up.

A
18
Q

Is it possible to write a method that accepts a variable number of parameters without method overloading?

A