• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Doubt on KB Question #12 Chapter 5

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I've got a big doubt on one of the answers to this question:


Given:
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 answers accoding to the book are: A, D, and F. My answers were A, C, and F when I took the Self Test.

I believe D is wrong because z was not declared as int anywhere in the code. Could someone please clarify?

Thanks!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jart Bo:
...I believe D is wrong because z was not declared as int anywhere in the code...


And yet when you tested this, you found that it compiles and runs without any problems, right?

Isn't the following line a declaration of z as an int?

int y=0, z=0;
 
Jart Bo
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc! I must have overlooked that one!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic