1
Name:_______________________
Chapters 6, 7, and 8
75 mins
CSCI 1301 Introduction to Programming
Georgia Southern 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 _____________________
Part I.
a. (3 pts) The following code performs a binary search. Fill the missing
code:
public class BinarySearch {
}
}
b. (3 pts) Declare, create, and initialize a three-dimensional array
of doubles, x, that has 3 rows, each of which has 2 columns where each
column is an array of 4 ints.
2
Part II. Show the output of the following code:
a. (2 pts)
public class TestArrayArguments {
public static void main(String[] args) {
b. (2 pts)
What is the output of the following code?
1 public class Test {
c. (3 pts)
Show the output of the following code:
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:
}
public static double m(int i) {
// Fill in the code here
}
}
4
b. (5 pts) Write a method that will return the sum of all numbers in
an array. The method header is as follows:
}
c. (8 pts) Write a method that returns the minimum number of in a two
dimensional array 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 Analyze the following code.
public class Test {
public static void main(String[] args) {
int[] x = new int[3];
System.out.println(“x[0] is ” + x[0]);
}
#
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 Analyze the following code:
public class Test {
public static void main(String[] args) {
double[] x = {2.5, 3, 4};
for (double value: x)
System.out.print(value + ” “);
}
}
a. The program displays 2.5, 3, 4
b. The program displays 2.5 3 4
c. The program displays 2.5 3.0 4.0
d. The program displays 2.5, 3.0 4.0
6
e. The program has a syntax error because value is undefined.
#
4 What is output of the following code:
#
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.
c. The program runs fine but displays things other than 5.
d. The program does not compile.
#
6 In the following code, what is the output for list2?
public class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
7
for (int i = 0; i < list2.length; i++)
System.out.print(list2[i] + ” “);
}
}
a. 1 2 3
b. 1 1 1
c. 0 1 2
d. 0 1 3
#
7 Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are
x[0].length?
#
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 What is the output of the following code?
public class Test {
public static void main(String[] args) {
int[][] matrix =
{{1, 2, 3, 4},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15}};
for (int i = 0; i < 4; i++)
System.out.print(matrix[i][1] + ” “);
}
}
a. 1 2 3 4
b. 4 5 6 7
c. 1 3 8 12
d. 2 5 9 13
e. 3 6 10 14
#
10. What is the output of the following code?
public class Test {
public static void main(String[] args) {
System.out.println(m(“ABCDEFG”));
8
}
public static String m(String s) {
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? ______________