Chapter 3 - Using Methods Flashcards
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
B all methods are prewritten saving the programmer work
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 call to another method
All method declarations contain _____________.
a) arguments
b) one or more explicitly named access specifiers
c) parentheses
d) the keyword static
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
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 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.
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)
C public static void displayFacts(int data)
this method will not return data but does accept an integer argument
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)
B public static void computeSum(double a, double b)
this method is accepting two double arguments as parameters labeled as ‘a’ and ‘b’
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.
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.
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’);
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
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.
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
The method with the declaration public static char procedure(double d) has a method type of _____________.
a) public
b) static
c) char
d) double
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.
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.
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)
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.
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
In the method header public static boolean(int age), age is a(n) _____.
a) argument
b) parameter
c) return value
d) final value
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.
The code between a pair of curly braces in a method is a _____________.
a) function
b) brick
c) block
d) sector
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.
When a block exists within another block, the blocks are _____________.
a) structured
b) illegal
c) sheltered
d) nested
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.