Sample Exam 1
Sample Exam 1
Sample Exam 1
Here is a mapping of the final comprehensive exam against the course outcomes:
1
Name:__________________ CSCI 1301 Final
Armstrong Atlantic State University
Covers chs 1-8 Instructor: Dr. Y. Daniel Liang
Please note that the university policy prohibits giving the exam score by email. If you need to know your
final exam score, come to see me during my office hours next semester.
I pledge by honor that I will not discuss the contents of this exam with
anyone.
i++;
}
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
a: (2 pts)
2
}
}
}
b. (3 pts)
Suppose the input is 2 3 4 5 0. What is the output of the
following code?
import java.util.Scanner;
while (number != 0) {
number = input.nextInt();
if (number > max)
max = number;
}
c. (3 pts)
public class Test {
public static void main(String[] args) {
System.out.println(xMethod(5672));
}
return result;
}
}
d. (3 pt)
public class Test {
public static void main(String[] args) {
int number = 0;
int[] numbers = new int[1];
3
m(number, numbers);
System.out.print(ttt(m));
}
return sum;
}
}
4
Part III: (Write programs)
5
2. (15 pts) Write a method to display a pattern as follows:
1
4 1
9 4 1
16 9 4 1
25 16 9 4 1
...
n*n ... 36 25 16 9 4 1
import java.util.Scanner;
displayPattern(n);
}
}
}
6
3. (15 pts) Write the following method that returns true if
the list is already sorted in increasing order.
public static boolean isSorted(int[] list)
<Output>
Enter list: 8 10 1 5 16 61 9 11 1
The list is not sorted
<End Output>
<Output>
Enter list: 10 1 1 3 4 4 5 7 9 11 21
The list is already sorted
<End Output>
7
public class Test {
public static void main(String[] args) {
// Fill in the code here
}
}
8
Part IV: Multiple Choice Questions: (1 pts each)
(1. Mark your answers on the sheet. 2. Login and click Take
Instructor Assigned Quiz for QFinal. 3. Submit it online
within 5 mins. 4. Close the Internet browser.)
1. 24 % 5 is _____
A. 2
B. 0
C. 4
D. 1
E. 3
#
2. If you attempt to add an int, a byte, a long, and a double, the result will be a
__________ value.
A. int
B. long
C. byte
D. double
#
3. What is the result of 45 / 4?
A. 11
B. 12
C. 10
D. 11.25
#
4. In Java, the word true is ________.
A. a Boolean literal
B. a Java keyword
C. same as value 0
D. same as value 1
#
5. The following code displays ___________.
9
System.out.println("too cold");
else
System.out.println("just right");
A. too hot
B. too cold
C. too hot too cold just right
D. just right
#
6. The "less than or equal to" comparison operator in Java is __________.
A. <<
B. =<
C. !=
D. <=
E. <
#
7. Analyze the following code fragments that assign a boolean value to the variable even.
Code 1:
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
even = (number % 2 == 0) ? true: false;
Code 3:
even = number % 2 == 0;
A. Code 3 has a syntax error, because you attempt to assign number to even.
B. Code 2 has a syntax error, because you cannot have true and false literals in the
conditional expression.
C. All three are correct, but Code 1 is preferred.
D. All three are correct, but Code 2 is preferred.
E. All three are correct, but Code 3 is preferred.
#
8. How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 10);
10
A. 10
B. 8
C. 0
D. 9
E. 11
#
9. What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
A. 11
B. 0
C. 9
D. 8
E. 10
#
10. What is the number of iterations in the following loop:
A. 2*n
B. n
C. n - 1
D. n + 1
#
11. What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
A. 45
B. 12
C. 13
D. 10
E. 11
11
#
12. What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
A. 9
B. 10
C. undefined
D. 11
#
13. __________ is to implement one method in the structure chart at a time from the top
to the bottom.
A. Stepwise refinement
B. Top-down approach
C. Bottom-up and top-down approach
D. Bottom-up approach
#
14. Analyze the following code.
12
}
}
#
15. Analyze the following code:
A. The program does not compile because the compiler cannot distinguish which
xmethod to invoke.
B. The program runs fine but displays things other than 5.
C. The program displays long, long followed by 5.
D. The program displays int, long followed by 5.
#
16. What is Math.floor(3.6)?
A. 5.0
B. 3
C. 3.0
D. 4
#
17. How many elements are in array double[] list = new double[5]?
13
A. 5
B. 4
C. 6
D. 0
#
18. The reverse method is defined in the textbook. What is list1 after executing the
following statements?
A. list1 is 6 5 4 3 2 1
B. list1 is 1 2 3 4 5 6
C. list1 is 0 0 0 0 0 0
D. list1 is 6 6 6 6 6 6
#
19. If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
a. 3.4
b. 2.0
c. 3.5
d. 5.5
e. undefined
#
20. In the following code, what is the output for list2?
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;
#
21. What is the output of the following code?
14
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
if (myList[i] >= max) {
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
a. 0
b. 1
c. 2
d. 3
e. 4
#
22. Suppose a method p has the following heading:
a. return 1;
b. return {1, 2, 3};
c. return int[]{1, 2, 3};
d. return new int[]{1, 2, 3};
e. return new int[][]{{1, 2, 3}, {2, 4, 5}};
15