1
Name:_______________________
Chapters 7-8
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. (6 pts) Show the output of the following code:
a.
public class Test {
b.
public class Test {
c. Show the output of the following program when invoked
using
java Test I have a dream
2
Part II. (4 pts) Fix the errors in the following code:
a.
public class Test {
b.
public class Test {
3
Part III:
a. (10 pts) Write a method that returns the index of the smallest element in an array of
integers. If the number of such elements is greater than 1, return the smallest index. Use
the following header:
b. (10 pts) Write a method that sums all the numbers in the major diagonal in an
nn
matrix of
double values using the following header:
4
Part IV: Multiple Choice Questions: (1 pts each)
(1. Mark your answers on the sheet. 2. Login and click Take
Instructor Assigned Quiz for Quiz4. 3. Submit it online
within 5 mins. 4. Close the browser.)
1. What will be displayed by the following code?
class Test {
public static void main(String[] args) {
#
2. Show the output of the following code:
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
increase(x);
int[] y = {1, 2, 3, 4, 5};
increase(y[0]);
System.out.println(x[0] + ” ” + y[0]);
}
public static void increase(int[] x) {
for (int i = 0; i < x.length; i++)
x[i]++;
}
public static void increase(int y) {
y++;
}
}
a. 1 2
b. 2 2
c. 0 0
5
d. 2 1
e. 1 1
#
3. Analyze the following code:
public class Test {
public static void main(String[] args) {
int[] x = new int[5];
int i;
for (i = 0; i < x.length; i++)
x[i] = i;
System.out.println(x[i]);
}
}
a. The program has a runtime error because the last statement in the main
method causes ArrayIndexOutOfBoundsException.
b. The program displays 4.
c. The program has a compile error because i is not defined in the last
statement in the main method.
d. The program displays 0 1 2 3 4.
#
4. Assume int[] t = {1, 2, 3, 4}. What is t.length?
a. 0
b. 4
c. 5
d. 3
#
5. 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] + ” “);
}
#
6. Suppose int[] numbers = new int[10], what is numbers[0]?
6
a. 0
b. null
c. undefined.
d. 10.
#
7. What is the output of the following code?
int[] myList = {1, 2, 3, 4, 5, 6};
for (int i = myList.length – 2; i >= 0; i–) {
myList[i + 1] = myList[i];
}
for (int e: myList)
System.out.print(e + ” “);
a. 6 2 3 4 5 1
b. 6 1 2 3 4 5
c. 1 1 2 3 4 5
d. 1 2 3 4 5 6
e. 2 3 4 5 6 1
#
8. Analyze the following code:
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
for (int i = 0; i < x.length; i++)
System.out.print(x[i] + ” “);
}
}
a. The program displays 0 0
b. The program displays 0 0 3 4
c. The program displays 1 2 3 4
d. The program displays 0 0 0 0
#
9. How many elements are array matrix (int[][] matrix = new int[5][5])?
a. 25
b. 20
c. 30
d. 14
7
#
10. Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are
x[0].length, x[1].length, and x[2].length?
a. 2, 3, and 4
b. 3, 3, and 3
#
11. Assume double[][][] x = new double[4][5][6], what are x.length,
x[2].length, and x[0][0].length?
a. 4, 5, and 6
b. 5, 5, and 5
c. 6, 5, and 4
d. 4, 5, and 4
#
12. What will be displayed by the following code?
public class Test {
public static void main(String[] args) {
double[][] m = {{1, 2, 3}, {1.5, 2.5, 3.5}, {0.1, 0.1, 0.1}};
System.out.println(sum(m));
}
public static double sum(double[][] m) {
double sum = 0;
for (int i = 0; i < m.length; i++)
sum += m[i][i];
return sum;
}
}
a. 4
b. 3.0
#
13. Assume double[][] x = new double[4][5], what are x.length and
x[2].length?
a. 4 and 5
b. 5 and 5
c. 5 and 4
d. 4 and 4
#
14. Which of the following statements are correct?
8
a. char[2][] charArray = {{‘a’, ‘b’}, {‘c’, ‘d’}};
#
15. Assume boolean[][] x = new boolean[5][7], what are x.length and
x[2].length?
a. 5 and 6
b. 4 and 5
c. 4 and 4
d. 5 and 5
e. 5 and 7
Key:e
#
16. Suppose a method p has the following heading:
public static int[][] p()
What return statement may be used in p()?
a. return 1;
b. return new int[][]{{1, 2, 3}, {2, 4, 5}};
#
17. Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are
x[0].length?
a. 2 and 3
b. 2 and 2
c. 3 and 3
d. 2 and 1
e. 3 and 2
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? ______________