• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scanner question

 
Maxwell Wood
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Maxwell Wood
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to CodeRanch, Jurica Krizanic
 
Jurica Krizanic
Ranch Hand
Posts: 38
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vijitha
 
Faheem Khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic