strings :: exercises

A.  Given the following local variable declarations:

      	String a = "abc";
      	String s = a;
      	String t;

What is the value of the following expressions (or ERROR)?

1.	__________ 	s.length() 
2.	__________ 	t.length() 
3.	__________ 	1 + a 
4.	__________ 	a.toUpperCase() 
5.	__________ 	"Tomorrow".indexOf("r") 
6.	__________ 	"Tomorrow".lastIndexOf('o') 
7.	__________ 	"Tomorrow".substring(2,4) 
8.	__________ 	(a.length() + a).startsWith("a") 
9.	__________ 	s == a 
10.	__________ 	a.substring(1,3).equals("bc") 


B. Assume the following:

	String s, t, h, a;
	String n, e;
	int i;
	h = "Hello";
	s = "  How are you?  ";
	a = "abc";
	n = null;
	e = "";

What is the value of the following expressions (or ERROR)?


1.	__________	h.length()
2.	__________	h.substring(1)
3.	__________	h.toUpperCase()
4.	__________	h.toUpperCase().toLowerCase()
5.	__________	h.indexOf("H")
6.	__________	h.startsWith("ell")
7.	__________	"Tomorrow".indexOf("o")
8.	__________	"Tomorrow".indexOf("o", 3)
9.	__________	"Tomorrow".lastIndexOf('o')
10.	__________	"Tomorrow".substring(2,4)
11.	__________	a.length() + a
12.	__________	"a = \"" + a + "\""
13.	__________	(a.length() + a).startsWith("a")
14.	__________	a.length() + a.startsWith("a")
15.	__________	">>>" + a.length() + a.startsWith("a")
16.	__________	a.substring(1,3).equals("bc")
17.	__________	a.substring(1,3) == "bc"
18.	__________	"a".compareTo("c")
19.	__________	"a".compareTo("A")
20.	__________	s.trim().charAt(2)

To check answers click here