• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Array1{
int[] Ki[]=new int[9][];
public int guessWhat( int arr[] ){
int x= 0;
for( int i = 0; i < arr.length; i++ ){
x = x < arr[i] ? arr[i] : x;
System.out.println(x);
}
return x;
}
public static void main(String args[]){
Array1 a=new Array1();
System.out.println(a.guessWhat(new int[9]));
}
}
/*
a) Returns the index of the highest element in the array
b) Returns true/false if there are any elements that repeat in the array
c) Returns how many even numbers are in the array
d) Returns the highest element in the array
e) Returns the number of question marks in the array
20. Which of the following are legal declarations of a two-dimensional array of integers?

Can anyone tell me which one is the correct answer (with detailed explanations)for the above code.
Thanks
sudha

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

Originally posted by sudha siva:
public class Array1{
int[] Ki[]=new int[9][];
public int guessWhat( int arr[] ){
int x= 0;
for( int i = 0; i < arr.length; i++ ){
x = x < arr[i] ? arr[i] : x;
System.out.println(x);
}
return x;
}
public static void main(String args[]){
Array1 a=new Array1();
System.out.println(a.guessWhat(new int[9]));
}
}
/*
a) Returns the index of the highest element in the array
b) Returns true/false if there are any elements that repeat in the array
c) Returns how many even numbers are in the array
d) Returns the highest element in the array
e) Returns the number of question marks in the array
20. Which of the following are legal declarations of a two-dimensional array of integers?

Can anyone tell me which one is the correct answer (with detailed explanations)for the above code.
Thanks
sudha



Hi sudha...
This code works but it prints all zero's. So i am sorry i am unable to comprehend the answer choices correctly to pick. However I believe the question should be little bit clearer. It must provide additional information like the input arrays contains positive values or something like that to pick the choice d
Ranchers any comments?
Ragu
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is d

Explanation:

  • At Line 7 gussWhat() is called.By passing the reference[/I] of int array of 9 objects.NOT INITIALIZED!THEREFORE EVERY ELEMENT OF INT ARRAY IS 0
  • At Line 3, local variable x is compared with every element of array arr[].Rememebr this array holds the reference of the array pased at Line 7.i.e [I]new int[9]
  • At Line 3 x is compared in such a way with arr[] elements that the greatest of either(i.e x or arr[]) will be assinged to x.
  • Since all the elements of arr[] are 0 therefore condition at Line 3 false & x is assinged to x.
  • Finnaly at Line 5 x is returned, which holds 0.


  • Hope this helps.Any frther quesry is Wellcome
    Bye.
    Viki
    ------------------
    Count the flowers of ur garden,NOT the leafs which falls away!
    [This message has been edited by Vikrama Sanjeeva (edited November 20, 2001).]

    [This message has been edited by Vikrama Sanjeeva (edited November 20, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic