CIS 1068
Fall 2012
First Mid-term Exam
You may use one sheet of notes. You must work alone
In this first section, each answer is worth 2 points
- Mark each of the following as either valid or invalid:
a. int thisisareallylongvariablename;
b. double cranbe77y;
c. boolean 2b;
d. boolean not2b;
e. String “That is the question.”;
- Given the following code fragment:
int m=2;
int k;
int j;
double z;
k = 4;
j = 17;
z = 2.6;
a. k / m;
b. k % m;
c. j / m + 1;
d. j / (m + 1);
e. j / k + z;
f. j / (k + z);
Determine the value of each of the following expressions. If the result is a floating-point (real) number, be sure to include a decimal point in your answer.
- Given these values for the boolean variables x, y, and z:
x= true
y = false
z = true
Evaluate the following expressions. Write T for true or F for false.
- x || (y & z)
- (x || !y) & (!x || z)
- (x || y) & z
- !(x ||y) & z
- (x & (y || z))
- Given the following code fragment:
int x, y; //line 1
int k = 13;//line 2
x = 0;//line 3
y = 7;//line 4
for (k = 0; k < y / 2; k++)//line 5
{
x = x + 2 * k;//line 6
System.out.println(“The accumulated total is now:” + x);//line 7
}
System.out.println(“After the loop, i = “ + i);//line 8
Fill in the following table to formally trace the operation of the code. I have done the first two lines for you. (15 points)
line x y k action
1 ? ? ? allocate storage for ints x and y
2 ? ? 13 allocate storage for int k and initialize it to contain 13
- Consider the following code fragment:
int k, j;//line 1
k = 0;//line 2
while (k < 3)//line 3
{
j = 0;//line 4
while (j < 2)//line 5
{
System.out.println(“k = “ + k + “ j = “ + j);//line 6
j++;//line 7
}
k++;//line 8
}
Fill in the following table, just as you did in the previous problem. (15 points)
line k j action
- Given the following code fragment:
double score;
char grade;
if (score > =90)
{
grade = ‘A’;
}
if (score >= 80)
{
grade = ‘B’;
}
else
{
grade = ‘F’;
}
System.out.println(“Your grade is “ + grade);
a)What will be printed if score contains the value 90______?
b)What will be printed if score contains the value 80______?
c)What will be printed if score contains the value 70______? (6 points total)
- Suppose that the line above which reads “if(score >= 80)” read instead “else if (score >= 80)”:
a)What will be printed if score contains the value 90 ______?
b)What will be printed if score contains the value 80______?
c)What will be printed if score contains the value 79______?( 6 points total)
- Consider the following program:
public class Xyzzy
{
public static void main(String args[])
{
double x, y, z, weightedAve;
Scanner kb = new Scanner(System.in);
// write code here to ask the user for 3 doubles and store them in x, y, and z
weightedAve = MyStats.wAverage(x, y, z, 1.0, 2.0, 1.0); //This line is explained below
//write code here to print out the value of weightedAve
}
}
a) (4 points) Write the code specified in the comments in the above program (4 points)
b) (2 point) What necessary line is missing from the beginning of the above program (1 point)
You are now to create a function named wAverage that calculates the weighted average of three doubles based on three weights that you pass in.
The declaration will look like this:
public static double wAverage(double v1, double v2, double v3, double w1, double w2, double w3)
c) (2 points) What is the name of the file that the function wAverage must be in ______?
d) (2 points) What does the second word of the declaration (static) mean ______?
e) (2 points) What does the third word of the declaration (double) mean______?
This function should perform the calculation (v1 * w1 + v2 * w2 + v3 * w3) / (w1 + w2 + w3) and return the resulting value.
(14 points) Write the function wAverage and put it in the proper class.
Exam #1CIS 10731