CSCI A201/A597 and I210

Spring Semester 2003


Directions:

Good luck and do well!

Write your name or username here:______________________


For the following 5 (five) exercises consider the following method definitions:

public static int f(int x) {
  return h(h(x)); 
}
public static int g(int x) { return k(k(x)); }
public static int h(int x) { return 1 + k(x); }
public static int k(int x) { return x - 1; }
Question 1.
What does g(2) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

Note: To save space I wrote other where I would have put "None of the above."


Question 2.
What does g(h(2)) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

Question 3.
What does k(g(2) + h(2)) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

Question 4.
What does f(0) + f(1) + f(2) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

Question 5.
What does f(2) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other


Question 6.
"0" is an example of what kind of constant?
  1.   int
  2.   char
  3.   boolean
  4.   String
  5.   other

Question 7.
Assume that x is an integer variable. Simplify the following expression:
!(((x - 1) >= 4) && ((x - 1) <= 4))
  1.   true
  2.   false
  3.   x != 5
  4.   x == 5
  5.   other

Warning: Don't overlook the logical negation (bang!) at the front.


Question 8.
Consider the following two program fragments. Assume that x is an int variable.
       // fragment 1         |         // fragment 2
       if (x == 5)           |         if (x == 5)
          x = x + 1;         |            x = x + 1;
       else                  |         if (x != 5)
          x = 8;             |            x = 8;
Which of the following statements is false?

  1. The two fragments are not logically equivalent.
  2. x always has the value of 8 after executing fragment 2.
  3. x is always divisible by 2 (even) after executing fragment 1.
  4. x has either a value of 5 or a value of 8 after executing fragment 2.
  5. The first fragment could be simplified as: x = (x != 5) ? 8 : 6;

Reminder: Last option above uses the selection operator (page 189 in your text).


Question 9.
You compile and run this program. What is the output (or outcome)?
class Wan {
    static int value; 
    Wan() { value += 1; }
    public static void main(String[] args) {
	Wan a = new Wan(); 
	a = new Wan(); 
	a = new Wan(); 
	System.out.println(Wan.value); 
    } 
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

Question 10.
Assume that x and y are integer variables and the following nested if statement.
if (x > 3) {
  if (x <= 5) y = 1;
  else if (x != 6) y = 2;
       else y = 3;
} else y = 4;
If y has a value of 3 after executing the above program fragment, then what do you know about x?
  1. x != 6
  2. x == 6
  3. x == 4
  4. x != 4
  5. (x > 3) && (x <= 5)
  1.    1
  2.    2
  3.    3
  4.    4
  5.    5

Question 11.
Which of the following expressions does not print a 4 (four)?
  1. System.out.println('M' - 'J' + 1);
  2. System.out.println((4 * 5 + 2) / 7 + 1);
  3. System.out.println("012".length() + 1);
  4. System.out.println(8 - 3 - 2 + 1);
  5. System.out.println('3' + 1);
  1.    1
  2.    2
  3.    3
  4.    4
  5.    5
Note: There are no typos!

Question 12.
After the following statement is executed,
int x = (int)(Math.random() * (30 - 10) + 20);
  1.   22
  2.   37
  3.   27
  4.   42
  5.   32
which of these numbers could NOT possibly be contained in x?

Question 13.
What is the output produced by the following three lines (when embedded in a complete program)?
int x, y;
x = 7; y = 5; 
System.out.println("1" + x + y); 
  1.   13
  2.   17
  3.   75
  4.   175
  5.   other

Question 14.
What is the output produced by the following three lines (when embedded in a complete program)?
int x, y;
x = 9; y = 10; 
System.out.println("1" + x * y); 
  1.   0
  2.   19
  3.   91
  4.   1910
  5.   other

Question 15.
What is the output produced by the following three lines (when embedded in a complete program)?
int x, y;
x = 9; y = 10; 
System.out.println("1" * x + y); 
  1.   0
  2.   19
  3.   91
  4.   1910
  5.   other

Question 16.
What is the output produced by the following two lines (when embedded in a complete program)?
int a; a = 'Q' - 'M'; 
System.out.println(a); 
  1.   Q
  2.   L
  3.   M
  4.   4
  5.   other

Question 17.
What is the output produced by the following program fragment (when embedded in a complete program)?
if ('Z' > 'B') 
  System.out.print("1" + 3 / 2); 
System.out.print(2);
  1.   12
  2.   11
  3.   "1"1.5
  4.   2.5
  5.   other

Question 18.
What is the type of this expression:
"1" + 'a'
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   other

Question 19.
What is the output produced by the following three lines (when embedded in a complete program)?
int a, b;
a = 3; b = 2;  
System.out.println(b * (a / b) + a % b);
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

Question 20.
What's the output produced by the following lines (when embedded in a complete program)?
String quote = "There's a tomato" +  
              "in every automaton!";
System.out.println(quote.substring(29, 35)); 
  1.   utomat
  2.   tamotu
  3.   otomat
  4.   tomato
  5.   other

Question 21.
What's the output produced by the following lines (when embedded in a complete program)?
String a = "sun"; 
String b = "dried"; 
System.out.println(
  "tomato".substring(b.length() - a.length(), 
                     a.length()));
  1.   a
  2.   m
  3.   o
  4.   t
  5.   other

Question 22.
Given String a = "tomato"; which of the following expressions is NOT of type String?
  1. a
  2. "a"
  3. 'a'
  4. a + a.length
  5. a.substring(0, 1) + 'a'
  1.   1
  2.   2
  3.   3
  4.   4
  5.   5

Question 23.
What's the output produced by the following lines (when embedded in a complete program)?
String a = "a"; 
a.toUpperCase();
System.out.println(a + "a" + a.toUpperCase());
  1.   aaa
  2.   aaA
  3.   AaA
  4.   AAA
  5.   other

Question 24.
Which of the following expressions is NOT logically equivalent to (a >= b)?
  1. (b <= a)
  2. (b < a) || (b == a)
  3. (b == a) && (b < a)
  4. (b - a) <= 0
  5. (b <= a) && (a >= b)
  1.   1
  2.   2
  3.   3
  4.   4
  5.   5

Question 25.
What's the output produced by the following lines (when embedded in a complete program)?
int a = 100;
if (a >= 100) 
  System.out.print("a ");
else if (a >= 10)
  System.out.print("b ");
if (a <= 1000) 
  System.out.print("c ");
  1.   a
  2.   a b
  3.   a c
  4.   a b c
  5.   other

Question 26.
Consider the following expression in which m is an int.
(m < 10) && (m > 12) 
Which of the following is a valid Java simplification of the expression above?
  1.   (m == 11)
  2.   true
  3.   (m != 11)
  4.   false
  5.   (10 > m > 12)
  1.   1
  2.   2
  3.   3
  4.   4
  5.   5

Question 27.
Assume a and b are integer variables. If a has a value of 2 after executing the following program fragment what was the value of b at the beginning of the fragment code?
if (b > 2 && b < 6) 
  a = 1; 
else 
  a = 3; 
if (b % 2 == 0) 
  a = a + 1; 
  1.   2
  2.   3
  3.   4
  4.   5
  5.   other

Question 28.
What is the output produced by the following three lines (when embedded in a complete program)?
int a, b;
a = 8; b = 3;  
System.out.println(b * a / b + a % b);
  1.   3
  2.   5
  3.   8
  4.   24
  5.   other

Question 29.
What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.)
int x = 1;
if (2 > x) 
  System.out.print(1); 
else 
  System.out.print(11); 
if (x < 5) {
  System.out.print(1);  
  System.out.print(1);
} 
  1.   1
  2.   11
  3.   111
  4.   1111
  5.   other

Question 30.
What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.)
int x = 1;
if (x > 2) { 
  if (x < 5) 
    System.out.print(1); 
} else 
  System.out.print(1); 
System.out.print(11);
  1.   1
  2.   11
  3.   111
  4.   1111
  5.   other

Question 31.
What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.)
int x = 2; 
while (x > 1) 
  x = x - 1;
  System.out.print(x);
  1.   1
  2.   2
  3.   21
  4.   0
  5.   other

Question 32.
What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.)
int i; 
for (i = 0; i < 3; i = i + 1) 
  System.out.print(1); 
System.out.print(0); 
  1.   0
  2.   1110
  3.   11110
  4.   101010
  5.   10101010

Question 33.
What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.)
if (2 <= 3) 
  if (0 != 1) 
    System.out.print(0); 
  else 
    System.out.print(1); 
System.out.print(2); 
if (2 > 3) 
  if (0 == 1)
    System.out.print(3); 
  else 
    System.out.print(4); 
System.out.print(5); 
  1.   023
  2.   024
  3.   025
  4.   125
  5.   other

Question 34.
What is the output produced by the following code when embedded in a complete program?
boolean x; 
if (true) 
  System.out.print(0); 
else 
  System.out.print(1); 
x = (1 < 2) && (4 < 3); 
if (x) 
  System.out.print(2);   
else 
  System.out.print(3); 
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

Question 35.
What is the output produced by the following code when embedded in a complete program?
boolean x = false; 
if (true) 
  System.out.print(0); 
else 
  System.out.print(1); 
x = x || !x; 
if (x) 
  System.out.print(2);   
else 
  System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

Question 36.
What is the output produced by the following code when embedded in a complete program?
boolean x = false; 
if (true) 
  System.out.print(0); 
else 
  System.out.print(1); 
x = (false && false || true); 
if (x) 
  System.out.print(2);   
else 
  System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

Question 37.
What is the output produced by the following code when embedded in a complete program?
boolean x = false; 
if (true) 
  System.out.print(0); 
else 
  System.out.print(1); 
x = (false && (false || true)); 
if (x) 
  System.out.print(2);   
else 
  System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

Question 38.
What is the output produced by the following code when embedded in a complete program?
int x = 10, y = 3; 
while (x > 0 && y > 0) { 
  x = x - y; 
}
System.out.print(x);
  1.   0
  2.   -1
  3.   -2
  4.   -3
  5.   other

Question 39.
What is the output produced by the following code when embedded in a complete program?
int x = 10, y = 3; 
while (x > 0 && y > 0) { 
  x = x - y; 
  y = y + 1;
}
System.out.print(x);
  1.   0
  2.   -1
  3.   -2
  4.   -3
  5.   other

Question 40.
What is the output produced by the following code when embedded in a complete program? (Note that the fragment may or may not be indented correctly so please think about it carefully.)
int x;
for (x = 0; x < 10; x = x + 2) 
  if (x % 3 == 1) 
    System.out.print(x);
  System.out.print(x % 3); 
  1.   4
  2.   41
  3.   02402
  4.   024102
  5.   other

Question 41.
Consider the following program, it prints out a pattern. Which of the following letter patterns is closest in shape to the pattern that the program produces?
public class One {
  public static void main(String[] args) {
    int size = 10, x, y;
    for (x = 0; x < size; x++) { 
      for (y = 0; y < size; y++) 
        if ( y == 0 || 
             y == size - 1 || 
            (x - y == 0))
          System.out.print(" *"); 
        else 
          System.out.print("  "); 
      System.out.println(); 
    }
  } 
}
  1.   Z
  2.   A
  3.   N
  4.   Y
  5.   other

Question 42.
Consider the following program, it prints out a pattern. Which of the following letter patterns is closest in shape to the pattern that the program produces?
public class One {
  public static void main(String[] args) {
    int size = 10, x, y;
    for (y = 0; y < size; y++) { 
      for (x = 0; x < size; x++) 
        if ( y == 0 || 
             y == size - 1 || 
            (x + y == size - 1))
          System.out.print(" *"); 
        else 
          System.out.print("  "); 
      System.out.println();
    } 
  } 
}
  1.   Z
  2.   A
  3.   N
  4.   Y
  5.   other

Question 43.
Assume that
  • expr1 evaluates to true,
  • expr2 evaluates to false, and
  • expr3 evaluates to true.
Which of the following expressions evaluates to false?
  1.    expr2 || expr1 && expr3
  2.    expr2 || (expr1 && expr3)
  3.   (expr2 || expr1) && expr3
  4.   !expr2 && expr1 && expr3
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

Question 44.
What gets printed when you compile and run the following program?
public class A {
  public static void main(String[] args) {
    System.out.println(nuf(fun(5, nuf(fun(4, 3), 2)), 1)); 
  } 
  public static int fun(int a, int b) {
    return a - b;
  } 
  public static int nuf(int b, int a) {
    return a - b; 
  }
}
  1.   -3
  2.   -2
  3.   -1
  4.   0
  5.   other
There are no typos, so please don't rush!

Question 45.
You compile and run this contrived program. What is the output?
class Account {
  public static void main(String[] args) {
    Account e = new Account(); 
    Account f = new Account(10); 
    System.out.println(e.getBalance() + f.getBalance()); 
  } 
  Account() { this(20); } 
  Account (int n) { balance = n; } 
  int balance; 
  int getBalance() { return balance + 30; }
}
  1.   10
  2.   30
  3.   60
  4.   90
  5.   other

Question 46.
Exactly how many question marks will appear on the screen when the following program fragment is executed?
for (int i = 0; i < 10; i += 3) 
  for (int j = 0; j < 10; j += 3) 
    System.out.print("?"); 
System.out.println(); 
  1.   4
  2.   16
  3.   25
  4.   36
  5.   other

Question 47.
What does the following program's output most closely resemble?
public class Nine {
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++) {
               if (i == 9 || j == 0 || 9 == j) {
                 System.out.print(" " + j);
               } else {
                 System.out.print("  ");
               }
           }
           System.out.println();
         }
     }
}
  1.   C
  2.   Z
  3.   U
  4.   N
  5.   other

Question 48.
You compile and run this program. What is the output that it produces?
class One {
  int a = 8;
  int b = 1;
  void fun() {
    b *= 2;
    a /= 2;
  }
  void report() {     
    System.out.print(a + " " + b + " "); 
  }
  public static void main(String[] args) {
    One alpha = new One();
    alpha.fun();
    alpha.fun();
    alpha.fun();
    alpha.report(); 
  }
}
  1.   1 8
  2.   4 2
  3.   8 1
  4.   4 8
  5.   other


Question 49.
You compile and run this program. What is the output that you obtain?
class One {
  int a = 8;
  int b = 1;
  void fun() {
    b *= 2;
    a /= 2;
  }
  void report() {     
    System.out.print(a + " " + b + " "); 
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
    beta.report(); 
  }
}
  1.   1 8
  2.   4 2
  3.   8 1
  4.   4 8
  5.   other

Question 50.
You compile and run this program. What is the output that you obtain?
class One {
  int a = 8;
  static int b = 1;
  void fun() {
    b *= 2;
    a /= 2;
  }
  void report() {     
    System.out.print(a + " " + b + " "); 
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
    beta.report(); 
  }
}
  1.   1 8
  2.   4 2
  3.   8 1
  4.   4 8
  5.   other

Question 51.
What is the result of attempting to compile and run the following code?
class Example {
  public static void main(String[] args) {
    Example e = new Example(); 
    System.out.println(e.fun() + e.fun(1)); 
  } 
  int fun() { return 1 + fun(1); } 
  int fun(int n) { return 1 + n; } 
} 
  1.   1
  2.   3
  3.   5
  4.   7
  5.   other

Question 52.
Which of the following numbers is closest to the output of this line if embedded in a complete program, that is then compiled and run?
int a, b;
a = 10; b = 4;  
System.out.println(b * a / b + a % b);
  1.   10
  2.   20
  3.   30
  4.   40
  5.   50

Question 53.
What is the output produced by the following code when embedded in a complete program?
if (false) System.out.print(0); 
else System.out.print(1); 
boolean x = (1 < 2) && (! (4 < 3)); 
if (x) System.out.print(2);   
else System.out.print(3); 
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

Question 54.
What is the output produced by the following code when embedded in a complete program?
boolean x; 
if (x = (false && (false || true))) System.out.print(0); 
else System.out.print(1); 
if (! x) System.out.print(2);   
else System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other
(Think carefully. Yes, it compiles).
Question 55.
What is the output produced by the following code when embedded in a complete program?
boolean x; 
if (x = (false && false || true)) System.out.print(0); 
else System.out.print(1); 
if (! x) System.out.print(2);   
else System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other
(Think carefully. Yes, it compiles).
Midterm Two - Morrison Hall 007 7-9pm Apr 2 2003 A201/A597/I210 Exam