## CPSC 124, Fall 2017: Sample Answers to Quiz #2

detailed than the ones given here can still receive full credit.

Question 1. What is meant by an infinite loop? What could make such a thing happen in Java?

Answer. An infinite loop is one that runs forever, repeating the same sequence of instructions over and over indefinitely. In Java, a while loop can be an infinite loop if nothing ever happens in the loop to make the loop's boolean test expression true. For example a loop that begins while (ct<100) would be an infinite loop if the value of ct is not modified inside the loop.

Question 2. Show the exact output from the following Java code segment:

```nt n, ct;
n = 1;
ct = 1;
while ( n < 50 ) {
n = 2 * n;
ct++;
System.out.println( ct + ": " + n );
}
System.out.println("DONE");
```

```2: 2
3: 4
4: 8
5: 16
6: 32
7: 64
DONE
```

(Each time through the loop, n is doubled and ct goes up by one. Note that n = 2*n and ct++ are executed once before any output is done, so that the first values of n and ct that are printed are both equal to 2. Also, when n is 32, the loop continues because 32 < 50, but then n and ct are changed to 7 and 64 before they are printed for the last time.)

Question 3. Complete the following program to perform this task: When the program is run, it will ask the user to type in two integers. It will read the integers and then print out the larger of the two integers. (If the integers are the same, just print out either one.) Use an if statement! Use TextIO for input.

```public class CompareInts {
public static void main(String[] args) {
int a, b;  // The numbers to be input from the user.