II.
Write down the syntax to perform the following tasks:
1. char chr=’s’;
boolean p=Character.isUpperCase(chr);
2. String str1=”COMPUTER”;
String str2=”SCIENCE”;
boolean p=str1.equals(str2);
3. String wd=”COMPUTER”;
int l=wd.length();
char chr=wd.charAt(l-1);
4. String s=”applications”;
int n=s.indexOf(‘a’);
5. String st=”old is always old”;
String st1=st.replace(”old”,”new”);
6. String str=”COMPUTER”;
boolean b=Character.is UpperCase(str.charAt(1));
III. Predict the output of the following:
1. String str=”Computer Aplications10”;
System.out.println(“Understanding” + “Computer Applications10”);
Therefore, Outpit will be: UnderstandingComputer Applications10
2. System.out.println(name + “is” + age + “years old”);
Therefore, Output will be: Mr. Guptais45years old
3. int total = Integer.pasrseInt(“46”) + Integer.parseInt(“64”);
int total = 46 + 64;
int total = 110;
System.out.println(“The sum of ” + “46” + “and” + “ 64” + “is” +total);
Therefore, Output will be: The sum of 46and 64is110
4. boolean p=(“BLUEJ”.length( ) > “bluej”.length( )) ? true : false;
variable = test expression? Expression1: Expression2;
test expression i.e. “BLUEJ”.length( ) > “bluej”.length( ) is false
Hence, expression2 i.e. false will be stored in variable p. Therefore, p=false.
5. int p = “Information Technology”.indexOf(‘n’);
Since, indexOf( ) retruns the position of the first occurrence of ‘n’ in the String p. Hence, 1 will
be stored in variable p i.e. p=1;
Therefore, Output will be: 1
6. char ch =”ICSE”.charAt(0);
Since, charAt(0) returns the character of the String “ICSE” at 0th position i.e. I. Hence, I will be
stored in variable ch i.e. ch=’I’; and p=10;
Therefore, Output will be: I:10
7. boolean p = “Information Technology”.equalsIgnoreCase(information technology);
Since, equalsIgnoreCase( ) compares both the strings after ignoring the case of the strings.
Hence, both strings are equal and true will be stored in variable p i.e. p=true.
Therefore, Output will be: The result istrue
8. int n = ‘A’ + 26;
Since, the ASCII value of ‘A’ is 65. Hence, n = 65 + 26. Hence, 91 will be stored in n i.e. n=91.
Therefore, Output will be: 91
9. a. System.out.println(“Hello”+World”);
Therefore, Output will be: HelloWorld
b. “Hello”.length(); will return number characters present in string “Hello”. Hence, Output will
be: 5
c. “Hello”.charAt(3); will return character present at 3rd index i.e. ‘I’. Hence, Output will be: I
d. “Hello”.equals(“World”); Since both the string “Hello” and “World” are unequal. Hence,
equals( ) will return false. Therefore, Output will be: false
10. “abc””.toUpperCase(); will retrun string in upper case. Hence, Output will be: ABC
11. four :42
four :4
12. HiequalsHi true
Hiequalsthere false
HiequalsHI false
HiequalsIgnoreCaseHI true
13. i) “computer”.charAt(3); will return character present at 3rd index i.e. p. Hence, p will be stored
in variable ch. Therefore, Output will be: p
ii) “Rajesh”.replace(‘j’ , ‘k’); will replace ‘j’ with ‘k’. Hence, Output will be: Rakesh
14. i) “Computer”.substring(1,5); will return a string string from Ist index excluding 5th index
Hence, Output will be: ompu
ii) “Computer”.indexOf(“Computer”.charAt(4));
“Computer”.charAt(4); will return character at 4th index i.e. ‘u’
Hence, “Computer”.intexOf(‘u’); and indexOf(‘u’); will return index of ‘u’ i.e. 4
Therefore, Output will be: 4
iii) “Applications” + “Computer”.substring(5);
“Computer”.substring(5); will return a string starting from 5th index i.e. “ter”
Hence “Applications” + “ter”; will be “Applicationster
Therefore, Output will be: Applicationster
iv) “Computer”.equals(“Applications”); Since both the strings are unequal. Hence, equals( )
method will return false. Therefore, Output will be: false
IV. Differentiate the following:
1.
equals ( ) = =
1. equals ( ) is String function. 1. = = is a relational operator.
2. It is used to check equality of objects. 2. It is used to compare primitives as well as
objects.
3. It returns true or false based on its 3. It returns true only when both references
overridden implementation. points to same objects.
2.
concatenation truncation
1. concatenation means to combine 1. truncation means to shorten or reduce
2. concat( ) and ‘+’ operator can be used 2. String can be truncated using delete ( ),
for concatenating to Strings substring ( ) and setLength methods
3.
compareTo( ) equals( )
1. compareTo( ) method not only checks the 1. equals( ) method only checks whether
equality of the strings but also check strings are identical or not.
whether a string is bigger or smaller than
the other or not.
2. compareTo( ) method returns an integer 2. equals( ) method returns a boolean type
type of value. of value.
4.
toLowerCase( ) toUpperCase( )
1. toLowerCase( ) method returns a string 1. toUpperCase( ) method returns a string
after converting all the characters in after converting all the characters in
lower case letters. upper case letters.
5.
charAt( ) substring( )
1. charAt( ) method returns a character from 1. substring ( ) method returns all the
the given index of the string. characters from string, starting from
given index.
2. charAt( ) method returns a character type 2. substring( ) method returns a string type
of value. of value.
6.
Character.isLowerCase( ) Character.toLowerCase( )
1. Character.isLowerCase( ) method returns 1. Character.toLowerCase( ) method returns
true if the given argument is lower case. a character in lower case