|
String Test Study Guide All those things that you need to know before the Strings test in AP Computer Science. |
Because of this new rule, be sure you know how to use the BlueJ debugger!!!!! It will really help you prepare for this test. If you need a review check out the link in the Helpful Hints section of the course.
1) Know how all of the string functions work. Here's a link you should review:
http://www.tutorialspoint.com/java/java_strings.htm
For example how does this block of code work?
public static void main(String args[])
{
String joeString = "AP CompSci";
String temp;
int k = 1;
for ( int j = 1; j <= joeString.length(); j++)
{
temp = joeString.substring(k,j);
System.out.print(temp + "\n");
}
}
Here's the sample output.
P
P
P C
P Co
P Com
P Comp
P CompS
P CompSc
P CompSci
Why do I get what I get? Why was the "A" not printed? Why is "P" printed twice…or is it? Run it in the debugger so you see what's going on.
2) Know your integer arithmetic!!! We've tested it before and we'll test it again. Check out this code segment in the debugger….
int result;
int c = 13;
result = 13/3;
result = c/3;
Do you get the same result? Why or why not?
3) Be able to walk through the code, especially when you're using string functions like length and substring.
4) Know the important difference between the "==" and .equal() method. Check out the reference link here and run their example in the debugger and understand why you get what you get.
Reference Link: http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/
5) Know all there is to know about .compareTo method as well!! Test this code and be sure you know which ones return values of "True'" and which ones return "False”.
public static void main(String args[])
{
String strA = "TARHEELS";
String strB = "tarheels";
String strC = "tar";
boolean result;
result = strA.compareTo(strB)<0 && strB.compareTo(strC)<0;
result = strB.compareTo(strA)<0 || strC.compareTo(strA)<0;
result = !(strA.equals(strB) && strC.compareTo(strB)<0);
result = !(strA.equals(strB)) && strC.compareTo(strA)<0;
}
6) Make sure you understand how to use the indexof method. What would the value of 'str" be and why?
Example:
String line = "Some more silly stuff on Strings!";
int x = line.indexOf("m");
String str = line.substring(10,15) + line.substring(25,25+x);
Check out this code as well…..
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
int k = alpha.indexOf("H");
int j = alpha.indexOf("A");
int z = alpha.indexOf("a");
char xstr=alpha.charAt(12);
What's the values of xstr, k, j, and z? Be sure you understand why!
Now, write some test code using them in the substring method and be sure you understand how they work. Here's a good reference link: http://www.tutorialspoint.com/java/java_string_substring.htm
7) What does it mean to be "equal to" as opposed to "reference" the same string object?
For example: After executing the following block of code does s1 and s2 reference the same string object?
String s1="Mr. Zaengle";
String s2="Dr. Fisher";
s1=s2;
8) Try out the following blocks of code and see what you get. Are they correct expressions? If not, then why not? If they are, what do you get?
String s1="Mr. Zaengle"; String s2="Dr. Fisher"; String s3; s3 = s1 + s2;
|
String s1="Mr. Zaengle"; String s2="Dr. Fisher"; String s3; s3 = s1 – s2;
|
s1.charAT(3) = "B";
|
char c = s1.charAt(s1.length());
|
char c = s1.charAt(s1.length()-1);
|
9) Check out this link: http://www.tutorialspoint.com/java/java_string_comparetoignorecase.htm Be sure to try the example code in the debugger and make sure you completely understand what's happening.
10) Are these correct statements? If they are, then what are the resulting values?
String stringValue1 = String.valueOf(false);
String stringValue2 = String.valueOf(54567)
String stringValue3 = String.valueOf(54.567)
Another example, what would the value of stringValue3 be after this code executes?
String stringValue1 = String.valueOf(10);
String stringValue2 = String.valueOf(13);
String stringValue3 = stringValue1 + stringValue2;
Consider a string with the value s=“This is a Java String”.
What is the value of
1. s.charAt(3);
2. s.length();
3. s.charAt(s.length());
4. s.replace(‘s’,’?’);
5. s.replace(“is”,”IS”);
6. s.toLowerCase(s);
7. s.substring(3,6);
8. System.out.println(s.substring(1,8).substring(1,3).substring(0,2));
9. s.indexOf(“i”);
10. s.indexOf(‘s’,s.indexOf(‘J’));
11. s.indexOf("J",s.indexOf("h"))
12. "xyz".compareTo("xyx")
13. "XyX".compareToIgnoreCase("XyZ")
14. "xyz".equalsIgnoreCase("XyZ")
15. String t=s+" plus " ;
16. t.trim();
17. s=””;
18. char m[]=s.toCharArray();
19. m[5];
What code would you use to convert
20. an array of characters to a string?
21. a boolean into a string?
22. a double into a string?
23. an integer into a string
24. a string into an integer?
25. a string into a double?
Explain or Diagram Each of your responses below this table
No. |
Declaration/Statement(s) |
True/False |
s1 and s2 Reference the same string y/n |
26 |
String s1=new String("Hello World”); String s2=s1; s1.equals(s2)) |
||
27 |
String s1=new String(“Hello World”); String s2=new String(“Hello World”); s1.equals(s2);
|
||
28 |
s2.equals(s1); |
||
29 |
s1=s2; |
||
30 |
String s1=new String(“Hello World”); String s2=s1.toUpperCase(); s1==s2; |
||
31 |
String s1=new String(“Hello World”); String s2=s1 s1+= “and programming in Java”; s1=s2; |
||
32 |
String s1="Hello World"; String s2="Hello World"; s1=s2; |
Suppose the follow String declarations.
String s1=new String("string one”);
String s2=new String("string two");
If the expression is correct then indicate true and what the value of the expression would return, otherwise indicate the expression is false.
NO |
Expression |
True/False |
Value |
33 |
String s1= new String("new string"); |
||
34 |
String s3 = s1 + s2; |
||
35 |
System.out.println(s1>=s2); |
||
36 |
int i = s1.length |
||
37 |
s3=s1-s2 |
||
38 |
boolean b=s1.compareTo(s2); |
||
39 |
char c=s1[0]; |
||
40 |
int c=s1.compareTo(s2); |
41. Define “immutable” and how it applies to strings
42. What is the difference between . String s=”This is a Java string” and String s= new(“This is a Java string”) (Explain the differences between these two declarations and when we would use one over the other. )
43. What is a “Wrapper” class? And why are they sometimes necessary in Java?