CS 120  Test II

  1. (T/F) An array can store elements of two different types.  For example, the same array can hold both integers and strings.
  2. For each object, when is the constructor for that object called.
    1. At the end of the program.
    2. When the paint() routine is called.
    3. When the object is created.
    4. At the beginning of the program.
  3. (T/F) If an array 'myArray' has been declared to have 30 elements of type int, and the value stored in 'myArray[ i]'  is 9, then 'i' must be 9.
  4. (Yes/No) Suppose a java applet has a parent class and a child class.  Must the parent class have a paint() assuming the child class already has a paint()?
  5. (Yes/No) Suppose a program wants to put one line of text upon the screen.  Is this a reasonable paint function to do so.

  6.                     public void paint (Graphics g){
                    g.drawString("One Line of Text", 100, 100);
                    repaint();
            }
  7. Suppose I have a child class named fredFlinstone.  The constructor takes no arguments.  How would I declare an object  named foo of type   fredFlinstone. ________________________________________________________________
  8. _____________________________ What is the name of the constructor for the class wilma?
  9. Suppose I have a child class named button, which has a method paint().  Which is true.
    1. The system calls button's paint, just like the system calls the main object's paint().
    2. The main object's paint() should call the button's paint().
    3. The main object should call repaint(), which will call the child object button's paint() method.
    4. Java does not have paint() methods.
  10. __________ How many elements does an array String names[10][20][30] have.  Be careful to get the math right.
  11. Please write a line of code that sets 'a' to a random number between 1 and 6, like a single die.  Assume there is an object of type Random named 'r' already created and ready to use. _________________________________________________.
  12. Same as the question above, but the numbers should be between 50 and 500, including both the fifty and five hundred.
    ___________________________________________________________________
  13. Give the output of the following Java program

  14. import java.awt.*;
    import java.applet.Applet;
    public class Test2P3 extends java.applet.Applet
    {
            int  arr [ ];
            public void init () {
                    arr = new int [ 5 ];
            }
            public void paint ( Graphics g ) {
                    arr[ 0 ] = 1;
                    arr[ 1 ] = 1;
                    for (  int i = 2; i < 5;  ++i )
                            arr[ i ] = arr[ i - 1 ] + arr[ i - 2 ];
                    g.setColor( Color.black );
                    for (  int k = 0; k < 5; ++k )
                            g.drawString( "" + arr[ k ], 30, 30 + 15 * k )
            }
    }
  15. (T/F) Is it true that a boolean function should end return 0.
  16. Suppose that a program has the parent object/class  ticktacktoe  and a child object/class button.  Fill in the blank for the *.html file.

  17. < applet code="__________________________" height =500 width=500>
  18. Please write one line that declares an 8x8 array of ints named chessboard. _______________________________________________________________________
  19. (Yes/No) Is the last semicolon on the line that starts for correct?

  20.         for(i = 0; i < 10; i++) ;
                a[i] = b[1];
  21. Fill in the constructor below.

  22.         public class Barney {
                int size;
                String Name;
                float Weight;
                // Constructor goes here ....

     
     
     
     
     
     
     
     

                paint(Graphics g) {
                    // some code that paints
                }
            }

  23. Please fill in the program below so that the 10 place array killThemAll is filled with objects of type  Barney.  These objects should all be initiallized, and space allocated as needed.  The constructor for Barney takes no arguments.

  24.         public class ChildTVShow {
                   int a;
     
     
     
     

                    init() {
                        a=5;
     
     
     
     
     
     
     
     
     

                                        }
              }

  25. Write me some loops that zero out the array bamBam,which as declared float bamBam[10][99];











  26. Write me some loops that find the largest element of the array Pebbles, which is declared float Pebbles[10][20].  Leave the answer in a variable named max.







  27. Suppose that integer arrays 'a' and 'b' already exist and have space allocated for them.  They each can hold thirty elements.  Write me a loop that takes the values of 'a' and stores them into 'b', but 'b' is backwards.  For example, if the first element of 'a' is 10, then the last element of 'b'  must become 10.

  28.  
     









     

  29. Write me a program that prints the numbers 1...20 on the screen.  This should be an entire working program, and not just some loops (though it can contain loops).   No number should be printed ontop of another.