Unit 3 Test (CH 4 5 6) 11-6-2013 KEY
Unit 3 Test (CH 4 5 6) 11-6-2013 KEY
Unit 3 Test (CH 4 5 6) 11-6-2013 KEY
a) 0
b) 3.14
c) 6.28
d) compile-time error
Answer: d
Answer: b
3) What will be the value stored in the variable x after the execution of the following code
snippet?
int a = 10;
a) 1
b) 2
c) 4
d) The code has a syntax error
Answer: b
4) Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the
following code snippet, what is the output?
Answer: c
Title: What is output of snippet (that calculates value based on user input)?
Difficulty: Medium
Section Reference 1: 4.2 Arithmetic
a) Constants are written using capital letters because the compiler ignores constants declared in
small letters.
b) The data stored inside a constant can be changed using an assignment statement.
c) You can make a variable constant by using the final reserved word when declaring it.
d) Constant variables can only be changed through the Math library.
Answer: c
Answer: d
a) OVE MY
b) OVE MY C
c) VE MY CO
d) VE MY C
Answer: d
I. 22 / 7
II. 22.0 / 7
Answer: b
9) What (if any) type of error occurs with the following code if the user input is ABC?
a) Compile-time error
b) Run-time error
c) Overflow error
d) Illegal expression
Answer: b
a) Java is powerful
b) Java + is powerful
Answer: d
a) Compile-time error
b) The average is 91.5
c) The average is 91.5
The average is 91.5
d) The average is 91.5
The average is 0.0
Answer: a
(45 / 6) % 5
a) 2
b) 7
c) 2.5
d) 3
Answer: a
Title: What is the result of evaluating this arithmetic expression using the mod operator?
Difficulty: Easy
13) Which one of the following is a correct method for defining and initializing an integer
variable with name value?
Answer: a
14) When you purchase donuts, they come in boxes of 12, so 24 donuts take 2 boxes. All
donuts need to be in a box, so if you buy 13 donuts, you'll get 2 boxes with 12 in the first box
and 1 in the second. If the integer variable numberOfDonuts contains the positive number of
donuts purchased, which of the following will correctly give the number of boxes needed?
Answer: a
15) Which of the following is the mathematical equivalent of the following Java expression?
h = (4.0 * a * b – Math.pow(b, 2)) / c;
a) h = 4ab – 2b / c
b) h = (4ab – 2b) / c
c) h = 4ab – b2 / c
d) h = (4ab – b2) / c
16) Assume the following variables have been declared and given values as shown:
int i = 2345;
double m = 67.8;
Answer: b
17) Assume the following variables have been declared and given values as shown:
int i = 2345;
double m = 67.8;
Answer: a
System.out.print ("O\"my\t\\\"no!");
a)O"myt\"no!
b)O\"my\t\\\"no!
c)O\ my\t\\\ no!
d)O"my \"no!
Answer: d
19) Assume the following variable has been declared and given values as shown:
String name = "Mamey, Jean";
Answer: a
a)str.charAt (str.length())
b)str.lastChar()
c)str.charAt (str.length() – 1)
d)str.substring(str.length())
Answer: c
Chapter Number: 5
Answer: a
a)
if (x < 10) { size = "Small"; }
else (x < 20) { size = "Medium"; }
b)
if (x < 10); { size = "Small"; }
else (x < 20) { size = "Medium"; }
c)
if (x < 10) { size = "Small"; }
else { size = "Medium"; }
d)
if { size = "Small"; }
else (x < 20) { size = "Medium"; }
Answer: c
Answer: c
4) Assuming that a user enters 15 as input, what is the output of the following code snippet?
Scanner in = new Scanner(System.in);
System.out.print("Please enter a number: ");
int number = in.nextInt();
if (number > 20)
{
System.out.println("The number is LARGE!");
}
else
{
System.out.println("The number is SMALL!");
}
a)
There is no output due to compilation errors.
b)
The number is LARGE!
c)
The number is SMALL!
d)
The number is LARGE!
The number is SMALL!
Answer: c
Title: What is output of (if/else with < test) with this input?
Difficulty: Medium
Section Reference 1: 3.2 Comparing Values
5) Assuming that a user enters 25 as the value for x, what is the output of the following code
snippet?
int x;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
x = in.nextInt();
if (x < 100)
a) 27
b) 28
c) 29
d) 30
Answer: c
Answer: c
7) Suppose one needs an if statement to check whether an integer variable pitch is equal to 440
(which is the frequency of the note “A” to which strings and orchestras tune). Which condition is
correct?
a) if (pitch – 440 = 0)
b) if ((pitch !< 440) && (pitch !> 440))
c) if (pitch = 440)
d) if (pitch == 440)
Answer: d
8) Assuming that a user enters 5 as the value for num, what is the output of the following code
snippet?
int num = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
if (num < 50)
{
num = num + 5;
}
if (num < 10)
{
num = num - 2;
}
if (num > 5)
{
num++;
}
else
{
num--;
}
System.out.println(num);
a) 0
Answer: d
9) Consider the following code snippet. Assuming that the user enters first 20 and then 12 as the
two input values, what is the output of the code snippet?
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num1 = in.nextInt();
System.out.print("Enter a number: ");
num2 = in.nextInt();
if (num1 < num2)
{
num3 = num1;
}
else
{
num3 = num2;
}
if (num1 < num2 + 10)
{
num4 = num1;
}
else if (num1 < num2 + 20)
{
num5 = num1;
}
System.out.println("num1 = " + num1 + " num2 = " + num2
+ " num3 = " + num3 + " num4 = " + num4
+ " num5 = " + num5);
Answer: c
Title: What is output of (if/else if/else if) snippet with two inputs?
10) The flow chart shows the order in which steps should be executed, and the diamond-shaped
boxes indicate
a) input
b) algorithms
c) tasks
d) conditional tests
Answer: d
11) Assuming that a user enters 64 as his score, what is the output of the following code snippet?
int score = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter your score: ");
score = in.nextInt();
if (score < 40)
{
System.out.println("F");
}
else if (score >= 40 || score < 50)
{
System.out.println("D");
}
else if (score >= 50 || score < 60)
{
System.out.println("C");
}
else if (score >= 60 || score < 70)
{
System.out.println("B");
}
else if (score >= 70 || score < 80)
{
System.out.println("B+");
}
else
{
System.out.println("A");
}
a) D
b) C
c) B
Answer: a
Title: What is output of (if/else if with Boolean or) with this input?
Difficulty: Hard
Section Reference 1: 5.7 Boolean Variables and Operators
a) 90
b) 100
c) 99
d) 101
Answer: a
Title: What is output of (if with > condition) with this input?
Difficulty: Hard
Section Reference 1: 5.1 The if statement
a) 95
b) 100
c) 105
d) 110
Answer: d
14) Assuming that a user enters 45, 78, and then 12, what is the output of the following code
snippet?
int num1 = 0;
int num2 = 0;
int num3 = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num1 = in.nextInt();
System.out.print("Enter a number: ");
num2 = in.nextInt();
System.out.print("Enter a number: ");
num3 = in.nextInt();
if (!(num1 > num2 && num1 > num3))
{
System.out.println(num1);
}
else if (!(num2 > num1 && num2 > num3))
{
System.out.println(num2);
}
else if (!(num3 > num1 && num3 > num2))
{
System.out.println(num3);
}
a) 12
b) 45
c) 78
d) There is no output due to compilation errors.
Answer: b
Title: What is output of (if/else if/else if) snippet with these three inputs?
Difficulty: Hard
Section Reference 1: 5.7 Boolean Variables and Operators
15) Assuming that the user provides 3 as input, what is the output of the following code snippet?
int x;
int y;
x = 0;
System.out.print("Please enter y: ");
Scanner in = new Scanner(System.in);
y = in.nextInt();
if (y > 0)
{
x = 2 * y;
}
else
{
x = 2 + x;
}
System.out.println("x: " + x);
a) x: 2
b) x: 4
c) x: 6
d) There is no output due to compilation errors.
Answer: c
Title: What is output of (if/else with > test) snippet with this input?
Difficulty: Hard
Section Reference 1: 3.2 Comparing Values
Answer: c
17) Assuming that a user enters 68 as the score, what is the output of the following code snippet?
a) D
b) C
c) B
d) A
Answer: a
Title: What is output of (if/else if with Boolean or) with this input?
Difficulty: Hard
Section Reference 1: 5.7 Boolean Variables and Operators
18) Which of the following conditions will correctly check if the String variable greeting
is "bonjour"?
Answer: c
19) Which of the following conditions will correctly check if the String variable early
comes before "middle" alphabetically?
Answer: d
Title: Which of the following conditions will correctly check if the String variable early
comes before "middle" alphabetically?
Difficulty: Medium
Section Reference 1: 5.4 Nested Branches
20) Which of the following conditions can be added to the code below so it will assign the larger
value of two integer variables a and b to the integer variable maximum?
a)a == b
b)b > a
c)a > b
d)a.compareTo (b) > 0
Answer: c
1) The code snippet below checks whether a given number is a prime number. What will be the
result of executing it?
public static void main(String[] args)
{
int j = 2;
int result = 0;
int number = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a number: ");
number = reader.nextInt();
while (j <= number / 2) // better is while (j * j <= number)
{
if (number % j == 0)
{
result = 1;
}
j++;
}
if (result == 1)
{
System.out.println("Number: " + number + " is Not Prime.");
}
else
{
System.out.println("Number: " + number + " is Prime. ");
}
}
Answer: B
2) What are the values of i and j after the following code fragment runs?
int i = 60;
int j = 50;
int count = 0;
while (count < 5)
{
a) i = 1951, j = 0
b) i = 1951, j = 45
c) i = 65, j = 1
d) i = 65, j = 45
Answer: A
3) In the following code snippet, when does the execution of the program switch from the inner
loop to the outer loop?
int i;
int j;
for (i = 0; i <= 9; i++)
{
for (j = 1; j < 5; j++)
{
System.out.println("Hello");
if (j == 2)
{
j = 6;
}
}
}
Answer: A
4) What values does counter variable i assume when this loop executes?
a) 20, 14, 8, 2
b) 20, 14, 8, 2, –4
c) 20, 14, 8
d) 14, 8, 2
Answer: B
Title: Which values does the counter variable assume in for loop?
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
5) Insert a statement that will correctly terminate this loop when the end of input is reached.
boolean done = false;
while (!done)
{
String input = in.next();
if (input.equalsIgnoreCase("Q"))
{
__________
}
else
{
double x = Double.parseDouble(input);
data.add(x);
}
}
a) stop;
b) done = 1;
c) exit;
d) done = true;
Answer: D
a) Exactly once
b) Exactly twice
c) Can't be determined
d) Always infinite loop
Answer: C
a) 0 5 10 15 20 25 30 35 40 45 50 55
b) 0
c) No output, compilation error
d) 0 5 10
Answer: C
Answer: B
9) How do you fix this code snippet to make it print out the sum when the user enters Q?
System.out.print("Enter a value, Q to quit: ");
double sum = 0;
Scanner in = new Scanner(System.in);
boolean hasData = true;
do
{
double value = in.nextDouble();
sum = sum + value;
System.out.print("Enter a value, Q to quit: ");
}
while (in.hasNext());
System.out.println("sum " + sum);
a) while (in.hasData());
b) while (!in.hasEnded());
c) while (in.hasNextDouble());
d) while (hasData);
Answer: C
Title: How do you fix code snippet to print when the user enters Q?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
10) What are the values of i and j after the following code snippet executes?
int i = 60;
int j = 50;
int count = 0;
while (count < 5)
{
i = i + i;
i = i + 1;
j = j - 1;
j = j - j;
count++;
}
System.out.println(i);
System.out.println(j);
Ans: C
int i = 1;
while (i != 9)
{
System.out.print(i + " ");
i++;
if (i == 9)
{
System.out.println("End");
}
}
a) 1 End
b) 1 End (infinite loop)
c) 1 2 3 4 5 6 7 8 End
d) 1 2 3 4 5 6 7 8 End (infinite loop)
Answer: C
int i;
int j;
for (i = 0; i < 7; i++)
{
for (j = 7; j > i; j--)
{
System.out.print("*");
}
System.out.println("");
a) A rectangle with six rows and seven columns of asterisks. The number of rows increments by
one on completion of one iteration of the inner loop.
b) A right triangle with six rows and seven columns of asterisks. The number of columns
increments by one on completion of one iteration of the inner loop.
c) A rectangle with seven rows and six columns of asterisks. The number of rows increments by
one on completion of one iteration of the inner loop.
d) A right triangle with six rows and seven columns of asterisks. The number of columns
decrements by one on completion of one iteration of the inner loop.
Answer: D
13) In the following code snippet, when does the execution of the program switch from the inner
loop to the outer loop?
int i;
int j;
for (i = 0; i <= 9; i++)
{
for (j = 1; j < 5; j++)
{
System.out.println("Hello");
}
}
Answer: A
Title: When does execution switch from inner to outer loop in snippet?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
int i = 0;
int j = 1;
do
{
a) 1 time
b) 2 times
c) 3 times
d) 4 times
Answer: C
String s = "aeiou";
int i = 0;
do
{
System.out.print(s.substring(i, i + 1));
i++;
if (i >= 3)
{
i = 5;
}
}
while (i < 5);
a) a
b) ae
c) aeiou
d) aei
Answer: D
Answer: C
int val = 1;
int sum = 0;
while (val < 5)
{
sum = sum + val;
val++;
}
System.out.print (sum);
a)4
b)5
c)10
d)15
int val = 1;
int sum = 0;
while (val < 5)
{
sum = 0;
sum = sum + val;
val++;
}
System.out.print (sum);
a)15
b)10
c)5
d)4
Answer: d
a)0 1 2 3 4 5 6 7 8 9 10
b)0 1 2 3 4 5 6 7 8 9
c)0 2 4 6 8
d)0 1 3 5 7 9
Answer: b
20) Which of the following conditions can be added to the code below so it will loop until the
user enters "no" or "NO"?
a)response.equals("no") != response.equals("NO")
b)!(response.equals("no") || response.equals("NO"))
c)!response.equals("no") || !response.equals("NO")
d)!(response.equals("no") && response.equals("NO"))
Answer: b
Title: Which of the following conditions can be added to the code below so it will loop until the
user enters "no" or "NO’?
Difficulty: Medium
Section Reference 1: 6.7 Two Dimensional Arrays