Question:

Give the output of the following Java program segment:

String a[] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"};
System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2));

The output of the above statement is:

Show Hint

In Java strings, indexing starts from 0. Also, \texttt{charAt(n)} gives the character at position \texttt{n}, so always count from 0 carefully.
  • da
  • d * a
  • ti
  • t * i
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Identify the required array elements.
The array is: \[ \texttt{a[ ] = \{"Atasi", "Aditi", "Anant", "Amit", "Ahana"\}} \] Now check the required positions: \[ \texttt{a[1] = "Aditi"} \] \[ \texttt{a[2] = "Anant"} \]
Step 2: Apply the \texttt{charAt()} method.
The expression uses: \[ \texttt{a[1].charAt(1)} \] In the word \texttt{"Aditi"}: \[ A = 0,\ d = 1,\ i = 2,\ t = 3,\ i = 4 \] So, \[ \texttt{a[1].charAt(1) = 'd'} \] Now: \[ \texttt{a[2].charAt(2)} \] In the word \texttt{"Anant"}: \[ A = 0,\ n = 1,\ a = 2,\ n = 3,\ t = 4 \] So, \[ \texttt{a[2].charAt(2) = 'a'} \]
Step 3: Combine the output.
The complete statement is: \[ \texttt{a[1].charAt(1) + "*" + a[2].charAt(2)} \] So it becomes: \[ \texttt{'d' + "*" + 'a'} \] This prints: \[ \texttt{d*a} \] In the given options, this corresponds to d * a.
Step 4: Conclusion.
Therefore, the output of the statement is d * a.
Final Answer:d * a.
Was this answer helpful?
0
0

Questions Asked in ICSE Class X board exam

View More Questions