PracticeMidterm

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=6; m = 12;
k = k + m;
m = k - m;
k = k - m;
Sysem.out.println( "k = " + k + " m = " + m);
2. Evaluate the following logical expressions assuming f = 5:
a. (f >= 0 || f != 10)
b. (f >= 0 && f != 10)
c. ! (f >= 0 || f < 5 )
3. What does the following code print out?
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?
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?
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. You may use the trace table to help you:
i
i<=4 x
x>20
Console
int x;
int i=1;
while (i<=4)
{
x = i*10 + i;
if ( x > 20 ){
System.out.println(x+5);
}
else {
System.out.println(x-2);
}
i=i+1;
}
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);
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)
b)
c)
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 ){
}
}
Download