Chap4+5rev

advertisement
PART 1 SHORT ANSWER QUESTIONS: samples only indicate types of questions,
coverage of material is not exhaustive.
1.
What would be the output of the following code fragment Indicate blank spaces
by b. Indicate blank lines by bl
int one=1, two=-20;
System.out.println("Print integer” + two +
”, number is ” + max);
System.out.print("\n\n");
while(one > -15)
{
System.out.print(one + “ “);
one++;
}
System.out.print("\n\n");
For(one = 9; one > 5; one--)
{
System.out.print((one*one)+ “ “);
}
System.out.println();
System.out.println("I’m done”);
Example;
System.out.println(“This is a test, print integer \n“ +
“number is, “ + two + “ next number is ”, one);
Thisbisbabtest, bprintbintegerb
number is, b-20bnextbnumberbisb1
2. Define the following terms (not an exhaustive list)
a. Repetition structure
b. Decision or selection structure
c. Branch
d. loop
e. Sentinel variable and sentinel value
f. Infinite loop
g. Relational expression
h. Logical expression
i. Counter controlled while loop
j. Sentinel controlled while loop
k. Flag controlled while loop
l. EOF controlled while loop
m. Flag variable
n. One way selection
o. initial statement, loop condition and update statement in a for loop
p. loop condition
q. short circuit OR
3. List the relational operators in Java.
4. List the logical operators in Java
5. What is the precedence of the logical and relational operators in Java?
6. Use flowcharts and one or two short sentences to explain the difference
between a while loop and a do…while loop..
7. Write a do loop to sum the integers between 45 and 74. It will be assumed that
all variables you use are of type integer. You need not declare your variables.
8. Write a while loop to print the characters from ‘a’ to ‘j’.
9. What would be printed by the following switch statement for each value of
integer x between 0 and 5.
switch(x)
{
case 0: System.out.println(“Hello”);
case 1; System.out.println(“This is easy”);
break;
case 2: System.out.println(“case 2”);
case 4: break;
default: System.out.println(“exiting on error”);
}
.
10. Based on the following variable declarations indicate the value of each logical
or relational expression below concisely explaining how that value is
determined. If the expression is invalid explain why. Indicate whether each
expression is a relational expression.
Boolean h1=true, h2=true, h3=false;
Integer a=0, b=23, c=87, d=45;
Char c1=’A’, c2=’C’, c3 = ‘Z’;
a) c2 < c1
b) h1 & h2
c) a>=b || h3
d) !(h1 && h3)
e) !h2 || h3
f) !h3
g) c1<c2 && !(c3>c1)
h) c1==c3
11. Based on the declarations in problem 10 what is the difference between the
following pairs of statements and how they are evaluated
a. b<a !! b<c
b<a | b<c
b. c1>c2 && h2
c1>c2 & h2
12. Shown below, between two lines of dashes is a Java code fragment, and the
content of a data file. Based on the code fragment, the data file, and the
following assumptions answer the questions below the data file.
Assumptions:
i. the data file has been opened and connected to BufferedReader
inFile
ii. all correct data values are larger than zero
iii. ncount is an integer variable
iv. all other undeclared variables are doubles.
----------------------------------------------------------------------String newLine;
CODE FRAGMENT
min2
max2
ncount
mean2
=
=
=
=
100000000.0;
-10000000.0;
0;
0.0;
// read data and accumulate statistics
newLine = inFile.readLine();
while( newLine != null)
{
datapoint = Double.parseDouble(newLine);
mean2 += datapoint;
if( datapoint < 0.0 )
{
break;
}
if( datapoint > max2)
{
max2 = datapoint;
System.out.println(" Maximum changed to: \n" + max2);
}
if( datapoint < min2)
{
min2 = datapoint;
System.out.println(" Minimum changed to: \n" + min2);
}
ncount++;
newLine = inFile.readLine();
}
mean2 /= ncount;
----------------------------------------------------------------------43.55
DATA FILE
27.6
23
97 43
11.111
0.98765
421.3
777
1.3333333333
-23.5
22.8907
-----------------------------------------------------------------------
a. How many times is the phrase ‘Maximum changed’ printed in the
resulting output?
b. What is the value of min2 at the end of the fragment? How would this
value change if the break statement was changed to a continue statement
and why?
c. What is the value of ncount after the code fragment has completed?
d. Does the variable mean2 hold the correct mean at the end of the code
fragment? Why or why not?
PART 2: Indicating the difficulty level of programming problems to expect
13. (Harder)Write a Java program to calculate the sum and the sum of the squares
of a list of integers of length N in a file named myinfile. You may assume there
are no errors in the data that is you need not verify your data in any way
Your main method will include a loop in which one data value is read from data
file myinfile into int variable Datain. The value of Datain is added to the sum,
and the square of the value of Datain is added to the sum of squares.
After the loop is complete your main method will print two lines of output to
the console Each line of output includes an explanatory message and the final
values of one of the two variables SumData and SumDataSq.
14. (Easier)Write a Java main program to determine which of the following bins an
integer should be placed into.
Bin 1: Nextdata < 0.0
Bin 2: 0.0<=NextData<= 50
Bin 3: 50 < NextData
The program prompts for and reads an integer from the keyboard, determines which
bin the integer belongs in, then prints a message including the value of the input
integer and the Bin number into which it should be placed. You need not verify
your input data.
Download