Students
Teachers & SchoolsStudents
Teachers & SchoolsAPCS: Review of the Basics Drill 2, Problem 4. What is the output of the following code segment?
AP | AP Computer Science |
AP Computer Science | Review of the Basics |
Computer Science | AP Computer Science |
Language | English Language |
Program Analysis | Testing |
Test Prep | AP Computer Science |
and here the potential answers All right pausing thinking and
we're going to answer this question We need to know
what exactly an escape character or escape sequence is And
when it's used Well this is one of those things
that doesn't get used frequently but when you need it
you'll wonder how you would have managed without it Yeah
like knowing how to catch a fish with your shoe
laces but not quite as dramatic In java a backslash
followed by a certain character is called in escape sequence
Let's suppose you want to print the following string in
a program The sign at the door red no hats
please but i wasn't technically wearing a hat It was
a raccoon not wanting to cause trouble mr scratchy and
on a left the interesting program you've got there The
problem with throwing that string in a normal print statement
is the quotation marks will confuse the compiler It believes
the first quotation mark from no hats please is the
end of the print statement after that it's expecting to
see more code and when that doesn't happen the compiler
will return an error There are a couple ways to
solve this problem First one would be tio not where
raccoon is a hat because second and more useful way
would be to escape the quotation marks in the string
So the compiler doesn't treat him like a code really
by placing a backslash in front of the quotation marks
we want to compiler to ignore We escape them and
they'll get asked like right through the prince stay just
like that though notice that we use the back slash
on the apostrophe too that's to prevent it from being
recognized as a single There are some special escape sequences
to a backslash followed by an end will insert a
new line at that position Backslash t will insert tab
and there are a few others that are a little
more obscure like this stuff Our question is asking us
what will happen with the following print statement so let's
look at the back slashes in the string The first
one will ensure the apostrophe gets past and not confused
for code So time will come out just fine Quotation
marks around testing are also properly escape so those quotation
marks will be printed correctly Then there's the backslash end
which is we just learned prints a new line and 00:02:38.63 --> [endTime] that's answered