This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes scanner question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "scanner question" Watch "scanner question" New topic
Author

scanner question

Maxwell Wood
Ranch Hand

Joined: Feb 15, 2011
Posts: 49
What is the output for the below code ?



options
A)1 2 red blue
B)Compile Error - because Scanner is not defind in java.
C)1 fish 2 fish red fish blue fish
D)1 fish 2 fish red blue fish

Correct answer is : A


i don't know what Delimiter does so maybe that's why i don't understand it
???

[Devaka: Edited to add code tags]
Jurica Krizanic
Ranch Hand

Joined: Dec 11, 2008
Posts: 38

Well Scanner brakes this string into tokens which are separated with a delimiter.
In your example you say that delimiter is the "fish" word with 0 or more whitespace characters before and after the "fish" word.
Zero or more characters are marked with * quantifier, while whitespace is marked with \s metacharacter.

I hope my explanation was understandable.

Please check the quantifiers and searching using metacharacters in chapter 6 of K&B book.


Jurica Krizanic - Java developer, OCPJP6, Spring Core certified developer!
Maxwell Wood
Ranch Hand

Joined: Feb 15, 2011
Posts: 49
source : http://www.javacertifications.net/javacert/scjp1.6Mock.jsp


oh now i understand it so what's going on is that the string is being split first and then we are doing what ever we are doing
thank you Jurica Krizanic
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

And welcome to CodeRanch, Jurica Krizanic


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jurica Krizanic
Ranch Hand

Joined: Dec 11, 2008
Posts: 38

Thanks Vijitha
Faheem Khan
Greenhorn

Joined: Feb 21, 2011
Posts: 3
Maxwell Wood wrote:What is the output for the below code ?



options
A)1 2 red blue
B)Compile Error - because Scanner is not defind in java.
C)1 fish 2 fish red fish blue fish
D)1 fish 2 fish red blue fish

Correct answer is : A


i don't know what Delimiter does so maybe that's why i don't understand it
???

[Devaka: Edited to add code tags]



Hi all,

If you follow the logic and understand how delimiters work then the correct answer is indeed A.

But!

I was told if the source code begins from 'line 1' then you have to assume you're given the whole source code.

The source code we're given doesn't import java.util.Scanner nor does it explicitly refer to Scanner as java.util.Scanner in the code... instead it's using Scanner like it's already been imported.

As java.util.* isn't implicitly imported like java.lang.* surely it would be a compilation error?


Faheem Khan.
Jurica Krizanic
Ranch Hand

Joined: Dec 11, 2008
Posts: 38



Faheem Khan wrote:
Maxwell Wood wrote:What is the output for the below code ?

options
A)1 2 red blue
B)Compile Error - because Scanner is not defind in java.
C)1 fish 2 fish red fish blue fish
D)1 fish 2 fish red blue fish

Correct answer is : A


i don't know what Delimiter does so maybe that's why i don't understand it
???

[Devaka: Edited to add code tags]



Hi all,

If you follow the logic and understand how delimiters work then the correct answer is indeed A.

But!

I was told if the source code begins from 'line 1' then you have to assume you're given the whole source code.

The source code we're given doesn't import java.util.Scanner nor does it explicitly refer to Scanner as java.util.Scanner in the code... instead it's using Scanner like it's already been imported.

As java.util.* isn't implicitly imported like java.lang.* surely it would be a compilation error?

Faheem Khan.


I think you make a good point with your question. I am not sure what the answer B)Compile Error - because Scanner is not defind in java.
means when saying not defined in java? Is it related to missing imports ?
Are the imports going to be always displayed in a code sample in the exam, or we should assume that imports are written even if we don't see imports in the code sample?
Maybe some expert could provide us an answer?!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

Jurica Krizanic wrote:...Are the imports going to be always displayed in a code sample in the exam, or we should assume that imports are written even if we don't see imports in the code sample?...

You should not assume anything, everything needed for particular question will be provided either in the question itself or in the code. Always there will be line numbers in the code so that you know whether it's a complete code or a code snippet.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

Faheem Khan wrote:....I was told if the source code begins from 'line 1' then you have to assume you're given the whole source code.

Yes. Otherwise in the real exam it specifically mentions anything else required like the imports etc...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scanner question
 
Similar Threads
why doesnt this work?
Doubt regarding regex
About regex
error
Scanner