# CMIS 102A Practice exam

## Arithmetic Expressions

Show the output from these statements.

1. __________`System.out.println( 2 + 3 * 4 );`
2. __________`System.out.println( 2 * 3 + 4 );`
3. __________`System.out.println( 4+3 * 2+1 );`
4. __________`System.out.println( 4 + 3*2 + 1 );`
5. __________`System.out.println( 10 / 5 );`
6. __________`System.out.println( 5 / 10 );`
7. __________`System.out.println( 5 / 2 );`
8. __________`System.out.println( 5.0 / 2 );`
9. __________`System.out.println( 5 / 2.0 );`
10. __________`System.out.println( 5.0 / 2.0 );`
11. __________`System.out.println( 7 % 3 );`
12. __________`System.out.println( 6 % 3 );`
13. __________`System.out.println( 3 % 3 );`
14. __________`System.out.println( 3 % 7 );`

## String Expression Exercise

Assume the following:

    String t = "testing";
    String a = "Auf Wiedersehen";

Show what is printed by each of these statements.

1. __________ `System.out.println( t.length() );`
2. __________ `System.out.println( "Goodbye".length() );`
3. __________ `System.out.println( t + 2 );`
4. __________ `System.out.println( " real slow ".trim().length() );`
5. __________ `System.out.println( t.substring(1) );`
6. __________ `System.out.println( "Welcome".substring(2,4) );`
7. __________ `System.out.println( t.toUpperCase() );`
8. __________ `System.out.println( t.toUpperCase().toLowerCase() );`
9. __________ `System.out.println( a.indexOf("A") );`
10. __________ `System.out.println( a.indexOf("e") );`
11. __________ `System.out.println( a.lastIndexOf("e") );`
12. __________ `System.out.println( a.substring(a.indexOf(" ")+1) );`
13. __________ `System.out.println( t.equals("TESTING") );`
14. __________ `System.out.println( t.equalsIgnoreCase("TESTING") );`
15. __________ `System.out.println( t.substring(1,3) == "bc" );`
16. __________ `System.out.println( "t".compareTo(t) < 0 );`
17. __________ `System.out.println( t.compareTo("test") >= 0 );`
18. __________ `System.out.println( "t: \"" + t + "\"" );`
19. __________ `System.out.println( "\\\\".length() );`

## Boolean Expression Exercises

Assume the following:

    int small = 4;
    int large = 22;
    boolean t = true;

Show what is printed by each of the following statements.

1. __________ `System.out.println( 100 >= 1000 );`
2. __________ `System.out.println( t );`
3. __________ `System.out.println( large > small && small != 5 );`
4. __________ `System.out.println( small < large && large == 10 );`
5. __________ `System.out.println( large/2 >= small && small/2 == 3 );`
6. __________ `System.out.println( large > large && small == 7 );`
7. __________ `System.out.println( large > small || small != 5 );`
8. __________ `System.out.println( small < large || large == 22 );`
9. __________ `System.out.println( large/2 >= small || small/2 == 2 );`
10. __________ `System.out.println( large > large || small == 7 );`

## If Statement Exercises

What is printed by the following code fragments?

Assume:

    int alpha = 5;
    int n = 0;

1.     n = 17;
       if (alpha < 2) {
           n = 18;
       }
       System.out.println( n );

   Output = __________________

2.     n = 36;
       if (alpha != 314159)
           n = 87;
       System.out.println( n );

   Output = __________________

3.     n = 12;
       if (alpha == 12345)
           n = 20;
           n = n + 1;
       System.out.println( n );

   Output = __________________

4.     n = 0;
       if (alpha > 6) {
           n = 100;
       } else {
           n = 200;
       }
       System.out.println( n );

   Output = __________________

5.     n = 0;
       if (alpha <= 6) {
           n = 100;
       } else {
           n = 200;
       }
       System.out.println( n );

   Output = __________________

6.     n = 0;
       if (alpha == 5) {
           n = 1;
           if (alpha > 44) {
               n = 2;
           } else {
               n = 3;
           }
       } else {
           n = 4;
           if (alpha > 1) {
               n = 5;
           } else {
               n = 6;
           }
       }
       System.out.println( n );

   Output = __________________

## Loop Statement Exercises

What is printed in each of these questions? If a loop never finishes (or an integer "wraps around" the maximum or minimum int values), put infinite in the answer space.

1.     int n = 0;
       while (n < 4) {
          n = n + 2;
       }
       System.out.println( n );

   Output = __________________

2.     int n = 10;
       while (n >= 20) {
          n = n + 1;
       }
       System.out.println( n );

   Output = __________________

3.     int n = 0;
       int i = 0;
       while (i < 5) {
          if (i != 3 ) {
              n++;
          } else {
              n--;
          }
          i++;
       }
       System.out.println( n );

   Output = __________________

4.     String str = "";
       while (str.length() <= 4) {
          str = str + "cha";
       }
       System.out.println( str.length() );

   Output = __________________

5. Transform the following code into the equivalent `for` loop.

       sum = 0;
       int i = 1;
       while (i < 5) {
          sum = sum + i;
          i = i + 1;
       }

   ANSWER:

       sum = 0;
       for (int i = 1; i < 5; i = i + 1) {
           sum = sum + i;
       }

   This answer is "good enough", altho the scope of i has changed from known outside the loop to local to the loop. Other changes such as rewriting the incrementation of i as "i++" would be acceptable, but not really relevant to this problem.

6. Transform the following code into the equivalent using `while` loop.

       String greeting = "How are you?";
       int n = greeting.length();
       for (int i=0; i<n; i++) {
           char c = greeting.charAt(i);
           if (c != ' ') {
              System.out.print(c);
           }
       }

   ANSWER:

       String greeting = "How are you?";
       int n = greeting.length();
       int i = 0;
       while (i<n) {
           char c = greeting.charAt(i);
           if (c != ' ') {
              System.out.print(c);
           }
           i++;
       }

   Again the scope of i has changed from local to the loop to being known outside the loop.