CSCI 121 Exam 1 Answers

 

1

There are 8 bits in a byte

2

256 (28) different numbers may be represented using one byte

3

getHeight returns the number of rows in a Picture object

4

False

5

True

6a

Subscripts of arr range from 0 to 6

6b

The value of arr.length is 7 (the number of elements in arr)

6c

The value of arr[3] is 4

7

int, double, char, boolean, and float are Java primitive types

8

t.penDown() will cause the Turtle to draw when it is moved

9

p.show() causes a Picture p to display itself

10

 is drawn by method10 when invoked with a Turtle object

11

'a' is a valid char literal

12

"I" and "I am" are both valid String literals

13

The loop will be executed for the values of index ranging from 1 through 9. Therefore, “Thirteen” is printed 9 times

14

False, since Java is a case-sensitive language

15

The reserved word static is used to indicate class methods in Java

16

31 % 4 = 3 (the operator % returns the remainder)

17

31 / 4 = 7 (the integer quotient)

18

31 / 4.0 = 7.75

19

3 + 4 * 5 = 23 (multiplication is done before addition)

20

True—methods may be defined only within a class declaration

21

Sets the color of the pixel in column 30, row 50 to the color green

22

Since s is null, the value null is printed

23

turtle1.turn(-65);

24

for(int index = 47; index <= 96; index++)
{
   sum = sum + index;
}

25

for (Pixel p : pixels)
{
   int blue = p.getBlue();
   p.setBlue(blue / 2);
}

26

int ndx = 0;
while (ndx < pixels.length)
{

   int blue = pixels[ndx].getBlue();
   pixels[ndx].setBlue(blue / 2);
   ndx++;
}

27a

public MyPicture(String fileName)
{
   super(fileName);
}

27b

public MyPicture(int columns, int rows)
{
   super(columns, rows);
}

27c

public void grayscale()
{
   Pixels[] pixels = this.getPixels();
   for (Pixel p : pixels)
   {
      int red = p.getRed();
      int green = p.getGreen();

      int blue = p.getBlue();
      int gray = (red + green + blue) / 3;

      p.setRed(gray);

      p.setGreen(gray);
      p.setBlue(gray);
   }
}

 

or, using nested loops:

public void grayscale()
{
   for (int col = 0; col < this.getWidth(); col++)
   {
      for (int row = 0; row < this.getHeight(); row++)
      {
         Pixel p = this.getPixel(col, row);
         int red = p.getRed();
         int green = p.getGreen();

         int blue = p.getBlue();
         int gray = (red + green + blue) / 3;

         p.setColor(new Color(gray, gray, gray));
   }
}

28

public void shiftColors(int x1, int y1, int x2, int y2)
{
   for (int x = x1; x <= x2; x++)
   {
      for (int y = y1; y <= y2; y++)
      {
         Pixel p = this.getPixel(x, y);
         int red = p.getRed();

         int green = p.getGreen();

         int blue = p.getBlue();

         p.setColor(new Color(green, blue, red));
      }
   }

}

29

When outer = 1, the inner loop executes 1 time (inner = 0)
When outer = 2, the inner loop executes 2 times (inner = 0, 1)

When outer = 3, the inner loop executes 3 times (inner = 0, 1, 2)

When outer = 4, the inner loop executes 4 times (inner = 0, 1, 2, 3)

Thus, “twenty nine” is printed  1 + 2 + 3 + 4 = 10 times