AP Computer Science 2.5 Standard Algorithms

In this computer science drill question, figure out which implementation will copy one array over to another.

APAP Computer Science
AP Computer ScienceStandard Algorithms
Computer ScienceAP Computer Science
LanguageEnglish Language
Standard Data StructuresPrimitive data types (int, boolean, double), Strings, Classes, Lists, Arrays (1-dimensional and 2-dimensional)
Test PrepAP Computer Science

Transcript

00:17

All right Which of the following implementation successfully copies over

00:21

array my alf to a realist alphabet All right And

00:26

here your potential answers brought to you by the romans

00:32

Okay option one uses a for each loop which will

00:35

cycle once for each element in an array In this

00:39

case for each string in the array my alf will

00:42

add the content of that element to the array list

00:45

alphabet and the loop will in as soon as we've

00:48

reached the end of my alf that's about as simple

00:51

as it gets and it will totally work all right

00:54

Option two option two creates an iterated that begins at

00:57

zero And while the current element of my health is

01:01

not equal to z where it'll add that particular value

01:06

to alphabet add one to the generator and do it

01:08

all over again Do that to me one more time

01:13

Great hold when the wild statement finally does find a

01:16

value equal to z it'll end the loop and travel

01:19

down to the next statement adding the current value of

01:21

my alf meaning z we just found the alphabet so

01:25

option two fits the bill to woo hoo wiseguys will

01:29

have already noticed that none of the possible answers are

01:31

all three options work and think we're totally done here

01:33

but because we're also smart and uptight about things we're

01:37

going to not just assume the answer is d all

01:39

right so let's just check out option three and be

01:41

sure it doesn't work looks decent on first glance right

01:45

Well standard looking loop something niggling with the array list

01:47

Nothing obviously a miss Ah wait we can't use that

01:51

alphabet dot set call on indices that don't exist yet

01:55

remember our ray list alphabet is totally empty all set

01:58

replaces elements that already existe with other elements We need

02:01

to use an ad call instead and if we're being

02:04

nitpicky Well that four loop set up is saying well

02:08

i is less than alphabet dot size and that would

02:11

have to be changed to the size of alphabet at

02:13

this stage would be zero so the loop would never

02:16

run in all changing that terminator parameter to while i

02:20

is less than my health dot length it would allow

02:22

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