CSCI A201/A597 and I210

Midterm Exam One

Second Summer 2002


Directions:

Good luck and do well!

Write your name or username here:______________________


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

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


2. What is the output produced by the following three lines (when embedded in a complete program)?
int x, y;
x = 3; y = 2; 
System.out.println("1" + (x + y)); 
  1.   132
  2.   33
  3.   13
  4.   15
  5.   6

3. What is the output produced by the following two lines (when embedded in a complete program)?
char a; a = 3 + 'B'; 
System.out.println(a); 
  1.   E
  2.   B
  3.   C
  4.   3
  5.   other

4. What is the output produced by the following program fragment (when embedded in a complete program)? Note that the indentation may or may not be right so please be careful.
if ('C' < 'A' + 3) 
  System.out.print(1 + 3 / 2); 
  System.out.print(2);
  1.   2
  2.   1.52
  3.   12
  4.   22
  5.   other

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


6. What is the type of this expression:
1 == 3
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   other

7. What is the type of this expression:
1 / 2
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   other

8. What is the type of this expression:
"abc".length()
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   other

9. What is the type of this expression:
"abc".length() == 3
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   other

10. What is the type of this expression:
"appearance".substring(2, 6)
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   other


11. What is the type of this expression:
"abc".toUpperCase()
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   other


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

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

14. 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 = 3;
if (2 > x) 
  System.out.print("*"); 
else 
  System.out.print("*"); 
if (x < 2) 
  System.out.println("*"); 
System.out.print("*");
  1.   *
  2.   **
  3.   ***
  4.   ****
  5.   other

15. 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 = 3;
if (x > 5) { 
  if (x < 10) 
    System.out.print("**"); } 
else 
  System.out.print("*"); 
  System.out.print("*");
  1.   *
  2.   **
  3.   ***
  4.   ****
  5.   other

16. 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 = 3;
if (x <  5 || x > 10)  
  System.out.print("*"); 
if (x <  5 && x > 10) 
  System.out.print("*"); 
if (x < 10 && x >  5) 
  System.out.print("*"); 
if (x < 10 && x >  5) 
  System.out.print("*"); 
System.out.print("*");
  1.   *
  2.   **
  3.   ***
  4.   ****
  5.   other

17. 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 = 3;
if (x <  5 || x > 10)  
  System.out.print("*"); 
else if (x <  5 && x > 10) 
  System.out.print("*"); 
else if (x < 10 && x >  5) 
  System.out.print("*"); 
else if (x < 10 && x >  5) 
  System.out.print("*"); 
else System.out.print("*");
  1.   *
  2.   **
  3.   ***
  4.   ****
  5.   other

18. 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 && 0 != 1) 
  System.out.print("*"); 
else 
  System.out.print("*"); 
System.out.print("*"); 
if (2 > 3 || 0 == 1) 
  System.out.print("*"); 
else 
  System.out.print("*"); 
System.out.print("*"); 
  1.   *
  2.   **
  3.   ***
  4.   ****
  5.   other

19. What is the output produced by the following code when embedded in a complete program?
boolean x = false; 
if (true) 
  System.out.print(10); 
else 
  System.out.print(1); 
x = x || !x; 
if (x) 
  System.out.print(0);   
else 
  System.out.print(00);  
  1.   10
  2.   100
  3.   1000
  4.   10000
  5.   other

20. What is the output produced by the following code when embedded in a complete program?
if (!false && false || true) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

21. What is the output produced by the following code when embedded in a complete program?
if (false && (!false || true)) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

22. What is the output produced by the following code when embedded in a complete program?
if (false && (false || !true)) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

23. What is the output produced by the following code when embedded in a complete program?
if (false && !(false || true)) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

24. What is the output produced by the following code when embedded in a complete program?
if (!false && (!false || !true)) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

25. What is the output produced by the following code when embedded in a complete program?
if (!false && !false || !true) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

26. What is the output produced by the following code when embedded in a complete program?
if (!(false && false || true)) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

27. What is the output produced by the following code when embedded in a complete program?
if (!true) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

28. What is the output produced by the following code when embedded in a complete program?
if (false) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

29. What is the output produced by the following code when embedded in a complete program?
if ((2 > 3) == false) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

30. What is the output produced by the following code when embedded in a complete program?
if (! (2 > 3)) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

31. What is the output produced by the following code when embedded in a complete program?
if (2 <= 3) {
  System.out.print(false); 
} else {
  System.out.print(true); 
}
  1.   true
  2.   false

32. Are these two code fragments equivalent (do they always change x in the exact same way)?

Fragment 1 Fragment 2
if (x == 8) 
  x = x + 3; 
else 
  x = 5;
if (x == 8)
  x = x + 3;
if (x != 8) 
  x = 5;

  1.   Yes.
  2.   No.


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

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


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

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

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

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

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

39. true is an example of what kind of constant?
  1.   int
  2.   char
  3.   boolean
  4.   String
  5.   other

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

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


41. How many instance methods do you see defined below?
class One {
  int balance; 
  int f() { return 1; } 
  static int g() { return 2; }
  One(int x) { this.balance = x; } 
  public static void main(String[] args) { }  
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

42. How many static methods do you see defined below?
class One {
  int balance; 
  int f() { return 1; } 
  static int g() { return 2; }
  One(int x) { this.balance = x; } 
  public static void main(String[] args) { }  
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

43. How many instance variables do you see defined below?
class One {
  int n; 
  int m; 
  int fun(int p) {
    int q = p + 1; 
    return q; 
  }
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

44. How many instance variables do you see defined below?
class One {
  static int n;
  int m; 
  int fun(int p) {
    int q = p + 1; 
    return q; 
  }
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

45. How many instance variables do you see defined below?
class One {
  static int n;
  int fun(int p) {
    int m; 
    int q = p + 1; 
    return q; 
  }
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

46. How many local variables do you see defined below?
class One {
  static int n;
  int fun(int p) {
    int m; 
    int q = p + 1; 
    return q; 
  }
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

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


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


49. Which of the following expressions does NOT print a 4 (four)?
  1. System.out.println('P' - 'K');
  2. System.out.println(3 % 5 + 1);
  3. System.out.println("123".length() + 1);
  4. System.out.println((char)('3' + 1));
  5. System.out.println(16 / (1 + 1) / 2);
Note: There are no typos!

50. What's the output produced by the following lines (when embedded in a complete program)?
String a = "blue"; 
String b = "berry"; 
System.out.println(
  "blueberry".substring(a.length(), b.length()));
  1.   b
  2.   l
  3.   u
  4.   e
  5.   other

51. 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(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.   0
  2.   1
  3.   2
  4.   3
  5.   other
There are no typos, so please don't rush!

52. 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())); 
  } 
  int fun() { return 1; } 
  int fun(int n) { return 1 + n; } 
} 
  1.   0
  2.   1
  3.   2
  4.   3
  5.   other

A201/A597/I210 Midterm Exam Fri Jul 12 2002 in LH102