can someone help me with this question? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can someone help me with this question?

What is the output of this code? int arr[ ] = new int[3]; for (int i = 0; i < 3; i++) { arr[i] = i; } int res = arr[0] + arr[2]; System.out.println(res);

2nd Feb 2017, 3:42 AM
OmgPotato
OmgPotato - avatar
17 Answers
+ 25
2
2nd Feb 2017, 3:57 AM
Kawaii
Kawaii - avatar
+ 10
array after declaration arr: [ 0, 0, 0 ] index:. 0 1 2 Loop cycle 1:. i=0 0<3 ---> true therefore: arr[0] = 0; Loop cycle 2:. i=1 1<3 ---> true therefore: arr[1] = 1; Loop cycle 3:. i=2 2<3 ---> true therefore: arr[2] = 2; Loop cycle 4:. i=3 3<3 ---> False therefore:. loop ends array after loop arr: [ 0, 1, 2 ] index:. 0 1 2 int res = 0 + 2; System.out. println(2);
2nd Feb 2017, 6:34 AM
POJO
POJO - avatar
+ 3
2
28th Sep 2017, 6:25 PM
Shashank Hanwante Jain
Shashank Hanwante Jain - avatar
0
2
14th Nov 2018, 11:06 AM
P.Venkata Ramana
P.Venkata Ramana - avatar
0
2
8th Apr 2020, 3:17 PM
Hiramani Dahal
Hiramani Dahal - avatar
0
2
17th Oct 2020, 12:42 PM
Bolli sahith
Bolli sahith - avatar
0
2
6th Apr 2021, 10:21 AM
Joseph S. Lablah, Jr
Joseph S. Lablah, Jr - avatar
0
2
29th Apr 2021, 9:37 AM
Rithick.R
Rithick.R - avatar
0
2
23rd Apr 2022, 6:42 PM
Joshua Konadu
0
23rd Apr 2022, 6:42 PM
Joshua Konadu
0
2
30th Apr 2022, 5:37 AM
MAREESWARAN S
MAREESWARAN S - avatar
0
2
30th Apr 2022, 7:24 AM
Максетов ТоребеKK
0
2
4th May 2022, 9:33 AM
GOVINDAPRAKASH N
0
2
21st Jun 2022, 10:51 AM
aknandan sharma
aknandan sharma - avatar
0
2
29th Jun 2022, 1:06 PM
Opanuga Stephen
Opanuga Stephen - avatar
0
2
1st Jul 2022, 3:51 PM
Antenhe Yimer
0
This was hard for me, too! Thanks everyone!
22nd Jul 2022, 3:30 PM
123Venus
123Venus - avatar