(a) State the purpose and return data type of the following String functions:
- (a)indexOf()
- (b)compareTo()
Show SolutionHide Solution↓
- indexOf() returns the index within the string of the first occurrence of the specified character or -1 if the character is not present. Its return type is int.
- compareTo() compares two strings lexicographically. Its return type is int.
(b) What is the result stored in x, after evaluating the following expression?
int x = 5; x = x++ * 2 + 3 * --x;
x = x++ * 2 + 3 * --x
⇒ x = 5 * 2 + 3 * 5
⇒ x = 10 + 15
⇒ x = 25
(c) Differentiate between static and non-static data members.
| Static Data Members | Non-Static Data Members |
|---|---|
| They are declared using keyword 'static'. | They are declared without using keyword 'static'. |
| All objects of a class share the same copy of Static data members. | Each object of the class gets its own copy of Non-Static data members. |
| They can be accessed using the class name or object. | They can be accessed only through an object of the class. |
(d) Write the difference between length and length().
| length | length() |
|---|---|
| length is an attribute i.e. a data member of array. | length() is a member method of String class. |
| It gives the length of an array i.e. the number of elements stored in an array. | It gives the number of characters present in a string. |
(e) Differentiate between private and protected visibility modifiers.
Private members are only accessible inside the class in which they are defined and they cannot be inherited by derived classes. Protected members are also only accessible inside the class in which they are defined but they can be inherited by derived classes.