Page 449 - CA_Blue( J )_Class10
P. 449
{
System.out.print(ch + " ");
}
}
}
public static void main()
{
Overload_string ob= new Overload_string();
ob.check("India is my country",'i');
ob.check("Republic Day");
} }
17. State the difference between == operator and equals() method. [2016]
Ans. The == operator compares two numeric or character values for equality.
The equals() method compares the contents of two String values for equality.
18. Give the output of the following string functions:
(i) "MISSISSIPPI".indexOf('S') + "MISSISSIPPI".lastIndexOf('I')
(ii) "CABLE".compareTo("CADET") [2016]
Ans. (i) 2 + 10 = 12 (ii) 66 - 68 = -2
19. Write the return type of the following library functions:
(i) isLetterOrDigit(char) (ii) replace(char, char) [2016]
Ans. (i) boolean (ii) String
20. Using the switch statement, write a menu-driven program for the following: [2016]
(i) To print the Floyd’s triangle given below:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(ii) To display the following pattern:
I
IC
ICS
ICSE
For an incorrect option, an appropriate error message should be displayed.
Ans. import java.util.*;
class switchcase
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int ch,k=1,i,j;
String str="ICSE";
System.out.println("Enter 1. Floyd's Triangle");
System.out.println("Enter 2. ICSE");
System.out.print("Enter your choice: ");
ch = sc.nextInt();
switch(ch)
{
case 1:
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= i; j++)
{
447
String Handling 447

