Name: 
 

Java Final 15 Studyguide



Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 1. 

Which part of a computer contains the CPU, the RAM, and connectors to peripheral devices?
a.
network
c.
motherboard
b.
bus
d.
optical disk
 

 2. 

Which statement is true about running a Java program on a different CPU?
a.
You need different Java source code for each processor.
b.
You can take code that has been generated by the Java compiler and run it on different CPUs.
c.
You need to recompile the Java program for each processor.
d.
You cannot run the program on a computer with a different processor because Java, being a high-level programming language, is machine dependent.
 

 3. 

Text enclosed between this and the end of line is ignored by the compiler.
a.
''
c.
()
b.
//
d.
""
 

 4. 

The Java compiler ignores any text between ____.
a.
(* and *)
c.
{* and *}
b.
/* and */
d.
// and //
 

 5. 

Which statement is true about the following Java statement:

   System.out.println("Hello!);
a.
There is a run-time error.
c.
There is a compile-time error.
b.
There are no errors.
d.
There are multiple errors.
 

 6. 

Assume that the following Java statement is contained in the main method of the class named Welcome:

   System.out.printLine("Welcome!");

What is the name of the file generated by the Java compiler?
a.
Welcome.class
c.
No file is generated due to an error.
b.
Welcome.java
d.
Welcome
 

 7. 

What is the purpose of the following algorithm?

num = 0
Repeat the following steps for 10 times
input var1
   if var1 > num then
   num = var1
   end of if
end of repeat
print num
a.
To print out the 10 numbers
b.
To search for a particular number among 10 numbers
c.
To find the largest among 10 numbers
d.
To find the smallest among 10 numbers
 

 8. 

Assume that the variable count has been declared as type int. Which statement adds 10 to count?
a.
count = 10;
c.
count = count + 10;
b.
count == count + 10;
d.
count + 10;
 

 9. 

Which of the following method calls illustrates the return value of a method as a parameter?
a.
greeting.length();
b.
greeting.println("Hello");
c.
System.out.println(length.greeting());
d.
System.out.println(greeting.length());
 

 10. 

What terminology describes a method that modifies the internal data of its implicit parameter?
a.
public
c.
mutator
b.
void
d.
accessor
 

 11. 

Which package is automatically imported in any Java program?
a.
java.system
c.
java.language
b.
java.lang
d.
java.util
 

 12. 

Which method would you use to obtain the string "1234567890" from the string "123-456-7890"?
a.
isEmpty
c.
trim
b.
replace
d.
length
 

 13. 

Assuming the following Java statement:

int num = 10;

What does the variable num store?
a.
A reference to the memory location where the value 10 is stored.
b.
A reference to the int primitive type.
c.
An object representing the number 10.
d.
The numeric value 10.
 

 14. 

What does an object store its data in?
a.
files
c.
instance variables
b.
methods
d.
access specifiers
 

 15. 

What is the name of the instance variable for a BankAccount object?
a.
makeDeposit
c.
getBalance
b.
makeWithdrawl
d.
balance
 

 16. 

Given this method comment, fill in the blank in the method implementation.

/**
   Deposits money into the bank account
   @param amount the amount to deposit
*/
public _____  deposit(double amount)
{
   balance = balance + amount;
}
a.
double
c.
return
b.
void
d.
null
 

 17. 

What is a parameter variable?
a.
A variable that is declared in the header of a method.
b.
A variable that is declared in the body of the class.
c.
A variable that is declared in the body of a method.
d.
A variable that is declared in the header of a class.
 

 18. 

Assuming that the user inputs a value of 25000 for the pay and 10 for the bonus rate in the following code snippet, what is the output?

public static void main(String[] args)
{
   Scanner in = new Scanner(System.in);
   System.out.print("Enter the pay: ");
   double pay = in.nextDouble();
   System.out.print("Enter the bonus rate: ");
   double bonus = in.nextDouble();
  
   System.out.println("The new pay is " +
      (pay + pay * (bonus / 100.0)));
}
a.
The new pay is 25000
c.
The new pay is 27500
b.
The new pay is 25100
d.
The new pay is 30000
 

 19. 

What is the output of the following code snippet?

public static void main(String[] args)
{
   double x;
   x = Math.pow(3.0, 2.0) + Math.pow(4.0, 2.0);
   System.out.println(x);  
}
a.
25.0
c.
7.0
b.
34
d.
14
 

 20. 

Which of the following statements about constants in Java are true?
a.
I, II, III
b.
II, III, IV
c.
I, III, IV
d.
I, II, IV
e.
Although not required, constants are commonly named using uppercase letters
II. Only integer values can appear as constants
III. A variable can be defined with an initial value, but the reserved word final prevents it from being changed
IV. A named constant makes computations that use it clearer
 

 21. 

Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output?
  
Scanner in = new Scanner(System.in);
System.out.print("Enter the price: ");
double price = in.nextDouble();

System.out.print("Enter the discount rate: ");
double discount = in.nextDouble();

System.out.print("The new price is ");
System.out.println(price - price * (discount / 100.0));
a.
The new price is 30.0
c.
The new price is 27.0
b.
The new price is 20.0
d.
The new price is 33.0
 

 22. 

Assume the following variables have been declared and given values as shown:
int i = 2345;
double m = 67.8;
Which statement will give the output below?
Values are       2345 and   67.80
a.
System.out.printf ("Values are %10d and %7.2f", i, m);
b.
System.out.printf ("Values are %6d and %2.2f", i, m);
c.
System.out.printf ("Values are %i and %m");
d.
System.out.println ("Values are " + i + " and " + m);
 

 23. 

What will be printed by the statement below?
System.out.print ("O\"my\t\\\"no!");
a.
O"myt\"no!
c.
O\ my\t\\\ no!
b.
O\"my\t\\\"no!
d.
O"my      \"no!
 

 24. 

Assume the following variable has been declared and given values as shown:
String name = "Mamey, Jean";
Which statement will print the name as "Jean Mamey"?
a.
System.out.print (name.substring(7) + " "
   + name.substring(0, 5));
b.
System.out.print (name.substring(8, 4) + " "
   + name.substring(1, 5));
c.
System.out.print (name.substring(8) + " "
   + name.substring(1, 4));
d.
System.out.print (name.substring(2) + " "
   + name.substring(1));
 

 25. 

What is the value of the following expression?
2 + 3 + 5 / 2
a.
5
c.
7
b.
6
d.
7.5
 

 26. 

Assume the following variables have been declared as shown and given values elsewhere:
double a, b, c;
The discriminant of a quadratic equation is defined to be:

Which of the following statements will assign the correct value to discriminant?
a.
double discriminant = Math.sqrt (b * b – 4 * a * c) ;
b.
double discriminant = Math.sqrt (b * b)   – 4 * a * c;
c.
double discriminant = Math.sqrt (b * b)  - Math.sqrt (4 * a * c) ;
d.
double discriminant =   ;
 

 27. 

The operator !> stands for
a.
not less than.
b.
not greater than.
c.
not equal to.
d.
this is not an operator in Java
 

 28. 

Which statement about an if statement is true?
a.
The condition in an if statement using relational operators will evaluate to a Boolean result
b.
The condition in an if statement should make exact comparisons to floating-point numbers
c.
The condition in an if statement should always evaluate to true
d.
The condition in an if statement should never include integer variables
 

 29. 

Which of the following options is a legally correct expression for inverting a condition?
a.
if (!(a == 10))
c.
if (a !== 10)
b.
if (!a == 10)
d.
if (a ! 10)
 

 30. 

An if statement inside another if statement is called a
a.
switch statement
b.
nested if statement
c.
break statement
d.
syntax error, since that is not permitted in Java
 

 31. 

Consider the following code snippet:

int number = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
number = in.nextInt();
if (number > 30) { . . . }
else if (number > 20) { . . .. }
else if (number > 10) { . . . }
else { . . . }

Assuming that the user input is 40, which block of statements is executed?
a.
if (number > 30) { .  .  . }
b.
else if (number > 20) { .  .  . }
c.
else if (number > 10) { .  .  . }
d.
else { . . . }
 

 32. 

What is the output of the following code snippet?

final int MIN_SPEED = 45;
final int MAX_SPEED = 65;  
int speed = 55;  
if (!(speed < MAX_SPEED) )
{
   speed = speed - 10;
}  
if (!(speed > MIN_SPEED) )
{
   speed = speed + 10;
}  
System.out.println(speed) ;
a.
45
c.
65
b.
55
d.
50
 

 33. 

Which of the following options correctly represents a “nested if” structure?
a.
if (cost < 70)
{  
   if (tax_rate < 0.10) { . . . }
}
b.
if (cost < 70) { . . . }
if (tax_rate < 0.10) { . . . }
c.
if (cost < 70) { . . . }
else { . . . }
if (tax_rate < 0.10) { . . . }
d.
if (cost < 70) { . . . }
{  
   else  
   {     
      if (tax_rate < 0.10) { . . . }
   }
}
 

 34. 

Which of the following coding techniques can hand-tracing be applied to?
a.
Pseudocode
b.
Java code
c.
Both pseudocode and Java code
d.
Neither pseudocode nor Java code
 

 35. 

Assuming that a user enters 22 as the price of an object, which of the following hand-trace tables is valid for the given code snippet?

int price = 0;
String status = "";
Scanner in = new Scanner(System.in);
System.out.print("Please enter object’s price: ");
price = in.nextInt();
if (price >= 50)
{  
   status = "reasonable";
   if (price >= 75)  
   {
       status = "costly";
   }
}
else
{
   status = "inexpensive";
   if (price <= 25)
   {
      status = "reasonable";
   }
}
a.
price      status
0      “”
22      “inexpensive”
      “reasonable”
b.
price      status
0      “inexpensive”
22      “reasonable”
c.
price      status
0      “”
22      “reasonable”
      “costly”
d.
price      status
0      “reasonable”
22      “costly”
 

 36. 

What will be printed by the statements below?
int a = 10;
int b = 20;
int count = 0;
if (a > 5)
   if (b > 5)
   {
      count ++;
   }
else
   count = 7;
System.out.print (count);
a.
0
c.
7
b.
1
 

 37. 

Assume a, b, and c have been defined to be integer variables.  Which of the following values make the expression !(a == b)    && (c > a)  true?
a.
a = 10, b = 10, c = 15
c.
a = 10, b = 2, c = 5
b.
a = 10, b = 20, c = 15
d.
a = 10, b = 20, c = 10
 

 38. 

Assume the following variables have been declared and given values elsewhere:
boolean isFelon;
int age;
A person may vote if they are  18 or older and not a felon.  Which of the following statements assigns the Boolean variable mayVote correctly?
a.
mayVote = (age >= 18) || !isFelon;
b.
mayVote = !((age >= 18) && isFelon);
c.
mayVote = (age >= 18) && !isFelon;
d.
mayVote = !((age >= 18) || isFelon);
 

 39. 

What is the output of the code snippet given below?

String s = "12345";
int i = 1;
while (i < 5)
{
   System.out.print(s.substring(i, i + 1));
   i++;
}
a.
No output
c.
12345
b.
1234
d.
2345
 

 40. 

What are the values of i and j after the following code snippet is run?

int i = 10;
int j = 20;
int count = 0;
while (count < 5)
{
   i = i + i;
   i = i + 1;
   j = j - 1;
   j = j - j;
   count++;
}
System.out.println("i = " + i + ", j = " + j);
a.
i = 45, j = 1
c.
i = 351, j = 2
b.
i = 351, j = 0
d.
i = 1311, j = 35
 

 41. 

Which of the following conditions can be added to the code below so it will loop until the value of sum is greater than 100?

Scanner in = new Scanner (System.in);
int sum = 0;
do {
   sum += in.nextInt();
}
while (/* put condition here */)
a.
sum != 0
c.
sum > 100
b.
sum <= 100
d.
sum == 100
 

 42. 

Which loop does not check a condition at the beginning of the loop?
a.
I and II
b.
I and III
c.
I only
d.
III only
e.
The do loop
II.  The while loop
III. The for loop
 

 43. 

What is the output of the code snippet given below?

String s = "abcdefghijkl";
int i = 1;
do
{
   if (i > 2)
   {
      System.out.print(s.substring (1, i));
   }
   i++;
}
while (i < 5);
a.
abcd
c.
bcbcd
b.
bcde
d.
cdef
 

 44. 

Which of the following loops executes exactly 10 times?
a.
for (int i = 0; i <= 10; i++) { }
b.
int i = 0;
boolean found = false;
do 
{
   i++;
   if (i % 10 == 0)
   {
      found = true;
   }
}
while (i < 10 && !found) ;
c.
int i = 0;
while (i <= 10)
{
   i++;
}
d.
for (int i = 1; i <= 10; i++)
 

 45. 

Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedals of a bicycle based on its gear combinations).  The user must provide the gear sizes, which must be converted into roll-out for all different gear combinations. How can the flow of user interaction for this problem be designed?
a.
Hand-tracing can confirm code that implements gear selection.
b.
Pseudocode can guide algorithm design through divide-and-conquer strategy.
c.
A storyboard can be used.
d.
The physical gears can lead to ideas for the correct algorithm to use.
 

 46. 

What is the last output line of the code snippet given below?

int i = 0;
while (i < 10)
{
   int num = 1;
   for (int j = i; j > 1; j--)
   {
      System.out.print(j + " ");
      num = num * 2;
   }
   System.out.println("***");
   i++;
}
a.
3 2 ***
c.
8 7 6 5 4 3 2 ***
b.
9 8 7 6 5 4 3 2 ***
d.
2 ***
 

 47. 

What will be the range of the random numbers generated by the following code snippet?

Random generator = new Random();
int r1 = generator.nextInt(50) + 1;
a.
Between 1 and 49
c.
Between 0 and 49
b.
Between 0 and 50
d.
Between 1 and 50
 

 48. 

Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with different probabilities? (Assume Random generator = new Random();)
a.
int sum = generator.nextInt(6) + generator.nextInt(6) + 2
b.
int sum = generator.nextInt(12) + 1
c.
int sum = generator.nextInt(6 + 1) + generator.nextInt(6 + 1)
d.
int sum = generator.nextInt(11) + 2
 

 49. 

What does the following code do?

int sum = 0;
final double count = 1000;
Random generator = new Random();

for (int i = 1; i <= count; i++)
{
   sum = sum + generator.nextInt(101);
}
System.out.println(sum / count);
a.
It simulates the outcome of throwing a coin.
b.
It calculates the average of 1000 random numbers between 0 and 100.
c.
It performs Monte Carlo simulation of fluid dynamics.
d.
It calculates the average of 1000 random numbers between 1 and 101.
 

 50. 

Which of the following is correct for simulating the toss of a pair of coins to get 0 (head)  or 1 (tail) with different probabilities? (Assume Random generator = new Random();)
a.
System.out.println(generator.nextInt(1) + " " + generator.nextInt(2));
b.
System.out.println((generator.nextDouble() + 2) * 2);
c.
System.out.println(generator.nextInt(1) + " " + generator.nextInt(1));
d.
System.out.println(generator.nextInt(2) + " " + generator.nextInt(2));
 



 
Check Your Work     Start Over