aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 5 K&B -Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chapter 5 K&B -Doubt" Watch "Chapter 5 K&B -Doubt" New topic
Author

Chapter 5 K&B -Doubt

Naresh Bafna
Ranch Hand

Joined: Mar 23, 2007
Posts: 35
Hi..
Question 12 says
1. class Loopy {
2. public static void main(String[] args) {
3. int[] x = {7,6,5,4,3,2,1};
4. // insert code here
5. System.out.print(y + " ");
6. }
7. }
8. }
Which, inserted independently at line 4, compiles? (Choose all that apply.)
A. for(int y : x) {
B. for(x : Int y) {
C. int y = 0; for(y : x) {
D. for(int y=0, z=0; z<x.length; z++) { y = x[z];
E. for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];
F. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];


the answer given is ADE whereas the following code does not compile
class Loopy {
public static void main(String[] args) {
int[] x = {7,6,5,4,3,2,1};
// insert code here
for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];
System.out.print(y + " ");
}
}
}

Can anyone explain why is it given as the right answer when it does not compile?


SCJP 5 - 94%
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Well its a typo, an extra int placed before z. Try running this-:
class Loopy {
public static void main(String[] args) {
int[] x = {7,6,5,4,3,2,1};
// insert code here
for(int y=0,z=0; z<x.length; z++) { y = x[z];System.out.print(y + " ");
}
}
}
as you cant decalre two integers as-:
int a,int b;

either you do
int a,b;
or
int a;
int b;


as later is not possible in for loop so try it as int a,b;
Naresh Bafna
Ranch Hand

Joined: Mar 23, 2007
Posts: 35
Thanks Pranav.. that clears my doubt!!
Naresh Bafna
Ranch Hand

Joined: Mar 23, 2007
Posts: 35
Why din they include this typo in the K*B errata? or they can atleast now!!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hey!

I think I've been misquoted!

Please double check your book, because mine doesn't say what you posted here

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Naresh Bafna
Ranch Hand

Joined: Mar 23, 2007
Posts: 35
Yes, you are right. I am sorry..
sitaram
Greenhorn

Joined: Jan 24, 2008
Posts: 26
class Loopy {
2. public static void main(String[] args) {
3. int[] x = {7,6,5,4,3,2,1};
4. // insert code here
5. System.out.print(y + " ");
6. }
7. }
8. }
Which, inserted independently at line 4, compiles? (Choose all that apply.)
A. for(int y : x) {
B. for(x : Int y) {
C. int y = 0; for(y : x) {
D. for(int y=0, z=0; z<x.length; z++) { y = x[z];
E. for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];
F. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];


The Answer is A,D only. E is not correct because z is defined two times. A is correct in java5. B is correct in java 4 & java5.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Howdy "d sitaramayya",

Kindly do adjust your display name according to Ranch's naming policy.

The instruction to do so has already been specified here.

The display name should contain first name and a last name separated by space which should not be fictitious.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Originally posted by d sitaramayya:
..
B. for(x : Int y) {


Is there a datatype and keyword named "Int" ["I" in caps]?

How come then it would be a correct answer?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chapter 5 K&B -Doubt