[go: up one dir, main page]

0% found this document useful (0 votes)
51 views7 pages

Chapter 9 Logical Question For Practice-1

The document contains a series of programming exercises focused on string manipulation in Java, including outputs for various string methods and operations. It provides examples of string replacement, concatenation, comparison, and character extraction, along with expected outputs for each case. The exercises cover fundamental string functions and their behavior in different scenarios.

Uploaded by

yamanpathan65
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)
51 views7 pages

Chapter 9 Logical Question For Practice-1

The document contains a series of programming exercises focused on string manipulation in Java, including outputs for various string methods and operations. It provides examples of string replacement, concatenation, comparison, and character extraction, along with expected outputs for each case. The exercises cover fundamental string functions and their behavior in different scenarios.

Uploaded by

yamanpathan65
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/ 7

Chapter 9 : String Manipulation

Answer as directed
1. Give the output of the following program fragment:
String s=new String(“He went to the market”);
String r=s.replace(“went”,“is going”);
System.out.println(r);
Ans.
Output:
He is going to the market
2. Give the output of the following program fragment:
String s=“String”;
int a=12,b=45;
System.out.println(s+a+b);
System.out.println(a+s+b);
System.out.println(a+b+s);
Ans.
Output:
String1245
12String45
57String
3. Give the output of the following program fragment: String
s=“india”,s1=“IndIA”,s2=s;
System.out.println(s.equals(s1));
System.out.println(s.equalsIgnoreCase(s1));
System.out.println(s2==s);
System.out.println(s.toUpperCase()==s1.toUpperCase());
System.out.println(s.startsWith(“IN”.toLowerCase()));
System.out.println(s1.endsWith(“iA”.toUpperCase()));
Ans.
Output:false
true
true
false
true
true
4. What do the following functions return for:
String x =“hello”;
String y =“world”
System.out.println(x + y);
System.out.println(x.length());
System.out.println(x.charAt(3));
System.out.println(x.equals(y));
Ans.Output: helloworld5
l
false
5. If, String x = “Computer”;
String y = “Applications”;
What do the following functions return for:
(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));
Ans.
(i) ompu (ii) 4
(iii) Applicationster (iv) false

6. What will be the output for the following program segment?


String s = new String(“abc”);
System.out.println(s.toUpperCase());
Ans.ABC
7. What will be the output of the following code?
char x = ‘A’; int m;
m=(x= =’a’) ? ‘A’ : ‘a’;
System.out.println(“m=”+m);
Ans. m=97
8. Write statements to show how finding the length of a character array and char[] differs from
finding the length of a String object str.
Ans. char.length;
str.length();
9. 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
Ans.
(i) System.out.println(s.lastIndexOf(“ ”));
(ii) double d=Double.parseDouble(x);
10. 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.
(ii) Check if the second character of a string str is in uppercase.
Ans.
(i) char sl=wd.charAt(wd.length()-2);
(ii) if(Character.isUpperCase(str.charAt(1)))
11. Give the output of the following string functions:
(i) “ACHIEVEMENT”.replace(‘E’,‘A’)
(ii) “DEDICATE”.compareTo(“DEVOTE”)
Ans.
ACHIAVAMANT
-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));
Ans.false
JAI
13. String x[] = {“SAMSUMG”, “NOKIA”, “SONY”, “MICROMAX”, “BLACKBERRY”};
Give the output of the following statements:
(i) System.out.println(x[1]);
(ii) System.out.println(x[3].length());
Ans.
(i) NOKIA
(ii) 8
14. Write the output for the following:String
s=“Today is Test”;
System.out.println(s.indexOf(‘T’));
System.out.println(s.substring(0,7)+“ ”+“Holiday”);
Ans. 0
Today i Holiday
15. Give the ouput of the following string functions:
(i) “MISSISSIPPI”.indexOf(‘S’)+“MISSISSIPPI”.lastIndexOf(‘I’)
(ii) “CABLE”.compareTo(“CADET”)
Ans. (i) 12 (II) -2
16. State the output of the following program segment when executed:
String a = “Smartphone”, b = “Graphic Art”;
String h = a.substring(2, 5);
String k = b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
Ans. art
True
17. State the output of the following program segment:
String str1 = “great”; String str2 = “minds”;
System.out.println(strl.substring(0,2).concat(str2.substring(1)));
System.out.println((“WH” + (strl.substring(2).toUpperCase())));
Ans.grinds
WHEAT
18. 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));
19. What will the following code output?
String s = “malayalam”;
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’));
Ans.
0
8
20. 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”));
Ans Computer Applications
true
21. Predict the output:
(i) “ARTIFICIAL”.endsWith(“AL”);
(ii) “Java_Basics”.trim().length();
Ans.(i) true
(ii)11
22. Predict the output:?

System.out.println(“four :” + 4 + 2);
System.out.println(“four :” + (2 + 2));
Ans four:42
four:4

23. What will be the output of the following program segment?


String s= "application";
int p= s.indexOf(‘a');
System.out.println(p);
System.out.printin(p+s);
Ans: 0
0application
24. What will be the output of the following program segment?
String st = "PROGRAM";
System.out.printin(st.indexof(st.charAt(4);
Ans: 1
25. Give the output of the following:
(a) System.out.println("Hello" +2+5);
(b) System.out.printin("Hello"+ (2+5);
(c) System.out.println("'B".compareTo(A"));
(d) System.out.println("Welcome".charAt(2) + "Welcome".charAt(o));
Ans: (a) Hello25
(b) Hello7
(c)1
26. What is the output of the following?
char c= 'A;
short m 26;
int n = C + m;
System.out.printin(n); Ans: 65 + 26
91
27. . Given the following code:
String rules="knowledge is less important than Imagination";
String rules1,rules2;
rules1=rules.replace("less""more");
System.out.println(rules1);
rules2=rules1.replace("lmagination"knowledge");
System.out.println(rules2);
What will be the output of the above code?
Ans: knowledge is more important than
Imagination
knowledge is more important than
knowledge
28. . What do the following functions return for:
String x = "Guten
String y = "Morgan";
(a) System.out.printin(x.equals(y));
(b) System.out.println(y.indexOf(g));
(c) System.out.println(x.compareTo(y));
Ans: (a) false
(b) 3
(c)-6
29. If String x = "Tora";
String y = "Tora";
String z = "Flora";
What are returned by the following functions?
(a) System.out.println(x+y+z);
(6) System.out.printin(z.length());
(c) System.out.printin(x.equals(y));
(d) System.out.println((x+z+y).toUpperCase() );
Ans: (a) ToraToraFlora
(b) 5
(c) true
(d) TORAFLORATORA
30. What do the following functions return?
String a = "Brave";
String b = "New";
String c = "World";
(a)System.out.println(a + b + c) ;
(b) System.out.printin((a + b +c).length());
(c) System.out.printin((a + b+ c).lastindexof(r);
Ans: (a) BraveNewWorld
(b) 13
(c) 10
31. If String s = "cheers", then what does the following functions returns?
(a) s.startsWith('Hi")
(b) s.indexof(‘h’);
(c) s.lastindexof('e’);
(d) s.substring(1,3);
Ans: (a) false
(b) 1
(c) 3
(d) he

You might also like