Chapter 6 Flashcards

1
Q

Arguments to methods always appear within __________.

A

parentheses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
You should fill in the blank in the following code with \_\_\_\_\_\_\_\_\_\_\_\_\_\_.
public class Test {
     public static void main(String[] args) {
          System.out.print("The grade is ");
          printGrade(78.5);
          System.out.print("The grade is ");
          printGrade(59.5);
     }
     public static \_\_\_\_\_\_\_\_\_\_ printGrade(double score) {
          if (score >= 90.0) {
          System.out.println('A');
          }
         else if (score >= 80.0) {
              System.out.println('B');
          }
         else if (score >= 70.0) {
              System.out.println('C');
         }
         else if (score >= 60.0) {
              System.out.println('D');
         }
         else {
              System.out.println('F');
         }
     }
}
A

void

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
Consider the following incomplete code:
public class Test {
     public static void main(String[] args) {
          System.out.println(f(5));
     }
     public static int f(int number) {
          // Missing body
     }
}
The missing method body should be \_\_\_\_\_\_\_\_.
A

return number;

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

Which of the following should be defined as a void method?

A

Write a method that prints integers from 1 to 100.

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

Which of the following is the best for generating random integer 0 or 1?

A

(int)(Math.random() + 0.5)

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

Suppose your method does not return any value, which of the following keywords can be used as a return type?

A

void

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

_________ is a simple but incomplete version of a method.

A

stub

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
Analyze the following code:
public class Test {
     public static void main(String[] args) {
          System.out.println(xMethod(5, 500L));
     }
     public static int xMethod(int n, long l) {
          System.out.println("int, long");
          return n;
     }
     public static long xMethod(long n, long l) {
          System.out.println("long, long");
          return n;
     }
}
A

The program displays int, long followed by 5.

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

(int)(Math.random() * (65535 + 1)) returns a random number __________.

A

between 0 and 65535

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

Does the method call in the following method cause compile errors?
public static void main(String[] args) {
Math.pow(2, 4);
}

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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
13
Q
You should fill in the blank in the following code with \_\_\_\_\_\_\_\_\_\_\_\_\_\_.
public class Test {
     public static void main(String[] args) {
          System.out.print("The grade is " + getGrade(78.5));
          System.out.print("\nThe grade is " + getGrade(59.5));
     }
     public static \_\_\_\_\_\_\_\_\_ getGrade(double score) {
          if (score >= 90.0)
               return 'A';
          else if (score >= 80.0)
               return 'B';
          else if (score >= 70.0)
               return 'C';
          else if (score >= 60.0)
               return 'D';
          else
               return 'F';
     }
}
A

char

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

All Java applications must have a method __________.

A

public static void main(String[] args)

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

The signature of a method consists of ____________.

A

method name and parameter list

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

When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

A

pass by value

17
Q
What is k after the following block executes?
{
     int k = 2;
     nPrint("A message", k);
}
System.out.println(k);
A

k is not defined outside the block. So, the program has a compile error

18
Q

__________ is to implement one method in the structure chart at a time from the top to the bottom.

A

Top-down approach