Sample Exam 1 questions


Sample true/false questions

  1. public is a reserved word in Java.

  2. The following prints the numbers 2 through 5 (including 2 and 5)
        for (int i=2; i<5; i++) {
           System.out.println (i);
        }
        

Sample multiple-choice questions

  1. How many compiler errors does the following have?
      public class ForLoopPrint {
          public static void main (String[] argv)
          {
              for (i=0; i<5 i++) {
                  System.out.println (i)
              }
          }
      }
      

  2. What is the output of this program? (Only the code inside main is shown.)
            int a=1, b=2, c=3; 
            for (int i=0; i<5; i++) {
                c = a + b;
                b = c;
            }
            System.out.println (c);
      

Sample programming problems

  1. Write a program to add up the negative and only the negative elements of an array. Thus, the output for this array
      int[] A = {1, -2, 3, 4, -5, 6, -7};
      
    should be -14.

  2. Write a program that, given an array of elements such as
      int[] A = {1, 2, 3, 4};
      
    prints out all possible pairs of elements, with each pair on one line, and where each element in a pair must be different. We'll count the pair 1,2 the same as the pair 2,1. Thus the output for the above will be:
       1  2
       1  3
       1  4
       2  3
       2  4
       3  4