Cracking Java String class questions
- Categories Java, Strings in Java, String
- Give the output of the following:
- System.out.println (“Simply”. charAt(1));
- String na = “Simply Coding”;
System.out.println( na . length( ) ); - System.out.println( “SiMPly”.indexOf(“M”));
- System.out.println(“SiMPly”.equalsIgnoreCase(“simply”));
Ans.
- i
- 13
- 2
- True
- What is the output of the following program?
public class check
{
public static void change(String nm)
{
nm = “Simply”;
}
public static void main()
{
String name = “Coding”;
System.out.println(name);
change(name);
System.out.println(name);
}
}
Ans
Coding
Coding
- What is the output of the following statements when executed?
public class Mathtest
{
public static void change(char [] nm)
{
nm[1] = 'S';
}
public static void main (String args[])
{
char name[] = {'a', 'b'};
System.out.println(name[1]);
change(name);
System.out.println(name[1]);
}
}
Ans
b
S
- What is the output of the following statements when executed?
char ch[ ]= {‘I’ 'N', ‘T', ‘E', 'L', ‘P', ‘E’, ‘N', ‘T’, ‘I’, ‘U', 'M'};
String obj= new String(ch, 3, 4);
System.out.println("The result is = "+ obj );
Ans.:
The result is = ELPE
- State the output of this program Segment?
String str1 = ”Simply”;
String str2= “Coding”;
System.out.println(str1.substring(0,2). concat(str2.substring(1)));
System.out.println(( “WH”+(str1.substring(2).toUpperCase() )));
Ans.
The 1st output is= Sioding
( because str1. Substring(0,2)gives “Si” & str2. Substring(1) gives “oding”)
The 2nd output is= WHMPLY
( because str1. Substring(2). toUpperCase() gives “MPLY”, so ”WH”+” MPLY”= “WHMPLY”)
- If String n1 = “89”; and String n2=”100”; Give the output of the following:
- String st= n1+n2; System.out.println (st) ;
- System.out.println( n1.charAt( 1 ));
- System.out.println( n2 + n1);
- System.out.println(n1+ “ ”+n2);
Ans.
- 89100
- 9
- 10089
- 89 100
- Give the output of following program.
class MainString{
public static void main(String args [ ])
{
StringBuffer s = new Stringbuffer(“Simply”);
if( (s.length()>5) && (s.append(“Coding”). equals(“X”)) ); //empty statement
System.out.println(s);
}
}
Ans.
SimplyCoding
- What is the output of this code
String st = " SimplyCoding";
int len= st.length();
for(int j= len-1; j>= 0 ; j-- )
System.out.print( st.charAt(j) );
Ans. gnidoCylpmiS
- Assume a string “Delhi” is assigned in a variable st and “Singapore” is assigned in variable na. Write a small code to interchange values of st and na.
Ans.
String temp= st; st= na; па= temp;
- If String x = “SimplyCoding”;
String y= “Programs”;
What do the following functions return for:
- System.out.printin( x. substring( 1, 7));
- System.out.println (x. indexOf( x.charAt( 6)));
- System.out.println (y +x. substring( 5));
- System.out.println( x.equals( y));
Ans.
- implyCo ( extracts substring from st to 6th index, 1 means 1st index and 6 means 6th index as array starts with 0)
- 6
- ProgramsyCoding
- False
- Write valid java statements to perform the following operations on strings.
- Extract 10 characters from the 1st character to 10th character in string address to a new string str.
- To print the position of the first occurrence of the letter ‘R’ in the string city
Ans.
- String str = address.substring( 0, 10 );
- System.out.println( “The position =” + city. indexOf (‘R’));
- What will be the output of the following program, when Method invoke() is called:
public class StringArray
{
void change(String arr[])
{
for(int i=0;i<arr.length;i++)
{
arr[i]=arr[i].substring(0,1).toUpperCase()+arr[i].substring(1);
}
}
void invoke()
{
String ar[]={“simply”,“coding”,“programs”};
for(int i=0;i<ar.length;i++)
System.out.println(ar[i]);
change(ar);
for(int i=0;i<ar.length;i++)
System.out.println(ar[i]);
}
}
Ans.
simply
coding
programs
Simply
Coding
Programs
- Give the output of the following program fragment:
String s=new String("Hello! this is simply coding");
String t;
t=s.replace("this is","Welcome to");
System.out.println(t);
Ans.
Hello! Welcome to simply coding
- Give the output of the following program fragment:
String s="india",s1="SimPLY",s2=s;
System.out.println(s.equals(s1));
System.out.println(s.equalsIgnoreCase(s1));
System.out.println(s2==s);
System.out.println(s.toUpperCase()==s1.toUpperCase());
System.out.println(s.startsWith("IM".toLowerCase()));
System.out.println(s1.endsWith("iA".toUpperCase()));
Ans.
false
false
true
false
false
false
- What do the following functions return for:
String x ="Simply";
String y ="Coding";
System.out.println(x + y);
System.out.println(x.length());
System.out.println(x.charAt(3));
System.out.println(x.equals(y));
Ans.
SimplyCoding
6
p
false
- What is the output of the following:
(i) System.out.println ("output1 :" + 6 + 4);
System.out.println ("output2 :"+(6+4));
(ii) String S1 = "Hi";
String S2 = "Hi";
String S3 = "Simply";
String S4 = "HI";
System.out.println(S1 + "equals" + S2 + "→" + S1.equals(S2));
System.out.println(S1 + "equals" + S3 + "→" + S1.equals(S3));
System.out.println(S1 + "equals" + S4 + "→" + S1.equals(S4));
System.out.println(S1 + "equalsIgnoreCase" +S4 + "→" + S1.equalsIgnoreCase(S4));
Ans.
(i) output1 :64
output2 :10
(ii) HiequalsHi→true
HiequalsSimply→false
HiequalsHI→false
HiequalsIgnoreCaseHI→true
- If, String x = “Simply”;
String y = “Coding”;
- System.out.println(x.substring(1,4));
- System.out.println(x.indexOf(x.charAt(4)));
- System.out.println(y+x.substring(5));
- System.out.println(x.equals(y));
Ans.
- imp
- 4
- Codingy
- False
- Write a statement each to perform the following task on a string:
(i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type
Ans.
(i) System.out.println(s.lastIndexOf(“ ”));
(ii) double d=Double.parseDouble(x);
- Write a statement each to perform the following task on a string:
(i) Extract the second last character of a word stored in the variable wd.
(ii) Check if the second character of a string str is in uppercase.
Ans.
(i) char sl=wd.charAt(wd.length()-2);
(ii) if(Character.isUpperCase(str.charAt(1)))
- Give the output of the following string functions:
- “SimplyCoding”.replace(‘i’,’A’)
- “Eating”.compareTo(“Coding”)
Ans.
- SAmplyCodAng
- 2
- Consider the following String array and give the output:
String arr[]={"Java","PHP","Python",".Net","HTML"};
System.out.println(arr[0].length()>arr[3].length());
System.out.print(arr[2].substring(0,3));
Ans.
false
Pyt
- String x[]={“Simply”,”Coding”,”Online”,”Tutor”,”Courses”};
Give the output of the following statements:
(i) System.out.println(x[1]);
(ii) System.out.println(x[3].length());
Ans.
(i) Coding
(ii) 5
- Write the output for the following:
String s="Simply Coding";
System.out.println(s.indexOf('T'));
System.out.println("Welcome to"+" "+s.substring(0,10));
Ans.
-1
Welcome to Simply Cod
- Write a statement each to perform the following task on a string:
String s1 = “good”, s2 = “going”;
1 . Write coed to concat them into string s.
2 . Write coed to replace ‘g’ with ‘f’ in string s
Ans. s = s1.concat(s2)
s = s.replace(‘g’, f)
- Give the ouput of the following string functions:
(i) “SimplyCoding”.indexOf(‘l’)+”SimplyCoding”.lastIndexOf(‘C’)
(ii) “Simply”.compareTo(“SimplyCoding”)
Ans.
(i) 10
(ii) -6
- State the output of the following program segment when executed:
String a = "Simply", b = "Coding";
String h = a.substring(1, 3);
String k = b.substring(2).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
Ans.
im
false
- State the output of the following program segment:
String str1 = "Simply", str2 = "Coding";
System.out.println(str1.substring(0,2).concat(str2.substring(1)));
System.out.println(("HL" + (str1.substring(2).toUpperCase())));
Ans.
Sioding
HLMPLY
- State the value of characteristic and mantissa when the following code is executed.
String s = "6.2708";
int n = s.indexOf('.');
int characteristic = Integer.parseInt(s.substring(0,n));
int mantissa = Integer.valueOf(s.substring(n+1));
Ans.
characteristic=6
mantissa= 2708
- State the output of the following program segment.
String s = "SimplyCoding";
int n = s.length();
System.out.println(s.startsWith(s.substring(4, n)));
System.out.println(s.charAt(1) == s.charAt(9));
Ans.
false
true
- What will the following code output?
String s = "SimplyCoding";
System.out.println(s.indexOf('i'));
System.out.println(s.lastIndexOf('i'));
Ans.
1
9
- Give the output of the following:
String str1 = "replace";
str1.toUpperCase();
System.out.println(str1);
Ans. replace
- Give the output of the following:
String s1 = "Simply";
String s2 = "Coding";
System.out.println(s1.substring (0,3).concat(s2.substring(4)));
System.out.println(s1.endsWith("e"));
Ans.
Simng
False