• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from java se 7 programmer 1 practice test

 
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was testing my luck on the Java SE 7 Programmer I - Sample Questions and the very first question has me scratching my head. The print out is x,y,c,g. Why does y print before c? Thank You

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 4 is the body of the constructor of class Sequence.
Line 5 is an instance initializer block.

Instance initializer blocks execute before the constructor executes, that's why you see "y" before "c".

The code has been formatted in a way that is delibrately misleading - it looks like a trick question.

Line 16 is a static initializer block, which executes when class Sequence is loaded and initialized (even before the main() method executes) - that's why you see "x" first.
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Line 4 is the body of the constructor of class Sequence.
Line 5 is an instance initializer block.

Instance initializer blocks execute before the constructor executes, that's why you see "y" before "c".

The code has been formatted in a way that is delibrately misleading - it looks like a trick question.

Line 16 is a static initializer block, which executes when class Sequence is loaded and initialized (even before the main() method executes) - that's why you see "x" first.



Thank you very much!
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic