Chapter 6
Which of the following is the best for generating random integer 0 or 1? (
(int)(Matrandom() + 0.5)
) Suppose static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; }}What is k after invoking nPrint("A message", k)?int k = 2;nPrint("A message", k);
2
A variable that is declared inside a method is called ________ variable.
A local
Analyze the following code. public class Test { public static void main(String[] args) { System.out.println(m(2)); } public static int m(int num) { return num; } public static void m(int num) { System.out.println(num); } }
A) The program has a compile error because the two methods m have the same signature.
A method can be defined inside a method in Java.
False
Each Java class must contain a main method.
False
The signature of a method consists of the method name, parameter list and return type.
False
You can redeclare the variable if it is declared in the method signature.
False
Suppose static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); of the call nPrint('a', 4)? n--; }} What is the printout
Invailed call
Variables defined inside a method are called ________.
Local variables
Java allows you to declare methods with the same name in a class. This is called ________.
Method Overloading
Does the method call in the following method cause compile errors? public static void main (string()args( math.pow(2,4); }
No
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;
No
Variables defined in the method header are called ________.
Parameters
When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as ________.
Pass by value
Analyze the following code: class Test { public static void main(String[] args) { System.out.println(xMethod((double)5)); } public static int xMethod(int n) { System.out.println("int"); return n; } public static long xMethod(long n) { System.out.println("long"); return n;
Program does not compile
________ is a simple but incomplete version of a method.
Stub
________ is to implement one method in the structure chart at a time from the top to the bottom.
Top - down approach
You may have a return statement in a void method.
Top- down approach
A call for the method with a void return type is always a statement itself, but a call for the method with a non-void return type can be treated as either a statement or an expression.
True
A method can be declared with no parameters.
True
If a method does not contain any parameters, you must invoke it with a pair of empty parentheses.
True
Methods can be declared in any order in a class.
True
The modifiers and return value type do not contribute toward distinguishing one method from another, only the parameter list. true
True
You may have a return statement in a void method.
True
You must have a return statement in a non-void method.
True
Which of the following is not an advantage of using methods?
Using methods makes programs run faster.
Suppose your method does not return any value, which of the following keywords can be used as a return type?
Void
A return statement without any value can be used in ________.
Void method
Which of the following should be defined as a void method?
Write a method that prints integers from 1 to 100.
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 stack
(int)('a' + Math.random() * ('z' - 'a' + 1)) returns a random number ________.
between (int)'a' and (int)'z'
(int)(Math.random() * (65535 + 1)) returns a random number ________.
between 0 and 65535
(char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character ________.
between A and Z
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'; } }
char?
You can define two methods in the same class with the same name and parameter list.
false
The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as ________.
information hiding encapsulation
What is k after the following block executes? { int k = 2; nPrint("A message", k); } System.out.println(k);
k is not defined outside the block. So, the program has a compile error
The signature of a method consists of ________.
method name and parameter list
Arguments to methods always appear within ________.
parthenthiesis
All Java applications must have a method ________.
public static void main(String[] args)