Practice exam for CIS 263 Exam 1

You can print these problems out by using the "File -> Print..." menu item.

Problem 1:
What does the folowing code print out?
          System.out.print( "***" );
          System.out.print( "\n**\n" );
          System.out.println( "\n*" );
          System.out.println( "" );
          System.out.println( "**" );
    	  System.out.print( "***" );

Problem 2:
Rewrite the following program segment as a single if-else statement.
          if (n < 0)
             x = x - n;
          if (n >= 0)
             x = x + n;

Problem 3:
If x is an double, what does the following code segment print out for every possible value of x?
          if (x >= 0)
          if (x >= 1)
          System.out.println("over here");
          else
          System.out.println("no, over there");

Problem 4:
Rewrite the following program segment using a while-loop. What do these loops print out?
          int n = 10;
          for( int i = 0; i <= 2*n; i+=2 )
          {   System.out.println( i );
          }

Problem 5:
Identify and correct the errors in the following program segment.
          int x = 1; total;
          While (x <= 10)
             total += x;
             ++x;

Problem 6:
Find the error in the following if-statement and correct it.
          if (0 <= x <= 5)
             System.out.println( "x is between 0 and 5" );

Problem 7:
What does the following program print?
          public class Printing
          {
             public static void main( String args[] )
             {
                i = -1; j = 2;
                while (i + j < 5)
                {  while ( j < 5)
                   {  System.out.println(i + " and " + j);
                      j = j + 2;
                   }
                i = i + 2;
                j = j - 3;
                }
             }//main
          }//Printing

Problem 8:
Give the method headers for each of the following methods.

(a) Method randomArray which does not take any arguments and returns an array of doubles.

(b) Method findSomething which takes an array of integers and a single character and returns an integer.

(c) Method severalNumbers which takes four doubles and returns three integers. (Note: There are several possible answers to this; you only need to give one.)

Problem 9:
Find the errors in the following definition of method doSomething and show how the errors can be corrected.
                int doSomething(double n, double m)
                {   result = m*m + n;
                    if ( result < 0 )
                       return -result;
                    else result;
                }

Problem 10:
Write a program segment that declares an array variable, constructs an array, assigns the array to the array variable, and then uses a loop to fill the array with the following integers.
           -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10


Return to the main Java page.
Return to the CIS 263 home page.


compliments and criticisms