• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Potential error for Sybex Practice Tests 11

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 2, Question # 28: The code contains six pairs of curly braces. How many pairs can be removed without changing the behavior?



Since there is no termination condition or break statement inside the if-else block, the code runs but never ends.

The solution I think is to add "i++;" or "break;" to if and else blocks, respectively:



Now the curly braces of for (lines 3/14) and while (lines 4/13) are optional, while the braces of if-else (lines 5/8 & 8/12), switch (lines 15/17), and the main method (lines 1/18) are required.

If so, the answer to this question should be corrected correspondingly: there are two instead of three pairs of curly braces can be removed. That is, the correct answer is Option B instead of C.

 
Sheriff
Posts: 28385
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miles Jiang wrote:Since there is no termination condition or break statement inside the if-else block, the code runs but never ends.



True.

The solution I think is to add "i++;" or "break;" to if and else blocks, respectively



The solution to what problem? The question asks you to remove pairs of curly braces while preserving the behaviour of the code. Yes, the behaviour includes never ending, but so what?
 
Miles Jiang
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...tricky.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic