CSCI A201/A597

Midterm Exam One

Spring 2000

The correct answers are in underlined blue.


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.   6
2. 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.   6
3. What is the output produced by the following two lines (when embedded in a complete program)?
int a; a = 'E' - 'B'; 
System.out.println(a); 
  1.   E
  2.   B
  3.   C
  4.   3
  5.   0
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') 
  System.out.print("1" + 3 / 2); 
  System.out.print(2);
  1.   1
  2.   11
  3.   12
  4.   21
  5.   2
5. What is the type of this expression:
"1" + 'a'
  1.   int
  2.   char
  3.   boolean
  4.   double
  5.   String
6. What is the output produced by the following three lines (when embedded in a complete program)?
int a, b;
a = 12; b = 5;  
System.out.println(b * (a / b) + a % b);
  1.   5
  2.   12
  3.   14
  4.   60
  5.   300
7. What is the output produced by the following three lines (when embedded in a complete program)?
int a, b;
a = 12; b = 5;  
System.out.println(b * a / b + a % b);
  1.   5
  2.   12
  3.   14
  4.   60
  5.   300
8. 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(1); 
else 
  System.out.print(2); 
if (x < 2) 
  System.out.println(3); 
System.out.print(4);
  1.   23
  2.   24
  3.   234
  4.   134
  5.   1234
9. 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(1); } 
else 
  System.out.print(2); 
  System.out.print(3);
  1.   1
  2.   2
  3.   13
  4.   12
  5.   23
10. 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 = 14; 
while (x < 15) 
  x = x + 1;
  System.out.print(x);
  1.   14
  2.   15
  3.   1415
  4.   1516
  5.   141516
11. 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
12. 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.   145
13. 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.   0123
14. 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.   0123
15. What is the output produced by the following code when embedded in a complete program?
int x = 3; 
if (x > 0) System.out.print(x + 1); 
else if (x > 1) System.out.print(x); 
else if (x > 2) System.out.print(x - 1); 
else if (x > 3) System.out.print(2 * x); 
else System.out.print(x * x);
  1.   2
  2.   3
  3.   4
  4.   6
  5.   9
16. What is the output produced by the following code when embedded in a complete program? (The code is identical to the one in problem 15 above, except two else's have been removed). As always, please be careful about indentation in the resulting program.
int x = 3; 
if (x > 0) System.out.print(x + 1); 
else if (x > 1) System.out.print(x); 
else if (x > 2) System.out.print(x - 1); 
else if (x > 3) System.out.print(2 * x); 
else System.out.print(x * x);
  1.   4
  2.   43
  3.   46
  4.   369
  5.   439
17. 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.   -7
  2.   -3
  3.   -2
  4.   -1
  5.    4
18. 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.   -7
  2.   -3
  3.   -2
  4.   -1
  5.    4
19. 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.   -7
  2.   -3
  3.   -2
  4.   -1
  5.   4
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. Is this an example of an infinite loop?
int count = 0;
while (!(count != count)) {
  System.out.println(count); 
  count = count + 1; 
} 
  1.   Yes.
  2.   No.
23. Is this an example of an infinite loop?
int x = 0; 
while (x == 0) {
  x = x + 1; 
  System.out.println(x);
  x = x % x; 
} 
  1.   Yes.
  2.   No.
24. Is this an example of an infinite loop?
int x = -1;
while (x != 10) {
  System.out.println(x); 
  x = x + 2;   
} 
  1.   Yes.
  2.   No.
25. Is this an example of an infinite loop?
int x = -2;
if (x != 10) {
  System.out.println(x); 
  x = x + 3;
} 
  1.   Yes.
  2.   No.
26. Is this an example of an infinite loop?
int i;
for (i = 0; i < 10; i = i + 1) {
  System.out.println(i); 
  i = i - 3;
}
  1.   Yes.
  2.   No.
27. 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.   none of the above
28. 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.   none of the above
29. 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.   none of the above
30. 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); else 
  System.out.print(x % 3); 
  1.   4
  2.   41
  3.   02402
  4.   024102
  5.   none of the above
31. Is this an example of an infinite loop?
int count = -1; 
for (count = count + 1; count + 1 > 0; count = count + 1) {
  System.out.println(count); 

} 
  1.   Yes.
  2.   No.
32. Is this an example of an infinite loop?
int count = -1; 
for (count = count + 1; count + 1 > 0; count = count + 1) {
  System.out.println(count); 
  count = count - 2;
}
  1.   Yes.
  2.   No.
33. 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.   A
  2.   Z
  3.   N
  4.   Y
  5.   none of the above
34. 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.   A
  2.   Z
  3.   N
  4.   Y
  5.   none of the above
35. Assume the code fragment below. Does anything change in the way z gets updated if we remove the else keyword?
if (x > y)
  z = z + 1;
else if (x <= y) 
  z = z + 2;
  1.   Yes.
  2.   No.
36. Assume the code fragment below. Does anything change in the way z gets updated if we remove the else keyword?
if (! (x > y))  
  z = z + 2;
else  
  z = z + 1;
  1.   Yes.
  2.   No.
37. Are these two code fragments equivalent (in terms of the value of x at the end of the fragment, when the fragment gets executed)?
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.


Thu Mar 2 16:00:00 EST 2000 (A201, Rawles Hall 100)