Peer Instruction: Arrays Multiple Choice Questions¶
- Erases half of the sound
- Incorrect. This code modifies one half of the sound, but does not erase it.
- Changes the result sound based on the this sound
- Incorrect. The result sound is modified in a way involving the this sound, however this is not based on the contents of the this sound.
- Replaces the parameter sound with the calling object sound
- Incorrect. The parameter sound itself is not modified, even though a copy of it (result) is.
- Puts the last half of the calling object sound into the parameter sound
- Incorrect. The parameter sound itself is not modified, even though a copy of it (result) is.
- Replaces the last half of the parameter sound with the last half of the calling object sound
- Correct. Based on the for loop, the second half of the parameter sound is replaced by the second half of the this sound.
16-6-1: What does this code do?
public void lab7Quiz3(Sound mySound) { Sound[] source = this.getSamples(); Sound[] result = mySound.getSamples(); for (int i = source.length/2; i < source.length; i++) { int value = source[i].getValue(); result[i].setValue(value); } }
- 0,9
- Incorrect. Examine what the variables a and b hold. a holds a value found in the array, and b holds an index.
- 60,0
- Incorrect. Examine what the variables a and b hold. a holds a value found in the array, and b holds an index.
- 90,5
- Incorrect. Examine what the variables a and b hold. a holds a value found in the array, and b holds an index.
- 100,4
- Correct. The a value holds the greatest value found in the array. The b value holds the index of the greatest value.
- None of the above
- Incorrect. Examine what the variables a and b hold. a holds a value found in the array, and b holds an index.
16-6-2: What is printed by this code when it is called on the object {60, 80, 60, 65, 100, 90, 0, 0, 0, 0}?
public void guess() { SoundSample[] noiseArray = this.getSamples(); int a, b = 0; for (int i=0;i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; int foo = sample.getValue(); if (foo > a) { a = foo; b = i; } } System.out.println(a + ","+b); }
- 0,9
- Incorrect. Examine when the value of b is changed. When is the condition "foo < a" satisfied?
- 0,6
- Correct. a holds the smallest value found in the array, and b holds an index. b is only changed when "foo < a" is satified, which last occurs at index 6.
- 90,5
- Incorrect. Examine when a and b are changed. At one point these values are correct, but finish running the for loop.
- 32767,0
- Incorrect. Examine what the variables a and b hold. a holds a value found in the array, and b holds an index.
- None of the above
- Incorrect. Examine what the variables a and b hold. a holds a value found in the array, and b holds an index.
16-6-3: What is printed by this code when it is called on the object {60, 80, 60, 65, 100, 90, 0, 0, 0, 0}?
public void guess() { SoundSample[] noiseArray = this.getSamples(); int a = 32767; Int b = 0; for (int i=0;i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; int foo = sample.getValue(); if (foo < a) { a = foo; b = i; } } }
- Sets the end of the array to 0s
- Incorrect. The values of the original array are not reassigned in this code block.
- Finds the first value
- Incorrect. This code loops through the entire array, and the values can be changed to correspond to any value, not just the first one.
- Finds the minimum (and its location)
- Incorrect. a is only changed when "foo > a" is satified, which means that the current value is greater than the previous value.
- Finds the maximum (and its location)
- Correct. The value of a is the maximum value in the array. The value of b is the index where the maximum value is located.
- Finds the last value
- Incorrect. This code loops through the entire array, and the values can be changed to correspond to any value, not just the last one.
16-6-4: What does this code do for the object {60, 80, 60, 65, 90, 0, 0, 0, 0}?
int a,b = 0; for (int i=0;i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; int foo = sample.getValue(); if (foo > a) { a = foo; b = i; } }
- [160, 160, 160, 160]
- Incorrect. The value of yyy is the sum of all values divided by the size of the array (remember integer math). Is the original array ever changed using this value?
- [40, 40, 40, 40]
- Incorrect. The value of yyy is the sum of all values divided by the size of the array (remember integer math). Is the original array ever changed using this value?
- [53, 53, 53, 53]
- Incorrect. The value of yyy is the sum of all values divided by the size of the array (remember integer math). Is the original array ever changed using this value?
- [80, 100, 70, 70]
- Incorrect. The value of yyy is the sum of all values divided by the size of the array (remember integer math). Is the original array ever changed using this value?
- None of the above
- Correct. Even though the value of sample is continuously reassigned to the value 40, the values in the original array itself are never actually modified. Thus, the end array is exactly the same as the original.
16-6-5: How does the sound sample change if funky() is called on [40, 60, 30, 30]?
public void funky() { SoundSample[] noiseArray = this.getSamples(); int zzz = 0; for (int i=0;i<noiseArray.length; i++) { SoundSample sample = noiseArray[i]; int foo = sample.getValue(); zzz += foo; } int yyy = zzz / noiseArray.length; for (int i = 0; i < noiseArray.length; i++) { SoundSample sample = noiseArray[i]; sample.setValue(yyy); } }
- if (foo[i].getValue() >= 0)
- Correct. The term "foo[i].getValue()" retrieves the value at index i. This code successfully sets all value sero and greater to the maximum, and all others to the minimum.
- if (i >= 0)
- Incorrect. i is the index of a space in the array, but not a value found in the array itself. We need to know the value at i for this code to run properly.
- if (foo[i].getValue() < 0)
- Incorrect. This would set every negative value to the positive maximum and vis versa, which is the opposite of our goal.
- if (i < 0)
- Incorrect. i is the index of a space in the array, but not a value found in the array itself. We need to know the value at i for this code to run properly.
- None of the above
- Incorrect. One of the options above can successfully complete this code.
16-6-6: What if all positive values (including zero) were (set to) the maximum value (32,767) and all negative values were set to the minimum value (-32,768)? Which line would complete the code block to accomplish this?
SoundSample[] foo = this.getSamples(); for (int i = 0; i < foo.length; i++) { <<Pick a Line to Insert here>> foo[i].setValue(32767); else foo[i].setValue(-32768); }
- [143, 165, 110, 121, 99, 0, 0, 0, 0, 0]
- Incorrect. While the first values of the array are assigned new values, the second part is not altered.
- [143, 165, 110, 121, 98, 130, 150, 100, 110, 90]
- Incorrect. Look at how the new values of the array are calculated. When i is 5, the "value" variable is 13. This 13 is added to the number at a different index of the array.
- [53, 65, 70, 81, 109, 0, 0, 0, 0, 0]
- Incorrect. While the first values of the array are assigned new values, the second part is not altered.
- [53, 65, 70, 81, 109, 130, 150, 100, 110, 90]
- Correct. The pattern is that, starting at zero, the value at the current index is increased by one-tenth of the value of the current index + 5, until the end of the arry is reached.
- Array index out of bounds error
- Incorrect. This block of code only calls to valid indexes within the soundSample.
16-6-7: What is the resulting soundSample if the original soundSample is [40, 50, 60, 70, 100, 130, 150, 100, 110, 90] and foo is equal to 5?
public Sound funky2(int foo) { Sound s = new Sound(this.getFileName()); int value = 0; for (int i = foo; i < this.getLength(); i++) { value = (int) s.getSampleValueAt(i) * .1; this.setSampleValueAt(i-foo, value + this.getSampleValueAt(i-foo)); } }
- this;
- Incorrect. This will create a copy of the "this" sound, when we are trying to create a sound of half the length.
- new Sound(this);
- Incorrect. The value of this.getLength()/2 is an integer, but we need a valid Sound to assign highP to.
- this.getLength()/2;
- Incorrect. While the first values of the array are assigned new values, the second part is not altered.
- new Sound(this.getLength()/2);
- Correct. We need to use the keyword new to create a new object, and then insert the proper size parameter into the Sound constructor.
- None of the above
- Incorrect. There is a proper answer above.
16-6-8: What code would you replace <<X>> with is order to create a new sound of correct length 1/2 ?
public void raiseP() { Sound highP = <<X>> SoundSample[] original = this.getSamples(); SoundSample[] higher = highP.getSamples(); <<SOME LOOP HERE TO COPY ORIGINAL INTO HIGHER>> }
- higher[newPlace].setValue( original[origI].getValue()); newPlace = origI;
- Incorrect. This will cause an out-of-bounds error once origI surpasses the size of higher.
- higher[newPlace].setValue( original[origI].getValue()); newPlace++;
- Correct. We set new values into the higher array, not the original array. We use newPlace to track the current index of our new, smaller array, which only increases by one for every time origI increases by 2.
- original[origI].getValue( higher[newPlace].setValue()); newPlace = origI;
- Incorrect. This will cause an out-of-bounds error once origI surpasses the size of higher.
- original[origI].getValue( higher[newPlace].setValue()); newPlace++;
- Incorrect. While this answer will increment our index values correctly, no new values are actually assigned to our higher array.
- None of the above
- Incorrect. There is a proper answer above.
16-6-9: What lines should be inserted into the for loop in order to fill in our new higher array, which is 1/2 the length of the “this” array?
public void raiseP() { Sound higher = <<X>> SoundSample[] original = this.getSamples(); SoundSample[] higher = highP.getSamples(); int newPlace = 0; for (int origI = 0; origI < original.length; origI+=2){ //insert lines here } }
- [A] public void raisePitch(), [B] this.getSamples();
- Correct. If we want to modify an existing object, we do not need to return an object, so void is appropriate. This sound will also be passed in as an object, and not a parameter.
- [A] public void raisePitch(Sound noise), [B] noise.getSamples();
- Incorrect. Since this function is a method of an object class, we do not need to pass in the sound as a parameter.
- [A] public Sound raisePitch(), [B] this.getSamples();
- Incorrect. If we want to modify an existing object, we do not need to return an object, so we could use void instead of Sound in the method declaration.
- [A] public Sound raisePitch(Sound noise), [B] noise.getSamples();
- Incorrect. Since this function is a method of an object class, we do not need to pass in the sound as a parameter.
- None of the above
- Incorrect. One of the above options does successfully satisfy this question.
16-6-10: What header/value combo should we use in ored to modify an existing sound?
public Sound funky2(int foo) { Sound s = new Sound(this.getFileName()); int value = 0; for (int i = foo; i < this.getLength(); i++) { value = (int) s.getSampleValueAt(i) * .1; this.setSampleValueAt(i-foo, value + this.getSampleValueAt(i-foo)); } }
- [10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
- Incorrect. This sound is modified in some way within the for loop.
- [50, 15, 20, 25, 30, 35, 40, 45, 50, 55]
- Correct. Only noiseArr[0] is changed since newPlace isn't modified. The last time the loop runs the value of i is 8, so noiseArr[0] is changed to 50.
- [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
- Incorrect. While the first value of the array is assigned new values, track to see if the value of newPlace is changing as the for loop progresses.
- [10, 20, 30, 40, 50, 35, 40, 45, 50, 55]
- Incorrect. While the first value of the array is assigned new values, track to see if the value of newPlace is changing as the for loop progresses.
- None of the above
- Incorrect. One of the above options does successfully satisfy this question.
16-6-11: What is the result of running this code if noiseArr is [10, 15, 20, 25, 30, 35, 40, 45, 50, 55]?
int newPlace = 0; for(int i = 0; i < noiseAr.length; i+=2) { noiseAr[newPlace] = noiseAr[i]; }