# PracticeMidterm

```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( &quot;k = &quot; + k + &quot; m = &quot; + m);
2. Evaluate the following logical expressions assuming f = 5:
a. (f &gt;= 0 || f != 10)
b. (f &gt;= 0 &amp;&amp; f != 10)
c. ! (f &gt;= 0 || f &lt; 5 )
3. What does the following code print out?
double x = 4.1;
double y = 5;
if (x &gt; y) {
System.out.println(&quot;yes&quot;);
}
else {
System.out.println(&quot;no&quot;);
}
4. What will the following code print?
int number = 15;
if (number &lt;=13) {
System.out.print(&quot;1&quot;);
}
else if (number &lt;= 15) {
System.out.print(&quot;2&quot;);
}
if (number &lt; 17) {
System.out.print(&quot;3&quot;);
}
System.out.print(&quot;4&quot;);
5. What does the following code print out?
double x = 4.1;
double y = 5;
if (x &gt; 3.9 &amp;&amp; (x+y &gt;= 10 || y-x &gt; 0)) {
System.out.println(&quot;yes&quot;);
}
else {
System.out.println(&quot;no&quot;);
}
6. (5 pts) Show what is printed by the following program segment. You may use the trace table to help you:
i
i&lt;=4 x
x&gt;20
Console
int x;
int i=1;
while (i&lt;=4)
{
x = i*10 + i;
if ( x &gt; 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 = &quot;abra&quot;,
System.out.println(
System.out.println(
System.out.println(
System.out.println(
s1
s1
s2
s1
);
+ &quot; &quot; + s2);
+ s1);
+ &quot; &quot; + 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, &quot;x&quot;, is entered. For
each word entered it tells the length of the word and whether the word equals &quot;bravo&quot;. 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 &quot;x&quot; is entered, it displays the number of times &quot;bravo&quot; was
entered.
public class LetterCount {
public static void main (String [] args ){
}
}
```