TIM GALLAGHER: Hi, everybody and welcome to AP
practice sessions 2023 for AP Computer Science A.
My name is Tim Gallagher from Winter Springs High
School located just outside of beautiful Orlando, Florida.
ROB SCHULTZ: And I am Rob Shultz.
I teach at Bellbrook High School in Bellbrook, Ohio.
TIM GALLAGHER: And we are so excited to have you here today.
Over the next 8 sessions, we are going
to be looking at a lot of multiple choice
and free response questions.
And for today's questions, you can
find the PDF of these questions associated with this video
here.
So today, we're going to do some multiple choice questions.
So Rob, take it away let's get started.
ROB SCHULTZ: We've got our question up on the screen.
So let's take a look at what it says.
It says to consider the static method below.
So as we look below, we've got a public static method.
It has an int return type.
So we know we're going to return an int
value by the time this thing finishes running.
Our method is called calculate, and we take an int value
as our parameter.
The question says, which of the following
can be used to replace the body of calculate
so that the modified version, when we replace with one
of these, the modified version of calculate
will return the same result as the original version
for all values of x?
So here's my challenge to you.
I'd like you to take some time and trace through this code.
I want you to be the computer, follow every single line,
and I want you to see if you can take a value plug it in for x,
work it through the original version of calculate
and see which of these returns the same result.
So take a couple of seconds and pause.
Work through the question.
And when you're ready to go through it,
press play, and we'll go through it together.
So here's how I would approach this.
First thing I would do is I would take a value
and plug it in for x.
In this case, I picked the value 5, and then off to the side
beside each one of our multiple choice answers,
I wrote what the result would be if I did 3 plus 5.
So 3 plus 5 is 8.
3 times 5 is 15.
And so on and so on.
So now I'm going to go back to our original version
of calculate, and I'm going to be the computer,
and I'm going to plug-in 5 and trace line
by line to see what we return and which
of the multiple choice answers we match.
So here's our first line.
Our first line says to take the value of x plus the value of x.
In other words, 5 plus 5 to get 10, and then we're
going to assign the answer, assign our result, over to x.
So by the time we finish the first line,
x has a value of 10.
And then we're going to repeat that process.
We're going to take the value of x plus the value of x, 10
plus 10.
We're going to assign the result 20 over to x.
So now x has a value of 20.
And then we're going to repeat the process one last time,
the value of x plus the value of x, which
is 20 plus 20 gives us 40.
So we'll assign 40 to x And then we
return whatever our remaining value of x is, which is 40.
So as we look at our multiple choice answers, the only answer
that returns a value of 40, given five as the input,
option E, which says we return 8 times x.
So hopefully that was a fairly straightforward one
to start with.
Tim, you're up to bat.
TIM GALLAGHER: Thanks, Rob.
Let's go ahead and look at question number two.
So this is a question that involves a method
call to this method, whatIsIt.
And we've got three method calls,
and we want to know which of the following method calls
will cause a W to be printed.
And you'll notice that it gives us
these options: I only, II only, III only, I
and III, II and III.
So that means that one or more of these options could work.
So we're going to look at what of these method
calls will actually give us a W to be printed.
So I'd like you to go ahead and pause, like you
did for the other question with Rob,
and see if you can answer this on your own first.
OK?
Ready to trace it.
Here we go.
So one of the things that I like to do whenever
we do multiple choice questions is
make sure that you look at your answers
or be aware of what the multiple choice answers as you go along.
So if you could eliminate multiple choice answers
during the process of figuring this question out,
that'll really help you along to figure out what
the correct answer actually is.
So let's go ahead and try these one at a time.
Now notice if you look at it says, what
will cause W to be printed.
Well, the W being printed is really
just that first if statement.
Yeah, this question has a bunch of nested if statements
inside of it, but really, we're just
going to look for conditions where that first
if statement evaluates to true and then
a W is going to be printed.
So let's try them one at a time.
So the first one is whatIsIt 10 10.
So I'm sending the parameters or the arguments 10 and 10
to my parameters A and B, and let's see what happens
or what gets evaluated.
Well, the first one says if a is less than b.
Well, a is not less than b in this case, right?
Because 10 is equal to 10.
So it can't be I.
So since it can't be I, we can automatically
eliminate options A and options D. So keeping that in mind
as you're going through here will really
help you answer these types of questions.
Let's try number two.
WhatIsIt negative 5, 5.
So a has negative 5, and b has 5.
So as we look at, is a less than b?
Well, it is.
Negative 5 is less than b.
That's great.
And this is a compound IF statement, is a greater than 0?
Not in this case because a is negative 5, right?
So a is not greater than zero, so it can't be number II.
So since it can't be number II, well, look.
That eliminates B and E. All that's left here is C.
But just for a sanity check let's make sure
that III actually does work, and we didn't make
a mistake in an earlier one.
So whatIsIt 7, 10.
Well, that means a is going to be 7, b is going to be 10.
Is a less than b?
Yes, it is.
That evaluates to true.
Is a greater than 0?
7 greater than zero.
Yes, it is.
That evaluates to true.
Thankfully, we have one that works
and that leaves our answer as C, III only.
How did you do?
I hope you got that one right as well.
So tracing two questions and eliminating answers
as you do your multiple choice works really well for you also.
All right.
Question number three, back to you, Rob.
ROB SCHULTZ: OK.
Thanks, Tim.
All right.
We've got one question left before we end our time together
today.
So question three says consider the code segment below.
So let's take a quick look.
We've got two variables a and b that we declare.
Both are integer values.
So that's important to make note of.
We're initializing a to 24, and we're initializing b to 30.
We've got a while loop that's going to execute for a while.
Inside the while loop, we've got modulus going on.
We've got some variable assignments taking place.
And then once the while loop finishes execution,
then we have a system.out.println.
So let's take a look at what the question says to do.
It says, what is printed as a result
of executing the code segment?
And we've got some different integer answers: 0, 6,
12, 24, and 30.
All right.
So again, here's our chance to actually pause, take some time
to trace the code through, be the computer,
and see if you can find a match before we go through it.
All right.
Welcome back.
Are you feeling confident?
Let's check things out.
So as we trace through what's happening,
the first thing that happens is we declare a variable a,
and we set it equal to 24.
And then we declare a variable b, and we set it equal to 30.
We hit our while loop, and the while loop
says we're going to cycle through the while loop
as long as b is not equal to 0, and at this point,
b equals 30, which is great.
And then we declare a new variable r,
and we set it equal to a modulus b.
So let's do a little bit of quick review of what
modulus means.
Keep in mind that we're doing integer--
we have integer values.
So if I were to do integer division,
a divided by b, 24 divided by 30 is
going to give us 0 because remember
that in Java integer division results in an integer value.
So 24 divided by 30 gives me 0.
We're not looking for division.
We're looking for modulus, which means
we're looking for the remainder of that division problem.
So 24 divided by 30 equals 0, but 24 modulus 30 gives us 24.
24 would be the remainder of that division problem.
So as I work through this, I'm going to take 24 modulus 30.
I'm going to declare a new variable r,
and set it equal to the modulus, which is 24.
And then we're going to move on to the next line,
and it says to assign the value of b to a.
So a now has a value of 30.
We're going to assign the value of r
to b, which means b now has a value of 24,
and then we're going to repeat.
So when we get to the top of our while loop,
we're going to look at b.
b is not equal to 0.
24 is not equal to 0.
So we're going to start over again,
and we're going to sign r the value of 30
modulus 24, which would give us 6.
If I divide 30 by 24, the quotient would be 1,
but the remainder is 6.
So that's my value for r.
We repeat assigning the value of b to a.
So a now has a value of 24.
We repeat assigning the value of r to b.
So b now has a value of 6, and then
we go back to the top of the while loop
and reevaluate our condition again.
So 6 is still not equal to 0, our value
for b, which means we're going to go into the loop again.
And we're going to assign r the value of 24 modulus 6.
Well, 24 divided by 6 is going to give me 4
with a remainder of 0.
So our remainder in this case is going to be 0.
We're going to assign the value from b to a.
So a now has a value of 6.
And then we're going to assign the value of r to b, which
means b is going to become 0.
And this time, when we go back to the top of the while loop,
b is equal to 0, which means that's the end of the while
loop.
We're going to jump out of the while loop,
and we end up displaying the line, the value
of a, which in this case is 6.
So our answer has to be b I hope everybody did OK with that one.
ROB SCHULTZ: Well, I really hope that this first video
has been beneficial to you.
I hope that you found it helpful.
And remember the takeaways from this.
Take your time, read the question carefully,
be the computer as you trace through the code,
make sure that you're tracing through what
every single line does so that you can make sure that you get
an accurate answer at the end.
As you work through multiple choice,
any answers you can cross out as you
go to make the process a little easier for you.
On behalf of Tim and myself, have a great day
and make sure you join us for the next video.
We'll see you then.