• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions from K&B 6

 
Kapil Shardha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to understand the reason behind the answer for Qs 1 in Self Test for Chapter 6 in K&B's book for SCJP 6

And the command line:
java Regex2 "\d*" ab34ef
What is the result?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails


Moreover, the answer to question 2 of same test is also not clear. Any explanation to these two questions will be of great help.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kapil,
Maybe this FAQ will help you understand the problem.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34198
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kapil,
Welcome to JavaRanch!

That code matches 0 or more digits. The regular expression tries to find a match as soon as it can. So it thinks something like this.
Position 0 - finds a letter (a). Yup - that's zero digits.
Position 1 - Another letter (b). Great - zero digits.
Position 2 - Ooh - a number. Let's keep looking. Two digits. Outputs "34" and skips outputting the index for the third position since it already matched the "4"
Position 4 - Another letter (e) - Zero digits
Position 5 - Another letter (f) - Zero digits
Position 6 - "End of string" - Yup - zero digits

Kapil Shardha wrote:Moreover, the answer to question 2 of same test is also not clear. Any explanation to these two questions will be of great help.

Can you share question 2 (or a shorter version of it) ? I have the SCJP 5 book (not 6 so things may have changed.) My question #2 deals with Serializable not regular expressions. If you have a question about Serializable, you want to start a new thread. You'll also want to share what your specific doubt is as that isn't a question that can be traced/diagrammed.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34198
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And clearly I should have looked at the FAQ before typing this. I like reg exps .
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kapil please Use Code Tags when you post a source code. Edit your message using button and then add code tags to it...
 
Kapil Shardha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the question 2 from Self test of Chapter 6:


And its answer is "PCP" which is not clear.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the CardPlayer's and Player's constructor will run printing PC.

CardPlayer is serializable while Player isn't. So while deserializing, Player's constructor will run printing P

Hence PCP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic