CSIS 10A Practice Midterm Exam 100 Points

advertisement
CSIS 10A
Practice Midterm Exam
100 Points
Name_________________
Closed Computer  One double sided sheet of notes allowed
1. Give the output of the following program fragment:
int
k =
m =
k =
k=6; m = 12;
k + m;
k - m;
k - m;
// k now = 18
// m now = 6
// k now = 12
Sysem.out.println( "k = " + k + " m = " + m);
k = 12 m=6
2. Evaluate the following logical expressions assuming f = 5:
a. (f >= 0 || f != 10)
true
b. (f >= 0 && f != 10)
true
c. ! (f >= 0 || f < 5 )
false
3. What does the following code print out?
prints: no
double x = 4.1;
double y = 5;
if (x > y) {
System.out.println("yes");
}
else {
System.out.println("no");
}
4. What will the following code print?
prints: 2 3 4
int number = 15;
if (number <=13) {
System.out.print("1");
}
else if (number <= 15) {
System.out.print("2");
}
if (number < 17) {
System.out.print("3");
}
System.out.print("4");
5. What does the following code print out?
prints: yes
double x = 4.1;
double y = 5;
if (x > 3.9 && (x+y >= 10 || y-x > 0)) {
System.out.println("yes");
}
else {
System.out.println("no");
}
6. (5 pts) Show what is printed by the following program segment.
i
int x;
1
int i=1;
2
while (i<=4)
3
{
x = i*10 + i;
4
if ( x > 20 ){
5
System.out.println(x+5);
}
else {
System.out.println(x-2);
}
i=i+1;
You may use the trace table to help you:
i<=4 x
x>20
Console
T
11
F
9
T
22
T
27
T
33
T
38
T
44
T
49
F
}
7. What are the outputs of this code?
OUTPUT
String
s1 = "abra",
System.out.println(
System.out.println(
System.out.println(
System.out.println(
s1
s1
s2
s1
s2 = "cad";
);
+ " " + s2);
+ s1);
+ " " + s2 + s1);
abra
abra cad
cadabra
abra cadabra
The following Javadoc is for the NumericalStuff class we demonstrated in lecture:
class NumericalStuff
This class contains various static methods involving numbers.
Method Summary
static double halfOf(double d)
Returns half of the given double value.
static boolean isPrime(int p)
Returns true
or false, depending on whether or not the given integer is a prime.
static int smallestPrimeFactor(int f)
Finds and returns the smallest prime factor of the given int.
static int sumOfIntsInRange(int low, int high)
Returns the sum of the numbers low + (low+1) + (low+2) + ... + high.
8. Use the methods defined in the NumericalStuff class above to:
a. Print out the smallest prime factor of 350
b. Print out the sum of the integers between 100 and 200 inclusive
c. Tell whether or not 123457 is a prime number
a)
System.out.println( NumericalStuff.smallestPrimeFactor(350));
b)
System.out.println( NumericalStuff.sumOfIntsInRange(100,200) );
c) if ( NumericalStuff.isPrime(123457) ){
System.out.println( "prime");
}
else {
System.out.println( "not prime");
}
9. Write a program that inputs words one at a time until a word with only one letter, "x", is entered. For
each word entered it tells the length of the word and whether the word equals "bravo". For example,
the following session has the user input in bold and the computer output in normal font:
enter a list of words, 'x' to finish:
apple
length is 5
bravo
length is 5
bravo to you too!
cantaloupe
length is 10
x
length is 1
done. thank you for using LetterCount
OPTIONAL, 3 pts Extra Credit: after an "x" is entered, it displays the number of times "bravo" was
entered.
public class LetterCount {
public static void main (String [] args ){
Scanner in = new Scanner(System.in);
String word;
int bravoCount = 0;
System.out.println("enter a list of words, \'x\' to finish:");
do {
word = in.next();
System.out.println("length is " + word.length() );
if (word.equals("bravo"){
System.out.println("bravo to you too!");
bravoCount = bravoCount+1;
}
} while ( ! word.equals("x") );
}
}
Download