Page 280 - IT-802_class_12
P. 280
Write a command to create a constructor of the above class that initialized the data members of a class.
Ans. public class stud{
// data members
..
//constructor
Stud( String rno, String sname, String address,double m){
Rno = rno;
Sname = sname;
Address = address;
marks = m;
}
Void display () {
...
}
}
31. Write the method in Java to accept a string as parameter from the user and print whether string contains the characters ‘old’
or not. [2022]
Ans. void checkContainsOld (String str){
if(str.contains (‘old’)){
System.out.println(str + “contains old”);
}
else{
System.out.println(str + “does not contains old”);
}
}
32. Consider the following string variable: [2022]
String str1=”Hello”;
String str2= “morning”;
Write the code statements in Java:
(a) To convert the letter of a string variable ‘str1’ into uppercase.
(b) To display combined length of string str1 and str2.
(c) To display the index value of the first occurrence of letter ‘e’ in str1.
(d) To display str1 and str2 after joining them.
Ans. (a) str1 = str1.replace(Character.toString(str1.charAt(0))),
Character.toString(str1.charAt(0)).toUpperCase());
System.out.println(str1);
(b) System.out.println(str1.concat(str2).length());
(c) System.out.println(str1.indexOf(‘e’));
(d) str3 = str1.concat(str2)
System.out.println(str3);
278 Touchpad Information Technology-XII

