Good luck and do well!

CSCI A201/A597

Final Examination

Summer II 2001


Directions:

Good luck and do well!


1. You compile and run the following program. What does it print?
class One {
  public static void main(String[] args) {
    boolean p = false, 
            q = true, 
            r = true;
    System.out.println(! (p && (q || !r))); 
  }
} 
  1.    true
  2.   false
  3.   other

One can compile and run this code to obtain the correct answer, which is: true


2. Consider the following condition.
!(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))
Which of the following represents a simplification of it (obtained perhaps using DeMorgan).
  1.   ((x % 100) == 0)
  2.    ((x % 400) == 0)
  3.   ((x % 100) != 0)
  4.   ((x % 400) != 0)
  5.   none of the above

This question appears in the warmups for chapter 5.


3. Consider the following condition.
a == false
Which of the following represents a simplification of it.
  1.   a
  2.    !a
  3.   a && ! a
  4.   a || ! a
  5.   none of the above

This is a problem we have discussed in class.


4. Consider the following program fragment.
int x = 8, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;
What value does y have at the end?
  1.   0
  2.    1
  3.   2
  4.   cannot be determined with the information given
  5.   this program fragment would not compile
Here's how you get the right answer to this and the next two questions.

class One {
    public static void main(String[] args) {

	{ int x = 8, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2; 
  	  System.out.println(y); 
        } 
	{ int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; } else y = 2; 
	  System.out.println(y); 
        } 
	{ int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; else y = 2; } 
	  System.out.println(y); 
        } 

    }
} 

5. Consider the following program fragment.
int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; } else y = 2;
What value does y have at the end?
  1.   0
  2.    1
  3.   2
  4.   cannot be determined with the information given
  5.   this program fragment would not compile

6. Consider the following program fragment.
int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; else y = 2; }
What value does y have at the end?
  1.   0
  2.    1
  3.   2
  4.   cannot be determined with the information given
  5.   this program fragment would not compile

7. Consider the following program fragment.
int x = 8, y = 0; { if (x < 10) if (x > 5) y = 1; } else y = 2; 
What value does y have at the end?
  1.   0
  2.   1
  3.   2
  4.   cannot be determined with the information given
  5.    this program fragment would not compile
frilled.cs.indiana.edu%
frilled.cs.indiana.edu%javac One.java
One.java:3: 'else' without 'if'.
	int x = 8, y = 0; { if (x < 10) if (x > 5) y = 1; } else y = 2; 
	                                                    ^
1 error
frilled.cs.indiana.edu%

8. How many question marks will be printed by the following code fragment?
for (int i = -10; i <= 10; i = i + 3)
  System.out.print("?"); 
  1.    7
  2.   8
  3.   9
  4.   10
  5.   other

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	for (int i = -10; i <= 10; i = i + 3)
	    System.out.print("?");
    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
???????frilled.cs.indiana.edu%


9. How many question marks will be printed by the following code fragment?
for (i =  7; i >=  0; i--) 
  System.out.print("?"); 
  1.   7
  2.    8
  3.   9
  4.   10
  5.    other

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	for (int i =  7; i >=  0; i--) 
	    System.out.print("?"); 
    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
????????frilled.cs.indiana.edu%

Arguably the index of the loop had to be declared explicitly for this to remove all doubts, so we have two correct answers here, let's say.


10. How many question marks will be printed by the following code fragment?
for (i =  7; i >=  0; i--) ;
  System.out.print("?"); 
  1.   7
  2.   8
  3.   9
  4.   10
  5.    other
No matter how you look at this question the answer is other (the semicolon was intentional).
11. Consider the following fragment. What does it print?
int x = 3, y = 11; 
x = x + y;
y = x - y; 
x = x - y; 
System.out.println("(" + x + ", " + y + ")"); 
  1.   (3, 11)
  2.    (11, 3)
  3.   (14, 14)
  4.   (7, 7)
  5.   other

This is known as swapping two numbers without a third variable.

Correct answers for this and the next problem are obtained as follows:

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {

	{
	    int x = 3, y = 11; 
	    x = x + y;
	    y = x - y; 
	    x = x - y; 
	    System.out.println("(" + x + ", " + y + ")"); 
	}

	{
	    int x = 11, y = 3; 
	    y = x + y;
	    x = y - x; 
	    y = y - x; 
	    System.out.println("(" + x + ", " + y + ")");
	}

    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
(11, 3)
(3, 11)
frilled.cs.indiana.edu%


12. Consider the following fragment. What does it print?
int x = 11, y = 3; 
y = x + y;
x = y - x; 
y = y - x; 
System.out.println("(" + x + ", " + y + ")"); 
  1.    (3, 11)
  2.   (11, 3)
  3.   (14, 14)
  4.   (7, 7)
  5.   other

13. Consider the following fragment. What does it print?
int[] v = new int[10];
for (int i = 0; i < 10; i++) v[i] = i * i;
System.out.print(v[3] + v[5])
  1.   6
  2.   8
  3.   10
  4.   31
  5.    other
No matter how you look at it the answer is other.
14. Consider the following fragment. What does it print?
int[] v = { 1, 2, 3, 4, 5, 6},
      m = { 6, 5, 4, 3, 2, 1};      
for (int i = 0, sum = 0; i < 10; i++) 
  sum += (v[i] - m[i]); 
System.out.print(sum)
  1.    0
  2.   21
  3.   42
  4.   84
  5.    other
Again, no matter how you look at it the answer is other. The first answer will also be considered correct, but please try to make this code run and see how many reasons you collect in support of the last of the options.
15. Consider the following program. What does it print?
int[] a = { 0, -1, 1, -2, 2, -3, 3}; 
int fun = a[a.length - 1];
for (int i = a.length - 1; i >= 0; i--) 
  if (a[i] > fun) 
    fun = a[i];
System.out.println(fun); 
  1.   -3
  2.   0
  3.    3
  4.   7
  5.   other
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	
        int[] a = { 0, -1, 1, -2, 2, -3, 3}; 
        int fun = a[a.length - 1];
        for (int i = a.length - 1; i >= 0; i--) 
	    if (a[i] > fun) 
		fun = a[i];
        System.out.println(fun); 
	    
    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
3
frilled.cs.indiana.edu%

16. Consider the following program fragment. What would it print?
int[] a = { 4, 3, 5, 2, 6, 1, 7}; 
boolean done = false;
while (! done) {
  done = true; 
  for (int i = 0; i < a.length - 1; i++) 
    if (a[i] > a[i + 1]) { // focus here! 
     done = false; 
     int temp = a[i]; 
     a[i] = a[i + 1];
     a[i + 1] = temp; 
    } 
} 
for (int i = 0; i < a.length; i++)
  System.out.print(a[i]);
System.out.println(); 
  1.    1234567
  2.   7654321
  3.   3425167
  4.   4536217
  5.   other

17. Consider the following program fragment. What would it print?
int[] a = { 4, 3, 5, 2, 6, 1, 7}; 
boolean done = false;
while (! done) {
  done = true; 
  for (int i = 0; i < a.length - 1; i++) 
    if (a[i] < a[i + 1]) { // focus here!
     done = false; 
     int temp = a[i]; 
     a[i] = a[i + 1];
     a[i + 1] = temp; 
    } 
} 
for (int i = 0; i < a.length; i++)
  System.out.print(a[i]);
System.out.println(); 
  1.   1234567
  2.    7654321
  3.   3425167
  4.   4536217
  5.   other

18. Consider the following program fragment.
public class One {
  public static void main(String[] args) {
    int size = 10, nuf, fun;
    for (nuf = 0; nuf < size; nuf++) { 
      for (fun = 0; fun < size; fun++) 
        if ( fun == 0 || 
             fun == size - 1 || 
             __________________ )
          System.out.print(" *"); 
        else 
          System.out.print("  "); 
      System.out.println(); 
    }
  } 
}
What's need for the output to resemble an H?
  1.   fun == size / 2
  2.    nuf == size / 2
  3.   fun + nuf == size - 1
  4.   fun - nuf == size - 1
  5.   other

This is a problem like in lab six.


19. Consider the following command line invocation:
java One Two Three Four Five
Assume your program contains this line inside
System.out.println(args[1]);
What does this line print?
  1.   java
  2.   One
  3.   Two
  4.    Three
  5.   other

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	System.out.println(args[1]);
    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One Two Three Four Five
Three
frilled.cs.indiana.edu%


20. Consider the following code fragment, what does it print?
String plum = "nectarine"; 
System.out.println(plum.substring(0, "plum".length()); 
  1.   nec
  2.    nect
  3.   nectarin
  4.   nectarine
  5.    other

Yes, I forgot to type a right parenthesis.


21. Consider the following code fragment, what does it print?
String plum = "nectarine";  
System.out.println((char)('Q' - plum.length())); 
  1.    H
  2.   O
  3.   M
  4.   E
  5.   other
Here's how you get the key for this and the next problem.

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {

	{
	    String plum = "nectarine";  
	    System.out.println((char)('Q' - plum.length()));
	}

	{
	    String plum = "nectarine";  
	    System.out.println((char)('Q' - "plum".length()));
	}

    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
H
M
frilled.cs.indiana.edu%

22. Consider the following code fragment, what does it print?
String plum = "nectarine";  
System.out.println((char)('Q' - "plum".length())); 
  1.   H
  2.   O
  3.    M
  4.   E
  5.   other

23. Does this source code compile?
class Vegetable { } 
class Tomato extends Vegetable { }
class Kroger {
  public static void main(String[] args) {
    Tomato a = new Vegetable(); 
  }
}
  1.   yes
  2.    no

Not all vegetables are tomatoes.

frilled.cs.indiana.edu%javac One.java
One.java:5: Incompatible type for declaration. 
            Explicit cast needed to convert Vegetable to Tomato.

	Tomato a = new Vegetable(); 
	       ^
1 error
frilled.cs.indiana.edu%


24. Does this source code compile?
class Vegetable { } 
class Tomato extends Vegetable { }
class Kroger {
  public static void main(String[] args) {
    Vegetable a = new Tomato(); 
  }
}
  1.    yes
  2.   no

Yes, all unicorns are horses (as defined in class).


25. Does this source code compile?
class Vegetable { } 
class Cabbage extends Vegetable { }
class Kohlrabi extends Cabbage { } 
class Kroger {
  public static void main(String[] args) {
    Cabbage a = new Kohlrabi(); 
  }
}
  1.    yes
  2.   no

Not that it really matters, but to remove all doubts:

frilled.cs.indiana.edu%webster kohlrabi
kohl-ra-bi \ko^-l-'rab-e^-, -'ra^:b-\ n,  pl -bies
[G, fr. It cavolo rapa, fr. cavolo cabbage (fr. L caulis stalk)
     + rapa turnip, fr. L -- more at HOLE, RAPE]
(1807)
:any of a race of cabbages having a greatly enlarged, fleshy, turnip-shaped
     edible stem 


frilled.cs.indiana.edu%

This exercise courtesy of . Maybe.


26. Does this source code compile?
class Vegetable { } 
class Cabbage extends Vegetable { }
class Kohlrabi extends Cabbage { } 
class Kroger {
  public static void main(String[] args) {
    Kohlrabi a = new Vegetable(); 
  }
}
  1.   yes
  2.    no

Many things are not kohlrabi, and this Java program knows that.

frilled.cs.indiana.edu%javac One.java
One.java:6: Incompatible type for declaration. 
            Explicit cast needed to convert Vegetable to Kohlrabi.

	Kohlrabi a = new Vegetable(); 
	         ^
1 error
[2]  - Done                 emacs One.java
frilled.cs.indiana.edu%


27. You compile and run this code, what is the output?
class One {
  int red; 
  One() { this(10); }
  One (int blue) { red += blue; }
  int report() { System.out.println(red); }
  public static void main(String[] args) {
    One x = new One(20); 
    x = new One(30); 
    x = new One(); 
    x.report(); 
  }
} 
  1.    10
  2.   20
  3.   30
  4.   60
  5.    other

For both this and the next exercise the report method should have been declared as void to compile. So other will be considered correct. Assume the compilation error is fixed then the two questions both have a second correct answer, as indicated.


28. You compile and run this code, what is the output?
class One {
  static int red; 
  One() { this(10); }
  One (int blue) { red += blue; }
  int report() { System.out.println(red); }
  public static void main(String[] args) {
    One x = new One(20); 
    x = new One(30); 
    x = new One(); 
    x.report(); 
  }
} 
  1.   10
  2.   20
  3.   30
  4.    60
  5.    other

29. If you compile and run this code, what is the output?
class One {
  public static void main(String[] args) {
    int[][] a = { {1, 2, 3}, 
                  {4, 5, 6}, 
                  {7, 8, 9}, 
                  {6, 2, 7}
                }; 
    System.out.println(a[a[0][1] - 1][0]); 
  }
} 
  1.   2
  2.    4
  3.   6
  4.   8
  5.   other

30. If you compile and run this code, what is the output?
class One {
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    fun(x[1]); // focus here!!
    System.out.println(x[0] + x[1] + x[2]);
  }
  public static void fun(int value) {
    value = value - 3 * value;;
  }
} 
  1.   0
  2.   2
  3.   4
  4.    6
  5.   other

31. If you compile and run this code, what is the output?
class One {
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    fun(x); // focus here!!
    System.out.println(x[0] + x[1] + x[2]);
  }
  public static void fun(int[] value) {
    value[1] = value[1] - 3 * value[1];;
  }
} 
  1.    0
  2.   2
  3.   4
  4.   6
  5.   other

32. What gets printed when you compile and run the following program?
class One {
  public static void main(String[] args) {
    System.out.println(kanga(kanga(kanga(kanga(ru(1, 2), 
                                               1), 
                                         2), 
                                   ru(1, 2)),
                             1)); 
  } 
  public static int kanga(int a, int b) {
    return a + b;
  } 
  public static int ru(int a, int b) {
    return a - b; 
  }
} 
  1.   0
  2.    2
  3.   4
  4.   6
  5.   other

Here's to running all our programs at least once:

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	System.out.println(kanga(kanga(kanga(kanga(ru(1, 2), 
						   1), 
					     2), 
				       ru(1, 2)),
				 1)); 
    } 
    public static int kanga(int a, int b) {
	return a + b;
    } 
    public static int ru(int a, int b) {
	return a - b; 
    }
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
2
frilled.cs.indiana.edu%


33. What gets printed when you compile and run the following program?
class One {
  public static void main(String[] args) {
    System.out.println(fun(fun(fun(fun(6))))); 
  }
  static int fun(int value) {
    return value - 1; 
  } 
} 
  1.   0
  2.    2
  3.   4
  4.   6
  5.   other

'nuf said.


34. What does the next fragment print if embedded in a complete program?
String tsew = "stue".charAt(0) + "tsue".charAt(0) + "ew"; 
System.out.println(tsew); 
  1.   stew
  2.   stue
  3.   tsew
  4.    231ew
  5.   other
Note: please think carefully about the order of operations.

Just as discussed in class the plus between characters happens first, which makes them participate with their codes, thus producing an integer. The next plus is between the resulting integer and a String and that produces a String. Keep this in mind as you enter the next three questions.


35. What does mix("bbb", "aaa") return if mix is defined as below?
public static String mix(String one, String two) {
  String answer = ""; 
  int lengthOne = one.length(), lengthTwo = two.length();
  for (int i = 0; i < olen && i < tlen; i++) {
    ans = one.charAt(i) + two.charAt(i) + answer; 
  }  
  return ans; 
} 
  1.   bbbaaa
  2.   aaabbb
  3.   ababab
  4.   bababa
  5.    other
Note: please recall what you did in the previous exercise.

This program does not compile. And neither do the next two. But does it matter?

Let's see... Suppose I fix it to compile:

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {

	System.out.println(mix("bbb", "aaa")); 

    }

    public static String mix(String one, String two) {
	String answer = ""; 
	int lengthOne = one.length(), lengthTwo = two.length();
	for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
            answer = one.charAt(i) + two.charAt(i) + answer; 
	}  
	return answer; 
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
195195195
frilled.cs.indiana.edu%
As you can see, it doesn't matter.

For the same reasoning used in the exercise before this one.

So now we know the answers for the next two questions.


36. What does mix("bd", "pq") return if mix is defined as below?
public static String mix(String one, String two) {
  String answer = ""; 
  int lengthOne = one.length(), lengthTwo = two.length();
  for (int i = 0; i < olen && i < tlen; i++) {
    ans = one.charAt(i) + two.charAt(i) + answer + two.charAt(i); 
  }  
  return ans; 
} 
  1.   dqbppq
  2.   qdpqqp
  3.   bdqppq
  4.   pbpqqp
  5.    other
Note: please think carefully.

No characters can come out of this code on the left of the original value of answer, as the characters codes will be summed up into integers. Here's a version of this program that compiles:

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {

	System.out.println(
			   mix("bd", "pq")
			   ); 

    }

    public static String mix(String one, String two) {
	String answer = ""; 
	int lengthOne = one.length(), lengthTwo = two.length();
	for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
            answer = one.charAt(i) + two.charAt(i) + answer + two.charAt(i); 
	}  
	return answer; 
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
213210pq
frilled.cs.indiana.edu%


37. What does mix("bd", "pq") return if mix is defined as below?
public static String mix(String one, String two) {
  String answer = ""; 
  int lengthOne = one.length(), lengthTwo = two.length();
  for (int i = 0; i < olen && i < tlen; i++) {
    ans = one.charAt(i) + two.charAt(i) + answer + two.charAt(i); 
  }  
  return ans; 
} 
  1.   dqbppq
  2.   qdpqqp
  3.   bdqppq
  4.   pbpqqp
  5.    other
Please don't rush, think carefully.

I did get distracted when I was putting these together on Tuesday and I didn't get the time to review them. Hence the typos. But the outcome of the exercises would have still been the same, and I told you about it at the beginning of the exam today.


38. Assuming that the following program fragment is syntactically correct...
  int[][] a = new int[10][10];
  ...
  a[0][1] = fun(a[2], a); 
... select the correct header for method fun.
  1.   int[][] fun(int[] int)
  2.    int fun(int[], int[][])
  3.   int[] fun(int, int[])
  4.   int fun(int[][], int)
  5.    none of the above

For this and the next three questions I left out some commas and the names of the formal params. So for this reason I will count other as a good answer. But I will also indicate the answers that are closest (syntactically speaking) to the right answers.


39. Assuming that the following program fragment is syntactically correct...
  int[][] a = new int[10][10];
  ...
  a[0] = fun(a[1][2], a[3]); 
... select the correct header for method fun.
  1.   int[][] fun(int[] int)
  2.   int fun(int[], int[][])
  3.    int[] fun(int, int[])
  4.   int fun(int[][], int)
  5.    none of the above

40. Assuming that the following program fragment is syntactically correct...
  int[][] a = new int[10][10];
  ...
  a = fun(a[1], a[2][3]); 
... select the correct header for method fun.
  1.    int[][] fun(int[] int)
  2.   int fun(int[], int[][])
  3.   int[] fun(int, int[])
  4.   int fun(int[][], int)
  5.    none of the above

41. You compile this program ...
class One {
  public static void main(String[] args) {
    System.out.println(args[args.length - args[3].length()]);     
  }
} 
... then run it as follows:
java One this is my song for the asking
What does the program print (if anything)?
  1.   this
  2.    song
  3.   ing
  4.   the program compiles but does not run
  5.   other

42. You compile this program ...
class One {
  public static void main(String[] args) {
    System.out.println(args[3]);     
  }
} 
... then run it as follows:
java One ask me and I'll play so sweetly I'll make you smile 
What does the program print (if anything)?
  1.   ask
  2.   me
  3.   and
  4.   the program compiles but does not run
  5.    other

43. You compile and run this program. What's the output?
public class Four {
    public static void main(String[] args) {
        Four a = new Four(); 
        a.nuf(1); 
        a.fun(); 
        a.nuf(3); 
        a.fun(); 
        a.nuf(5); 
        System.out.println(Four.m + a.n); 
    } 
    void nuf(int p) {
        this.n = this.n + p; 
        Four.m = Four.m - p; 
    } 
    void fun() {
        int temp = this.n; 
        this.n = Four.m; 
        Four.m = temp; 
    } 
    int n; 
    static int m; 
} 
  1.    0
  2.   1
  3.   2
  4.   3
  5.   other

44. You compile and run this program. What's the output?
public class Five {
    public static void main(String[] args) {
	Five a = new Five(); 
	Five b = new Five(); 
	a.nuf(1); 
	a.fun(); 
	b.nuf(3); 
	b.fun(); 
	a.nuf(5); 
	System.out.println(Four.m + a.n); 
    } 
    void nuf(int p) {
	this.n = this.n + p; 
	Four.m = Four.m - p; 
    } 
    void fun() {
	int temp = this.n; 
	this.n = Four.m; 
	Four.m = temp; 
    } 
    int n; 
    static int m; 
} 
  1.   0
  2.   1
  3.    2
  4.   3
  5.    other

I left behind the name of the class from the previous exercise. So there are three cases: you either have that class around, in which case this compiles and prints 2. Or you don't, in which case this does not compile. Or you make the change (from Four to Five) and then this prints 4. So the third, the fifth, amd the fifth again were the right answers, and all the others impossible.


A201/A597 TTFN Exam Fri Aug 10 2001 in SW119