1
Name:_______________________
Covers Chapters 4-5
CSCI 1301 Introduction to Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
Solution:
Part I.
(a)
count < 100 is always true at Point A.
(b)
9 8 7 6 5
(c)
1G
1G3G
Part II:
(a)
public class Test {
public static void main(String[] args) {
int total = 0;
(b)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
2
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 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“Enter a hex digit: “);
String hexString = __ input.nextLine();__;
// Display decimal value for the hex digit
char ch = Character.toUpperCase(hexString.charAt(0));
3
else {
System.out.println(ch +is an invalid input“);
}
}
}
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)
System.out.println(x++);
a. The loop runs forever.
b. The code does not compile because the loop body is not in the braces.
c. The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair
of parentheses.
d. The numbers 1 to 99 are displayed.
e. The numbers 2 to 100 are displayed.
#
3. How many times will the following code print “Welcome to Java”?
int count = 0;
do {
4
System.out.println(“Welcome to Java”);
count++;
} while (count < 10);
A. 8
B. 0
C. 11
D. 10
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 {
public static void main (String args[]) {
int i = 0;
for (i = 0; i < 10; i++);
System.out.println(i + 4);
}
}
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?
5
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;
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?
int i = 1;
int j = 1;
while (i < 5) {
i++;
j = j * 2;
}
System.out.println(j);
a. 4
b. 8
c. 16
d. 32
e. 64
#
6
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
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?
a. s1.equals(s2)
b. s2.equals(s1)
c. s1.contains(s2)
d. s2.contains(s1)
e. !s1.contains(s2)
#
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”;
7