These are the answers to the questions in this exam

http://www.cs.indiana.edu/classes/c212/spr2015/02232015/abcdef.pdf

 1. int and 45

 2. boolean and false

 3. int and 2

 4. double and the square root of 3 (something like 1.73...)

 5. 9

 6. true 

 7. boolean and false 

 8. String and "3345"

 9. int and 9

10. char and 'a' 

11. String and "tomato"

         _________ 
12.   | /        _ 
      |/ a  +  |/b   


13. Math.sqrt(Math.pow(Math.sqrt(Math.pow(Math.sqrt(2), 2)), 2))


14. 9

15. s = s0 + v0 * t + 1 / 2.0 * g * t * t


16. http://www.sciweavers.org/free-online-latex-equation-editor

            _______       
           /      v       
        | / 1  +  -       
        |/        c       
 m  *  (-----------  -  1)
            _______       
           /      v       
        | / 1  -  -       
        |/        c       

17. double and 0.0

18. double and 1.5 

19. int and 3

20. \"

21. Welcome to DrJava.  Working directory is C:\Users\dgerman
> System.out.println("\\n\"\\b\\t")
\n"\b\t


22. 03

23. (a) 31

    (b) 310

    (c) 3210 

Here's the code

class Exercise {
  public static void main(String[] args) {
    { // problem 22
      boolean x; // local variable of type boolean (no value yet) 
      if (true) // this is like a traffic light always on green 
        System.out.print(0); // prints 0 
      else
        System.out.print(1);
      
      x = (1 < 2) && (4 < 3); // false

      if (x) // x is false, remember? 
        System.out.print(2);
      else
        System.out.print(3); // prints 3
      
    } // prints 03 and x is gone 
    System.out.println("----------------------------"); 
    { // problem 23 part I 
      boolean x;
      if (true)
        System.out.print(3);
      else
        System.out.print(2);
      x = (1 < 2) || (4 < 3);
      if (x)
        System.out.print(1);
      else
        System.out.print(0);   
    } // prints 31
    System.out.println("----------------------------"); 
    { // problem 23 part I 
      boolean x;
      if (true)
        System.out.print(3);
      else
        System.out.print(2);
      x = (1 < 2) || (4 < 3);
      if (x)
        System.out.print(1);
      System.out.print(0); 
      
    } // 310
    System.out.println("----------------------------");     
    { // problem 23 part I 
      boolean x;
      if (true)
        System.out.print(3);
      
        System.out.print(2);
      x = (1 < 2) || (4 < 3);
      if (x)
        System.out.print(1);
      
        System.out.print(0); 
    } // prints 3210
    System.out.println("----------------------------"); 

    
  }
}

24. 

boolean x = false; // x variable initialized
if (true)
 System.out.print(0); // prints a 0
else
 System.out.print(1);
x = x || !x; // true 
if (x)
 System.out.print(2); // prints a 2
else
 System.out.print(3);

25. false 

26. false && (false || true)
             ---------------
                  true
    ------------------
           false 

if (false && (false || true)) {
  System.out.print(false);
} else {
  System.out.print(true); // prints this 
}

27. false 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 3 + ( 3 < 4 ? 5 : -2 )
8
> 3 + ( 3 >= 4 ? 5 : -2 )
1


28. ((m > n) || ((m + n) % 19 == 0))

29. (x < 5) 

http://silo.cs.indiana.edu:8346/c212/fall2015/0904a.phps

30. (x < 25) 

31. true 

32. (x == 4)

33. Here's the code

class Exercise {
  public static void main(String[] args) {
    
    int x = 6, y = 1001; 
    
    if (x > 3) {
      if (x <= 5)
        y = 1;
      else if (x != 6)
        y = 2;
    } else
      y = 3;

    System.out.println( y ); // y unchanged prints 1001
    
  }
}

Quick question: what if we remove the curly braces? (Answer: y is set to 3.)

Best advice: draw a flowchart. 

34.   int j = 3;.
      // What happens when the following expression is executed?
      j = ++j + j++;
        // 4 (and j is 4 now)
        //    + 4 (and j becomes 5) 
        // then j becomes 8 

35. Infinite loop. Should use i++ by itself as the iteration step. 

What is wrong with the following for loop? Why?

  for (int i = 0; i < 10; i = i++) {
    System.out.println( "Hi there." );
  }

7 Johnson,Robert Zacharias

10 Long,Qingyue

1 Chen,Shengyu currently in the bathroom 

6 Jiang,Kongchen

The statement i = i++; does not modify i. 

36. int count = 0; 
    for (count = count + 1; ! (count > 0); count = count - 1) {
      System.out.println( count ); 
    }
// ---------------------------------------------------------------------------
    for (<INIT>; <COND>; <STEP>) {
      <BODY>
    }
// ---------------------------------------------------------------------------
    <INIT>
    while (<COND>) {
      <BODY>
      <STEP> 
    }
// ---------------------------------------------------------------------------
    int count = 0;
    count = count + 1;
    while (! (count > 0)) {
      System.out.println( count );
      count = count - 1;
    }

37.       5 7 -4

38.       if has empty body prints Oops.

39.       infinite loop, while has empty body

40.       see below with a 2 and b 100

41. int sum =0; 
    for (int i = a; i <= b; i++) {
      if (i % 2 == 1)
        sum += i; 
    }

42. 
     do {
      <BODY>; 
     } while (<COND>); 


     <BODY>
     while(<COND>) {
       <BODY>; 
     }


     int n = 1;
     double x = 0;
     double s;
     s = 1.0 / (n * n);
     x = x + s;
     n++;
     while (s > 0.01) {
       s = 1.0 / (n * n);
       x = x + s;
       n++;
     } 


43.      i     j   
       -------------------------
         0     0    System.out.print( 0 * 0 % 3 ); // 0
               1    System.out.print( 0 * 1 % 3 ); // 0
               2    System.out.print( 0 * 2 % 3 ); // 0
                    System.out.println(); 
         1     0    System.out.print( 1 * 0 % 3 ); // 0
               1    System.out.print( 1 * 1 % 3 ); // 1
               2    System.out.print( 1 * 2 % 3 ); // 2
                    System.out.println(); 
         2     0    System.out.print( 2 * 0 % 3 ); // 0
               1    System.out.print( 2 * 1 % 3 ); // 2
               2    System.out.print( 2 * 2 % 3 ); // 1
                    System.out.println(); 

44.

45.     x     y    x > 0 && y > 0 
      --------------------------
       10     3         true
        7               true 
        4               true 
        1               true 
       -2               false 

It prints -2.