Q1)
Fill in the missing codes
public class Question {
/** returns the number of occurences of char ch in the string */
public static int numOfChar(String string, char ch) {
// your codes here
}
/** returns a new string resulting from removing all occurences of char ch */
public static String remove(String string, char ch) {
// your codes here
}
/** returns a new string resulting from moving the first n characters to the end */
public static String move(String string, int n) {
// your codes here
}
/** returns the number represented by the string */
public static int stringToNumber(String string) {
// we assume the string consists of only digits
// your codes here
}
}
Q2)
Which of the following lines change the content of the String s.
a) s.toUpperCase();
b) s.toLowerCase();
c) s.charAt(5);
d) s.replace('a','b');
e)s.toCharArray();
Q3)
There are 2 Strings s1 and s2.
Suppose System.out.println(s1) and System.out.println(s2) have the same outputs.
What is the output of the following codes?
if(s1==s2) {
System.out.println("They are equal");
}
Q4)
Suppose s1, s2 are 2 Strings. What is the difference between
s1==s2 and s1.equals(s2).
Q5)
Suppose string is a String and array is an int[].
Which of the followings are correct?
a) string.length
b) string.length()
c) array.length
d) array.length()
Q5
What is the output of the following codes?
a)
public class StringTest {
public static void main(String[] args) {
String s = "Java is an object oriented language.";
System.out.println(s.substring(5));
System.out.println(s.substring(5,20));
System.out.println(s.charAt(5));
System.out.println(s.length());
System.out.println(s.charAt(12));
System.out.println(s.indexOf('a'));
System.out.println(s.lastIndexOf('a'));
System.out.println(s.indexOf('a', 10));
System.out.println(s.lastIndexOf('o', 15));
System.out.println(s.indexOf("an"));
System.out.println(s.indexOf("an",15));
System.out.println(s.lastIndexOf("an"));
System.out.println(s.lastIndexOf("an", 5));
System.out.println(s.substring(0,18).replace('a','o'));
System.out.println(s.startsWith("Java"));
System.out.println(s.endsWith("Java"));
}
}
Q6)
I have a String consists of integers seperated by commas. example
String s = "12,13,24,55,102,33,47,11,23,45,899,1,-5,8,23".
Write a program to add all the numbers.