CSCI A201/A597

Lecture Notes Sixteen

Second Summer 2000


Let's practice some more.
What do we do today? I'd like to work some exercises.

What kind? The toughest, most tedious kind, if possible.

What for? I just want to be in very good shape.

Then let's pretend we are taking an exam. Great. Let's do it.

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

Wait, wait, wait. Isn't this new?


Somewhat. Have you seen anything like it before? I don't really think so...

How about the table on page 732. I used that in lexicographic ordering. So characters have codes that act as whole numbers sometimes.

Yes, and we need to revisit this later. But meanwhile it's a good intermezzo. Let's move on...

Yes, this was an intermezzo.

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

This is tricky, but I can make an educated guess now.


That was the whole point. I see you have one more and then perhaps we can get into the real deal.

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

I've seen this one before.


And the next one. You have to be patient with these...

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?
Oh, this kind of questions I like best!


Very good. Is it an infinite loop or not?

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.
What do you think?


This last one's a bit cruel...
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.

Ready for some more? Infinite loops exercises are nifty.

Easy perhaps. What's next?

Some heavy-duty for loops. Looking at the answers I feel loopy already.

Ignore the answers. They are designed to give you as little help as possible. I was going to say...

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


I can see you're in a state of amazement so here's two of your favorite kind...

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.

OK, I feel refreshed already!


Don't worry, we'll take care of that...

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

They're both in a shape of Y for me...


If you think carefully about them they're EZ... The first one might be a Z but the second one?

N-EZ-problem as well. Easy for you to say that.

U bet. Hang in there!

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.

These last ones were a bit better. Can we take a break now and get updated?


Sounds reasonable.

Last updated: July 17, 2000 by Adrian German for A201