What is the output from the following program? public class Modifier { public st
ID: 3772338 • Letter: W
Question
What is the output from the following program?
public class Modifier {
public static void modify( int [] array2 ) {
for ( int counter = 0; counter < array2.length; counter++ )
array2[ counter ] *= 2;
}
public static void modify ( int element ) {
element *= 2;
}
public static void main( String args[] ) {
int array[] = { 1, 2, 3, 4, 5 };
modify( array[ array.length – 1 ] );
System.out.println( “The last array element is “ + array[ array.length - 1 ] );
modify( array );
System.out.println( “The last array element is “ + array[ array.length - 1 ] );
}
}
Explanation / Answer
public class Modifier {
public static void modify( int [] array2 ) {
for ( int counter = 0; counter < array2.length; counter++ )
array2[ counter ] *= 2;
}
public static void modify ( int element ) {
element *= 2;
}
public static void main( String args[] ) {
int array[] = { 1, 2, 3, 4, 5 };
modify(array[ array.length-1]);
System.out.println("The last array element is "+ array[ array.length - 1 ] );
modify( array );
System.out.println( "The last array element is " + array[ array.length - 1 ] );
}
}
OUTPUT:
The last array element is 5
The last array element is 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.