Methods Flashcards

1
Q

What are methods used for?

A

Methods make it easier to modularize and make code reusable.

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

What values can methods return?

A

Methods can return any value-type, or none at all. If nothing is returned, the returnvaluetype keyword must be “void”

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

What does it mean that a method can be overloaded?

A

It means that two methods can have the same name, as long as their parameter list differ

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

What is method abstraction?

A

The act of putting code into methods. The method can be used without knowing how it is implemented. The implementation details are hidden inside the method. This is known as information hiding

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

Why is method abstraction a good thing?

A

Because it modularizes programs in a neat, hierarchical manner. Programs written as collections of concise methods are easier to write, debug, maintain, and modify than would otherwise be the case

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

The signature of a method consists of __

A

Method name and parameter(s)

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

Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as __, which stores elements in last-in first-out fashion

A

A stack

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

A variable inside a method is referred to as __

A

A local variable

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

To execute a method you need__

A. method name
B. method name and arguments/parameters
C. return type, method name, and arguments/parameters
D. arguments/parameters

A

B. method name and arguments/parameters

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

Does the return statement in the following method cause compile errors?

public static void main(String[] args) {
int max = 0;
if (max != 0)
System.out.println(max);
else
return;
}

A

No.

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

Analyze the following code:

class Test {
public static void main(String[] args) {
System.out.println(xmethod(5));
}

public static int xmethod(int n, long t) {
System.out.println(“int”);
return n;
}

public static long xmethod(long n) {
System.out.println(“long”);
return n;
}
}

A. The program displays int followed by 5.
B. The program displays long followed by 5.
C. The program runs fine but displays things other than 5.
D. The program does not compile because the compiler cannot distinguish which xmethod to invoke.

A

B. The program displays long followed by 5.

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

Analyze the following code.

public class Test {
public static void main(String[] args) {
System.out.println(max(1, 2));
}

public static double max(int num1, double num2) {
System.out.println(“max(int, double) is invoked”);

if (num1 > num2)
  return num1;
else
  return num2;   }

public static double max(double num1, int num2) {
System.out.println(“max(double, int) is invoked”);

if (num1 > num2)
  return num1;
else
  return num2;   } }

A. The program cannot compile because you cannot have the print statement in a non-void method.
B. The program cannot compile because the compiler cannot determine which max method should be invoked.
C. The program runs and prints 2 followed by “max(int, double)” is invoked.
D. The program runs and prints 2 followed by “max(double, int)” is invoked.
E. The program runs and prints “max(int, double) is invoked” followed by 2.

A

B. The program cannot compile because the compiler cannot determine which max method should be invoked.

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

what are the 4 parts required for creating/defining a method?

A

returnType - data type or void
methodName - camel case
methodParams - input data types and names - can be blank
Curly Brackets -

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

what 2 things does a return statement do?

A

returns the value
stops the running of the method

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

do void methods need to return a value?

A

No

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

What are the method naming restrictions?

A

Can contain letters, digits, underscores, but cannot contain whitespace
Cannot start with a digit
Cannot be a Java reserved keyword (i.e.: return)
Are case sensitive (“thisMethod” and “thismethod” are two different methods)

17
Q

what are 2 common naming conventions for methods?

A

The first word is usually a verb or action followed by a noun or adjective
Camel case

18
Q

Java only supports what type of arguments?

A

Java only supports input arguments.

19
Q

When a reference is passed in as an argument to a method is the argument returned when the method ends?

A

No, when a reference is passed in as an argument, a copy is made and the copy is deleted when the method ends.

20
Q

When a reference is passed in as an argument to a method and changes are made to the argument will they be reflected in the original variable? Why?

A

The changes will be reflected in the original variable because the argument points to the original object.

21
Q

What is the @Override annotation and why can it be useful?

A

The @Override annotation indicates that the child class method is over-writing its base class method. It can be useful for two reasons. It extracts a warning from the compiler if the annotated method doesn’t actually override anything. It can improve the readability of the source code.

22
Q

what are the variables defined in a method signature called?

A

Parameters are variables that are defined in the method signature and are used to accept input values from the caller. So the parameters are the variables defined in the method signature, and the arguments are the values passed in when the method is called.

23
Q

what are the values passed into a method called?

A

When the method is called, the actual values that are passed in are called arguments. So the parameters are the variables defined in the method signature, and the arguments are the values passed in when the method is called.

24
Q

Is java a pass by reference or value language?

A

Java is a “pass-by-value” language, which means that when passing arguments to a method, a copy of the argument is passed to the method. However, when the argument is an object or an array, the copy is actually a reference to the original object or array. This means that changes made to the object or array inside the method will affect the original object or array outside the method. In this sense, it may seem like Java is pass-by-reference for objects and arrays, but it’s actually pass-by-value for the reference to the object or array.