{"id":140,"date":"2020-04-03T09:00:00","date_gmt":"2020-04-03T09:00:00","guid":{"rendered":"https:\/\/temp.ashkerala.com\/?p=140"},"modified":"2020-04-03T09:00:00","modified_gmt":"2020-04-03T09:00:00","slug":"java-string-basics","status":"publish","type":"post","link":"https:\/\/temp.ashkerala.com\/?p=140","title":{"rendered":"Java String Basics"},"content":{"rendered":"<p><strong>String Handling<\/strong><\/p>\n<p> \u00a0\u00a0\u00a0 1) Which package does define String and StringBuffer classes?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : java.lang package.<\/p>\n<p> \u00a0\u00a0\u00a0 2) Which method can be used to obtain the length of the String?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : length( ) method.<\/p>\n<p> \u00a0\u00a0\u00a0 3) How do you concatenate Strings?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : By using &#8221; + &#8221; operator.<\/p>\n<p> \u00a0\u00a0\u00a0 4) Which method can be used to compare two strings for equality?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : equals( ) method.<\/p>\n<p> \u00a0\u00a0\u00a0 5) Which method can be used to perform a comparison between strings that ignores case differences?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : equalsIgnoreCase( ) method.<\/p>\n<p> \u00a0\u00a0\u00a0 6) What is the use of valueOf( ) method?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : valueOf( ) method converts data from its internal format into a human-readable form.<\/p>\n<p> \u00a0\u00a0\u00a0 7) What are the uses of toLowerCase( ) and toUpperCase( ) methods?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : The method toLowerCase( ) converts all the characters in a string from uppercase to<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lowercase.<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The method toUpperCase( ) converts all the characters in a string from lowercase to<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 uppercase.<\/p>\n<p> \u00a0\u00a0\u00a0 8) Which method can be used to find out the total allocated capacity of a StrinBuffer?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : capacity( ) method.<\/p>\n<p> \u00a0\u00a0\u00a0 9) Which method can be used to set the length of the buffer within a StringBuffer object?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : setLength( ).<\/p>\n<p> \u00a0\u00a0\u00a0 10) What is the difference between String and StringBuffer?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : String objects are constants, whereas StringBuffer objects are not.<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String class supports constant strings, whereas StringBuffer class supports growable, modifiable strings.<\/p>\n<p> \u00a0\u00a0\u00a0 11) What are wrapper classes?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : Wrapper classes are classes that allow primitive types to be accessed as objects.<\/p>\n<p> \u00a0\u00a0\u00a0 12) Which of the following is not a wrapper class?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Integer<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Boolean<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Character<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : a.<\/p>\n<p> \u00a0\u00a0\u00a0 13) What is the output of the following program?<\/p>\n<p> \u00a0\u00a0\u00a0 public class Question {<\/p>\n<p> \u00a0\u00a0\u00a0 public static void main(String args[]) {<\/p>\n<p> \u00a0\u00a0\u00a0 String s1 = &#8220;abc&#8221;;<\/p>\n<p> \u00a0\u00a0\u00a0 String s2 = &#8220;def&#8221;;<\/p>\n<p> \u00a0\u00a0\u00a0 String s3 = s1.concat(s2.toUpperCase( ) );<\/p>\n<p> \u00a0\u00a0\u00a0 System.out.println(s1+s2+s3);<\/p>\n<p> \u00a0\u00a0\u00a0 }<\/p>\n<p> \u00a0\u00a0\u00a0 }<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 abcdefabcdef<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 abcabcDEFDEF<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 abcdefabcDEF<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 None of the above<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ANS : c.<\/p>\n<p> \u00a0\u00a0\u00a0 14) Which of the following methods are methods of the String class?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delete( )<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 append( )<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 reverse( )<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 replace( )<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : d.<\/p>\n<p> \u00a0\u00a0\u00a0 15) Which of the following methods cause the String object referenced by s to be changed?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s.concat( )<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s.toUpperCase( )<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s.replace( )<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s.valueOf( )<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : a and b.<\/p>\n<p> \u00a0\u00a0\u00a0 16) String is a wrapper class?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 True<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 False<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : b.<\/p>\n<p> \u00a0\u00a0\u00a0 17) If you run the code below, what gets printed out?<\/p>\n<p> \u00a0\u00a0\u00a0 String s=new String(&#8220;Bicycle&#8221;);<\/p>\n<p> \u00a0\u00a0\u00a0 int iBegin=1;<\/p>\n<p> \u00a0\u00a0\u00a0 char iEnd=3;<\/p>\n<p> \u00a0\u00a0\u00a0 System.out.println(s.substring(iBegin,iEnd));<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Bic<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ic<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 icy<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error: no method matching substring(int,char)<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : b.<\/p>\n<p> \u00a0\u00a0\u00a0 18) Given the following declarations<\/p>\n<p> \u00a0\u00a0\u00a0 String s1=new String(&#8220;Hello&#8221;)<\/p>\n<p> \u00a0\u00a0\u00a0 String s2=new String(&#8220;there&#8221;);<\/p>\n<p> \u00a0\u00a0\u00a0 String s3=new String();<\/p>\n<p> \u00a0\u00a0\u00a0 Which of the following are legal operations?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s3=s1 + s2;<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s3=s1 &#8211; s2;<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s3=s1 &amp; s2<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s3=s1 &amp;&amp; s2<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : a.<\/p>\n<p> \u00a0\u00a0\u00a0 19) Which of the following statements are true?<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a)The String class is implemented as a char array, elements are addressed using the stringname[] convention<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 b) Strings are a primitive type in Java that overloads the + operator for concatenation<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 c) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d) The size of a string can be retrieved using the length property.<\/p>\n<p> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : b.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>String Handling \u00a0\u00a0\u00a0 1) Which package does define String and StringBuffer classes? \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : java.lang package. \u00a0\u00a0\u00a0 2) Which method can be used to obtain the length of the String? \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : length( ) method. \u00a0\u00a0\u00a0 3) How do you concatenate Strings? \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Ans : By using &#8221; + &#8221; operator. \u00a0\u00a0\u00a0 4)&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[12],"tags":[],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-java-programming"],"_links":{"self":[{"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=140"}],"version-history":[{"count":0,"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions"}],"wp:attachment":[{"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/temp.ashkerala.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}