SCJP MOCK QUESTIONS - 2

Question 2

Given:
10. public class Bar {
11.static void foo(int...x) {
12. // insert code here
13. }
14. }
Which two code fragments, inserted independently at line 12, will allow
the class to compile? (Choose two.)
A. foreach(x) System.out.println(z);
B. for(int z : x) System.out.println(z);
C. while( x.hasNext()) System.out.println( x.next());
D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);


Answer: BD

Answer:


type... x is used in function arguments where type can be any data type
for example:
int... x


which can take variable arguments
for example a function named var having declarations

var(int... x)

we can pass var(1); , var(1,1); , var(1,2,3);

so it can take 1 to any number of integers as arguments


int... x here x is just like an array and we can iterate over its elements using simple and enhanced for loop

other options are invalid

foreach is a method which is not defined
And hasnext() is used to iterate over collections(sets,lists,maps)

Hence the answer is B,D

4 comments:

Anonymous said...

Sun Java Certification (Newbie)
http://career-assessments.blogspot.com/2008/01/sun-java-certifications.html

SCJA/SCJP/SCJD/SCWCD/SCMA/SCEA… Aspirants
http://career-assessments.blogspot.com/2008/01/sun-java-certifications-aspirants.html

Nimesh Kumar said...

When preparing for java certification, you must practice mock exams to check your readiness. You can check out some free java certification mock exams to practice. Certification4Career.com provides 500+ free practice questions with web based exam simulator, and most importantly, you can save your test session and later try only incorrectly answered question.

TV de LCD said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the TV de LCD, I hope you enjoy. The address is http://tv-lcd.blogspot.com. A hug.

Krishna said...

Here you can find the quality SCJP 1.5 questions,
SCJP 1.5 Questions

Post a Comment