• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question from java se 7 programmer 1 practice test  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • 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

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 178
3
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!