Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

String Handling Exercise Ques Ans

Important questions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

String Handling Exercise Ques Ans

Important questions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COMPUTER APPLICATION

String Handling
Fill in the blanks
1. concat() method is used to join two strings.
2. The output of the following statement, when executed:
System.out.println("COMPUTER".charAt(4)); is U
3. The output of the statement, when executed:
System.out.println("Object Oriented".length()); is 15
4. + operator is used to concatenate two strings.
5. String declaration is terminated by the symbol ;(semicolon).
6. Character literal is enclosed within single quotes (').
7. The return type of the statement equals( ) is boolean.
8. The output of "VIDYALAYA."substring(2,5) will result in DYA
Write down the syntax to perform the following tasks
1. To check whether a character(chr) is in upper case or not.
boolean res = Character.isUpperCase(chr);
2. To compare two Strings(str1, str2) are same or not.
boolean res = str1.equals(str2);
3. To extract the last character of a word(wd) stored in the variable chr.
char chr = wd.charAt(wd.length() - 1);
4. To return the first occurrence of 'a' in the word "applications".
int res = "applications".indexOf('a');
5. To replace the word "old" with the word "new" in a given String st = "old
is always old"
String str = st.replace("old", "new");
6. To check if the second character of a String(str) is in upper case.
boolean res = Character.isUpperCase(str.charAt(1));

Predict the output of the following:


1. String str = "Computer Applications" + 1 + 0;
System.out.println("Understanding" + str);
Output
UnderstandingComputer Applications10
2. String n1 = "46", n2 = "64";
int total = Integer.parseInt(n1) + Integer.parseInt(n2);
System.out.println("The sum of " + "46 " + "and" + " 64" + " is " + total);
Output
The sum of 46 and 64 is 110
3. boolean p;
p = ("BLUEJ".length() > "bluej".length()) ? true: false;
Output
false
4. String str = "Information Technology";
int p;
p = str.indexOf('n');
System.out.println(p);
Output
1
5. String str1 = "Information Technology";
String str2 = "information technology";
boolean p = str1.equalsIgnoreCase(str2);
System.out.println("The result is " + p);
Output
The result is true
6. What do the following functions return?
String x = "Vision";
String y = "2020";
(a) System.out.println(x + y);
Output
Vision2020
(b)System.out.println(x.length());
Output
6
(c)System.out.println(x.charAt(3));
Output
i
(d) System.out.println(x.equals(y));
Output
false
7. String S1 = "Computer World";
String S2 = "COMPUTER WORLD";
String S3 = "Computer world";
String S4 = "computer world";
System.out.println(S1 + " equals "+ S2 + " " + S1.equals(S2));
System.out.println(S1 + " equals "+ S3 + " " + S1.equals(S3));
System.out.println(S1 + " equals "+ S4 + " " + S1.equals(S4));
System.out.println(S1 + " equalsIgnoreCase "+ S4 + " " +
S1.equalsIgnoreCase(S4));
Output
Computer World equals COMPUTER WORLD false
Computer World equals Computer world false
Computer World equals computer world false
Computer World equalsIgnoreCase computer world true
8. If:
String x = "Computer";
String y = "Applications";
What do the following functions return?
(i) System.out.println(x.substring(1,5));
Output
ompu
Explanation
x.substring(1,5) will return a substring of x starting at index 1 till index 4 (i.e. 5 -
1 = 4).
(ii) System.out.println(x.indexOf(x.charAt(4)));
Output
4
(iii) System.out.println(y + x.substring(5));
Output
Applicationster
(iv) System.out.println(x.equals(y));
Output
False

9. Give the output of the following:


String n = "Computer Knowledge";
String m = "Computer Applications";
System.out.println(n.substring(0,8).concat(m.substring(9)));
System.out.println(n.endsWith("e"));
Output
ComputerApplications
true
10. Give the output of the following statements:
String x[] = {"SAMSUNG", "NOKIA", "SONY", "MICROMAX", "BLACKBERRY"};
(i) System.out.println(x[1]);
Output
NOKIA
(ii) System.out.println(x[3].length());
Output
8
11. Give the output of the following string functions:
(i) "ACHIEVEMENT".replace('E', 'A')
Output
ACHIAVAMANT
(ii) "DEDICATE".compareTo("DEVOTE")
Output
-18

12. Consider the following String array and give the output
String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};
System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0,3));
Output
false
JAI
State the difference between :

a) == equals( )
Checks whether two strings belong to Checks whether two string quantities
the same memory location or not. are same or not.
Checks whether two quantities are Checks only two string quantities are
equal or not. The quantities may be same or not
any data type other than string.

b) compareTo( ) equals( )
Checks two strings lexicographically. Checks two strings for equality.
Returns the first difference between the Returns either a true or a false.
Unicode of the characters where it differs.
Return type is int. Return type is Boolean.

c) toLowerCase( ) toUpperCase( )
Converts a given character or a string to Converts a given character or a string to
lower case. upper case.

d) startsWith( ) endsWith( )
Checks whether a string begins with a Checks whether a string ends with a
certain character or a string or not and certain character or a string or not and
returns true or false accordingly. returns true or false accordingly.

e) indexOf( ) lastIndexOf( )
Searches for a certain character or string Searches for a certain character or string
from to left to right in a given string and from to right to left in a given string and
return the index for the first occurrence of return the index for the first occurrence of
the found character or string otherwise the found character or string otherwise
return -1. return -1.
Write short answers
Q1. What is exception? Name two exception handling blocks.
Exception in general refers to some contradictory or unusual situation which
can be encountered while executing a program. Two exception handling blocks
are try and catch.

Q2.State the purpose and return data type of the following String functions:

(a) indexOf(): It 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.

(b) compareTo(): It compares two strings lexicographically. Its return type is


int.

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

(i) Extract the second last character of a word stored in the variable wd.

char ch = wd.charAt(wd.length() - 2);


(ii) Check if the second character of a string str is in upper case.

boolean res = Character.isUpperCase(str.charAt(1));

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

(i) Find and display the position of the last space in a string s.

System.out.println(s.lastIndexOf(' '));
(ii) Convert a number stored in a string variable x to double data type.

double a = Double.parseDouble(x);
Q5. How does endsWith() and startsWith() differ? Explain with an example.

endsWith() tests if the string object ends with the string specified as its
argument. startsWith() tests if the string object starts with the string specified
as its argument. Consider the below example:

public class Example {


public static void main(String args[]) {
String str = "ICSE Computer Applications";
System.out.println("Does " + str + " starts with ICSE? " +
str.startsWith("ICSE"));
System.out.println("Does " + str + " ends with tions? " +
str.endsWith("tions"));
}
}
Here, both str.startsWith("ICSE") and str.endsWith("tions") returns true as str
starts with "ICSE" and ends with "tions".

Describe the purpose of the following functions with their syntax


Q1. toUpperCase()

It converts a string into upper case characters. If any character is already in


uppercase or is a special character then it will remain same.
Syntax:
String <variable-name> = <string-variable>.toUpperCase();
Q2. trim()
It removes all leading and trailing space from the string.
Syntax:
String <variable-name> = <string-variable>.trim();
Q3. toLowerCase()
It converts a string into lowercase characters. If any character is already in
lowercase or is a special character then it will remain same.
Syntax:
String <variable-name> = <string-variable>.toLowerCase();
Q4.length()
It returns the length of the string i.e. the number of characters present in the
string.
Syntax:
int <variable-name> = <string-variable>.length();
Q5. replace()

It replaces a character with another character or a substring with another


substring at all its occurrences in the given string.

Syntax:

String <variable-name> = <string-variable>.replace(<character or substring to


replace>, <new character or substring>);
Q6. compareTo()

It compares two strings lexicographically.


Syntax:
int <variable-name> = <string-variable>.compareTo(<string-variable2>);

Q7.reverse()
It is a method of StringBuffer class used to reverse the sequence of characters.
Syntax:
<StringBuffer-Variable>.reverse();
Q8. indexOf(): It returns the index within the string of the first occurrence of
the specified character or -1 if the character is not present.
Syntax:
int <variable-name> = <string-variable>.indexOf(<character>);
Q9. startWith(): It tests if the string object starts with the string specified as its
argument.

Syntax:
boolean <variable-name> = <string-variable>.startWith(<string>);
Q10. equalsIgnoreCase()
It ignores the case of the characters and checks if the contents of two strings
are same or not.
Syntax:
boolean <variable-name> = <string-variable>.equalsIgnoreCase(<string>);

You might also like