Welcome Guest | Sign in | Register
Language Fundamentals - Discussion Page For Q.3008 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Language Fundamentals > Questions and Answers

Q:   What is the result of trying to compile and run this program.
public class Test{
public static void main(String[] args){
int[] a = {1};
Test t = new Test();
t.increment(a);
System.out.println(a[a.length - 1]);
}
void increment(int[] i){
i[i.length - 1]++;
}
}
A. Compiler error. B. Compiles and runs printing out 2
C. Compiles and runs printing out 1
D. An ArrayIndexOutOfBounds Exception at runtime

Answer and Explanation

Answer:Compiles and runs printing out 2

Explanation:
 You are passing a reference to an array as
the argument to the method. The method may not
modify the passed object reference but it can modify
the object itself.  

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.