Methods Test Flashcards

(27 cards)

1
Q

What are methods?

A
  • A method is a collection of statements that performs a specific task.
  • Methods are commonly used to break a problem into small, manageable pieces.
  • Instead of writing one long method that contains all of the statements necessary to solve a problem, several small methods that each solve a specific part of the problem can be written.
  • These small methods can then be executed in the desired order to solve the problem.
  • This approach is sometimes called “divide and conquer” because a large problem is divided into several smaller problems that are easily solved.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the parts of a method header:

public static void displayMessage()

A

public static: Method Modifiers
void: Return Type
displayMessage: Method Name

New method has to be in the class definition NOT the main method – has a method header, parentheses beside method name and its own open and close curly braces to encapsulate code

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

What does displayMessage(); mean?

A

The JVM branches to the “displayMessage()” method and performs the statement in its body.

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

Where can method call statements be used?

A

Method call statements may be used in control structures like loops, if statements, and switch statements.

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

How else can methods be called?

A

Methods can also be called in a hierarchical, or layered fashion. In other words, method A can call method B, which can then call method C. When method C finishes, the JVM returns to method B. When method B finishes, the JVM returns to method A.

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

What is an argument?

A
  • Values that are sent into a method are called “arguments”. We are already familiar with how to use arguments in a method call. For example, look at the following statement:
      System.out.println("Hello");

This statement calls the System.out.println method and passes “Hello” as an argument. Here is another example:

    number = Integer.parseInt(str);

This statement calls the Integer.parselnt method and passes the contents of the str variable as an argument. By using “parameter variables”, you can design your own methods that accept data this way. See the displayValue
method.

    displayValue(5);

This line is an example of a call to the displayValue method, passing 5 as an argument

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

What else can you pass as the contents in the argument?

A

You may also pass the contents of variables and the values of expressions as arguments, For example, the following statements call the displayValue method with various arguments passed:
// int x = 10;
// displayValue(x);
// displayValue(x * 4);
// displayValue(Integer.parseInt(“700”));

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

What is a parameter variable?

A

A “parameter variable”, sometimes simply referred to as a parameter, is a special variable that holds a value being passed into a method.

public static void displayValue(int num)
{
System.out.println(“The value is “ + num);

Notice the integer variable declaration that appears inside the parentheses (int num).This is the declaration of a “parameter variable”, which enables the displayValue method to accept an integer value as an argument.

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

What does Java do if the argument’s data type is ranked lower than the parameter varaible’s data type?

A

Java will automatically perform a widening conversion if the argument’s data type is ranked lower than the parameter variable’s data type.

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

What do you do to convert an argument to a lower-ranking data type?

A

Java will not automatically convert an argument to a lower-ranking data type. This means that a long, float, or double value cannot be passed to a method that has an int parameter variable. You can use a cast operator to convert a value manually to a lower-ranking data type.

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

How to pass more than one agrument to a method?

A

double value1 = 2.5;
double value2 = 3.5;
showSum(value1, value2);

When the showSum methods executes as a result of this code, the num parameter will contain 2.5 and the num2 parameter will contain 3.5.

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

What is a parameter list?

A

public static void showSum(double num1, double num2)
{
double sum; // To hold the sum
sum = num1 + num2;
System.out.println(“The sum is “ + sum);

Notice that two parameter variables, num1 and num2 are declared inside the parentheses in the method header. This is often referred to as a “parameter list”. Also notice that a comma separates the declarations. See the “showSum” call statement in the main method.

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

What happens when a string is passed as an argument?

A
  • When an object, such as a String, is passed as an argument, it is actually a reference to the object that is passed.
  • In this example code, the name variable is a string reference variable.
  • It is passed as an argument to the changeName method.
  • The changeName method has a parameter variable, str, which is also a String reference variable, that receives the argument.
  • Recall that a reference variable holds the memory address of an object. When the changeName method is called, the address that is stored in name is passed into the str parameter variable.
  • This means that when the changeName method is executing, both name and str reference the same object.
  • Strinq objects in java are immutable, which means that they cannot be changed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a local variable?

A
  • A local variable is declared inside a method and is not accessible to statements outside the method.
  • Different methods can have local variables with the same names because the methods cannot see each other’s local variables.
  • Local variables: whatever variable to declare in a method’s block of code it is local to the method – can’t be accessed by other methods or the main method
  • Two methods with the same name but in different methods ARE NOT the same variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does this code mean?

public static void main(String[] args)
{
texas();
california();
}

public static void texas()
{
    int birds = 5000;
    System.out.println("In texas there are " + birds + " birds.");
}

public static void california()
{
    int birds = 3500;
    System.out.println("In California there are " + birds + " birds.");
}
A
  • Although there are two local variables named birds, the program can see only one of them at a time because they are in different methods.
  • When the texas method is executing, the birds variable declared inside texas is visible.
  • When the California method is executing, the birds variable declared inside California is visible.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does a method send a value back to the statement that called the method?

A
  • A method may send a value back to the statement that called the method.
  • You’ve seen that data may be passed into a method by way of parameter variables. Data may also be returned from a method, back to the statement that called it.
  • Methods that return a value are appropriately known as “value-returning methods”.
17
Q

What does this line mean?

total = sum(value1, value2);

A
  • This statement calls the sum method, passing value1 and value2 as arguments. It assigns the value returned in the “sum” method to the “total” variable. In this case, the method will return 60.
18
Q

What does this mean?

return result;

A

This line return the value in the result variable back to the method that called it.

19
Q

What is this:

int x = 10, y= 15; double average; average = sum(x, y) / 2.0;

A
  • In this statement, the sum method is called with x and y as its arguments.
  • The method’s return value, which is 25, is divided by 2.0. The result, 12.5, is assigned to average. Here is another example:
  • int x = 10, y = 15; System.out.println(“The sum is “ + sum(x, y)); This code sends the sum method’s return value to System.out.println, so it can be displayed on the screen.The message “The sum is 25” will be displayed.
20
Q

What is this line of code?

customerName = fullName(“John”, “Martin”);

A

This line calls the fullName method, passing “John” and “Martin” as arguments. The method returns a reference to a String object containing “John Martin”. The reference is assigned to the customerName variable.

21
Q

What is a method signature?

A

Method signature – void + method name

22
Q

What is a method header?

A

Method header – line before opening and closing curly braces

23
Q

What is public?

A

Public – all files have access to it – access specifier (if you use private – only a given file has )
Public static is not a class method

24
Q

What is void?

A

Void – non value returning method – runs block of code without returning anything

25
What is a method call?
Method call -- the statement to call the method in main
26
What are global variables?
public static int x = 5; In the class definition -- this is how you create a global variable -- all methods have access to the variable and can change/update it
27
What is the return method?
Return method -- no void -- if you use void, you can’t use the return statement The return method format: public static [return type] methodName (parameter variables) { [data type] variable1 = calculation involving the parameter variables; return variable1; } public static void main(String[] argos) { [data type matching return type] variable2 = methodName call(value for parameter variable1, value for parameter variable2); System.out.println(variable2); }