Methods Flashcards

1
Q

What is not an acceptable method declaration?

A

public static void doWork(int number, void thing) {

}

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

A user calls a method with the following syntax:

doSomething(3,”Hello”);
What could represent the Java method signature for the method they are calling?

A
public static void doSomething(int number, String text) {
//some code
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What lines of code could be added to the following method to make it semantically and syntactically correct?

public static double someMethod(){

int a = 5;
double b = 5.0;
String c = "5";
char d = '5';

}

A

return b

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

The code below shows a method declaration and a call to that method.

public static int doSomething(int number){
return “’“+number+”’”;
}

String x = doSomething(5);

There is an error. What single change could resolve the error?

A

Change the method’s return type.

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

True or False?
The type of a variable passed into a method must match the type of the corresponding parameter in the method declaration.

A

True

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

True or False?
The name of a variable passed into a method must match the name of the corresponding parameter in the method declaration.

A

False

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

Can a method omit a return statement?

A

Yes, when the method’s return type is void

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

What is the correct type for a method that does not return a value?

A

Void

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