1
Name:_______________________
Chapter 6 on Methods
50 mins
CSCI 1301 Introduction to Programming
Armstrong Atlantic State University
Instructor: Dr. Y. Daniel Liang
I pledge by honor that I will not discuss this exam with anyone until my
instructor reviews the exam in the class.
Signed by ___________________ Date _____________________
(50 minutes)
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. Show the output of the following code:
a. (1 pts)
public class Test {
public static void main(String[] args) {
System.out.println(m(1, 2));
System.out.println(m(2, 1));
}
2
}
c. (3 pts)
public class Test {
public static void main(String[] args) {
int i = 0;
while (i <= 4) {
method1(i);
i++;
}
3
Part III:
a. (8 pts) Write a method to compute the following sum:
12
5
2
3
1
)( +
+++= i
i
im
Write a test program that displays the following table:
i m(i)
1 0.3333
}
public static double m(int i) {
// Fill in the code here
}
}
4
b. (5 pts) Write a method that will display numbers from 1 to n, 7
numbers per line. The numbers are separated by one space. The
method header is as follows:
c. (8 pts) Write a method that returns the number of uppercase letters
in a string s using the following header:
5
Part IV: Multiple Choice Questions: (1 pts each)
(1. Mark your answers on the sheet. 2. Login and click Take
Instructor Assigned Quiz for Quiz3. 3. Submit it online
within 5 mins. 4. Close the browser.)
1 What is Math.rint(3.5)?
A. 3
B. 5.0
C. 4
D. 3.0
E. 4.0
#
2 Suppose
#
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:
6
return n;
}
public static long xMethod(long n) {
System.out.println(“long”);
return n;
}
}
#
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.
#
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) {
7
return s.substring(0, 3) + s.substring(5);
}
#
11. Given the following method:
public static double m(double x) {
x++;
return 2 * x;
}
What is the output of the following code?
Please double check your answer before clicking the Submit
button. Whatever submitted to LiveLab is FINAL and counted
for your grade.
Have you submitted your answer to LiveLib? ______________
What is your score? ______________