Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Array and For Loop

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends Pls Explain....
1. Which declaration for 2D array are true ?
a> int a[][] = new int[][]
b> int []a[] = new int[5][]
c> int []a[] = new int[][5]
d> int [][]a = new int[5][]

ans given are : b, d.
If I am not wrong according to me the ans is : a,d.
Pls let me clarify .....

2. What is the result of the following program if it is compiled.
class Continue {
public static void main(String args[]) {
outer: for(int i=0;i<2;i++) {
for: for (int k=0;k<2;k++) {
if (i==j) break for;
System.out.println(i+ " " +k);
}
}
}
}
Ans given is : 10
11
According to me the code will not compile for two reasons.
1. variable 'j' is not defined .(i.e. int, byte, short..etc)
2. 'for' is the java keyword and can not be used as label.
Pls Let me sure.
Tejas Nakawala.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
regd 1) a is definitely wrong coz you have to tell the array it's size when you initialize it. note, that this is the case of an array of arrays. the individual array elements when initialized have declare the size of their respective arrays.
b) is a correct convention.
in fact, so is int[] a[]!
regd. the second,
you are right on both accounts.
 
Tejas Nakawala
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ansuman,
I have not see such Declaration even in JLS 2.0 Will u pls tell me where u have read that kind of declaration is valid ?
Thankx.
Tejas
 
Tejas Nakawala
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ansuman,
I have not see such Declaration even in JLS 2.0 Will u pls tell me where u have read that kind of declaration is valid ?
Thankx.
Tejas
 
reply
    Bookmark Topic Watch Topic
  • New Topic