1
Name:_______________________
Covers Chapters 1-3
75 mins
CSCI 1301 Introduction to Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
Solution:
Part A.
A.1 (1 pt) Write a statement to display 3.5 ^ 9.2.
System.out.println(Math.pow(3.5, 9.2));
A.2 (2 pts) Write an if-else statement for y = |x 1|. Assume that x and
y are already declared.
A.3 (2 pts) Assume that x is an int variable. Write a conditional
expression to fill in the following blank for the statement that displays
“x is evenif x is even, orx is oddif x is odd.
System.out.println(“x is + (x % 2 == 0 ?even: “odd“));
Part B:
0
8.0
6
false
1.
import java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“Enter the exchange rate from dollars to RMB: “);
2
2.
import java.util.Scanner;
Part D:
1. The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
a. 76
b. 76.0252175
c. 76.03
d. 76.02
#
2. What is y after the following switch statement?
int x = 0;
int y = 0;
switch (x + 1) {
case 0: y = 0;
case 1: y = 1;
default: y = -1
3
}
a. 2
b. 1
c. 0
d. -1
#
3. Assume x is 0. What is the output of the following statement?
if (x > 0)
System.out.print(“x is greater than 0”);
else if (x < 0)
System.out.print(“x is less than 0”);
else
System.out.print(“x equals 0”);
a. x is less than 0
b. x is greater than 0
c. x equals 0
d. None
#
4. Analyze the following code:
Code 1:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
boolean even = (number % 2 == 0);
a. Code 2 has syntax errors.
b. Code 1 has syntax errors.
c. Both Code 1 and Code 2 have syntax errors.
d. Both Code 1 and Code 2 are correct, but Code 2 is better.
4
#
5. What is the printout of the following switch statement?
char ch = ‘a’;
switch (ch) {
case ‘a’:
case ‘A’:
System.out.print(ch); break;
case ‘b’:
case ‘B’:
System.out.print(ch); break;
case ‘c’:
case ‘C’:
System.out.print(ch); break;
case ‘d’:
case ‘D’:
System.out.print(ch);
}
a. ab
b. a
c. aa
d. abc
e. abcd
#
6. What is x after evaluating
x = (2 > 3) ? 2 : 3;
a. 5
b. 2
c. 3
d. 4
#
7. Analyze the following code.
int x = 0;
if (x > 0);
{
5
System.out.println(“x”);
}
a. The value of variable x is always printed.
b. The symbol x is always printed twice.
c. The symbol x is always printed.
d. Nothing is printed because x > 0 is false.
#
8. To declare a constant MAX_LENGTH inside a method with value 99.98, you
write
a. final double MAX_LENGTH = 99.98;
b. double MAX_LENGTH = 99.98;
c. final MAX_LENGTH = 99.98;
d. final float MAX_LENGTH = 99.98;
#
9. Which of the following is a constant, according to Java naming
conventions?
a. read
b. MAX_VALUE
c. ReadInt
#
10. What is y after the following switch statement is executed?
x = 3;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
a. 1
b. 4
c. 3
d. 2
#
11. Which of the following code displays the area of a circle if the radius is
positive.
a. if (radius <= 0) System.out.println(radius * radius * 3.14159);
b. if (radius != 0) System.out.println(radius * radius * 3.14159);
6
c. if (radius >= 0) System.out.println(radius * radius * 3.14159);
d. if (radius > 0) System.out.println(radius * radius * 3.14159);