1
Sample Final Exam for CSCI 1301 (Java) Covers chapters 1-8
FINAL EXAM AND COURSE OUTCOMES MATCHING
COURSE OUTCOMES
Upon successful completion of this course, students should be able to
1. analyze and design strategies for solving basic programming problems.
2. use primitive data types, selection statements, loops, methods to write programs.
3. develop programs to solve a variety of problems in math, science, business, and
games.
Here is a mapping of the final comprehensive exam against the course outcomes:
Question Matches outcomes
Part I(a) 2
Part I(b) 2
2
Name:__________________
Covers chs 1-8
CSCI 1301 Final
Armstrong Atlantic State University
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.
Signed by ___________________ Date ___________________
Part I. (2 pts each)
a. How many times is the following loop body repeated? What is the
output of the loop?
b. Convert the following for loop into a do-while loop.
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
c. Convert the following if statement using a switch statement
// Find interest rate based on year
Part II: Show the output of the following code:
a: (2 pts)
3
}
}
}
b. (3 pts)
Suppose the input is 2 3 4 5 0. What is the output of the
following code?
}
c. (3 pts)
public class Test {
public static void main(String[] args) {
System.out.println(xMethod(5672));
d. (3 pt)
public class Test {
public static void main(String[] args) {
int number = 0;
int[] numbers = new int[1];
4
public static void m(int x, int[] y) {
x = 3;
y[0] = 3;
}
}
e. (3 pt) Suppose the input is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4. What is the output of the following code?
}
5
Part III: (Write programs)
1. (15 pts) Write a program that prompts the user to enter
an integer n (assume n >= 2) and displays its largest factor
other than itself.
6
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*n362516941
The method header is
public static void displayPattern(int n)
import java.util.Scanner;
}
}
7
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)
Write a test program that prompts the user to enter a list
and displays whether the list is sorted or not. Here is a
sample run. Note that the first number in the input
indicates the number of the elements in the list.
<Output>
8
public class Test {
public static void main(String[] args) {
// Fill in the code here
}
}
9
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?
#
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 ___________.
double temperature = 50;
10
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. =<
#
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
#
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);
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);
#
10. What is the number of iterations in the following loop:
for (int i = 1; i <= n; i++) {
// iteration
}
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);
12
#
12. What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
#
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.
public class Test {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2) {
System.out.println(“max(int, double) is invoked”);
if (num1 > num2)
return num1;
else
return num2;
}
13
}
}
A. The program runs and prints 2 followed by “max(double, int)” is invoked.
B. The program cannot compile because the compiler cannot determine which max
method should be invoked.
#
15. Analyze the following code:
public class Test {
public static void main(String[] args) {
System.out.println(xMethod(5, 500L));
}
public static int xMethod(int n, long l) {
System.out.println(“int, long”);
return n;
}
public static long xMethod(long n, long l) {
System.out.println(“long, long”);
return n;
}
}
#
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]?
14
A. 5
B. 4
C. 6
D. 0
#
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?
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
#
21. What is the output of the following code?
15
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
}
System.out.println(indexOfMax);
a. 0
b. 1
c. 2
d. 3
e. 4
#
22. Suppose a method p has the following heading:
public static int[][] p()
What return statement may be used in p()?
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? ______________