Midterm

 

  1. Which program is to generate a random integer between –100 and 100?
    A. int a = (int)(Math.random()*100.) - 100;
    B. int a = (int)(Math.random()*200.) - 100;
    C. int a = (int)(Math.random()*200.)-(int)(Math.random()*100.);
    D. int a = (int)(Math.random()*(-100.))+100.;

 

  1. Which program is to generate 8 by 4 grid such as the following image.
    ********
    ********
    ********
    ********
    A. for(int i=1; i<=8; i++){
             for(int j=1; j<=4; j++){
                  g.drawString(“*”, i, j);
             }
        }
    B. for(int i=0; i<=8; i++){
             for(int j=0; j<=4; j++){
                  g.drawString(“*”, i, j);
             }
        }
    C. for(int i=0; i<8; i++){
             for(int j=0; j<4; j++){
                  g.drawString(“*”, j, i);
             }
        }
    D. for(int i=1; i<8; i++){
             for(int j=1; j<4; j++){
                  g.drawString(“*”, j, i);
             }
        }

 

  1. I programmed the following, but it could not compile successfully. Choose the reason why the program did not work well.

    public class MyPoint3D {
          double  x, y, z;
          public MyPoint3D( ){
                       x=y=z=0.;
          }
          public void drawMyPoint3D(java.awt.Graphics g){
                       g.drawString(“.”, x, y);
          }
    }


    A
    . x=y=z=0. is incorrect in JAVA.
    B. There is no
    import.
    C. There is no
    extend.
    D. Casting problems.

 

 

  1. Choose one method, which JAVA does not support.
    A. mouseDrag (Event e, int x, int y)
    B. mouseUp (Event e, int x, int y)
    C. mouseClick (Event e, int x, int y)
    D. mouseDown (Event e, int x, int y)

 

 

  1. I want to create a Color object whose color is black. Which is the correct statement?
    A. Color myColor = new Color(255, 255, 255);
    B. Color myColor = new Color.black;
    C. Color myColor = new Color (0,0,0);
    D. Color myColor = new Color (black);

 

 

  1. What is the result of the following program?

    int a=0;
    for (int i=0; i<5; i++){
          a++;
          a += i;
    }

    A. a=10
    B. a=15
    C. a=21
    D. a=0

 

  1. There are two integer variables, x and y. If you want to change their value of x and y to x+y and x-y respectively. Which program is correct?

    A. x = x + y;
       y = x – y;

    B. x += y;
       y –= x;

    C. int tempX = x;
       int tempY = y;
       tempX = x + y;
       tempY = x – y;
     
    D. int tempX = x;
       int tempY = y;
       x=tempX + tempY;
       y=tempX – tempY;

 

 

  1. Which is safe to use as a variable name in JAVA?

    A. 3D_Point
    B. 3DPoint
    C. Point-3D
    D. three_Dimensional_Point_3

 

  1. There is a two dimensional integer array, which is a 4 by 3 and named “a”. The data is
    a[0][0]=0   a[0][1]=2   a[0][2]=4   a[0][3]=6
    a[1][0]=8   a[1][1]=10  a[1][2]=12 a[1][3]=14
    a[2][0]=16  a[2][1]=18  a[2][2]=20 a[2][3]=22.

    I want to make a new one-dimensional array named “b” to store the a’s data. In other words, the data of  “b” should be

    b[0]=0   b[1]=2  b[3]=4  ......  b[11]=22.

    Which is the correct program?

    int[] b = new int[12];
    for (int i=0; i<3; i++) {
        for (int j=0; j<4; j++) {
            //***** Choose one from A, B, C, and D.
         }
    }

    A. b[i] = a [i][j];
    B. b[j*4 + i] = a[i][j];
    C. b[i*4 + j]= a[j][i];
    D. b[i*4 + j]= a[i][j];
     

 

  1. What is the result of this program?

    int a = 0;
    for (int i=0; i<100; i++){
        if( i %10 ==0){
               a ++;
        }
    }

    A. a=0
    B. a=1
    C. a=10
    D  a=100

 

  1. How many bits does the “boolean” primitive have?
    A. 2 bits
    B. 1 bit
    C. 8 bits
    D  32 bits

 

12.     After compiling your java files, Symantec Cafe generates class files with the same names. Which is the correct explanation about the class files.

A. Class file is ASCII(text) file that we can understand the program.
B. Class file is bytecode file that we cannot understand but computers can.
C. Class file is HTML file that can run in Browser applications (NetScape, InternetExplore).
D. Class file is application execute file to run application by double clicking the file.

 


13.  To display a Button with a size of 30x30 at position 100, 100 I need to do the following:

 

 

Button b = new Button(“Click Here”);

b.setSize(30, 30);

b.setLocation(100, 100)

applet.add(b);

 

What am I missing?

 

 

 

 

  1.  What does the codes 10, 20, and 30 mean in DXF?

 

 

 

 

 

  1. What does the java object called Vector do?

 

 

 

 

 

  1. What is a beta version of a program?

 

 

 

 

 

17.  If degs is an angle in degrees, how do we convert it in radians:

 

A.        Math.PI/180.* degs

B.           180./Math.PI * degs

C.           degs/Math.PI * 180.

D.          Degs/180. * Math.PI

 

 

  1.  What does this method do?  When is it called?

 

public void action(Event e, Object o){

 

. . .

 

}

 

 

 

 

 

 

 

 

  1. Write the code that would draw a point on the screen:

 

public void drawPoint(Graphics g, double x, double y){

 

 

 

 

 

 

 

}

 

 

  1. What does method fuzzyShape do? Draw it.

 

public void fuzzyShape(Graphics g){

 

for(int i=0; i<10; i++)

         g.drawLine(0, i*10, 100, i*10);

for(int j=0; j<10; j++)

         g.drawLine(j*10, 0, j*10, 100);

}

 

 

 

 

Answers

 

1.     Which program is to generate a random integer between –100 and 100?


A. int a = (int)(Math.random()*100.) - 100;
B. int a = (int)(Math.random()*200.) - 100;
C. int a = (int)(Math.random()*200.)-(int)(Math.random()*100.);
D. int a = (int)(Math.random()*(-100.))+100.;

 

As we already know Math.random() creates always positive numbers.  To get a negative range we need to generate random numbers within the positive range and then subtract enough to get negative numbers.  So we first create a range of random numbers between 0 and 200 and then we subtract 100.

The answer is B.

 

  1. Which program is to generate 8 by 4 grid such as the following image.
    ********
    ********
    ********
    ********
    A. for(int i=1; i<=8; i++){
             for(int j=1; j<=4; j++){
                  g.drawString(“*”, i, j);
             }
        }
    B. for(int i=0; i<=8; i++){
             for(int j=0; j<=4; j++){
                  g.drawString(“*”, i, j);
             }
        }
    C. for(int i=0; i<8; i++){
             for(int j=0; j<4; j++){
                  g.drawString(“*”, j, i);
             }
        }
    D. for(int i=1; i<8; i++){
             for(int j=1; j<4; j++){
                  g.drawString(“*”, j, i);
             }
        }

 

Look at the g.drawString method.  The two last arguments are the x and y, which we have replaced with i and j.  The correct answer is A.

 

  1.  I programmed the following, but it could not compile successfully. Choose the reason why the program did not work well.

    public class MyPoint3D {
          double  x, y, z;
          public MyPoint3D( ){
                       x=y=z=0.;
          }
          public void drawMyPoint3D(java.awt.Graphics g){
                       g.drawString(“.”, x, y);
          }
    }


    A
    . x=y=z=0. is incorrect in JAVA.
    B. There is no
    import.
    C. There is no
    extend.
    D. Casting problems.

 

The correct answer is D.

 

  1. Choose one method, which JAVA does not support.
    A. mouseDrag (Event e, int x, int y)
    B. mouseUp (Event e, int x, int y)
    C. mouseClick (Event e, int x, int y)
    D. mouseDown (Event e, int x, int y)

 

There is no MouseClick.

 

  1. I want to create a Color object whose color is black. Which is the correct statement?
    A. Color myColor = new Color(255, 255, 255);
    B. Color myColor = new Color.black;
    C. Color myColor = new Color (0,0,0);
    D. Color myColor = new Color (black);

 

Correct answer is C

 

  1. What is the result of the following program?

    int a=0;
    for (int i=0; i<5; i++){
          a++;
          a += i;
    }

    A. a=10
    B. a=15
    C. a=21
    D. a=0

 

When i=0 a=1 and then a=1+0=1

When i=1 a=2 and then a=2+1=3

When i=2 a=4 and then a=4+2 = 6

When i=3 a=7 and then a=7+3 = 10

When i=4 a=11 and then a=11+4=15

Therefore the correct answer is B

 

  1. There are two integer variables, x and y. If you want to change their value of x and y to x+y and x-y respectively. Which program is correct?

    A. x = x + y;
       y = x – y;

    B. x += y;
       y –= x;

    C. int tempX = x;
       int tempY = y;
       tempX = x + y;
       tempY = x – y;
     
    D. int tempX = x;
       int tempY = y;
       x=tempX + tempY;
       y=tempX – tempY;

 

The correct answer is D because all the others either affect the values of x and y or do not assign anything to x and y

 

  1. Which is safe to use as a variable name in JAVA?

    A. 3D_Point
    B. 3DPoint
    C. Point-3D
    D. three_Dimensional_Point_3

A variable name cannot start with a number and cannot contain any arithmetic operation (+, -, *, /).

The correct answer is D.

 

9.  There is a two dimensional integer array, which is a 4 by 3 and named “a”. The data is
a[0][0]=0   a[0][1]=2   a[0][2]=4   a[0][3]=6
a[1][0]=8   a[1][1]=10  a[1][2]=12 a[1][3]=14
a[2][0]=16  a[2][1]=18  a[2][2]=20 a[2][3]=22.

I want to make a new one-dimensional array named “b” to store the a’s data. In other words, the data of  “b” should be

b[0]=0   b[1]=2  b[2]=4  ......  b[11]=22.

Which is the correct program?

int[] b = new int[12];
for (int i=0; i<3; i++) {
    for (int j=0; j<4; j++) {
        //***** Choose one from A, B, C, and D.
     }
}

A. b[i] = a [i][j];
B. b[j*4 + i] = a[i][j];
C. b[i*4 + j]= a[j][i];
D. b[i*4 + j]= a[i][j];
 

We need to multiply the number of rows and add the number of columns.

Since i is the number of rows and j the number of columns the correct answer is D

 

  1.  What is the result of this program?

    int a = 0;
    for (int i=0; i<100; i++){
        if( i %10 ==0){
               a ++;
        }
    }

    A. a=0
    B. a=1
    C. a=10
    D  a=100

For all the integer numbers between 0 and 99 there are only 10 numbers that when divided have a remainder of 0.  These numbers are 0, 10, 20, 30, 40, 50, 60, 70, 80, and 90.  The correct answer is C.

 

  1. How many bits does the “boolean” primitive have?
    A. 2 bits
    B. 1 bit
    C. 8 bits
    D  32 bits

 

One bit that can be turned either on (true) or off (false).  Correct is B.

 

12.     After compiling your java files, Symantec Cafe generates class files with the same names. Which is the correct explanation about the class files.

A. Class file is ASCII(text) file that we can understand the program.
B. Class file is bytecode file that we cannot understand but computers can.
C. Class file is HTML file that can run in Browser applications (NetScape, InternetExplore).
D. Class file is application execute file to run application by double clicking the file.

A class file is a bytecode file that we cannot understand but computers can.  Correct is B.

 

  1. To display a Button with a size of 30x30 at position 100, 100 I need to do the following:

 

Button b = new Button(“Click Here”);

b.setSize(30, 30);

b.setLocation(100, 100);

applet.add(b);

 

What am I missing?

 

applet.setLayout(null);

 

14.  What does the codes 10, 20, and 30 mean in DXF?

 

 

The x, y, and z values.

 

 

15.  What does the java object called Vector do?

 

 

It is an array that allocates memory on-the-fly as needed

 

 

  1. What is a beta version of a program?

 

 

It is a pre-release for testing

 

 

17.  If degs is an angle in degrees, how do we convert it in radians:

 

A.        Math.PI/180.* degs

B.           180./Math.PI * degs

C.           degs/Math.PI * 180.

D.          Degs/180. * Math.PI

 

180 degrees is equal to Math.PI so

degs degrees are equal to  Math.PI/180.*degs

Correct is A

 

  1.  What does this method do?  When is it called?

 

public void action(Event e, Object o){

 

. . .

 

}

Handles events and objects in the scene.  It is used to handles GUI objects.

 

 

 

  1. Write the code that would draw a point on the screen:

 

public void drawPoint(Graphics g, double x, double y){

 

g.drawRect(x, y, 1, 1);

 

or

 

g.drawString(“.”, x, y);

 

}

 

 

  1. What does method fuzzyShape do? Draw it.

 

public void fuzzyShape(Graphics g){

 

for(int i=0; i<10; i++)

         g.drawLine(0, i*10, 100, i*10);

for(int j=0; j<10; j++)

         g.drawLine(j*10, 0, j*10, 100);

}

 

A grid of 10 lines separated by 10 pixels