0% found this document useful (0 votes)
30 views17 pages

String Handling STD X

Uploaded by

sumedhdarshan5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views17 pages

String Handling STD X

Uploaded by

sumedhdarshan5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

STRING HANDLING

Question 1

A string is:

1. A set of letters
2. A set of letters and digits
3. A set of letters, digits and special characters
4. All the above

Answer

All the above

Reason — A string literal may consist of a set of letters, a set of letters and digits or a set of
letters, digits and special characters.

Question 2

A string internally creates:

1. An integer array
2. A numeric array
3. A character array
4. A double type array

Answer

A character array

Reason — String data type creates a character array to store a set of characters in different
cells.

Question 3

If a string contains 12 characters, what will be the index of the last character?

1. 12
2. 11
3. 10
4. 0

Answer

11
Reason — A String data type manages character array for storing a string character-wise
starting from 0th subscript. Hence, the index number of the characters starts from 0 (zero) and
ends with (k-1), if k is length of the string. Hence, the last index will be 11 (12-1), as 12 is the
size of the string.

Question 4

What will be the value stored in the variable 'c' if the following statement is executed?
c = "COMPUTER".charAt("COMPUTER ".indexOf('P'))

1. 3
2. 4
3. P
4. M

Answer

Reason — charAt(int index) returns a character from the given index of the string and
indexOf(char ch) returns the index of first occurrence of a character in the string.
Since P is at 3rd position so indexOf('P') will return 3 and charAt(3) function will return the
character at the third index i.e., 'P'. So, the given statement is solved as follows:

c = "COMPUTER".charAt("COMPUTER ".indexOf('P'))
⇒ c = "COMPUTER".charAt(3)
⇒ c = 'P'

Question 5

Which of the following functions is used to remove leading and trailing white spaces from
the string?

1. trim( )
2. trail( )
3. truncate( )
4. slice( )

Answer

trim( )

Reason — trim( ) function is used to remove leading and trailing white spaces from the
string.

Question 6

What will be the output of the following program snippet?


s1 = "COMPUTER";
s2 = "computer";
System.out.println(s1.equals(s2));
1. True
2. False
3. 0
4. 1

Answer

False

Reason — equals() method treats uppercase and lowercase characters differently. So, the two
strings will not be considered equal and the boolean value 'false' will be returned.

Question 7

What will be the output of the following program snippet?


str1 = "AMAN";
str2 = "AMIT";
System.out.println(str1.compareTo(str2));

1. 8
2. 0
3. -8
4. 2

Answer

-8

Reason — compareTo() returns a negative value if the first string is smaller than the second
string in terms of the ASCII values of the corresponding characters.
"AMAN" and "AMIT" differ at the third character with 'A' and 'I', respectively. So, output
of compareTo() method will be ASCII Code of 'A' - ASCII Code of 'I' ⇒ 65 - 73 ⇒ -8.

Question 8

Which of the following packages contain string functions?

1. java.awt
2. java.string
3. java.math
4. java.lang

Answer

java.lang

Reason — Java Class Library contains a class called String which is available under
java.lang package.

Fill in the blanks


Question 1

concat() method is used to join two strings.

Question 2

The output of the following statement, when executed:


System.out.println("COMPUTER".charAt(4)); is U.

Question 3

The output of the statement, when executed:


System.out.println("Object Oriented".length()); is 15.

Question 4

Plus ( + ) operator is used to concatenate two strings.

Question 5

String declaration is terminated by the symbol semicolon ( ; ).

Question 6

Character literal is enclosed within single quotes (').

Question 7

The return type of the statement equals( ) is boolean.

Question 8

The output of "VIDYALAYA".substring(2,5) will result in DYA

Write down the syntax to perform the following tasks

Question 1

To check whether a character(chr) is in upper case or not.

Answer

boolean res = Character.isUpperCase(chr);

Question 2

To compare two Strings(str1, str2) are same or not.

Answer

boolean res = str1.equals(str2);


Question 3

To extract the last character of a word(wd) stored in the variable chr.

Answer

char chr = wd.charAt(wd.length() - 1);

Question 4

To return the first occurrence of 'a' in the word "applications".

Answer

int res = "applications".indexOf('a');

Question 5

To replace the word "old" with the word "new" in a given String st = "old is always old"

Answer

String str = st.replace("old", "new");

Question 6

To check if the second character of a String(str) is in upper case.

Answer

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

Predict the output of the following

Question 1

String str = "Computer Applications" + 1 + 0;


System.out.println("Understanding" + str);
Answer

Output

UnderstandingComputer Applications10

Explanation

In the first line, + operator concatenates 1 and 0 to the end of "Computer Applications"
so str becomes "Computer Applications10". Next line prints "UnderstandingComputer
Applications10" to the console.

Question 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);
Answer

Output

The sum of 46 and 64 is 110

Explanation

Integer.parseInt() method will convert the strings n1 and n2 to their corresponding numerical
integers. So, 46 and 64 are added as numerical values and the result 110 is stored in int
variable total.

Question 3

boolean p;
p = ("BLUEJ".length() > "bluej".length()) ? true: false;
Answer

Output

false

Explanation

Both "BLUEJ" and "bluej" have the same length 5. So, condition of ternary operator is false
and false is assigned to boolean variable p.

Question 4

String str = "Information Technology";


int p;
p = str.indexOf('n');
System.out.println(p);
Answer

Output

Explanation

str.indexOf('n') will return the first index of n in str which is 1. So, the output of this
program is 1.

Question 5
String str1 = "Information Technology";
String str2 = "information technology";
boolean p = str1.equalsIgnoreCase(str2);
System.out.println("The result is " + p);
Answer

Output

The result is true

Explanation

str1.equalsIgnoreCase(str2) will do a case insensitive comparison of str1 and str2. As both


strings contain the same characters if we ignore the case so it returns true which is stored in
boolean variable p.

Question 6

What do the following functions return?


String x = "Vision";
String y = "2020";

(a) System.out.println(x + y);

(b) System.out.println(x.length());

(c) System.out.println(x.charAt(3));

(d) System.out.println(x.equals(y));

Answer

(a) System.out.println(x + y);

Output

Vision2020

Explanation

x and y are concatenated and printed.


(b) System.out.println(x.length());

Output

Explanation

x.length() will give the number of characters in string x which is 6.


(c) System.out.println(x.charAt(3));

Output
i

Explanation

x.charAt(3) will return the character at index 3 of string x which is i.


(d) System.out.println(x.equals(y));

Output

false

Explanation

x.equals(y) will return false as x and y contains different characters.

Question 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));
Answer

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

Explanation

As the strings S1, S2, S3 and S4 differ in the case of the characters so equals() method will
return false but equalsIgnoreCase() method will return true.

Question 8

If:
String x = "Computer";
String y = "Applications";

What do the following functions return?

(i) System.out.println(x.substring(1,5));

(ii) System.out.println(x.indexOf(x.charAt(4)));
(iii) System.out.println(y + x.substring(5));

(iv) System.out.println(x.equals(y));

Answer

(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

Explanation

x.charAt(4) returns the character at index 4 of string x which is 'u'. First index of 'u' in x is 4
so output is 4.

(iii) System.out.println(y + x.substring(5));

Output

Applicationster

Explanation

x.substring(5) will return the substring of x starting at index 5 till the end of the string. It is
"ter". This is added to the end of string y and printed to the console as output.
(iv) System.out.println(x.equals(y));

Output

false

Explanation

As strings x and y are not equal so x.equals(y) returns false.

Question 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"));
Answer

Output

ComputerApplications
true

Explanation

n.substring(0,8) returns the substring of n starting at index 0 till 7 (i.e. 8 - 1 = 7) which is


"Computer". m.substring(9) returns the substring of m starting at index 9 till the end of the
string which is "Applications". concat() method joins "Computer" and "Applications"
together to give the output as ComputerApplications.

Question 10

Give the output of the following statements:


String x[] = {"SAMSUNG", "NOKIA", "SONY", "MICROMAX", "BLACKBERRY"};
(i) System.out.println(x[1]);

(ii) System.out.println(x[3].length());

Answer

(i) System.out.println(x[1]);

Output

NOKIA

Explanation

x[1] gives the second element of the array x which is "NOKIA"


(ii) System.out.println(x[3].length());

Output

Explanation

x[3].length() gives the number of characters in the fourth element of the array x.

Question 11

Give the output of the following string functions:

(i) "ACHIEVEMENT".replace('E', 'A')

(ii) "DEDICATE".compareTo("DEVOTE")

Answer
(i) "ACHIEVEMENT".replace('E', 'A')

Output

ACHIAVAMANT

Explanation

All 'E's are replaced with 'A's.

(ii) "DEDICATE".compareTo("DEVOTE")

Output

-18

Explanation

"DEDICATE" and "DEVOTE" differ at the third character with 'D' and 'V', respectively. So,
output of compareTo() method will be ASCII Code of 'D' - ASCII Code of 'V' ⇒ 68 - 86 ⇒ -
18.

Question 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

Explanation

As length of the string "DELHI" is less than that of "LUCKNOW" so first output is false.
arr[4].substring(0,3) will return the substring of "JAIPUR" starting at index 0 till index 2
(i.e. 3 - 1 = 2).

Differentiate between the following

Question 1

equals() and ==

Answer
equals() ==

It is a method. It is a relational operator.

It is used to check if the contents of two strings are It is used to check if two variables refer to the same obje
same or not. in memory.

Example: Example:
String s1 = new String("hello"); String s1 = new String("hello");
String s2 = new String("hello"); String s2 = new String("hello");
boolean res = s1.equals(s2); boolean res = s1 == s2;
System.out.println(res); System.out.println(res);

The output of this code snippet is true as contents of The output of this code snippet is false as s1 and s2 poin
s1 and s2 are the same. to different String objects.

Question 2

compareTo() and equals()

Answer

compareTo() equals()

It checks if contents of two strin


It compares two strings lexicographically.
are same or not.

The result is a negative, positive or zero integer value depending on whether The result is true if the contents
the String object precedes, follows or is equal to the String argument. are same otherwise it is false.

Question 3

toLowerCase() and toUpperCase()

Answer

toLowerCase() toUpperCase()

Converts all characters of the String object to lower Converts all characters of the String object to upper
case. case.

Example: Example:
String str = "HELLO"; String str = "hello";
System.out.println(str.toLowerCase()); System.out.println(str.toUpperCase());
toLowerCase() toUpperCase()

Output of this code snippet will be hello. Output of this code snippet will be HELLO.

Question 4

charAt() and substring()

Answer

charAt() substring()

It returns a character from the string at the It extracts a part of the string as specified by its arguments and
index specified as its argument. returns the extracted part as a new string.

Its return type is char. Its return type is String.

Example: Example:
String str = "Hello"; String str = "Hello";
char ch = str.charAt(1); String subStr = str.substring(1);
System.out.println(ch); System.out.println(subStr);

Output of this code snippet is e. Output of this code snippet is ello.

Write short answers

Question 1

What is exception? Name two exception handling blocks.

Answer

An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program's instructions. Two exception handling blocks are try and catch.

Question 2

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

(a) indexOf( )

(b) compareTo( )

Answer

(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. It results in the difference of
the ASCII codes of the corresponding characters. Its return type is int.

Question 3

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.

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

Answer

(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));

Question 4

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.

(ii) Convert a number stored in a string variable x to double data type.

Answer

(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);

Question 5

How does endsWith() and startsWith() differ? Explain with an example.

Answer

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

Question 1

toUpperCase()

Answer

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();

Question 2

trim()

Answer

It removes all leading and trailing space from the string.

Syntax:

String <variable-name> = <string-variable>.trim();

Question 3

toLowerCase()

Answer

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();

Question 4

length()

Answer

It returns the length of the string i.e. the number of characters present in the string.
Syntax:

int <variable-name> = <string-variable>.length();

Question 5

replace()

Answer

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>);

Question 6

compareTo()

Answer

It compares two strings lexicographically. It results in the difference of the ASCII codes of
the corresponding characters. Its return type is int.

Syntax:

int <variable-name> = <string-variable>.compareTo(<string-


variable2>);

Question 7

reverse()

Answer

It is a method of StringBuffer class and it is used to reverse the sequence of characters.

Syntax:

<StringBuffer-Variable>.reverse();

Question 8

indexOf()

Answer

It returns the index of the first occurrence of the specified character within the string or -1 if
the character is not present.

Syntax:
int <variable-name> = <string-variable>.indexOf(<character>);

Question 9

startWith()

Answer

It tests if the string object starts with the string specified as its argument.

Syntax:

boolean <variable-name> = <string-variable>.startWith(<string>);

Question 10

equalsIgnoreCase()

Answer

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(<strin

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy