Augustana Faculty logo and wordmark


COMPUTING SCIENCE 110
Introduction to Computing Science

Sample Exam Questions


Java

  1. In Java, what mathematical operators are represented by the symbols *, /, and %?

  2. In Java, what symbols are used to represent the logical operators AND, NOT, and OR?

  3. True or False?

    1. Java checks for out-of-bounds array indices at run time.

    2. Arrays can contain strings as elements.

    3. Arrays can use strings as subscripts.

    4. The method heading

              public void someFunc( double[] x )
           

      causes a compile-time error because the size of the array is missing.

    5. An array can be declared without specifying its size by initializing it with a list of elements enclosed in braces, as in

         int[] primes = { 2, 3, 5, 7, 11, 13 };
    6. A Java source file can contain at most one public class definition.

  4. In the following code fragment, the programmer mistakenly placed a semicolon at the end of the for statement heading. What is the result?

      System.out.print("A");
      for ( int count = 1; count <= 3; count++ );
         System.out.print("B");
      System.out.print("C");
      
    1. a compile-time error

    2. an infinite loop

    3. the output ABC

    4. the output ABBC

    5. the output ABBBC

  5. How often does each of the following loops execute? Assume that i is not changed in the loop body.

    1. for ( int i = 1; i <= 10; i++ ) ...

    2. for ( int i = 0; i < 16; i++ ) ...

    3. for ( int i = -10; i <= 10; i++ ) ...

    4. for ( int i = 8; i > 0; i-- ) ...

    5. for ( int i = 1; i <= 10; i = i + 2 ) ...

  6. What are the values of s and n after each of the following loops?

    1. int s = 1;
      int n = 1;
      while ( s < 10 )
      {
         s = s + n;
         n++;
      }
      

    2. int n;
      int s = 1;
      for ( n = 1; n < 6; n++ )
         s = s + n;
      

    3. int s = 1;
      int n = 1;
      do
      {
         s = s + n;
         n++;
      } while ( s < 3 * n )
      

  7. After execution of the code fragment

       int[] arr = new int[5];
       for ( int i = 0; i < 5; i++ )
       {
          arr[i] = i + 2;
          if ( i >= 3 )
             arr[i-1] = arr[i] + 3;
       }
    

    what is contained in arr[1] and arr[3]?

  8. What is the output of the following program fragment?

       int[] hundreds = { 100, 200, 300, 400, 500 };
       for ( int i = 4; i > 0; i-- )
          System.out.print( hundreds[i] + " " );
      
  9. Given the following declarations

       double x = 2.5;
       double y = -1.5;
       int m = 16;
       int n = 4;
       String s = "silly";
       String t = "thing";
      

    what is the value of each of the following expressions?

    1. x + n * y - (x + n) * y

    2. Math.sqrt( Math.sqrt(m))

    3. (int) Math.round(x)

    4. 1 - (1 - (1 - (1 - n)))

    5. s + t

    6. s + n

    7. s.length() + t.length()

  10. Given the following declarations

       double x = 12.8;
       double y = -1.5;
       int m = 18;
       int n = 4;
      

    what is the value of each of the following expressions?

    1. x / n + y

    2. m / n + m % n

    3. 4 * y - n / 5

    4. (int) x

    5. x + n * y

  11. Rewrite the following section of code using a for loop instead of a while loop.

       int sum = 0;
       int count = -5;
       while ( count <= 15 )
       {
          sum = sum + count;
          count++;
       }
      
  12. Fix the error in the following code fragment. It is intended that the loop will find the minimum element of array v.

       int[ ] v = new int[10];
       . . .  // values are assigned to elements of v
       int min = v[0];
       for ( int i = 1; i <= 10; i++ )
          if ( v[i] < min )
             min = v[i];
      
  13. Which of the following expressions would correctly extract the digit in the tens column of a four-digit decimal number store in value?

    1. value / 100

    2. 10 * value % 100

    3. value % 100 / 10

    4. value - value % 10

    5. none of the above

  14. Correct the error (or errors) in each of the following code excerpts by removeing, inserting, or changing one or more characters as required. You may assume that the variable x has been correctly declared and initialized.

    1.    if ( x > 5 );
            System.out.print("bigger");       
         

    2.    if ( x == 0 )
            return 0;
         else if ( x < 0 )
            return -1;
         else ( x > 0 )
            return 1;
         

    3.    if ( x == 0 ) || ( x == 1 ) return 1;
         

    4.    return ( -0.5 <= x <= 0.5 );
         

    5.    public static double average( int a; int b )
         {
            return ( a + b ) / 2.0;
         }
         

    6.    if ( x = 1 ) x++; else x = x + 2;    
         

    7.    int z = 5.0e-3;    
         

    8.    while x < 10
            x = x + 2;
         

Copyright © 2004 Jonathan Mohr