String Handling

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Multiple Choice Questions (MCQS)

. What is the correct method to extract a character from a string?

a. CharAt(0 b. charAt()
c. CharAT) d. charat()

2. The class string belongs to.. ... package.

a. java.awt b. java.lang
C. java.applet d. java.string

3. Which of these methods is used to find the number of characters ina string?
a. sizeOf() b. Length()
C. length() d. lengthOf()

4. Which of these methods does not return an integer value?


a. equals() b. compareTo()
c. indexOf() d. length()

5. What is the output of the statement: "Computers".substring(3,6)?


a. mput b. pute
C. mpu d. put

6. Give the output of the following String methods: [ICSE, 2022


SUCESS".indexof ( \S')+ "SUCCESS'".lastIndexof ( \S')
a. 0 b. 5
C. 6 d. -5

7. Give the output of the following code: ICSE, 2022

System.out.prinitin ("Good" .concat ("Day") );


a. GoodDay b. Good Day
c. Goodday d. goodDay
Give the output ofthe following code:
B. CSE, 2022
nGA"56.0", B-"94.0";
double C- Double.parsebouble (A) ;

uble D DoUble.parseDouble (B);


System.out.printin ( (C+D) ) ;
a. 100
b. 150.0
C. 100.0
d. 150
What will be the output of the following code?
9. PHICsE, 2022)
Custem.out.prinitin ("Lucknow" .3ubstring (,4));
a. Lucknow
b. Luckn
c. Luck d. luck

- Answerss

1. b 2. b 3. c 4. a 5. d 6. b 7. a 8. b 9. C

-Subjective Questions
Very Short/Short Answer Questions te
1. Differentiate between charAt() and indexOf().

Ans. charAt() returns the character at the given index whereas indexOf() returns the index of the first occurrence of
the given character.
2. Differentiate between equals() and compareTol).
Ans. equals() checks if two strings are identical or not, whereascompareTolcompares two strings lexicographically
and returns the difference //in the UNICODE values of the first pair of dissimilar characters.

3. Differentiate between length and length().


Ans. length is a property of arrays that gives the number of elements in an array, whereas length() is a string function

that returns the number of characters in a string.

4. Name the return type of each of the following functions:


(i) endsWith() (ii) indexOf)
() trim()
Ans. (i) String (i) boolean (ii) int

5. From the table given below, write the Java function that has been used to generate the output shown:

Variable Output

() s="Payable" "able"
(i) t"feet" "foot"

(ii) p= " book" "book"

Ans. () String ss S.substringl3);


=

(i) String r = t.replace('e,'o};


en
(n) String q p.trim|);
Write a
statementto accept a full path and file name in a variable
Using library functions, extract and output the file path, file name and file extension separate
shown in the
example.
Input: C:\Usersladmin\Pictures\tree.jpg
Output: Path: C:\Usersladmin\Pictures
File name: tree
Extension: jpg
Ans. String s =

"C:\Users\admin\Pictures\tree.jpg
Yenl. out.println("Path :"+ s.substring (0, s.lastindexot CT)+1)):
System.out.println ("File name:"+ s.substring (s. lastIndexor ("\")+1,
s.indexOf ('. ')));
System.out.println ("Extension "+ S.substring (s.indexof°.')+1)):
7. Using Java library functions, write statements with appropriate datatypes for the following:
Find the position of the first occurrence of the letter 'P" in string str and store it in n.
(i) Concatenate string City with string Pincode and store in string Address.
Ans. (i) int n=str.indexOf(P');
(i) String Address=City.concat(Pincode);
8. Give the output of the following Java statements:

System.out.println ("MISSISSIPPI" . last IndexOf ( 'S')) ;


System.out.println ("PRESIDENT" .substring (1, 7));
System. out.println ("APTITUDE" . compareTo ("Attitude"));
System.out.println ("APTI TUDE" .endsWith ("tude") ) ;
Ans. Output: 6
RESIDE
-36
false

9. Write a statement each to perform the following task on a string:


(i) Extract the second last character of a word stored in the variable wd.
(i) Change the second character of a string str to uppercase.

Ans. () char c=wd.charAt(wd. length()-2);


(i) char ch=Character.toUpperCase(str.charAt(1);
10. What is the data type returned by the library functions:
i) compareTo() (ii) equals(0 [ICSE, 2014
Ans. (i) int (ii) boolean

11. State the value of characteristic and mantissa when the following code is executed:
String s ="4.3756"
int n = S.indexof ( ' . ' ) ;

int characteristic= Integer .parseInt (s.substring (0, n) );


int mantissa = I n t e g e r. v a l u e 0 f ( s . s u b s t r i n g (n + 1) ) ;
characteristic =
4
Ans. manissa = 3756
the output:
12. Write
char ch = 'F'; [ICSE, 2017)

int m= ch;

m=m + 5b;

System. out.println (m +"


+ch)
Ans. 75F

13. Write the output for the following:


[ICSE, 2017]
String s =

Today is Test";
System.out.println (s.indexof ('T')):
System.out.println (s.substring (0, 7) +" + "Holiday")
Ans. 0

Today i Holiday

14. State the difference between == operator and equals() method. [ICSE, 2016]
Ans. The == operator compares two numeric or character values for equality.

The equals() method compares the contents of two String values for equality.

15. Give the output of the following string functions:

() "MISSISsIPPI".indexof"'s') + "MISSISSIPPI".lastindexOf('")

(ii) "CABLE".compareTo("CADET") [ICSE, 2016]


Ans. (i) 2+10 12
(ii) 66 - 68 = -2

16. Write the return type of the following library functions:

(i) isLetterOrDigit(char)
(i) replace(char, char) [ICSE, 2016]
Ans. (i) boolean (i) String
17. State the output when the following program segment is executed: [ICSE, 2015]
String a =
" Smartphone" , b
=

"Graphic Art";
String h =a.substring (2, 5);
();
String k =
b.substring(8) . toUpperCase
S y s t e m . o u t . p r i n t l n (h) ;

(k.equalsIgnoreCase (h) );
System.out.println

Ans. art

true
8. State the output of the following program segment:
str2 "minds";
String strl =
"great "; String
(0, 2) .concat (str2. substring (1))) :

S y s t e m . o u t . p r i n t l n (strl.substring

(strl. Substring(2) . toUPperCase () [ICSE, 2014]


+ ) )):
System.out.println ( ( "WH"
Ans. grinds
WHEAT
w i t e statements to show how finding the length of a character array char ch[] differs from finding the length

of a String object str. [ICSE, 2013)


Ans. For character array: int len
=ch.length;
ForString object str: int len = str.length();

20. State the values stored in the variables str1 and str2:
String sl = "good";

String s2 = "world matters" ;

String strl =
s2.substring (5) .replace ('t', 'n')i
String str2 = sl.concat (strl); [ICSE, 2013
Ans. str1:manners
str2 good manners

21. State the output of the following program segment:


String s = "Examination" ;
int n =
s.length ();
System.out.println (s.startsWith (s.substring (5, n)));
System.out.println (s.charAt (2) == s.charAt (6)); [ICSE, 2012
Ans. nation
true

22. State the data type and values of a and b after the following segment is executed:
String s1 = "Computer", s2 = "Applications"

a = (s1.compareTo (s2) ) ;

b = (s1.equals (s2) ) ; [ICSE, 2012]

Ans.

Variable
Data type int boolean
Value 2 false
23. What will the following code output:
String s = "malayalam";
System.out.println (s.indexof ( 'm'));
[ICSE, 2013]
System. out.println (s. last Indexof ('m '));
Ans.0
8

You might also like