SOLNS

CSIS 10A
Midterm Exam
100 Points
SOLUTIONS
1. (10 pts) For each of the following program fragments, what is the value of the variable x after the statements
execute?
Value of x after execution:
a. int y = 10;
int x = y;
10
y = y * 3;
b. int x = 10;
x = x / 2;
x = x + 3 * x - 3;
17
c. double x = 3.0 / 2.0;
1.5
d. boolean x = ( 2 < 3 );
true
( that's 5 + (3*5) – 3 ) due to operator precedence
2. (10 pts) What is the value of each of the following logical expressions?
a. (2 <= 2) && (2 >= 2)
T && T = T
b. (!true) || (!false)
F || T = T
c.
false
3 != 3
3. (10 pts) Consider the following statement:
if
(x>20 ){
System.out.println("Hi!");
}
else if (x>10){
System.out.println("OK!");
}
else {
System.out.println("Low!");
}
a) What would be the output from the above statement when the value of x is 22? HI!
b) How about when the value of x is 12?
OK!
c) How about when the value of x is 2?
LOW!
4. (10 pts) Consider the following statement:
if
(x>20 && y < x ){
System.out.println("rowdy!");
}
else if (x>20 || y < x){
System.out.println("stoked!");
}
else {
System.out.println("psyche!");
}
a) What would be the output when the value of x is 22 and y is 4?
rowdy!
b) How about when the value of x is 22 and y is 40?
stoked!
c) How about when the value of x is 2 and y is 1?
stoked!
d) How about when the value of x is 5 and y is 25?
psyche!
5. (5 pts) Show what is printed by the following program segment.
z
int z = 16;
16
while (z >= 0)
11
{
z = z - 5;
6
System.out.println(" z=" + z);
}
1
-4
You may use the trace table to help you:
z>=0
Console
T
z=11
T
z=6
T
z=1
T
z=-4
F
6. (5 pts) Show what is printed by the following program segment. You may use the trace table to help you:
z
a
z>=a
Console
int z = 16, a=2;
16
2
T
while (z >= a)
14
6
a=6 z=14
{
T
z = z - a;
8
10
a=10 z=8
a = a + 4;
F
System.out.println("a="+ a + " z=" + z);
}
7. (5 pts) How many times does the following code print out an asterisk?
int a = 0;
do {
a = a + 1;
System.out.print("*");
}while(a < 7);
8. (5 pts) Show what is printed by the following program segment.
z
int z = 16, a=2;
16
do {
11
z = z - 5;
6
if ( z < 10 ){
1
a = a + 4;
-4
}
System.out.println("a="+ a + " z=" + z);
} while (z >= 0);
You may use the trace table to help you:
a
z>=0 z<10
Console
2
T
F
a=2 z=11
6
T
T
a=6 z=6
10 T
T
a=10 z=1
14 F
T
a=14 z=-4
9. (5 pts) Add a logical expression after if that compares the two String objects to see if they are the same.
Scanner stuff = new Scanner(System.in);
String me = "yooper";
String you = stuff.next();
if (
me.equals(you)
) {
System.out.println("we are both yoopers");
}
10. (5 pts) Match the type of class with the description:
Description of class
type of class (from right)
a) class Math
lass wih static utility methds _
b) used to make objects
Instantiable class
c) used to write programs
application class
Type of Class
application class
class with static utility methods
Instantiable class
The following Javadoc is for the StringUtil class we demonstrated in lecture:
Class StringUtil this class contains static methods for working with Strings
Method Summary
static java.lang.String firstLetterOf(java.lang.String s)
Returns the first letter of the given String, s
static boolean isPalindrome(java.lang.String t)
Returns true if the given String is
a palindrome, and false otherwise.
static java.lang.String letterOf(java.lang.String s, int index)
Returns the letter of the String s at index index.
static int numberOfOccurrencesOf(java.lang.String letter, java.lang.String s)
Returns the number of times the given letter appers in the given String.
11. (10 pts) Suppose we have a String variable named name. It has been declared, and the user has entered
their name into it. Using methods of the StringUtil class listed above, write some Java statements that:
a. Prints out how many times the letter "a" appears in the variable name
b. Prints out what the first letter of name is.
c. Tells whether or not name is a Palindrome
For example, if name containts "Anna", your code for part a) will say "Number of a's is 2", b) will say "First letter
is A", and c) will say "it's a palindrome!".
However, if name contains "Jack", your could for part a) will say "Number of a's is 1", b) will say "First letter is J",
and c) will say "not a palindrome!".
a)
System.out.println( "Number of a's is " +
StringUtil.numberOfOccurrencesOf("a",name));
b)
System.out.println( "First letter is " +
StringUtil.firstLetterOF(name));
c)
if (StringUtil.isPalindrome(name)) {
System.out.println("it's a palindrome!");
}
else {
System.out.println("Not a palindrome!");
}
12. (worth 20 points) the Del Monte canning facility needs you to write a complete program that asks the
user to input how many artichokes they have. After that they will enter each of their artichoke weights
in ounces. For each weight entered, the program will output a message indicating if the artichoke is
TOO BIG (weight > 32) , NORMAL (weight between 24 and 32 ounces) or TOO SMALL (otherwise).
OPTIONAL, 3 pts Extra Credit: after all the weights are entered, it displays how many artichokes were
TOO SMALL.
import java.util.Scanner;
public class Artichoke {
public static void main (String [] args ){
// declare variables
Scanner input = new Scanner(System.in);
int number, wt, small = 0;
// get artichoke count
System.out.println("How many artichokes?");
number = input.nextInt();
// loop through all artichotes, getting weight and classifying
while (number > 0 ) {
System.out.println("Enter the weight");
wt = input.nextInt();
if (wt > 32){
System.out.println("Too Big");
}
else if (wt > 24) {
System.out.println("Normal");
}
else {
System.out.println("Too Small");
small = small + 1;
}
}
System.out.println("There were " + small + " too small");
}
}