1
Name:_______________________
Chapters 6-8
CSCI 1301 Introduction to Programming
Georgia Southern University
Instructor: Y. Daniel Liang
Solution:
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.
The elements in the first row are all 2.5, in the third row are all 7.1,
Part II. Show the output of the following code:
2
a. (2 pts)
public class TestArrayArguments {
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:
Part III: (8 pts):
a.
public class Test {
public static void main(String[] args) {
3
b.
public static double sum(int[] list) {
c.
public static int getMin(int[][] m) {
// Write your code here
4
}
Part IV:
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]);
}
}
a. The program has a compile error because the size of the array wasn’t
specified when declaring the array.
b. The program has a runtime error because the array elements are not
initialized.
c. The program runs fine and displays x[0] is 0.
d. The program has a runtime error because the array element x[0] is not
defined.
#
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
5
e. The program has a syntax error because value is undefined.
#
4 What is output of the following code:
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i – 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + ” “);
}
}
a. 1 2 3 4 5 6
b. 2 3 4 5 6 6
c. 2 3 4 5 6 1
d. 1 1 1 1 1 1
#
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};
6
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
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?
a. 2 and 1
b. 2 and 2
c. 3 and 2
d. 2 and 3
e. 3 and 3
#
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
7
#
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
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