Exam 1 Practice


1. Which of the following expressions performs the test "the number x is less than or equal to 500"

x =< 500
x < 500
x <= 500
500 > x

2. Given the declaration: int[] arr; What statement creates an array of 20 int elements and causes arr to reference it?

arr = int[20];
arr = new int[20];
arr[20];
arr = 20[int]
None of the above

3. Given the declaration int arr[]; What is printed by the statement System.out.println(arr);

arr
arr[]
arr@12fa84
null
None of the above

4. What method of the class Picture can be used to determine the number of pixels in each row of a Picture object?

getPixels()
getWidth()
getHeight()
getPixelsInRow()
None of the above

5. What does the statement below do?
Turtle t;

Declares a reference to a Turtle object.
Declares a Turtle object.
Creates a Turtle object.
None of the above

6. What does the following statement do?
Color color = new Color(200, 38, 145);

Creates a Color object with red=200, blue=38, and green=145.
Creates a Color object with red=200, blue=38, and green=145 and assigns its address to color.
Creates a Color object with red=200, green=38, and blue=145.
Creates a Color object with red=200, green=38, and blue=145 and assigns its address to color.

7. Given:
    int[] arr = {10, 20, 30, 40, 50};
    int s = 0;
What is printed by the following code?
  for (int value : arr)
  {
     s += value;
  }
  System.out.println(s);

50
150
s
0

8. What Turtle class method changes the pen color?

Answer:

9. What Turtle class method can be used to change a Turtle object's heading?

Answer:

10. Given:
 Picture p = new Picture(640, 480);  
Write a Java statement that sets the color of the pixel at coordinates (30, 200) to RED.

Answer:

11. How many times is the line "Hello" printed by the following code?
   for (int index = 0; index == 10; index++)
   {
      System.out.println("Hello");
   }

1 time
0 times
10 times
11 times
None of the above

12. Consider the following method declaration in the class Turtle:

public void question12()
{
   this.setHeading(180);
   this.forward(200);
   this.turnLeft();
   this.forward(100);
}

Suppose Turtle t is used to invoke this method, as in
  t.question12();  
What is the effect of this call?

Turtle t draws a square
Turtle t draws the character 'L'
Turtle t draws the character 'T'
Turtle t draws a rectangle
None of the above

13. How many different values can be represented with a byte?

8
32
256
65536
None of the above

14. Given the declaration

Picture p = new Picture("caterpillar.jpg");

Which of the following statements causes Pixel pix to refer to the pixel in row 13, column 233?

Pixel[] pixels = p.getPixels(); Pixel pix = pixels[13,233];
Pixel pix = getPixel(13, 233);
Pixel pix = getPixel(233, 13);
Pixel pix = p(13, 233);
Both (1) and (2)
None of the above

15. Write the Java statement that gets the red value from Pixel pix and assigns it to the int variable redLevel.

Answer:

16. Write the Java method call that returns a reference to the pixel at column 80 and row 23 of the Picture object referenced by Picture variable pic.

Answer:

17. Write a statement that declares an array of int elements named arr, with the elements initialized to the values 10, 17, 56, and 32.

Answer:

18. Write an expression that returns the number of elements in the array pixels.

Answer:

19. Assuming that arr is an integer array, what is the effect of the following code?
   for (int ndx = 0; ndx < arr.length; ndx++)
   {
      int tmp = arr[ndx];
      arr[ndx] = arr[arr.length-1-ndx];
      arr[arr.length-1-ndx] = tmp;
   }

It reverses the elements of the array arr
It results in a NullPointerException
It results in an ArrayIndexOutOfBoundsException
It reverses the elements of the array arr twice, thus having no effect
None of the above

20. What command causes the Turtle turtle1 to turn 45o clockwise?

turtle1.turnRight(45);
turtle1.turnRight();
turtle1.turn(45);
turtle1.turn(-45);
None of the above