Output of Java Program

What is the output of the following program?

class A
{
public static void main(String[] s)
{
System.out.println(s[1] + s[2] + s[3]);
}
}

java A 12345

Options
(i) 1
(ii) 12
(iii)1234
(iv)234
(v) Compilation Error


Questions by khadarzone   answers by khadarzone

Editorial / Best Answer

abuthahir.d  

  • Member Since Jan-2008 | Apr 14th, 2008


Khadar , i tried this code . it gives that exception

for the foll reason : 
 when u give a CLA(command line argument) , it will be inserted in the index 0 of the array and then will proceed to 1 ,2 ....

so as per the question , "12345" will be at s[0] . and length of that aray will be 1when u try to access s[1] , we are trying for 2nd element which is unavailable..... 


Hope this answers u  :-)

Showing Answers 1 - 75 of 195 Answers

abuthahir.d

  • Apr 14th, 2008
 

Khadar , i tried this code . it gives that exception

for the foll reason : 
 when u give a CLA(command line argument) , it will be inserted in the index 0 of the array and then will proceed to 1 ,2 ....

so as per the question , "12345" will be at s[0] . and length of that aray will be 1when u try to access s[1] , we are trying for 2nd element which is unavailable..... 


Hope this answers u  :-)

vegetto

  • Jun 9th, 2008
 

hi there,
                the answer for this question of yours will be option (d) because you have not entered any input after 12345 which is only stored in s[0] corresponding to it's string equivalent but as far as s[2] and s[3] are concerned then you have not submitted any input so it will lead to runtime error. supply with inputs after giving space after every input 3 times  and hit enter and you will get the result.byeeee

  Was this answer useful?  Yes

vegam_smart

  • Dec 19th, 2008
 

The output is V. compilation error..
because in the command line argument (java A 12345)  they have given 12345 as of index of s[0], but they trying to print the s[1]+s[2]+s[3]..

  Was this answer useful?  Yes

The answer is (v). Everything that is written on the command prompt as command line argument is a String. So, "12345" is a string and that too s[0]. So, if we try to access s[1...n], we are bound to get a compile time exception "ArrayIndexOutOfBoundsException" for sure.

The error will be
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at A.main(A.java:5)

  Was this answer useful?  Yes

No any answer is right here. There is run time error  "ArryIndexOutOfBoundsException" will occurs because 12345 is a single String and JVM cant find 2nd 3rd and 4th String.If we input 1 2 3 4 5 then it will give 234.

  Was this answer useful?  Yes

kiran

  • Jul 13th, 2011
 

compilation error

  Was this answer useful?  Yes

venkateswarareddy

  • Jul 21st, 2011
 

Exception in thread "main"

Code

  1. class abc

  2. {

  3.         public static void main(String[] s)

  4.         {

  5.                 System.out.println(s[1]+s[2]+s[3]);

  6.         }

  7. }

  8.  

  Was this answer useful?  Yes

Avinash

  • Jul 25th, 2011
 

It displays ArrayIndexOutOfBoundsException.because it takes 12345 as single string varible s[0] only.

  Was this answer useful?  Yes

prakash

  • Jul 29th, 2011
 

Answer is 234

  Was this answer useful?  Yes

ramakrishna

  • Aug 5th, 2011
 

answer is compilation error because when u write a program to accept a values from command line arguments on that time we pass three command line arguments above program we use three command line arguments but we pass only one argument value that is s[1]

we give this like java 12 34 5
that time only the answer is 12345

but we give java 12345 means what we run the program at the we pass the command line arguments but we pass only one argument that is 12345 means that value is stored in array index o s[0].

  Was this answer useful?  Yes

GOPI

  • Aug 25th, 2011
 

the answer is runtime error. because the string array stores the command line argument in the form of strings,it receives 12345 as string and stores in s[0]. so i gives runtime error.

  Was this answer useful?  Yes

shiva prasad

  • Sep 3rd, 2011
 

None of the above options the correct. Answer is 123compilationerror

  Was this answer useful?  Yes

Diwakar

  • Sep 7th, 2011
 

234

  Was this answer useful?  Yes

sekhar

  • Sep 7th, 2011
 

output is :234

  Was this answer useful?  Yes

Subidit Hazra

  • Sep 20th, 2011
 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at A.main(A.java:5)

Code
  1. class A

  2. {

  3. public static void main(String[] s)

  4. {

  5. System.out.println(s[1] + s[2] + s[3]);

  6. }

  7. }

  Was this answer useful?  Yes

Abhinav Trivedi

  • Nov 28th, 2011
 

It is pretty simple.

ArrayIndexOutOfBoundException will occure. Those who are saying 234,12345 misunderstood the working of console input(compile time input). Input is seperated by a delimiter then it will be going in next index of string other wise not.

  Was this answer useful?  Yes

Sumit Kumar Pradhan

  • Jan 12th, 2012
 

ans) v) compilation error

  Was this answer useful?  Yes

sathish

  • Mar 13th, 2012
 

Compilation error . System.out.println() is the syntaz not system.Out.Println

  Was this answer useful?  Yes

katrina joy dominguez

  • Jun 14th, 2012
 

s1+s2+s3



  Was this answer useful?  Yes

Praveen Kumar

  • Dec 13th, 2012
 

The arguments that are passed to the main are stored in array( the name of the array doesnt matter it can be argh,x,a ,args anything.., lets not get lost) so according to your piece of code we can deduce the following,
args[0]=12345
but there is nothing in args[2].., i.e it hasnt been initialized further, so the jvm will raise an array index out of bounds exception meaning which you are trying to access an element from the array which hasnt been initialized with a value.

Code
  1. your code will work with the following options :

  2. :> java a 1 2 3 4 5

  3. // if U include spaces and the output will be: "234"

  Was this answer useful?  Yes

GuruGovardhan

  • Dec 20th, 2012
 

When i run this program i got this Exception.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

  Was this answer useful?  Yes

d

  • Jan 19th, 2013
 

v) Compilation Error

  Was this answer useful?  Yes

akii

  • Apr 30th, 2013
 

Exception will be come when using class:

java.lang.ArrayIndexOutOfBoundsException: 1 , because 12345 input comes at s[0] places and when we are getting s[1] it come java.lang.ArrayIndexOutOfBoundsException: at postion 1.

Code
  1. java A  1 2 3 4 5 and out put will be 234

  Was this answer useful?  Yes

void

  • Jun 6th, 2013
 

ArrayIndexOutOfBounds Exception :
indexing starts from 0, and Since only 3 arguments are passed from the CLA
Java will fail to find the 4th argument when trying to search for s[3] and hence it will result in a compilation error

  Was this answer useful?  Yes

Saheb

  • Sep 4th, 2013
 

It will be compiled successfully....
it will throw a ArrayIndexOutOfBoundException....

  Was this answer useful?  Yes

saheb

  • Sep 4th, 2013
 

it will compile successfully but it will give ArrayIndexOutOfBoundException...
to run try System.out.print(s[0]);

  Was this answer useful?  Yes

Aneesh

  • Oct 11th, 2013
 

S[1]+s[2]+s[3]=>s[0]+s[1]+s[2]

javac A.java

java A 1 2 3

123

Code
  1. class A{

  2.  

  3.     public static void main(String[] s) {

  4.         System.out.println(s[0] + s[1] + s[2] ); // 3 args

  5.  

  6.        // System.out.println(s[3]);

  7.     }

  8. }

  Was this answer useful?  Yes

Poorna

  • May 30th, 2014
 

Compile time error :ArrayIndexOutOfBoundsException

  Was this answer useful?  Yes

Sarita

  • Dec 2nd, 2014
 

Compilation Error

  Was this answer useful?  Yes

kalyankarri

  • Dec 27th, 2014
 

compilation error

  Was this answer useful?  Yes

Suraj

  • Jan 8th, 2015
 

ArrayIndexOutofBoundsException

  Was this answer useful?  Yes

Avtar

  • Jan 24th, 2015
 

None of the above options, As the there will be ArrayIndexOutofBoundException.

  Was this answer useful?  Yes

Biswajit

  • Jan 29th, 2015
 

That's right. Onlty ArrayIndexOutofBoundsException will come, as silly mistake is there in the code.

  Was this answer useful?  Yes

disha bisht

  • May 11th, 2016
 

This code will give "Array out of bound exception".... but if we provide space after each number then it will give 234 as output.

  Was this answer useful?  Yes

kandi

  • Jun 22nd, 2016
 

(v) Compile Error

  Was this answer useful?  Yes

anu

  • Jul 6th, 2016
 

(v) Compilation Error

  Was this answer useful?  Yes

AnilYadav

  • Jul 26th, 2016
 

Compile Error

  Was this answer useful?  Yes

charles

  • Aug 4th, 2016
 

(v) Compilation Error

  Was this answer useful?  Yes

nikhil

  • May 14th, 2017
 

It will throw - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

  Was this answer useful?  Yes

shani

  • Mar 14th, 2018
 

(v) Compilation Error

  Was this answer useful?  Yes

shyamasundar

  • Jun 2nd, 2018
 

(v) Compilation Error

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions