Computer Science, asked by krishnamahanta9775, 2 months ago

4. What do the following function return for:
String p="computer", q="application":
System.out.println(p.indexOf('p'));
5. . What do the following function return for:
String p="Server", q="Computer";
System.out.println(p.length());
System.out.println(q.concat(p.substring(3));​

Answers

Answered by BrainlyProgrammer
38

String p="computer", q="application":

System.out.println(p.indexOf('p'));

indexOf() returns the index of first occurrence the given character in the parameter

Return data type: int

Remember: Index always starts with 0

So here, p.indexOf('p') returns the index of character 'p' in variable 'p'.

variable p is storing "computer" so we will find index of character 'p' in this string.

 \large{ \sf p=\boxed{ {0 |1 | 2 |3 |4 |5 |6 |7|  \over c| o| m|p| u| t| e|r }}}

In the above diagram, index of each characters are given so we can easily find the index of p which is 3

So we got the first answer ->

Question 5:-

String p="Server", q="Computer";

System.out.println(p.length());

System.out.println(q.concat(p.substring(3));


anindyaadhikari13: Good. Keep it up.
Answered by TheMoonlìghtPhoenix
46

Continued from Top (Original content credit : BrainlyProgrammer)

  • length() returns length of the string
  • concàt() is use to concatenate two strings
  • substrîng() is used to extract a part of a string

System.out.println(p.length());

//Returns length of variable p

variable 'p' is storing "Server"

So, let us find the length of the word "Server"

\sf{p =\boxed {0 |1 | 2 |3 |4 |5 \over S| e| r|v| e| r}}

As we can see, index of last character is 5 so length of the string will be 6.

System.out.println(q.concat(p.substring(3));

Here two string functions are used, so first we will solve the inner one:-

 \large{ \sf p=\boxed{ {0 |1 | 2 |3 |4 |5\over S| e| r|v| e| r}}}

 \large{ \sf q=\boxed{ {0 |1 | 2 |3 |4 |5 |6 |7| \over C| o| m|p| u| t| e|r }}}

Remember, whenever only 1 parameter I given in substrîng() it means that string from that index till the last Index will be extracted.

Given above , the first diagram which shows index of characters in variable p.

>System.out.println(q.concat(p.substring(3));

System.out.println(q.concat("ver")); //p.substring(3) returns "ver"

Now this "ver" will get concatenated to value of variable q

> System.out.println(q.concat("ver"));

System.out.println("Computer".concat("ver"));

System.out.println("Computerver");

Yay! we solved this! So the output not this question will be :-

6

Computerver

-----

Final Answers:-

  • Answer 4:-
  • 3

  • Answer 5:-
  • 6
  • Computerver.

anindyaadhikari13: Nice!
TheMoonlìghtPhoenix: Thank you!
Similar questions