Practice exam for CIS 263 Exam 2

The exam covers Chapters 8, 9, 10 (applets only), 11, and the streams used in the MyInput class for input from the keyboard.

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

Problem 1:
Consider the following code fragment.
          int[] myArray = {1, 2, 3, 4, 5, 6};
          int i = 0;
          try
          {
             while(true)
             {
                System.out.println( myArray[i] * myArray[i] );
                i++;
             }
          }
          catch(Exception e){}
Part (a): What is the output from this code fragment? Briefly explain why.

Part (b): What would be a much better way to accomplish the same thing that the try-catch clause does?

Problem 2:
What is wrong with the following outline of a code fragment?
          try
          {
             // some code that might throw an exception
          }
          catch(Exception e)
          {
             // some exception handling code
          }
          catch(IOException e)
          {
             // some more exception handling code
          }

Problem 3:
What happens if a method causes an exception but there is no catch clause in the method for the exception?

Problem 4:
Consider the following program.
        public class Question4
        {
           public static void main( String args[] )
           {
              final int CONSTANT = 10;
              int x;
              try
              {
                 x = foo(CONSTANT);
              }
              catch(Exception e)
              {
                 System.out.println("Caught an exception: " + e);
              }
              finally
              {
                 x = 99;
              }
              System.out.println(x);
           }//main

           static int foo(int x)
           {
              System.out.println("foo started with " + x);
              int temp = bar(x);
              System.out.println("foo returning " + temp);
              return temp;
           }//foo

           static int bar(int y)
           {
              System.out.println("bar started with " + y);
              if (y > 0)
                 throw new RuntimeException("just a test");
              System.out.println("bar returning " + y);
              return y;
           }//bar
        }//Question4
Part (a): What is the output of this program?

Part (b): What would be the output of this program if the value of CONSTANT is changed to -10?

Problem 5:
Consider the following program.
        import java.io.*;
        public class Question5
        {
           public static void main( String args[] )
           {
               isr = new InputStreamReader(System.in);
               BufferedReader br = new BufferedReader(isr);
               while(true)
               {  try
                  {  System.out.print("Input 1: ");
                     String input = br.readLine();
                     int number = Integer.parseInt(input);
                     if (number < 0) throw new Exception();
                  }
                  catch(NumberFormatException e)
                  {  System.out.println("Opps"); }
                  catch(Exception e)
                  {  System.out.print("Input 2: ");
                     String input = "";
                     try
                     {  input = br.readLine();}
                     catch(IOException ioe)
                     {  System.out.println("Opps");}
                     if (input.equals("yes")) System.exit(0);
                  }
               }
           }//main
        }//Question5
Part (a) What happens if a user types the string "yes" at the prompt "Input 1" and hits the Enter key? Briefly explain why.

Part (b) What happens if a user types the string "123" at the prompt "Input 1" and hits the enter key? Briefly explain why.

Part (c) What happens if a user types the string "-123" at the prompt "Input 1" and hits the enter key? Briefly explain why.

Problem 6:
Indicate which layout managers (BorderLayout, FlowLayout, or GridLayout) have each of the following properties.

(a) By default, puts no space between components.

(b) Requires an add method with two arguments.

(c) Keeps all components at their preferred size.

(d) Forces all components to be the same size.

(e) Keeps components in the same relative positions when the container is resized.

(f) Can accomodate any number of components.

Problem 7:
Consider the following program.
          import javax.swing.*;
          import java.awt.*;
          import java.awt.event.*;
          public class Question7 extends JFrame implements ActionListener
          {
             JTextField field1, field2;
             JButton button1, button2;
             public Question7()
             {  super("Question 7");
                Container c =  getContentPane();
                c.setLayout( new FlowLayout() );
                field1 = new JTextField(10);
                c.add(field1);
                button1 = new JButton("Button1");
                button1.addActionListener(this);
                c.add(button1);
                field2 = new JTextField(10);
                c.add(field2);
                button2 = new JButton("Button2");
                button2.addActionListener(this);
                c.add(button2);
                setSize(300,300);
                show();
             }
             public void actionPerformed(ActionEvent e)
             {  if (e.getSource() == button1 )
                { field2.setText( field1.getText() ); }
                else
                {  if ( field2.getText().equals("done") ) System.exit(0);
                   else field2.setText("");
                }
             }
             public static void main( String args[] )
             { Question7 app = new Question7(); }
          }//Question7
Part (a) Briefly describe what the graphical user interface looks like.

Part (b) What happens when a user clicks on Button1?

Part (c) What happens when a user clicks on Button2?

Part (d) What happens when a user enters the string "Done" into field2 and then presses the Enter key? Briefly explain why?


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


compliments and criticisms