Do all three parts of this exam. Maximum score is 100 points.
Part I Short Answer (20 points). Each question is worth 2 points. Partial credit may be
given.
1. The following statements generate a random number. What is the largest value that
randNum could be and what is the smallest value randNum could be?
Random generator = new Random();
int randNum = generator.nextInt(20) + 1;
. Largest is 20 Smallest is 1
2. Using a variable named grade, show the Java statement that represents the condition “if the
grade is between 70 and 100, inclusive”? (Inclusive means that you include 70 and 100.)
if(grade >= 70 && grade <= 100)
3. Which of the following logical operators has the highest precedence? Circle the correct
answer.
! && || < >
! has the highest precedence
4. What is the minimum number of times that the statements in a do-while loop will execute?
One
5. What happens if a case in a switch statement does not end with a break statement?
Execution will move into the next case.
CSE 110 Spring 2012 Name: ____Answers___________________
Exam 2 Seat: _______________________________________
6. Which of the following for loops header will cause the body of the loop to be executed
exactly 10 times? Circle the correct answer.
a) for(int i = 0; i <= 10; i++)
b) for(int i = 1; i < 10; i++)
c) for(int i = 1; i <= 11; i++)
d) for(int i = 0; i < 10; i++)
e) none of these for loops will execute the loop body 10 times
7. Consider the following Java statements where result is of type double:
if (result == 4.356)
result = Math.pow(2, 3);
What is the potential problem that may result from using the relational operator ==?
If result is really, really close, but not exactly 4.356, then the expression will evaluate to
false, which may not be what the programmer had intended.
8. A return statement is required at the end of every method. True or False?
9. What is the difference between an object and a class?
Class: template for an object with data and methods
Object: a specific instance of a class type
10. Show the Java statement you would need to have in your program if you wanted to use any
of the methods from the Math class.
import java.lang.Math or import.java.lang.*
Part IIa Reading Code (25 points)
Questions 11 – 16 are based upon the program below.
import java.util.*;
import java.lang.Math;
public class Exam2Program1A
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
int val;
System.out.print ("Enter an integer 1 - 4: ");
val = scan.nextInt(); // User input
do
{
switch (val)
{
case 1:
val++;
System.out.println (val);
break;
case 2:
val *= 2;
System.out.println (val);
case 3:
val = (int) Math.pow(val,2);
System.out.println (val);
break;
case 4:
val--;
System.out.println (val);
break;
default:
11. If the user enters a 1 when
prompted, what will the output be?
2
4
16
12. If the user enters a 2 when
prompted, what will the output be?
4
16
13. If the user enters a 3 when prompted,
what will the output be?
9
14. If the user enters a 4 when prompted,
what will the output be?
3
9
15. If the user enters a 9 when prompted,
what will the output be?
In default
Part II b Reading Code (25 points)
Consider the following program main and associated class Thing on the following page. Notice
that the Thing class requires the use of a random number generator from the Math class.
Furthermore, suppose that the first 8 random numbers that would be generated from this Thing
class are as follows (for ease of computation, I have simplified the actual values that would be
generated):
0.90 will make fValue = 6
0.30 will make fValue = 2
0.30 will make fValue = 2
0.50 will make fValue = 4
0.10 will make fValue = 1
0.60 will make fValue = 4
0.20 will make fValue = 2
0.60 will make fValue = 4
Given the information above, what is the output?
Face value 6 2
1 0
Face Value 2 4
1 0
Face value 1 4
2 0
import java.lang.*;
public class Gambling
{
public static void main (String [] args)
{
Thing d1 = new Thing();
Thing d2 = new Thing();
int counter;
for(counter = 1; counter <=3; counter++)
{
System.out.println ("Face value " + d1.roll() + "\t" + d2.roll());
System.out.println( + d1.howMany() + "\t" + d2.howMany());
}
System.out.println();
}
}
public class Thing
{
private final int MAX = 6;
private int fValue;
private int count = 0;
public Thing()
{
fValue =0;
}
public int roll()
{
fValue = (int) (Math.random() * MAX) + 1;
return fValue;
}
public int howMany()
{
if(fValue == 1 || fValue == 6)
count++;
return count;
}
public String toString()
{
String result = Integer.toString(fValue);
return result;
}
}
Part III Writing Code (30 points)
Write a complete Java program that uses nested for-loops to create the following output. Use all
appropriate style conventions for naming variables, aligning brackets, and so on.
Line 1 76543
Line 2 7654
Line 3 765
Line 4 76
Line 5 7
public class Exam2Program3a
{
public static void main (String [] args)
{
int count1;
int count2;
for(count1 = 1; count1<= 5; count1++)
{
System.out.print ("Line " + count1 + "\t");
for (count2 = 7; count2 >= count1 + 2; count2--)
{
System.out.print (count2);
}
System.out.println ();
}
}
}
Powered by TCPDF (www.tcpdf.org)