1
oName:_______________________
Covers Chapters 4-5
CSCI 1301 Introduction to Programming
Georgia Southern 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 ___________________.
Part I.
(a) (3 pts)
Analyze the following code. Is count < 100 always true, always false, or sometimes true or
sometimes false at Point A, Point B, and Point C?
(b) (2 pts) Show the output of the following code:
2
(c) (4 pts) show the output of the following code:
public class Test {
public static void main(String[] args) {
int i = 1;
do {
int num = 1;
Part II:
1. (4 pts each) The following program prompts the user to enter a hex digit and displays its
corresponding decimal value. Fill the code to complete the code.
import java.util.Scanner;
public class HexDigit2Dec {
3
}
// Display decimal value for the hex digit
char ch = Character.toUpperCase(hexString.charAt(0));
if (‘A’ <= ch && ch <= ‘F’) {
}
else if (_______________________________________) {
System.out.println(“The decimal value for hex digit
+ ch + is “ + ch);
Part III: (10 pts each):
1. Write a loop that computes (No need to write a complete program)
4
2. Write a program that prompts the user to enter a string and displays the number of digits in the string.
5
Part III: Multiple Choice Questions: (1 pts each)
(1. Mark your answers on the sheet. 2. Go to LiveLab,
login, and click Take Instructor Assigned Quiz for Quiz2.
3. Submit it online within 5 mins. 4. Close the browser.)
1 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. 11
c. 13
d. 12
e. 10
#
2 Analyze the following code.
int x = 1;
while (0 < x) && (x < 100)
3 How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
count++;
} while (count < 10);
A. 8
B. 0
C. 11
D. 10
6
E. 9
#
4 What is i after the following for loop is finished?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
A. 10
B. undefined
C. 9
D. 11
#
5 Analyze the following code:
public class Test {
A. The program compiles despite the semicolon (;) on the for loop line, and displays 4.
B. The program compiles despite the semicolon (;) on the for loop line, and displays 14.
C. The program has a compile error because of the semicolon (;) on the for loop line.
D. The program has a runtime error because of the semicolon (;) on the for loop line.
#
6 Which of the following expression yields an integer between 0 and 100, inclusive?
A. (int)(Math.random() * 100 + 1)
B. (int)(Math.random() * 101)
C. (int)(Math.random() * 100)
D. (int)(Math.random() * 100) + 1
#
7 Analyze the following code.
int count = 0;
7
while (count < 100) {
// Point A
System.out.println(“Welcome to Java!”);
count++;
// Point B
}
// Point C
A. count < 100 is always false at Point A
B. count < 100 is always true at Point B
C. count < 100 is always false at Point C
D. count < 100 is always true at Point C
E. count < 100 is always false at Point B
#
8. What is the output of the following fragment?
#
9. Assume that the ASCII code for character c is 99 and for a is 97. What is the printout
of the following code?
System.out.println(“AB” + ‘a’ + ‘c’);
a. ABac
b. AB9799
c. ABa99
d. AB196
#
10. “abcdefgh”.subString(1, 3) returns ________.
a. abc
b. bcd
c. bc
8
d. cde
#
11. Which of the following method returns the sine of 90 degree?
a. Math.sine(90)
b. Math.sin(90)
c. Math.sin(PI)
d. Math.sin(Math.toRadians(90))
e. Math.sin(Math.PI)
#
12. Given two Strings s1 = “Welcome to Java” and s2 = “Programming is fun”, which of
the following is true?
#
13. Which of the following assignment statements is correct to assign character 5 to
c?
a. char c = ‘5’;
b. char c = 5;
c. char c = “5”;
d. char c = “344”;
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? ______________