Chapter 5 - 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

What is stepwise refinement?

A

The act of breaking large problems into smaller ones, and the smaller into even smaller ones. Then write methods to solve each of the smaller problems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
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
8
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
9
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
10
Q

The signature of a method consists of __

A. method name
B. method name and parameter list
C. return type, method name, and parameter list
D. parameter list

A

B. method name and parameter list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
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
12
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
13
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