CH-6 More About Classes & Libraries PDF
CH-6 More About Classes & Libraries PDF
1
13 Write the code to display a IP 12 CBSE in a dialog box (JOPtiopnPane).
Ans. JOptionPane.showMessageDialog(null, "IP 12 CBSE");
14 Write the code to display a dialogbox (JOptionPane) which asks the user his/her name. Store the
returned value in a new string variable name and then display the first letter of the name.
Ans. String name;
name=JOptionPane.showInputDialog("enter your name");
char name2;
name2=name.charAt(0);
JOptionPane.showMessageDialog(null, name2);
Type A: Short/Long Answer Questions
1. Why is a StringBuffer class considered more flexible than String class?
Ans. stringBuffer class considered more flexible than string class because String class creates the strings of
fixed length whereas stringBuffer class creates the strings of flexible length where we can modify the
contents of a string both content and size.
2. Name some methods which are members of StringBuffer class but not in String class.
Ans. 1. append(x) method
2. insert(offset, x) method
3. delete(beg, end) method
4. reverse() method
5. setLength(n) method
3. Which library contains contains methods for common mathematical operations i.e., abs, sin, exp,
round, etc. ? How do you include this library in your program? (write statement)
Ans. Math library contains methods for common mathematical operations.
import java.lang.Math
4. What will be the output of the following code?
Math.round(1.5): 2
Math.round(-1.5): -1
Ans 2
-1
5. What is the output of the following code fragment ?
String s="Informatics practices !";
String t=null;
System.out.println(s.substring(3,17));
s=t;
System.out.println(s.charAt(1));
Ans. ormatics pract
Exception in thread "main" java.lang.NullPointerException //trying to copy null to variable.
6. What is the output of the following code fragment?
"Welcome".toLowerCase();
"Welcome".toupperCase();
"Welcome".trim();
"Welcome".replace('e', 'A');
"Welcome".replaceFirst("e", "AB");
"Welcome".replace("e", "AB");
"Welcome".replace("el","AB");
2
Ans. WELCOME
Welcome
WAlcomA
WABlcome
WABlcomAB
WABcome
7. What will be the output produced by the following code?
class StringClass
{
public static void main(String[] args){
//create a string
String myString="Hello World";
System.out.println(myString);
//copies substring in myString2
String myString2=myString.substring(4,8);
System.out.println(myString2);
//concatenates myString and myString2
String myString3=myString+myString2;
System.out.println(myString3.substring(3,15));
//Compares strings
System.out.println(myString.compareTo(myString2));
//checks if the string are equal, '==' will not work
System.out.println(myString.equals("Hello world"));
System.out.println(myString.indexOf(myString2));
//+ can work as string concatenation or addition
System.out.println(myString.length()+" "+ (myString2.length() + myString3.length()) +
myString.length());
//UPper case strings
System.out.println(myString.toUpperCase());
}
}
Ans. Hello World
o Wo
lo Worldo Wo
-39
false
4
11 1911
HELLO WORLD