1
Name:_______________________
Covers Chapters 1-3
75 mins SAMPLE TEST
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 ___________________ Date ___________________
Part A.
A.1 (1 pt) Write a statement to declare a constant named PI with value
3.14159.
Part B. (10 pts) Show the output of the following code: (write the output
next to each println statement if the println statement is executed in
the program).
public class Test {
public static void main(String[] args) {
System.out.println((int)(Math.random() * 10) / 10);
System.out.println(Math.pow(3, 2));
System.out.println(35 % 7);
System.out.println(3 + 4.5 * 2 < 2 * 9.5);
2
int y = -5;
++y;
System.out.println(y);
int value = 453;
int d1 = value % 10;
int d2 = (value / 10) % 10;
int d3 = (value / 100) % 10;
System.out.println(“+ d1 + d2 + d3);
}
}
Part C:
3
2. (10 pts) (Palindrome number) Write a program that prompts the user to
enter a threedigit integer and determines whether it is a palindrome
number. A number is palindrome if it reads the same from right to left
and from left to right. Here is a sample run of this program:
<outpsut>
4
Name:_______________________
Part D: Multiple Choice Questions: (1 pts each)
(Please circle your answers on paper first. After you
finish the test, enter your choices online to LiveLab. Log
in and click Take Instructor Assigned Quiz. Choose Quiz1.
You have 5 minutes to enter and submit the answers.)
1
The keyword _______ must be used to declare a constant.
2
You can define a constant twice in a block.
A. true
B. false
3
A Java statement ends with a __________.
A. period (.)
B. closing brace
C. semicolon (;)
D. comma (,)
4
The assignment operator = is left-associative.
A. true
B. false
5
What is y after the following switch statement?
5
6
Which of the Boolean expressions below has incorrect
syntax?
7
You can always convert an if statement to a switch
statement.
A. false
B. true
8
The default case must be specified in a switch statement.
A. false
B. true
9
Which of the following expression is equivalent to (x >
1).
A. !(x = 1)
B. x >= 1
C. !(x < 1)
D. !(x <= 1)
10
Analyze the following two code fragments.
(i)
int x = 5;
if (0 < x) && (x < 100)
System.out.println(“x is between 1 and 100”);
A. The second fragment has a syntax error.
B. The first fragment has a syntax error.
C. Both fragments produce the same output.
D. Both fragments compile, but produce different results.
11
Analyze the following code.
int x = 0;
int y = ((x < 100) && (x > 0)) ? 1: -1;
6
A. y becomes -1 after the code is executed.
B. y becomes 1 after the code is executed.
Keys:
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? ______________
Solution:
A.1 Write a statement to declare a constant named PI with value 3.14159.
A.2 Write an if-else statement that increase salary by 10% if age is
greater than or equal to 50, and 5% if age is less than 35, and 8%
otherwise.
A.3 Write a conditional expression that assigns 1 to x if y is greater
than 0 and1 to x otherwise.
x = (y >= 0) ? 1 :1;
Part B:
0
9.0
0
true
112
28
19
42
1942 is even
-4
354
Part C:
1.
import java.util.Scanner;
8
2.
import java.util.Scanner;
public class Exercise03_12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);