1
Name:_______________________
Chapter 6 on Methods
CSCI 1301 Introduction to Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
Solution:
Part I.
a. (3 pts) Complete the code:
public class Test {
public static void main(String[] args) {
int i = 4; int j = 5;
b. (3 pts) Complete the code:
public class Test {
public static void main(String[] args) {
int i = 4; int j = 5;
}
Part II. (7 pts) Show the printout of the following code:
a.
4
5
b.
c.
1
2 1
2
Part III: (8 pts):
a.
public class Test {
public static void main(String[] args) {
}
b.
public static void displayNumber(int n) {
for (int i = 1; i <= n; i++)
System.out.print(i + (i % 7 == 0 ? “\n” : ” “));
}
c.
public static int getUppsercaseCount(String s) {
Part IV:
1 What is Math.rint(3.5)?
A. 3
B. 5.0
C. 4
D. 3.0
E. 4.0
#
3
2 Suppose
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. invalid call, because ‘a’ is a character, not a string.
D. aaa
#
3 (char)(‘a’ + Math.random() * (‘z’ – ‘a’ + 1)) returns a random character
__________.
A. between ‘a’ and ‘y’
B. between ‘b’ and ‘z’
C. between ‘b’ and ‘y’
D. between ‘a’ and ‘z’
#
4 The signature of a method consists of ____________.
A. method name and parameter list
B. return type, method name, and parameter list
C. parameter list
D. method name
#
5. 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;
}
}
a. The program displays int followed by 5.
b. The program displays long followed by 5.
4
c. The program runs fine but displays things other than 5.
d. The program does not compile.
#
6 A variable defined inside a method is referred to as __________.
A. a local variable
B. a block variable
C. a global variable
D. a method variable
#
7 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 name
B. pass by value
C. pass by reference
D. method invocation
#
8 Which of the following should be declared as a void method?
A. Write a method that returns a random integer from 1 to 100.
B. Write a method that prints integers from 1 to 100.
C. Write a method that checks whether current second is an integer from 1 to
60.
D. Write a method that converts an uppercase letter to lowercase.
#
9 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. storage area
B. a heap
C. a stack
D. an array
#
10. What is the output of the following code?
public class Test {
public static void main(String[] args) {
System.out.println(m(“ABCDEFG”));
}
public static String m(String s) {
return s.substring(0, 3) + s.substring(5);
}
}
a. ABCEG
b. ABCFG
c. ABCEFG
5
d. ABFG
#
11. Given the following method:
public static double m(double x) {
x++;
return 2 * x;
}
What is the output of the following code?
int x = 1;
System.out.println(x + ” ” + m(x));
a. 1 2
b. 2 2
c. 1 4
d. 2 4