Page 1 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, , ASSIGNMENT 4, String Handling, Fill in the blanks, Question 1, ……………… method is used to join two strings., Question 2, The output of the following statement, when executed:, System.out.println("COMPUTER".charAt(4)); is ……………., Question 3, The output of the statement, when executed:, System.out.println("Object Oriented".length()); is ……………, Question 4, ……….. operator is used to concatenate two strings., Question 5, String declaration is terminated by the symbol ;………….., Question 6, Character literal is enclosed within …………….., Question 7, The return type of the statement equals( ) is …………., Question 8, The output of "VIDYALAYA."substring(2,5) will result in …………., Write down the syntax to perform the following tasks, Question 1, To check whether a character(chr) is in upper case or not., Question 2, To compare two Strings(str1, str2) are same or not.
Page 2 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, Question 3, To extract the last character of a word(wd) stored in the variable chr., Question 4, To return the first occurrence of 'a' in the word "applications"., Question 5, To replace the word "old" with the word "new" in a given, String st = "old is always old", Question 6, To check if the second character of a String(str) is in upper case., , Predict the output of the following, Question 1, String str = "Computer Applications" + 1 + 0;, System.out.println("Understanding" + str);, Question 2, String n1 = "46", n2 = "64";, int total = Integer.parseInt(n1) + Integer.parseInt(n2);, System.out.println("The sum of " + "46 " + "and" + " 64" + " is " + total);, Question 3, boolean p;, p = ("BLUEJ".length() > "bluej".length()) ? true: false;, Question 4, String str = "Information Technology";, int p;, p = str.indexOf('n');, System.out.println(p);, Question 5, String str1 = "Information Technology";, String str2 = "information technology";, boolean p = str1.equalsIgnoreCase(str2);, System.out.println("The result is " + p);
Page 4 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, System.out.println(n.substring(0,8).concat(m.substring(9)));, System.out.println(n.endsWith("e"));, Question 10, Give the output of the following statements:, String x[] = {"SAMSUNG", "NOKIA", "SONY", "MICROMAX", "BLACKBERRY"};, (i) System.out.println(x[1]);, Question 11, Give the output of the following string functions:, (i) "ACHIEVEMENT".replace('E', 'A'), (ii) "DEDICATE".compareTo("DEVOTE"), Question 12, Consider the following String array and give the output, String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};, System.out.println(arr[0].length() > arr[3].length());, System.out.print(arr[4].substring(0,3));, Differentiate between the following, Question 1, equals() and ==, equals(), , ==, , It is a method, , It is a relational operator, , It is used to check if the, contents of two strings are, same or not, , It is used to check if two, variables refer to the same, object in memory, , Example:, String s1 = new, String("hello");, String s2 = new, String("hello");, boolean res = s1.equals(s2);, System.out.println(res);, , Example:, String s1 = new, String("hello");, String s2 = new, String("hello");, boolean res = s1 == s2;, System.out.println(res);, , The output of this code, snippet is true as contents, , The output of this code, snippet is false as s1 and s2
Page 5 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, equals(), of s1 and s2 are the same., , ==, point to different String, objects., , Question 2, compareTo() and equals(), compareTo(), , equals(), , It compares two strings, lexicographically., , It checks if, contents of two, strings are same, or not., , The result is a negative, positive or, zero integer value depending on, whether the String object precedes,, follows or is equal to the String, argument, , The result is true, if the contents are, same otherwise it, is false., , Question 3, toLowerCase() and toUpperCase(), toLowerCase(), , toUpperCase(), , Converts all characters of the String, object to lower case, , Converts all characters of the String, object to upper case, , Example:, String str = "HELLO";, System.out.println(str.toLowerCase());, , Example:, String str = "hello";, System.out.println(str.toUpperCase());, , Output of this code snippet will be, hello., , Output of this code snippet will be, HELLO., , Question 4, charAt() and substring(), charAt(), It returns a character, , substring(), It extracts a part of the string
Page 6 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, charAt(), , substring(), , from the string at the, index specified as its, argument, , as specified by its arguments, and returns the extracted part, as a new string., , Its return type is char, , Its return type is String, , Example:, String str = "Hello";, char ch = str.charAt(1);, System.out.println(ch);, , Example:, String str = "Hello";, String subStr =, str.substring(1);, System.out.println(subStr);, , Output of this code, snippet is e., , Output of this code snippet is, ello., , Write short answers, Question 1, State the purpose and return data type of the following String functions:, (a) indexOf(), It returns the index within the string of the first occurrence of the specified, character or -1 if the character is not present. Its return type is int., (b) compareTo(), It compares two strings lexicographically. Its return type is int., Question 2, Write a statement for each to perform the following task on a string:, (i) Extract the second last character of a word stored in the variable wd., char ch = wd.charAt(wd.length() - 2);, (ii) Check if the second character of a string str is in upper case., boolean res = Character.isUpperCase(str.charAt(1));, Question 4, 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., System.out.println(s.lastIndexOf(' '));
Page 7 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, (ii) Convert a number stored in a string variable x to double data type., double a = Double.parseDouble(x);, Question 5, How does endsWith() and startsWith() differ? Explain with an example., endsWith() tests if the string object ends with the string specified as its, argument. startsWith() tests if the string object starts with the string specified, as its argument. Consider the below example:, public class Example, {, public static void main( ), {, String str = "ICSE Computer Applications";, System.out.println("Does " + str + " starts with ICSE? " + str.startsWith("ICSE"));, System.out.println("Does " + str + " ends with tions? " + str.endsWith("tions"));, }, }, Here, both str.startsWith("ICSE") and str.endsWith("tions") returns true as str, starts with "ICSE" and ends with "tions"., Describe the purpose of the following functions with their syntax, Question 1, toUpperCase(), It converts a string into upper case characters. If any character is already in, uppercase or is a special character then it will remain same., Syntax:, String <variable-name> = <string-variable>.toUpperCase();, Question 2, trim(), It removes all leading and trailing space from the string., Syntax:, String <variable-name> = <string-variable>.trim();, Question 3, toLowerCase()
Page 8 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, It converts a string into lowercase characters. If any character is already in, lowercase or is a special character then it will remain same., Syntax:, String <variable-name> = <string-variable>.toLowerCase();, Question 4, length(), It returns the length of the string i.e. the number of characters present in the, string., Syntax:, int <variable-name> = <string-variable>.length();, Question 5, replace(), It replaces a character with another character or a substring with another, substring at all its occurrences in the given string., Syntax:, String <variable-name> = <string-variable>.replace(<character or substring to, replace>, <new character or substring>);, Question 6, compareTo(), It compares two strings lexicographically., Syntax:, int <variable-name> = <string-variable>.compareTo(<string-variable2>);, Question 7, reverse(), It is a method of StringBuffer class used to reverse the sequence of characters., Syntax:, <StringBuffer-Variable>.reverse();, Question 8, indexOf()
Page 9 :
VXL TUTORIAL COMPUTER CLASSES PH: 9336061496, It returns the index within the string of the first occurrence of the specified, character or -1 if the character is not present., Syntax:, int <variable-name> = <string-variable>.indexOf(<character>);, Question 9, startWith(), It tests if the string object starts with the string specified as its argument., Syntax:, boolean <variable-name> = <string-variable>.startWith(<string>);, Question 10, equalsIgnoreCase(), It ignores the case of the characters and checks if the contents of two strings are, same or not., Syntax:, boolean <variable-name> = <string-variable>.equalsIgnoreCase(<string>);