Students
Teachers & SchoolsStudents
Teachers & SchoolsIn this computer science drill question, figure out which implementation will copy one array over to another.
AP | AP Computer Science |
AP Computer Science | Standard Algorithms |
Computer Science | AP Computer Science |
Language | English Language |
Standard Data Structures | Primitive data types (int, boolean, double), Strings, Classes, Lists, Arrays (1-dimensional and 2-dimensional) |
Test Prep | AP Computer Science |
All right Which of the following implementation successfully copies over
array my alf to a realist alphabet All right And
here your potential answers brought to you by the romans
Okay option one uses a for each loop which will
cycle once for each element in an array In this
case for each string in the array my alf will
add the content of that element to the array list
alphabet and the loop will in as soon as we've
reached the end of my alf that's about as simple
as it gets and it will totally work all right
Option two option two creates an iterated that begins at
zero And while the current element of my health is
not equal to z where it'll add that particular value
to alphabet add one to the generator and do it
all over again Do that to me one more time
Great hold when the wild statement finally does find a
value equal to z it'll end the loop and travel
down to the next statement adding the current value of
my alf meaning z we just found the alphabet so
option two fits the bill to woo hoo wiseguys will
have already noticed that none of the possible answers are
all three options work and think we're totally done here
but because we're also smart and uptight about things we're
going to not just assume the answer is d all
right so let's just check out option three and be
sure it doesn't work looks decent on first glance right
Well standard looking loop something niggling with the array list
Nothing obviously a miss Ah wait we can't use that
alphabet dot set call on indices that don't exist yet
remember our ray list alphabet is totally empty all set
replaces elements that already existe with other elements We need
to use an ad call instead and if we're being
nitpicky Well that four loop set up is saying well
i is less than alphabet dot size and that would
have to be changed to the size of alphabet at
this stage would be zero so the loop would never
run in all changing that terminator parameter to while i
is less than my health dot length it would allow
the loop to run the full twenty six Anyway we're 00:02:26.018 --> [endTime] done Wait Okay we're leaving we're leaving