of 16
1
FINAL EXAM AND COURSE OUTCOMES MATCHING
COURSE OUTCOMES
6. use an IDE to develop programs.
Here is a mapping of the final comprehensive exam against the course outcomes:
Question
Matches outcomes
Part I(a)
2
Part I(b)
2
Part I(c)
2
Part II(a)
2
Part II(b)
2
Part II(c)
2
Part II(d)
2
Part II(e)
2, 5
Part III(a)
1, 2, 3
Part III(b)
1, 2, 3, 4
Part III(c)
1, 2, 3, 4, 5
Part VI
1, 2, 3, 4, 5
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?
int i = 0;
while (i < 10) {
if ((i + 1) % 2 == 0)
System.out.println(i);
i++;
}
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
if (numOfYears == 7)
annualInterestRate = 7.25;
else if (numOfYears == 15)
annualInterestRate = 8.50;
else if (numOfYears == 30)
annualInterestRate = 9.0;
else {
System.out.println("Wrong number of years");
System.exit(0);
}
Part II: Show the output of the following code:
a: (2 pts)
}
System.out.println("number is " + number
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.
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
The method header is
public static void displayPattern(int n)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Enter the number of lines
System.out.print("Enter n: ");
int n = input.nextInt();
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>
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>
See the next page for the outline of the program:
public class Test {
public static void main(String[] args) {
// Fill in the code here
4. (15 pts) (Occurrences of a specified character) Write a
method that finds the number of occurrences of a specified
character in the string using the following header:
public static int count(String str, char a)
Part IV: Multiple Choice Questions: (1 pts each)
1. 24 % 5 is _____
A. 2
2. If you attempt to add an int, a byte, a long, and a double, the result will be a
__________ value.
3. What is the result of 45 / 4?
4. In Java, the word true is ________.
5. The following code displays ___________.
double temperature = 50;
if (temperature >= 100)
System.out.println("too hot");
else if (temperature <= 40)
System.out.println("too cold");
6. The "less than or equal to" comparison operator in Java is __________.
A. <<
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:
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
9. What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println("Welcome to Java");
10. What is the number of iterations in the following loop:
for (int i = 1; i <= n; i++) {
11. What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
12. What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
13. __________ is to implement one method in the structure chart at a time from the top
to the bottom.
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;
}
14
}
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;
17. How many elements are in array double[] list = new double[5]?
15
18. The reverse method is defined in the textbook. What is list1 after executing the
following statements?
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;
21. What is the output of the following code?
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) {
22. 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 {1, 2, 3};
c. return int[]{1, 2, 3};