Important String Questions
Important String Questions
NDEPARTMEN'OF
nvsc sit v
METHOD
of
on the Unicode value
compaeTo(): compares two st rings lexicographically. The comparison is based
each character in the strings.
lexicographic ordering•
wordi e.word2
compaeTo(): Negative number
wordl would come before word2
wordl >word2
compaeTo(): Positive number
wordl would come after word2
wordl word2
compaeTo(): 0
wordl is equivalent word2
1. wordl <word2
compaeTo(): Negative number
wordl would come before word2
String wordl="ape"
String word2="zebra" (a comes 25 letters before z)
wordl.compareTo(word2) Output: 25
2. wordl >word2
compaeTo(): Positive number
wordl would come after word2
Special Cases:
wordl<word2:
String wordl=" ape",
String word2="apple"
wordl.compareTo(word2) Output: -11
Stringwordl"Ape";
String word2="ape"
wordl.compareTo(word2) Output: -32 (uppercase letter has lesser Unicode value.)
1
String wordl:"
String IApe"
Wordl.compareTo(word2)
Output. 16 (489: O to
9, 6590: A to Z, 9/022:
a to 1)
OUTPUT QUESTIONS
Question 1:
int n = 97;
char ch =
System.out.println(ch
+ " Great Victory");
Question 2:
String s= "7";
int t =lnteger.parselnt(s);
t:t+1000;
System.out.println(t);
Question 3:
char c =
int n = (int) c + 32;
System.out.println((char)n);
Question 4:
char ch = 'A';
char chr = Character.toLowerCase(ch);
int n = (int)chr-32;
System.out.println((char)n + "\t" + chr);
Question 5:
char ch =
char chr = Character.toUpperCase(ch);
int p = (int) chr;
System.out.println(chr + "\t" + p);
Question 6:
string A -"26",
string
int x: Integer.parselnt(A);
int y = Integer.parselnt(B);
int d = X+y;
System.out.println("ReSult1 = " + D)
System.out.println("Result 2 = " + d)
Question 7:
String
int x=lnteger.parselnt(a);
int y=lnteger.valueOf(b);
System.out.println(x+y);
System.out.println(a+b);
2
Question 8:
String sl "phoenix"; String s2 -"island";
Question 9:
String x = "Computer";
String y = "Applications";
System.out.println(x.substring(1,5));
Question 10:
boolean p;
p = ("BLUEJ" .length() > "bluej" .length()) ? true: false;
Question 11:
string arrll= ("DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR");
System.out.print(arr(41.substring(O,3));
Answer:
I. A great victory
2. 1007 (Integer.parselnt() converts "7" into an int value i.e. the decimal number
7. t+1000 adds the number 7 to 1000 giving 1007 as the output)
3. a
4.
s. Y 89
6. Result 1 = 26100200
Result 2 = 126
• As A and B are strings so String joins A, B and "200" storing the string
"26100200" in D.
• Integer.parselnt() converts strings A and B into their respective numeric values. Thus, x
becomes 26 and y becomes 100.
• As Java is case-sensitive so D and d are treated as two different variables.
Sum of x and y. is assigned to d. Thus, d gets a value of 126.
7. 30
1020
(Integer.parselnt and Integer.valueOf convert the strings a and b into their
corresponding numeric integers. x + y adds 10 and 20 to give 30 as the output. a and b
are strings so addition operator concatenates them and prints 1020.)
8. Phoenixland
ISLAND
9. ompu
10. false
11. false
3
JAI
ractice uestion
String Programs
Question 1: Write a programto input twenty
names in an array. Arrange these names in descending
order of alphabets, using the bubble
sort technique.
import java.util.*;
class Quest 8
{ void sort()
for(int I
fot(int
;k++)
String
4
nnmes(k) names(k+
names(k+ I I—term',
in dcsccnding order");
fo<int
Question 2: Special words are those words which starts and ends
with the same letter.
Examples: EXISTENCE COMIC WINDOW
Palindrome words are those words which read the same from left to
right and vice-versa Examples:
MALAYALAM MADAM LEVEL ROTATOR CIVIC
All palindromes are special words, but all special words
are not palindromes.
Write a program to accept a word check and print whether the
word is a palindrome or only special
word.
import java .util.•;
class ques6
Question 3: Write a program to initialize the seven Wonders of the World along with their locations in
two different arrays. Search for a name of the country input by the user. If found, display the name of
the country along with its Wonder, otherwise display "Sorry Not Found!".
Seven wonders - CHICHEN ITZA, CHRIST THE REDEEMER,TAJMAHAL, GREAT WALL OF CHINA, MACHU
PICCHU, PETRA, COLOSSEUM
Locations - MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example - Country Name : INDIA
Output : INDIA - TAJMAHAL
Country Name : USA
Output : Sorry Not Found!
5
importjava utii.•;
class wonders
len=w.lcngth; // OR 1m = Llength;
boolen flag—fals
for // OR i=O;i<7;i++)
ills
flag=tnr;
if(flag== false)
Not
Question 4 : Write a program in Java to accept a in lower ase and charwe the first letter of every
word to upper case. Display the new string.
amporl java-util. e ;
class ques6 {
public static void main(String args[J) {
Scanner obj= new Scanncr(System-tn);
String
Systern-out-println(" enter a string");
String sen=obj .nextLine();
scn=• +sen;
int sen.length();
for(int x= O;x<l; x++) {
char ch= sen-charAt(x);
if (ch=æ {
6
l);
int i" sen.charAt(x+
else s=s+ch;)
System.out.print(s);
Question 5:
Write a program to input a sentence and convert it into uppercase and count and display the total
number of words starting with a letter
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER
CHANGING.
Sample Output: Total number of words starting with letter 'A' = 4
Import java-util. • ;
class loop
str.charAt(i);
ch I I );
if && ch=='A')