Introduction To Java Programming Comprehensive Version 10th Edition Liang Test Bank Download

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

chapter6.

txt
Introduction to Java Programming
Comprehensive Version 10th Edition Liang
Test Bank
Full download at link:

Test Bank: https://testbankpack.com/p/test-bank-for-introduction-to-


java-programming-comprehensive-version-10th-edition-liang-
0133761312-9780133761313/

Solution Manual: https://testbankpack.com/p/solution-manual-


for-introduction-to-java-programming-comprehensive-version-
10th-edition-liang-0133761312-9780133761313/
Chapter 6 Methods
Sections 6.2 Defining a Method
1. Suppose your method does not return any value, which of the following keywords
can be used as a return type?
a. void
b. int
c. double
d. public
e. None of the above
Key:a

#
2. 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
Key:b

#
3. All Java applications must have a method .
a. public static Main(String[] args)
b. public static Main(String args[])
c. public static void main(String[] args)
d. public void main(String[] args)
Page 1
chapter6.txt
e. public static main(String[] args)
Key:c

#
Sections 6.3 Calling a Method
4. Arguments to methods always appear within _.
a. brackets
b. parentheses
c. curly braces
d. quotation marks
Key:b

#
5. 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. Yes

Page 2
chapter6.txt
b. No
Key:b It is rare, but sometimes useful to have a return statement for circumventing
the normal flow of control in a void method.

#
6. Does the method call in the following method cause compile errors?

public static void main(String[] args) {


Math.pow(2, 4);
}
a. Yes
b. No
Key:b A value-returning method can also be invoked as a statement in Java. In this
case, the caller simply ignores the return value. This is rare, but permissible if
the caller is not interested in the return value.

#
7. 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 heap
b. storage area
c. a stack
d. an array
key:c

#
Sections 6.4 void Method Example
8. Which of the following should be defined as a void method?
a. Write a method that prints integers from 1 to 100.
b. Write a method that returns a random integer from 1 to 100.
c. Write a method that checks whether current second is an integer from 1 to 100.
d. Write a method that converts an uppercase letter to lowercase.
key:a

#
8. 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) {

Page 3
chapter6.txt
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. int
b. double
c. boolean
d. char
e. void
key:e

#
8. 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';
}
}

Page 4
chapter6.txt

a. int
b. double
c. boolean
d. char
e. void
key:d

#
Sections 6.5 Passing Parameters by Values
9. When you invoke a method with a parameter, the value of the argument is passed to
the parameter. This is referred to as .
a. method invocation
b. pass by value
c. pass by reference
d. pass by name
key:b

#
10. Given the following method

static void nPrint(String message, int n) {


while (n > 0) {
System.out.print(message);
n--;
}
}

What is the printout of the call nPrint('a', 4)?


a. aaaaa
b. aaaa
c. aaa
d. invalid call
Key:d Invalid call because char 'a' cannot be passed to string message

#
11. Given the following method

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;

Page 5
chapter6.txt
nPrint("A message", k);
a. 0
b. 1
c. 2
d. 3
Key:c

#
Section 6.8 Overloading Methods
12. 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.
b. The program displays long, 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.
Key:a

#
13. 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");

Page 6
chapter6.txt
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.
Key:b

#
14. 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.
Key:b This is known as ambiguous method invocation.

#
15. Analyze the following code.

Page 7
chapter6.txt
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.
b. The program has a compile error because the second m method is defined, but not
invoked in the main method.
c. The program runs and prints 2 once.
d. The program runs and prints 2 twice.
Key:a You cannot override the methods based on the type returned.

#
Section 6.9 The Scope of Variables
16. A variable defined inside a method is referred to as .
a. a global variable
b. a method variable
c. a block variable
d. a local variable
key:d

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

a. 0
b. 1
c. 2
d. k is not defined outside the block. So, the program has a compile error
Key:d k is defined inside the block. Outside the block, k is not defined.

#
Section 6.10 Case Study: Generating Random Characters
25. (int)(Math.random() * (65535 + 1)) returns a random number .
a. between 1 and 65536

Page 8
chapter6.txt
b. between 1 and 65535
c. between 0 and 65535
d. between 0 and 65536
key:c

#
26. (int)('a' + Math.random() * ('z' - 'a' + 1)) returns a random number _.
a. between 0 and (int)'z'
b. between (int)'a' and (int)'z'
c. between 'a' and 'z'
d. between 'a' and 'y'
key:b

#
27. (char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character
_.
a. between 'a' and 'z'
b. between 'a' and 'y'
c. between 'b' and 'z'
d. between 'b' and 'y'
key:a

#
28. Which of the following is the best for generating random integer 0 or 1?
a. (int)Math.random()
b. (int)Math.random() + 1
c. (int)(Math.random() + 0.5)
d. (int)(Math.random() + 0.2)
e. (int)(Math.random() + 0.8)
key:c

#
Section 6.11 Method Abstraction and Stepwise Refinement
18. 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 .
a. information hiding
b. encapsulation
c. method hiding
d. simplifying method
key:ab
#
29. is to implement one method in the structure chart at a time from the

top to the bottom.


a. Bottom-up approach
b. Top-down approach
c. Bottom-up and top-down approach

Page 9
chapter6.txt
d. Stepwise refinement
key:b
#
30. is a simple but incomplete version of a method.
a. A stub

b. A main method
c. A non-main method
d. A method developed using top-down approach
key:a

#
1. A method can be declared with no parameters.
a. true
b. false
Key:a

#
2. You can define two methods in the same class with the same name and
parameter list.
a. true
b. false
Key:b

#
3. A method can be defined inside a method in Java.
a. true
b. false
Key:b

#
4. The modifiers and return value type do not contribute toward distinguishing one
method from another, only the parameter list.
a. true
b. false
Key:a

#
5. 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.
a. true
b. false
Key:a

#
6. You must have a return statement in a non-void method.
a. true

Page 10
chapter6.txt
b. false
Key:a

#
8. You may have a return statement in a void method.
a. true
b. false
Key:a

#
9. The actual parameters of a method must match the formal parameters in type,
order, and number.
a. true
b. false
Key:a

#
11. You can redeclare the variable if it is declared in the method signature.
a. true
b. false
Key:b If a variable is declared as the formal parameter in the method, it's scope is
the entire method. Therefore, it cannot be redeclared inside the method.

#
12. The signature of a method consists of the method name, parameter list and
return type.
a. true
b. false
Key:b The signature of a method consists of the method name, parameter list, but
not the return type. A class cannot have the methods with identical signature.
#
1. A variable that is declared inside a method is called variable.
a. a static
b. an instance
c. a local
d. a global
e. a class
Key:c

#
2. Each Java class must contain a main method.
a. true
b. false
Key:b

#
4. Analyze the following code:

Page 11
chapter6.txt

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;
}
}

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.
Key:d

#
5. You may have a return statement in a void method.
a. true
b. false
Key:a

#
10. Which of the following is not an advantage of using methods.
a. Using methods makes programs run faster.
b. Using methods makes reusing code easier.
c. Using methods makes programs easier to read.
d. Using methods hides detailed implementation from the clients.
Key:a

#
15. If a method does not contain any parameters, you must invoke it with a pair of
empty parentheses.
a. true
b. false
Key:a
#
18. A method does not return a value.

a. void
b. non-void
Page 12
chapter6.txt
Key:a

#
19. Variables defined inside a method are called _.
a. parameters
b. arguments
c. local variables
d. global variables
Key:c

#
20. Variables defined in the method header are called .
a. parameters
b. arguments
c. local variables
d. global variables
Key:a

#
21. A variable or a value listed in a call to a method is called
a. a parameter
b. an argument
c. a local variable
d. a global variable
Key:b

#
22. Java allows you to declare methods with the same name in a class. This is called
_.
a. method duplication
b. method overriding
c. method overloading
d. method redeclaration
Key:c

#
23. A return statement without any value can be used in .
a. a non-void method
b. void method
Key:b

#
24. Methods can be declared in any order in a class.
a. true
b. false
Key:a

Page 13

You might also like