0% found this document useful (0 votes)
2 views5 pages

CH 4 Strings

The document consists of a series of programming exercises and questions related to Java strings, including string manipulation, output predictions, error types, and method functionalities. It covers various string functions, their outputs, and comparisons, as well as the differences between operators and methods. Additionally, it addresses data types, class variables, and composite data types in Java.

Uploaded by

KNC BS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

CH 4 Strings

The document consists of a series of programming exercises and questions related to Java strings, including string manipulation, output predictions, error types, and method functionalities. It covers various string functions, their outputs, and comparisons, as well as the differences between operators and methods. Additionally, it addresses data types, class variables, and composite data types in Java.

Uploaded by

KNC BS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 4: Strings

1. String x[ ] = {“SAMSUNG”, “NOKIA”, “SONY”, “MICROMAX”,


“BLACKBERRY”}; [2]
Give the output of the following statements :
i). System.out.prindn(x[1]);
ii). System.out.println(x[3].length{ ));
2. Give the output of the following code : [2]
String P = “20”, Q = “19”,
int a = Integer .parselnt(P);
int b = Integer. valueOf(Q);
System.out.println(a+””+b);
What are the various types of errors in Java ?
3. Predict the output for the following:
String s1 = "phoenix"; String s2 ="island";
String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};

a. System.out.println(s1.substring(0).concat(s2.substring(2)));
b. System.out.println(s2.toUpperCase());
c. System.out.println ("DEDICATE".compareTo("DEVOTE"));
d. System.out.println(arr[0].length() > arr[3].length());
4. Give the output of the following:
String n=”Computer Knowledge”;
String m=”Computer Applications”;
System. out.println(n. substring(0,8). concat(m. substring(9)));
System.out.println(n.ends With(“e”));
5. State the purpose and return data type of the following String functions :
(i) indexOf().
(ii) compareTo().
6. Write the output of the following String methods:
a. "ARTIFICIAL ".indexOf('F' )
b. “ DOG and PUPPY ”. trim().length()
7.
8. Write the difference between length and length() functions.
9. System.out.print(“BEST”); |2]
System.out.println(“OF LUCK”);
10. Write the return data type of the following function.
(i) endsWith( )
(ii) log( )
11. State the difference between == operator and equals ( ) method.
12. What is the value stored in variable res given below :
double res = Math.pow (“345”.indexOf(‘5’), 3);

13. Predict the output of the


following code snippet:String
a="20";
String b="23";
int p=Integer.parseInt(a); int q=Integer.parseInt(b); System.out.print(a+"*"+b);
14. Write the output for the following: [2]
String s1 = “phoenix”; String s2 =”island”;
System.out.prindn (s1.substring(0).concat (s2.substring(2)));
System.out.println(s2.toUpperCase( ));
15. String x[ ] = {“Artificial intelligence”, “IOT”, “Machine learning”, “Big
data”}; [2]
Give the output of the following statements:
(i) System.out.prindn(x[3]);
(ii) System.out.prindn(x.length);
16. 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.
17. 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. [2]
(ii) Check if the second character of a string str is in uppercase.
18. Write the output for the following :
System.out.prindn(“Incredible” + “\n” + “world”);
19. Give the output of the following string functions :
(i) “ACHIEVEMENT” .replaceCE’, ‘A’)
(ii) “DEDICATE”. compareTo(“DEVOTE”)

20. 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));

21. Write the output for the following : [2]


String s= “Today is Test”;
System.out.println(s.indexOf(‘T’));
System.out.println(s.substring(0, 7) + ” ” + “Holiday”);

22. Give the output of the following code:


String A = “26”, B=”100″;
String D =A+B+”200″;
int x = Integer.parselnt(A);
int y = Integer.parselnt(B);
int d = x+y;
System.out.println(“Result 1 = ”+D);
System.out.prinln(“Result 2 = “+d); ,

23. Give the output of the following string functions: [2]


(i) “MISSISSIPPI”.indexOf(‘S’) + “MISSISSIPPF”.lastIndexOf(‘I’)
(ii) “CABLE”.compareTo(“CADET”)
24. State the output when the following program segment is executed:
String a =“Smartphone”, b=“Graphic Art”;
String h=a.substring(2, 5);
String k=b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
25. Name a string function which removes the blank spaces provided in the prefix
and suffix of a string
26. State the output of the following program segment:
String str1 = “great”; String str2 = “minds”;
System.out.println (str1.substring (0,2).concat(str2.substring (1)));
System.out.println ((“WH”+(str1.substring (2).toUpperCase())));
27. What is the data type returned by the library functions :
(i) compareTo()
(ii) equals()

28. State the value of characteristic and mantissa when the following code is
executed:
String s = “4.3756”;
int n = s.indexOf(‘.’);
int characteristic=Integer.parseInt (s.substring (0,n));
int mantissa=Integer.valueOf(s.substring(n+1));

29. What will be the output when the following code segments are executed ?
(i) String s = “1001”;
int x = Integer. valueOf(s);
double y = Double.valueOf(s);
System.out.println(“x=”+x);
System.out.println(“y=”+y);
System.out.println(“The king said\”Begin at the beginning!\“to me.”);

30. State the values stored in the variables str 1 and str 2
String s1 = “good”; Strings s2 = “world / matters”;
String str 1 = s2. substring (5). replace (‘t’, ‘n’);
String str 2 = s1. concat (str 1);
31. State the output of the following program segment :
String s=“Examination”;
int n=s.length();
System.out.println(s.starts With(s.substring(5,n)));
System.out.println(s.charAt(2)==s.charAt(6));

32. 93. State the method that:


(i) Converts a string to a primitive float data type.
(ii) Determines if the specified character is an uppercase character.
33. State the data type and values of a and b after the following segment is
executed.
String s1=“Computer”, s2=“Applications”;
a=(s1.compareTo(s2));
b=(s1.equals(s2));

34. What will the following code output ?


String s=“malayalam”;
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’));

35. If, String x = “Computer”;


String y = “Applications”;
What do the following functions return for :
(i) System.out.print(x.substring(1,5));
(ii) System.out.print(x.indexOf(x.charAt(4)));
(iii) System.out.print(y+x.substring(5));
(iv) System.out.print(x.equals(y));
36. . Write a statement for each of the following :
(i) Store a number 275 as a string.
(ii) Convert the string to a numeric value.
(iii) Add it to the existing total of 1000 to update the total.
37. What is the output of the following :
(i) System.out.println (“four:” + 4 + 2);
System.out.println (“four:” + (2+2)); [2]
(ii) String S1 = “Hi”;
String S2 = “Hi”;
String S3 = “there”;
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 + “EquallgnoreCase” + S4 + “→” +
S1.EqualsIgnoreCase(S4));
38. Write the output for the following:
String s1 = ''Life is Beautiful'';
System.out.println (''Earth'' + s1.substring(4));
System.out.println( s1.endsWith(''L'') );

39. Write the output of the following statement:


System.out.println(''A picture is worth \t \''A thousand words.\'' '');
40. What is the data type returned by the following library methods? [2]
(i) isWhitespace()
(ii) compareToIgnoreCase()
41. Write statements to show how finding the length of a character array char [ ]
differs from finding the length of a String object str.
42. Name the keyword that
(i) informs that an error has occurred in an input output operation.
(ii) distinguishes between instance variables and class variables.
43. The access specifier that gives the most accessibility is ………… and the least
accessibility is ………… .
44. List the variables from those given below that are composite data types: [2]
(i) static int x;
(ii) arr[i]=10;
(iii) obj.display();
(iv) boolean b;
(v) private char chr;
(vi) String str;
45. Consider the following class:
public class myClass {
public static int x=3, y=4;
public int q=2, b=3;}
(i) Name the variables for which each object of the class will have its own
distinct copy.
(ii) Name the variables that are common to all objects of the class.
46. State the method that:
(i) Converts a string to a primitive float data type.
(ii) Determines if the specified character is an uppercase character.

You might also like