Name: Maximum marks: 40
Q1. Write the Java code to retrieve the index of the first occurrence of the character 'a' in the string str. [1]
Ans:
Q2. Which method is used to remove leading and trailing whitespace from a string in Java? [1]
Ans:
Q3.Write a statement to convert the string “34” to numeric value. [1]
Ans:
Q4. State the return type of the following functions: startsWith(), compareTo() [1]
Ans:
Q5.Write a statement to extract the second last character of a word stored in the variable wd. [1]
Ans:
Q6. Write a statement to check if last character of a string st is in uppercase. [1]
Ans:
Q7. Find the value of Double.parseDouble("2.25") +Integer.parseInt(“45”) Ans: [1]
Q8. State the values stored in the variable str2. [2]
String s1=”good”, s2=”world matters”;
String str1=s2.substring(5).replace(‘t’, ‘n’); String str2=s1.concat(str1);
Ans:
Q9. Find the output : System.out.println(“COMPARE”.compareTo(“COMMUNICATION”); [2]
Q10. Find the output: String s1= “APPLICATION”, s2= “PROGRAMMING”; [2]
System.out.println( s1.substring(5).concat(s2.substring(s1.indexOf(‘P’),s1.lastIndexOf(‘I’))));
Ans:
Q11. Find the output : String a= “TOmORroW”, b= “yEsteRDAY”; [2]
System.out.println( Character.isUpperCase(b.charAt(a.indexOf(‘r’)))); Ans:
System.out.println( Character.toUpperCase(b.charAt(a.lastIndexOf(‘R’)))); Ans:
Q12. Find the output: String st=“Indiana”; [2]
System.out.println(st.substring(st.indexOf(‘i’),st.lastIndexOf(‘n’))); Ans:
Q13. Find the value of st: String s= “Examination”; [2]
String st=s.substring(Integer.parseInt(“5”), s.lastIndexOf(‘n’)); Ans:
Q14. Find the output: char ch= ‘b’; String st= “b”; System.out.println(st+Character.toUpperCase(ch)+ ‘a’); [2]
Ans:
Q15. Find the output: String s= “TODAY”; int n=s.length(); System.out.println(s.startsWith(s.substring(2,n))); [2]
Ans:
Q16. Find the output: String s1= “Hello” , s2= “hi”; System.out.println(s1.compareToIgnoreCase(s2)); [2]
Ans:
Q17. Find the value of c: c = "COMPUTER".charAt("COMPUTER".indexOf('P')) Ans: [2]
Q18. Find the output: String n = "Computer Knowledge"; String m = "Computer Applications"; [2]
System.out.println(n.substring(0,8).concat(m.substring(9))); System.out.println(n.endsWith("e"));
Ans:
Q19. Find the output: String s1= “today”, s2= “sunday”;
System.out.println(s1.charAt(2)+s2.charAt(s1.indexOf(‘a’))+s2.substring(3)); Ans: [2]
Q20. Find the output: String x= “happy”, y= “holiday”; [2]
System.out.println(x.substring(0,1)+y.charAt(5)+x.charAt(4)); Ans:
Q21. Write a statement each to perform the following task on a string [2]
• Find and display the position of the last space in a string ST
• Convert a number stored in a string variable X to double data type
Q22. What will be the output of the following code? [2]
String n= “Republic Day” ; String m= “Independence Day”;
System.out.println (n . substring (0,5) . concat (m. substring (n.indexOf( ‘i’))));
Ans:
Q23. String x=”COMPUTER” ; String y=”APPLICATIONS”; [2]
System.out.println (x. substring (0, y.indexOf(‘I’)). concat (y. substring (x.indexOf(‘U’))));
System.out.println(y.charAt(5)+12);
Ans:
Q24. What is the use of trim() method? [1]
Ans: