• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Backslash in text blocks (from Chapter 1, OCP Java 17 Practice Tests)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 60 (page 20) asks how many backslashes in the following code can be removed without changing the value of the text block.



The correct answer is 4 backslashes can be removed (option E), as explained on page 449:

Both backslashes on line 13 can be removed since this is a text block. Additionally, the second and third backslashes on line 14 can be removed. The first one must be retained since there are three quotes in a row.



On line 13, both backslashes can be removed. No doubt about it. On line 14, up to two of the three backslashes can be removed, but at least one must remain. However, does the backslash to be retained have to be the first one? Probably not. According to my running results, the code will compile regardless of which backslash remains.

(1) Keep the first backslash:



(2) Keep the second backslash:



(3) Keep the third backslash:



So should the explanation be adjusted?
 
Marshal
Posts: 80617
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miles Jiang wrote:. . . does the backslash to be retained have to be the first one? . . .

I think not; As you have seen as long as one \ remains to escape one of the quotes, the code will compile. You are probably right about changing the explanation.
[Edit] Your first code block has four quotes on line 4! I think you have copied it wrongly.
 
Miles Jiang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
[Edit] Your first code block has four quotes on line 4! I think you have copied it wrongly.



Thanks for the correction. I made a mistake while copying. There should be three quotes instead of four.
 
reply
    Bookmark Topic Watch Topic
  • New Topic