Chapter 3 - Using Methods Flashcards

1
Q

Advantages to creating methods in addition to the main() method include all of the following except _____.

a) the main() method is easier to follow

b) all methods are prewritten, saving the programmer work

c) it is easier to determine the overall intent of statements encapsulated in the methods

d) methods are reusable

A

B all methods are prewritten saving the programmer work

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

In Java, methods must include all of the following except _____________.

a) a call to another method

b) a declaration

c) curly braces

d) a body

A

A - a call to another method

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

All method declarations contain _____________.

a) arguments

b) one or more explicitly named access specifiers

c) parentheses

d) the keyword static

A

C parenthesis

the parenthesis is used to hold parameters for the method; arguments are passed to the parameters when the method is called

Example:
public static void main(String[] args){
int age, dob, year;

displayResults(age, dob, year) <— the arguments from the main int declarations are passed to the method displayResults which requires 3 parameters
}

public static int displayResults(int a, int d, int y) <— age, dob, year would be passed to these parameters, respectively, and used within the body of the method

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

A public static method named computeSum() is located in ClassA. To call the method from within ClassB, use the statement _____________.

a) ClassA.computeSum();

b) ClassB(computeSum());

c) ComputeSum(ClassA);

d) You cannot call computeSum() from within ClassB.

A

A ClassA.computeSum();

A complete name that includes the class is a fully qualified identifier . When you use a method within its own class, you do not need to use the fully qualified name (although you can); the simple method name alone is enough. However, if you want to use a method in another class, the compiler does not recognize the method unless you use the full name. You have used similar syntax (including a class name, dot, and method name) when calling the JOptionPane.showMessageDialog() method.

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

Which of the following method declarations is correct for a static method named displayFacts() if the method receives an int argument?

a) public static int displayFacts()

b) public void displayFacts(int data)

c) public static void displayFacts(int data)

d) public void displayFacts(static int)

A

C public static void displayFacts(int data)

this method will not return data but does accept an integer argument

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

Which of the following method declarations is correct for a static method named computeSum() if the method receives two double arguments?

a) public static double, double computeSum()

b) public static void computeSum(double a, double b)

c) public int computeSum(double a, double b)

d) public static int computeSum(double a, b)

A

B public static void computeSum(double a, double b)

this method is accepting two double arguments as parameters labeled as ‘a’ and ‘b’

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

The method with the declaration public static int aMethod(double d) is a method type of _____________.

a) static

b) int

c) double

d) You cannot determine the method type.

A

B int

the type of method is the return value; A return type describes the type of data the method sends back to its calling method. Not all methods return a value to their calling methods; a method that returns no data has a return type of void.

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

Which of the following is a correct call to a method declared as public static void aMethod(char code)?

a) void aMethod();

b) void aMethod(‘V’);

c) aMethod(char ‘M’);

d) aMethod(‘Q’);

A

D aMethod(‘Q’);

the ‘Q’ is the argument that is passed to the method; this argument will become a parameter once the method is called

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

A method is declared as public static void showResults(double d, int i). Which of the following is a correct method call?

a) showResults(double d, int i);

b) showResults(12.2, 67);

c) showResults(4, 99.7);

d) Two of these are correct.

A

B showResults(12.2, 67);

the proper data types have to be passed as arguments to the method call; in this case showResults() requires a double data type and an int data type as arguments

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

The method with the declaration public static char procedure(double d) has a method type of _____________.

a) public

b) static

c) char

d) double

A

C char

the type of method is the return value; A return type describes the type of data the method sends back to its calling method. Not all methods return a value to their calling methods; a method that returns no data has a return type of void.

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

The method public static boolean testValue(int response) returns _____________.

a) no value

b) an int value

c) a boolean value

d) You cannot determine what is returned.

A

C a boolean value

because the parameter is an int (int response) then the method would need a variable declared within the body of the method to return a boolean value (true or false)

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

Which of the following could be the last legally coded line of a method declared as public static int getVal(double sum)?

a) return;

b) return 77;

c) return 2.3;

d) Any of these could be the last coded line of the method.

A

B return 77;

the method declaration public static int getVal(double sum) is declaring a public static method that is returning an int data type; so an integer would need to be returned

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

In the method header public static boolean(int age), age is a(n) _____.

a) argument

b) parameter

c) return value

d) final value

A

B parameter

Some methods require that data items be sent to them when they are called. You have already learned that data items you use in a call to a method are called arguments. When a method receives a data item, it is called a parameter . Methods that receive data are flexible because they can produce different results depending on what data they receive.

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

The code between a pair of curly braces in a method is a _____________.

a) function

b) brick

c) block

d) sector

A

C block

Within any class or method, the code between a pair of curly braces is a block of code, or, more simply, a block. A block can exist entirely within another block or entirely outside and separate from another block, but blocks can never overlap.

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

When a block exists within another block, the blocks are _____________.

a) structured

b) illegal

c) sheltered

d) nested

A

D nested

describes the relationship of statements, blocks, or classes when one contains the other. A block can exist entirely within another block or entirely outside and separate from another block, but blocks can never overlap.

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

The portion of a program within which you can reference a variable is the variable’s _____________.

a) scope

b) space

c) domain

d) range

A

A scope

You cannot refer to a variable outside the block in which it is declared. You have already learned that the portion of a program within which you can refer to a variable is the variable’s scope; in this part of the program, the variable exists and can be accessed using its unqualified name. In Java, a variable comes into existence, or comes into scope , when you declare it, and a variable ceases to exist, or goes out of scope , at the end of the block in which it is declared. Programmers say that a Java variable’s scope level is its block.

17
Q

You can declare variables with the same name multiple times _____________.

a) within a statement

b) within a block

c) within a method

d) You never can declare multiple variables with the same name.

A

C within a method

a variable can have the same name as long as it is not in the same scope; you can declare a variable named empID in the main() method and then declare another variable named empID in a separate method for example getEmpID()

18
Q

Nonambiguous, overloaded methods must have the same _____________.

a) types of parameters

b) number of parameters

c) parameter names

d) name

A

D name

Overloading a method allows you to use one identifier to execute diverse tasks. In Java, it more specifically means writing multiple methods in the same scope that have the same name but different parameter lists.

19
Q

If a method is written to receive a double parameter, and you pass an int to the method, then the method will _____________.

a) work correctly; the int will be promoted to a double

b) work correctly; the parameter type will automatically become an int

c) execute but issue a warning

d) not work; an error message will be issued

A

A work correctly; the int will be promoted to a double

as long as the data type that is passed to the method can be promoted then there will not be an issue; an issue would arise if the method is supposed to receive an int and you pass a double

20
Q

If a method is written to receive an int parameter, and you pass a double to the method, then the method will _____________.

a) work correctly; the double will be promoted to an int

b) work correctly, but the double might lose some data

c) execute but issue a warning

d) not work; an error message will be issued

A

D not work; an error message will be issued