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

IMPORTANT STRING QUESTIONS FOR EXAM

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

String wordl=" zebra"


String word2="ape" (a comes 25 letters before z)
wordl.compareTo(word2) Output: 25
3. wordl word2
compaeTo(): O
wordl is equivalent word2
String wordl=" ape"
String word2="ape"
wordl.compareTo(word2) Output:

Special Cases:
wordl<word2:
String wordl=" ape",
String word2="apple"
wordl.compareTo(word2) Output: -11

String wordl=" cat",


String word2="catfish"
wordl.compareTo(word2) Output: -4 (catfish is four letter longer than cat)

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

What do the following function returns?

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

Question 1: What is the value stored tn variable res gjven below:


double res Math
is executed:
Question 2: State the output when the following program segment
String Art";
String
String k: b.substring(8).toUpperCase();
System.out.println(h);

Question 3: Give the output of the following string functions :

(ii) "CABLE" .compareTo("CADET")


Question 4: Write the output for the following:
String s:" Today is Test" ;
System.out.println(s.indexOfCT'));
System.out.println(s.substring(O,7)+ " " +"Holiday");
Question S: Give the output of the following string functions:
"ACHIEVEMENT'.replacece, (ii)
Question 6:
Write the output for the following:
String sl = "phoenix";
String s2 —"island" ,
System.out.println(sl.substring(O).concat (s2.substring(2) ) ); System.out.println(s2.toUpperCase())

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

Scanner br=new Scanner


(System. in);
String
String[201•,
20 names");
for(int i=0;i<20;i++)

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

public static void main (String sfl)//throws IOExccption

Scanner obj = new Scanner(System.in);


System.out.pnntln(" enter a word
String word = obj.nextLine();
String rword="";
int l,x;
I—word-length();

rword = rword+ word.charAt(x);

System.out.println( word is palindrome");


else
if(word.charAt(O) word.charAt(l- I ))
System.out.println( word +"is a special word");

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

public static void maan(Stnng


arp,slJ)

string CCIIICIIEN IRISTTHE "GRE-ATWAI.L


or
string Il ;
String s;int i,lcn;
Scanrrr br=new ScanncdSystemån),
tir name of the cannry be EarciEd for • j;

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.

Sample input: we are in cyber world

Sample output: We Are In Cyber World

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+

s—s+ ' '+ (char)i;

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

public static void main (String args())

Scanner sc=new Scanner (System. in);


String sir; char ch, chl; int c—0;
System.out.println("Enter a sentence");
str= sc.nextLine();
str=str.toUppcrCase();
int len= str.length();
for (int i—0;

str.charAt(i);
ch I I );
if && ch=='A')

else if ' && ch I—

Systcm.out.println("Numbcr of words started with Ictter 'A'

You might also like