Peer Instruction: 2D Arrays Multiple Choice Questions¶
- Removes all red from the picture
- Incorrect. While this code does modify the red value of each pixel, it does not completely remove it
- Changes half of the red pixels to not be red
- Incorrect. Based on the while loop, this code modifies every pixel, not just half
- Reduces the red component of half of the pixels
- Incorrect. Based on the while loop, this code modifies every pixel, not just half
- Reduces the red component of each pixel to half of its original value
- Correct. This code takes the original red value of a pixel, halves it, and then sets the red value of this pixel to our new value
- Sets the red component of each pixel to 0.5
- Incorrect. The value of each color must be an int, which 0.5 is not
8-10-1: What does this code do?
Pixel[] pixelArray = this.getPixels(); int value = 0; Pixel p = null; int index = 0; while (index < pixelArray.length) { value = pixelArray[index].getRed(); value = (int) (value * 0.5); pixelArray[index].setRed(value); }
- Location A
- Incorrect. This statement will only print once, and most of the code has yet to be called
- Location B
- Incorrect. This would result in the print statement being run in a loop, instead of the contents inside the curly braces
- Location C
- Incorrect. This way, the statement will print new information each time the code is run
- Location D
- Correct. Location D allows you to assess and print the all varibales used in this code block. This gives you the most data, and makes it the optiomal location.
8-10-2: For debugging, where is the best place to put a print statement?
Pixel[] pixelArray = this.getPixels(); int value = 0; Pixel p = null; int index = 0; //Location A while (index < pixelArray.length) //Location B { //Location C value = pixelArray[index].getRed(); value = (int) (value * 0.5); pixelArray[index].setRed(value); index = index + 1; //Location D }
- It has a compiler error
- Incorrect. This code can successfully compile.
- It sets the red value to be the same as blue
- Correct. The variable value is set to pix.getBlue() when both .setRed() and .setBlue() are called.
- It sets the blue value to be the same as red
- Incorrect. The variable value is set to pix.getBlue() when pix.setRed(value) is called.
- It really does swap
- Incorrect. Look closer at the variable value. Do you see where it is reassigned?
8-10-3: This code should swap the red and blue components at each pixel, what does it actually do?
Pixel[] pixelArray = this.getPixels(); int value = 0; int index = 0; while (index < pixelArray.length) { Pixel pix = pixelArray[index]; value = pix.getRed(); value = pix.getBlue(); pix.setRed(value); pixelArray[index].setBlue(value); index++; }
- value = pix.getRed(); pix.setBlue(pix.getRed()); pix.setRed(value);
- Incorrect. The value of the blue component is successfully changed, however the value of component is not.
- value = pix.getRed(); pix.setBlue(value); pix.setRed(pix.getBlue());
- Incorrect. The value of the blue component is successfully changed, however the value of component is not.
- value = pix.getRed(); pix.setRed(pix.getBlue()); pix.setBlue(value);
- Correct. In this case, "value" is the temporary variable we use to remember the original value of pix.getRed(), even after the red component is changed.
- value = pix.getRed(); pix.setRed(value); pix.setBlue(pix.getRed());
- Incorrect. If value is set to pix.getRed(), the call to pix.setRed(value) will leave the red component unchanged.
8-10-4: Which code chunk should be inserted into the marked location to swap the red and blue components at each pixel?
Pixel[] pixelArray = this.getPixels(); int value = 0; int index = 0; while (index < pixelArray.length) { Pixel pix = pixelArray[index]; **CODE GOES HERE** index++; }
- It tries to access pixelArray[-1]
- Incorrect. Even though this would throw an error, pixelArray[-1] is never called.
- It tries to access pixelArray[0]
- Incorrect. This code does try to access pixelArray[0], but due to zero-based indexing, this is not an error.
- It tries to access pixelArray[pixelArray.length]
- Correct. In the final iteration of the for loop, the value of "index" is pixelArray.length - 1. So, when "q" is assigned to pixelArray[index + 1], the code tries to access pixelArray[pixelArray.length], which does not exist.
- It tries to access pixelArray[pixelArray.length + 1]
- Incorrect. Due to the parameters in the for loop, the largest value index can take on is pixelArray.length - 1, and thus pixelArray[index+1] is never called.
- None of the above
- Incorrect. Consider the range of values index can have, and then examine the line where q is assigned.
8-10-5: Why does this code have an error?
Pixel[] pixelArray = this.getPixels(); Pixel p, q; for(int index = 0; index < pixelArray.length; index++) { p = pixelArray[index]; q = pixelArray[index+1]; p.setRed(q.getRed()); p.setBlue(q.getRed()); p.setGreen(q.getGreen()); }
- It doesn’t, this loops across rows, going down
- Incorrect. For each instance of the first for loop, every pixel of a given column is set to black.
- It doesn’t this loops down columns, going right
- Correct. For each instance of the first for loop, every pixel of a given column is set to black, moving downwards.
- It tries to index a pixel off the end of a row (foo value too big)
- Incorrect. The largest value of foo called is getHeight() - 1, which is an accessible value.
- It tries to index a pixel off the end of a column (bar value too big)
- Incorrect. The largest value of bar called is getWidth() - 1, which is an accessible value.
8-10-6: Why does this code have an error?
//A method in Picture.java Pixel p; for (int bar = 0; bar < getWidth(); bar++) { for (int foo = 0; foo < getHeight(); foo++) { p = getPixel(foo, bar); p.setColor(Color.BLACK); } }
- y increases faster than x
- Incorrect. For each increase of the y value by 1, the x value can increase by more than one.
- x increases faster than y
- Inorrect. Although this stament alone is true, consider the pattern it follows due to the for loops.
- x and y increase together, in step
- Incorrect. Consider the nesting. For each increase of the y value by 1, the x value can increase by more than one.
- x increases for a while, then y increases once, then x restarts and increases again
- Correct. The first loop increases the value of y by 1. Then the x value increases to the "mirrorPT" value. Then the x value is reset and the first loop runs again.
- y increases for a while, then x increases once, then y restarts and increases again
- Incorrect. Consider the nesting. For each increase of the y value by 1, the x value is reset.
8-10-7: Which of the following is the best answer?
//Code to mirror around the vertical axis int mirrorPt = getWidth()/2; Pixel leftP, rightP; for (int y = 0; y < getHeight); y++) { for (int x = 0; x < mirrorPt; x++) { leftP = getPixel(x,y); rightP = getPixel(getWidth()-1-x,y); rightP.setColor(leftP.getColor()); } }
- Copies top half into bottom half not mirrored
- Incorrect. Since the x parameter increases as countingDown increases (also used as an x paramter), there transformation involves mirroring.
- Copies left half into right half not mirrored
- Incorrect. Since the x parameter increases as countingDown increases (also used as an x paramter), there transformation involves mirroring.
- Mirrors around vertical axis, left into right
- Correct. There is mirroring occurring, and this happens within the second for loop. The values are changing around one given x value, so the transformation is around a vertical axis.
- Mirrors around horizontal axis, top into bottom
- Incorrect. There is mirroring occurring, and this happens within the second for loop. If the values are changing around one given x value, which axis are they transforming around?
- Some other bizarre transformation
- Incorrect. Examine the options again. Hint: There is mirroring occurring in the second for loop.
8-10-8: What does this code do?
int magic = getWidth()/2; Pixel foo, bar; for(int y = 0; y < getHeight(); y++) { int countingDown = getWidth()-1; for(int x = 0; x < magic; x++) { foo = getPixel(x,y); bar = getPixel(countingDown,y); bar.setColor(foo.getColor()); countingDown--; } }
- 10, 4
- Incorrect. You have the values correct, but consider which dimensions the x and y correspond to.
- 9, 5
- Incorrect. Consider how many times the first for loop runs. How many values are included in the span of 40 to <50 ?
- 4, 10
- Correct. The first for loop spans the range of 40-49 (10 values), which correspond to the height. The second for loop spans the range of 1-4 (4 values), and corresponds to the height.
- 5, 9
- Incorrect. Consider how many times the first for loop runs. How many values are included in the span of 40 to <50 ?
8-10-9: This code makes a red box of size (width, height)
Pixel foo; for(int y = 40; y < 50; y++) { for(int x = 1 ; x < 5; x++) { foo = getPixel(x,y); foo.setColor(Color.RED); } }
- 11, 5
- Incorrect. You have the values correct, but consider which dimensions the x and y correspond to.
- 10, 5
- Incorrect. Consider how many times the first for loop runs. How many values are included in the span of 40 to 50 inclusive?
- 5, 11
- Correct. The first for loop spans the range of 40-50 (11 values), which correspond to the height. The second for loop spans the range of 1-5 (5 values), and corresponds to the height.
- 5, 10
- Incorrect. Consider how many times the first for loop runs. How many values are included in the span of 40 to 50 inclusive?
8-10-10: This code makes a red box of size (width, height)
Pixel foo; for(int y = 40; y <= 50; y++) { for(int x = 1 ; x <= 5; x++) { foo = getPixel(x,y); foo.setColor(Color.RED); } }
- for (int w = 0; w <= x; w++) for (int h = 0; h <= y; h++)
- Incorrect. The range from 0 to x inclusive has has a total size of x+1. In addition, the call to getPixel(w,h) could be out of range. Think about our use of zero-based indexing.
- for (int w = 10; w < x +10; w++) for (int h = 20; h < y + 20; h++)
- Incorrect. Even though the range from 10 to x+10 does have a size of x, the call to getPixel(w,h) could fall out of range if x + 10 is greater than the width of the drawing area.
- for (int w = 0; w < x; w++) for (int h = 0; h < y; h++)
- Correct. The range of 0 to
- for (int w = 10; w <= x+10; w++) for (int h = 20; h <= y+20; h++)
- Incorrect. The range from 10 to x inclusive has has a total size of x+1. In addition, the call to getPixel(w,h) could be out of range. Think about our use of zero-based indexing.
8-10-11: What are the correct loop headers to make a black box of width x and height y?
public void foo(int x, int y) { Pixel foo; **LOOP HEADER 1** { **LOOP HEADER 2** { foo = getPixel(w,h); foo.setColor(Color.BLACK); } } }
- 1
- Incorrect. The value of pix is reassigned every time the innermost body of the for loops is run.
- this.getWidth() times
- Incorrect. The value of pix is reassigned every time the innermost body of the for loops is run. Everytime the inner loop runs, it does run this.getWidth() times, however this occurs more than once.
- this.getHeight() times
- Incorrect. The value of pix is reassigned every time the innermost body of the for loops is run. The outer loop does run this.getHeight() times, however this answer disregards the inner for loop.
- getHeight() * getWidth() times
- Correct. The outer loop executes this.getHeight() times, and each execution of this loop results in the inner for loop running this.getWidth() times.
- getHeight()/2 * getWidth() times
- Incorrect. The value of pix is reassigned every time the innermost body of the for loops is run. Examine how many respective times the inner loop runs, and how this is affecting by the running of the outer for loop.
8-10-12: How many times is the variable pix assigned a value?
public void everyColumn(Color newColor) { Pixel pix; for (int aaa = 0; aaa < this.getHeight(); aaa++) { for (int bbb = 0; bbb < this.getWidth(); bbb++) { pix = this.getPixel(bbb,aaa); pix.setColor(newColor); } } }
- getHeight()-1 * getWidth()-1
- Incorrect. Due to zero-based indexing, the statement "aaa < this.getHeight()" will execute this.getHeight() times. Ex. For an image of width 4, aaa will take on the values 0 1 2 and 3, for a total of 4 values.
- getHeight()-1 * (getWidth()-1)/2
- Incorrect. Due to zero-based indexing, the statement "aaa < this.getHeight()" will execute this.getHeight() times. Ex. For an image of width 4, aaa will take on the values 0 1 2 and 3, for a total of 4 values.
- getHeight() * getWidth()
- Incorrect. Examine how the first loop is incremented with "aaa++", but the second loop is incremented with "bbb = bbb + 2";
- getHeight() * getWidth()/2
- Correct. Due to zero-based indexing, the statement "aaa < this.getHeight()" will execute this.getHeight() times. The number of potential iterations is cut in half due to the second for loop using "bbb = bbb + 2" to increment.
- None of the above
- Incorrect. Examine both the less than conditions and the way the for loops are incremented.
8-10-13: How many iterations of the loop body are executed?
public void everyOtherColumn(Color newColor) { Pixel pix; for (int aaa = 0; aaa < this.getHeight(); aaa++) { for (int bbb = 0; bbb < this.getWidth(); bbb = bbb + 2) { pix = this.getPixel(bbb,aaa); pix.setColor(newColor); } } }
- if(bbb < this.getWidth()/2)
- Incorrect. Even though this would result in the inner body running in one half of cases, changing the for loop would result in the body running for every-other value of bbb, while this change results in a solid half of the pixels changing with no alternating pattern.
- if(bbb < this.getHeight()/2)
- Incorrect. Even though this would result in the inner body running in one half of cases, changing the for loop would result in the body running for every-other value of bbb, while this change results in a solid half of the pixels changing with no alternating pattern.
- if((bbb %2) == 0)
- Correct. Using a modulus in the if statement causes the inner body to run at every-other value of bbb, which is the same as incrementing bbb by 2 each time and using no if statement.
- if((this.getPixel(bbb,aaa)%2) == 0)
- Incorrect. The suggested change to the for loop runs based on the value of bbb, not the content of the pixel, as is suggested by answer d.
8-10-14: Adding which if statement at the marked line would result in the inner body of the for loop running the same way it would if the inner for loop was “for (int bbb = 0; bbb < this.getWidth(); bbb = bbb + 2)”?
public void everyOtherColumn(Color newColor) { Pixel pix; for (int aaa = 0; aaa < this.getHeight(); aaa++) { //inner for loop for (int bbb = 0; bbb < this.getWidth(); bbb++) { **Add If Statement Here** { pix = this.getPixel(bbb,aaa); pix.setColor(newColor); } } } }
- Comparing 2 pixels side by side and, if they are similar make the pixel white, otherwise black
- Incorrect. The pixels that this code compares are not side by side, as the y value is changed, not the x value.
- Comparing 2 pixels one on top of the other and, if they are similar make the pixel white, otherwise black
- Correct. We look at the pixel directly below the pixel of interest. If they are similar, the if statement turns the pixel white. Else, it becomes black.
- Comparing 2 pixels side by side and, if they are different make the pixel white, otherwise black
- Incorrect. The pixels that this code compares are not side by side, as the y value is changed, not the x value.
- Comparing 2 pixels one on top of the other and, if they are different make the pixel white, otherwise black
- Incorrect. Even though we are comparing two pixels, one on top of the other, the if statement does not turn similar pixels black.
8-10-15: What is this code doing?
//Inside loop over all pixels topP = this.getPixel(x,y); botP = this.getPixel(x,y+1); topAvg = topP.getAverage(); botAvg = botP.getAverage(); if (Math.abs(topAv – botAv) < 10) topP.setColor(Color.WHITE); else topP.setColor(Color.BLACK);
- Section A AND Section B may BOTH be executed
- Incorrect. Only one section can be executed, as once one if/else statement is satisfied, all others will be ignored for that execution.
- If Section B is executed then Section A is not executed
- Correct. If the if statement is satisfied, section A will be executed and section B will be ignored. If the if statement is not satisfied, section A will be ignored, and the else statement will cause section B to automatically execute.
- Neither Section is ever executed
- Incorrect. If an if statement is followed by an else statement, it is guaranteed that one section will always be executed.
- It is possible neither section will be executed (but sometimes one might be).
- Incorrect. If an if statement is followed by an else statement, it is guaranteed that one section will always be executed.
8-10-16: Which is most true about ONE execution of this code (for a specific diffValue)?
int diffValue = Math.abs(topAv – botAv); if (diffValue < 10) topP.setColor(Color.WHITE); //Section A else topP.setColor(Color.BLACK); //Section B
- Section A can be executed AND Section B may BOTH be executed but then C can’t be executed
- Incorrect. Only one section can be executed, as once one if/else statement is satisfied, all others will be ignored for that execution.
- If Section A is executed then neither Section B nor C can be
- Correct. If the first if statement is satisfied, section A will be executed and section B and C will be ignored. If the first if statement is not satisfied, section A will be ignored, and the process will be repeated for the second if statement. If neither are satisfied, section C will execute.
- All sections can be executed for a single diffValue
- Incorrect. Only one section can be executed, as once one if/else statement is satisfied, all others will be ignored for that execution.
- It’s possible no section is executed for a given diffValue
- Incorrect. If an if statement is followed by an else statement, it is guaranteed that one section will always be executed.
8-10-17: Which is most true about ONE execution of this code (for a specific diffValue)?
int diffValue = Math.abs(topAv – botAv); if (diffValue < 10) topP.setColor(Color.WHITE); //Section A else if (diffValue < 50) topP.setColor(Color.GREY); //Section B else topP.setColor(Color.BLACK); //Section C
- Based on the coordinates of the Pixel
- Incorrect. While the y value is considered, it is not the only quality considered in the if statements.
- Based on the color of the Pixel
- Incorrect. While the color is considered, it is not the only quality considered in the if statements.
- Based on the coordinates for some Pixels, the color for other Pixels
- Incorrect. All pixels are considered via the same criteria, regardles of coordinates.
- Based on a compound condition of color and coordinates of the Pixel
- Correct. Both the amount of green in a pixel and its y coordinate are considered when it is being determined if the pixel color should be changed.
8-10-18: Which best describes the conditions under which we change pixel color?
public void makeConvict() { for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { Pixel currentPix = this.getPixel(x,y); if ( (currentPix.getGreen() > 200) && (y%2==0)) { currentPix.setColor(Color.BLACK); } else if( (currentPix.getGreen() > 200) && y%2 == 1) { currentPix.setColor(Color.WHITE); } } }
- Picture changed = new Picture(p); p.mystery(changed); changed.show();
- Incorrect. Calling the mystery function on the object "p" will not alter the "changed" object, and thus changed.show() will display a picture identical to "p".
- Picture changed = new Picture(); p.mystery(changed); changed.show();
- Incorrect. Calling the mystery function on the object "p" will not alter the "changed" object, and thus changed.show() will display a default picture.
- Picture changed = new Picture(p); changed.mystery(p); changed.show();
- Incorrect. The "changed" object does not need to be initialized as a copy of "p", and can be initialized with the default constructor.
- Picture changed = new Picture(); changed.mystery(p); changed.show();
- Correct. The "changed" object can be initialized with the default constructor, as the next line calls the mystery function with the parameter "p". This is the simplest correct way to successfully accomplish this.
- None of the above
- Incorrect. One of the above answers is correct.
8-10-19: How would you call and display a flipped picture of Picture p?
- width * height / 2
- Incorrect. This line is executed as many times as the code innermost to both for loops is called. Consider only the for loop conditions.
- width * height
- Correct. The if statement is executed evey time it is called, which in this case is equal to the number of times the code within both for loops is called.
- width * height * 2
- Incorrect. This line is executed as many times as the code innermost to both for loops is called. Consider only the for loop conditions.
- width * height * 1.5
- Incorrect. This line is executed as many times as the code innermost to both for loops is called. Consider only the for loop conditions.
- Depends on the color of the Pixels in the picture
- Incorrect. This line is executed as many times as the code innermost to both for loops is called, which is not dependent on pixel color.
8-10-20: How many times is the marked line below executed?
public void makeConvict() { for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { Pixel currentPix = this.getPixel(x,y); if ( (currentPix.getGreen() > 200) && (y%2==0)) // THIS LINE { currentPix.setColor(Color.BLACK); } else if( (currentPix.getGreen() > 200) && y%2 == 1) { currentPix.setColor(Color.WHITE); } } } }
- width * height / 2
- Incorrect. This line is executed everytime the first if statement is not satified, consider what the first conditional examines.
- width * height
- Incorrect. This line is executed everytime the first if statement is not satified, consider what the first conditional examines.
- width * height * 2
- Incorrect. This line is executed everytime the first if statement is not satified, consider what the first conditional examines.
- width * height * 1.5
- Incorrect. This line is executed everytime the first if statement is not satified, consider what the first conditional examines.
- Depends on the color of the Pixels in the picture
- Correct. This line is executed everytime the first if statement is not satified, which is dependent upon the amount of green in each pixel of the picture.
8-10-21: How many times is the marked line below executed?
public void makeConvict() { for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { Pixel currentPix = this.getPixel(x,y); if ( (currentPix.getGreen() > 200) && (y%2==0)) { currentPix.setColor(Color.BLACK); } else if( (currentPix.getGreen() > 200) && y%2 == 1) // THIS LINE { currentPix.setColor(Color.WHITE); } } } }
- Line A is executed the same number of times as Line B
- Incorrect. While this is true if the if statement in Line A is always satified, consider that this may not always be the case.
- Line A is executed more times than Line B
- Incorrect. While this is true if the if statement in Line A is not always satified, consider that this may not always be the case.
- Line A is executed fewer times than Line B
- Incorrect. Line B can only be executed after Line A, and is only executed 0 or 1 time every time Line A is called. It cannot be executed more times than Line A.
- The relationship depends on the specific Picture that this code is run on
- Correct. Line B will be executed the same number of times or fewer times than Line A. If the if statement in Line A is always satisfied, Line B will be executed the same number of times as line A. Else, Line B will be executed fewer times.
8-10-22: Which of these statements is true?
public void makeConvict()
{
for (int x = 0; x < this.getWidth(); x++)
{
for (int y = 0; y < this.getHeight(); y++)
{
Pixel currentPix = this.getPixel(x,y);
if ( (currentPix.getGreen() > 200) && (y%2==0))
{
currentPix.setColor(Color.BLACK);
}
else if( (currentPix.getGreen() > 200) && y%2 == 1) // LINE A
{
currentPix.setColor(Color.WHITE); //LINE B
}
}
}
}
- This code modifies the middle half (from the top and bottom) of the picture
- Incorrect. This code modifies a 1D array, and thus there is no top-to-bottom dimension.
- This code modifies the middle half (from the left and right) of the picture
- Correct. Based on the for loop, the code modifies from the 1/4 length mark to the 3/4 length mark, moving left to right.
- This code loops over the pixels in the Pixel array starting at length/4 and up to 2*length/4 and gets the red, blue and green values adds them up and divides by 3 and sets that pixel to the calculated value
- Incorrect. Even though this line does correctly describe how the pixel colors are modified, it does not correctly describe which pixels are modified.
8-10-23: What does this code do?
Pixel[] pixelArray = this.getPixels(); int mystery; for(int i = pixelArray.length/4; i < 3*pixelArray.length/4; i++) { mystery = (pixelArray[i].getBlue() + pixelArray[i].getGreen() + pixelArray[i].getRed() ) / 3; Color thing = new Color(mystery, mystery, mystery); pixelArray[i].setColor(thing); }