Make up Exam for the first C212 Exam ("Early Evaluation Exam")              04/03/2013 

1. What values do we have in i and j at the end of each of these two fragments of code:

  int i = 3; 
  int j = 5;
  j = ++i + i++;

  int i = 3;
  int j = 5;
  j = i++ + ++i;

-bash-4.1$ javac One.java
-bash-4.1$ java One
(5, 8)
(5, 8)
-bash-4.1$ cat One.java
class One {
  public static void main(String[] args) {
    {
      int i = 3;
      int j = 5;
      j = ++i + i++;
      System.out.println( "(" + i + ", " + j + ")" );
    }

    {
      int i = 3;
      int j = 5;
      j = i++ + ++i;
      System.out.println( "(" + i + ", " + j + ")" );
    }
  }
}
-bash-4.1$

2. Write a loop that fills an array named values with ten random numbers between 1 and 100.

From the study guide (http://www.cs.indiana.edu/classes/c212-dgerman/spr2013/makeup0403.pdf) 

(Answers were posted before exam at http://silo.cs.indiana.edu:8346/c212/spr2013/0402/answers.phps)

-bash-4.1$ cat R64.java
import java.util.*;

class R64 {
  public static void main(String[] args) {
    int[] a = new int[10];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100) + 1;
      System.out.println( Arrays.toString( a ) );
    }
    a = new int[10];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100) + 1;
      System.out.println(i + ". Trying " + a[i]);
      boolean found = false;
      for (int j = 0; j < i; j++) {
        if (a[i] == a[j])
          found = true;
      }
      if (found) i--;
      System.out.println( Arrays.toString( a ) );
    }
  }
}
-bash-4.1$ javac R64.java
-bash-4.1$ java R64
[49, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[49, 32, 0, 0, 0, 0, 0, 0, 0, 0]
[49, 32, 98, 0, 0, 0, 0, 0, 0, 0]
[49, 32, 98, 88, 0, 0, 0, 0, 0, 0]
[49, 32, 98, 88, 79, 0, 0, 0, 0, 0]
[49, 32, 98, 88, 79, 22, 0, 0, 0, 0]
[49, 32, 98, 88, 79, 22, 14, 0, 0, 0]
[49, 32, 98, 88, 79, 22, 14, 27, 0, 0]
[49, 32, 98, 88, 79, 22, 14, 27, 97, 0]
[49, 32, 98, 88, 79, 22, 14, 27, 97, 47]
0. Trying 2
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1. Trying 73
[2, 73, 0, 0, 0, 0, 0, 0, 0, 0]
2. Trying 95
[2, 73, 95, 0, 0, 0, 0, 0, 0, 0]
3. Trying 78
[2, 73, 95, 78, 0, 0, 0, 0, 0, 0]
4. Trying 55
[2, 73, 95, 78, 55, 0, 0, 0, 0, 0]
5. Trying 43
[2, 73, 95, 78, 55, 43, 0, 0, 0, 0]
6. Trying 73
[2, 73, 95, 78, 55, 43, 73, 0, 0, 0] <------(73 already in)----
6. Trying 60
[2, 73, 95, 78, 55, 43, 60, 0, 0, 0]
7. Trying 42
[2, 73, 95, 78, 55, 43, 60, 42, 0, 0]
8. Trying 78
[2, 73, 95, 78, 55, 43, 60, 42, 78, 0] <----(78 already in)----
8. Trying 33
[2, 73, 95, 78, 55, 43, 60, 42, 33, 0]
9. Trying 68
[2, 73, 95, 78, 55, 43, 60, 42, 33, 68]
-bash-4.1$ 

------------------------------------------------------------------(R6.4)---------------------


3. Write code for two nested loops that fill an array named values with ten different random numbers between 1 and 100.   


4. True or false?
(a) Two-dimensional arrays always have the same number of rows and columns. 
(b) A method cannot change the length of an array argument. 
(c) A method cannot change the number of columns of an argument that is a two-dimensional array.

5. Simplify the following expressions in which b is a variable of type boolean:
b == true                     b == false

6. Rewrite the following for loop into a while loop with values and total of suitable types:
for (double x : values) { total = total + x; }


7. Write Java code for a loop that simultaneously computes both the maximum and minimum of an array.


Write Java statements for performing the following tasks with an array declared as
int[][] values = new int[ROWS][COLUMNS]; 
8. Fill elements alternately with 0s and 1s in a checkerboard pattern. 


9. Compute the sum of all elements. 


10. Print the array in tabular form.
True or false? 
11. A method has exactly one return statement. 
12. A method has at least one return statement. 

Simplify the following statements where b is a variable of type boolean and n is a variable of type int.
13.  if (n == 0) { b = true; } else { b = false; } 
14.  if (n == 0) { b = false; } else { b = true; } 
15.  b = false; if (n > 1) { if (n < 2) { b = true; } }
16.  if (n < 1) { b = true; } else { b = n > 2; } 

Write method headers for methods with the following descriptions:
17. Checking if an integer is a prime number, returning true if it is and false otherwise.


18. Computing the larger of two integers.


19. Checking whether a string is contained inside another string. 


20. Printing the balance of an account with a given initial balance, an annual interest rate, and a number of years of earning interest. 


How do you perform the following tasks with array lists of integers in Java?
21. Copy one array list to another. 


22. Test that two array lists contain the same elements in the same order. 


23. Fill an array list of integers with zeroes, overwriting all elements in it. 


24. What is the difference between  .a. + .b. + .. and .a. + (.b. + ..)

25. Make up an example in Java that demonstrates what is known as .the dangling else problem. using the following statement: .A student with a GPA of at least 1.5, but less than 2, is on probation. With less than 1.5, the student is failing.. Explain the dangling aspect of your example.