If, If/Else and Strings Test
String one = new String("compsci"); String two = new String("compsci"); if(one==two) out.println("=="); else out.println("!==");
!==
Integer one = 90; Integer two = 75; out.println(one.compareTo(two)); out.println(two.compareTo(one)); two=90; out.println(two.equals(one)); out.println(two.compareTo(one));
-1 1 true 0
What is returned by the call go(2) ? public static String go( int a ) { if(a>=0) { if(a>=1) return "1"; return "2"; } return "3"; }
1
String s = " 100 "; String trimmed = s.trim(); out.println(trimmed); out.println(Integer.parseInt(trimmed)*9);
100 900 good at removing white space
int p = 40; if(p >= 20) { out.print("1"); if(p >= 30) out.print("2"); } else out.print("3");
12
What is the output? int a = 40; if(a>=20) { out.print("1"); if(a>=30) out.print("2"); out.print("3"); }
123
What is the output? int x = 40; if( x >= 20 ) out.print("1"); out.print("2");
12`
What is the output? int a = 40; if(a>=20) out.print("1"); if(a>=50) out.print("2"); out.print("3");
13
What is the output? int a = 40; if(a>=50) out.print("1"); if(a>=40) out.print("2"); out.print("3");
23
What is returned by the call go(-3) ? public static String go( int a ) { if(a>=0) { if(a>=1) return "1"; return "2"; } return "3"; }
3
What is the output? int a = 40; if(a>=50) { out.print("1"); if(a>=30) out.print("2"); } out.print("3");
3
String sam = "hey there"; String ben = "hey thar"; out.println( sam.compareTo(ben) ); out.println( ben.compareTo(sam) ); out.println( ben.compareTo("abc") );
4 -4 7
What is returned by the call go(4) ? public static int go( int a ) { int ans = 0; if(a>=1) { if(a>=5) ans += 1; ans += 2; } ans += 3; return ans; }
5
public static int go( int a ) { int ans = 0; if(a>=0) { if(a>=1) ans += 1; ans += 2; } ans += 3; return ans; }
6
String sam = "hey there"; String ben = "hey thar"; out.println( ben.length() ); out.println( sam.length() );
9 8
Which methods can compare a string reference, while which one compares the contents
== equalt() compareTo()
If-else
A condition is checked and something may or may not happen based on the evaluation of that condition
String s = "compsci"; out.print(s.toUpperCase()); out.println(s); out.print(s.toLowerCase());
COMPSCI compsci compsci
What values will be stored in a and b after execution of the following program segment? int a = 7, b = 11; if(a<b){ if(a < 20) a *= 2; else a = b-a; b = b/3; } else { if(b < 15) b = b*2+3; else b = a-b+15; a = a/4; }
a. a = 14 b = 3
What is output by the code below? int n=70, s=81; if( n > 30){ if( s > 90) out.print("go"); } else out.print("stop"); out.print("aplus");
aplus
int a=7, b=11; if( a < 10 ) out.println("aplus"); if( b > 10 ) out.println("fun"); else out.println("speed");
aplus fun
What is output by the code below? int p = 160, m = 180; if( p > 90) if( m > 100) out.print("def"); else if( m > 150 ) out.print("xyz"); else out.print("ghi"); out.print("aplus");
defaplus
String sam = "hey there"; String ben = "hey thar"; out.println( sam.charAt(20) );
error
String sam = "hey there"; String ben = "hey thar"; out.println( sam.equals(ben) );
false
chArt();
finds the character at a location
What is returned by the call go(50,50)? public static String go(int x, int z) { String s = ""; if(x >= 50) if(z > 55) s += "def"; s += "fun"; return s; }
fun
What is output by the code below? int a = 50, b = 24; if( a > 40) if( b > 50) out.print("def"); else if( b > 35) out.print("xyz"); else if( b > 20) out.print("ghi"); out.print("fun");
ghifun
int c=40, d=30; if( c > 30) if( d > 10) out.print("go"); else out.print("run"); else out.print("fly"); out.print("nogo");
gonogo
Does tolowercase and touppercase change the original string?
no
int x=70, y=50; if( x > 50) if( y > 60) out.print("go"); out.print("nogo");
nogo
trim()
removes any leading or trailing white space
replaceAll(x,y)
returns a new string with all x changed to y
toUpperCase()
returns a new string with uppercase chars
toLowerCase()
returns a new strong with lowercase chars
indexOf();
returns the location of a character
String sam = "hey there"; String ben = "hey thar"; out.println( sam.substring(3,6) ); out.println( sam.substring(0,4) );
th hey
int w=50, x=68; if(w>50) if(x>60) out.print("it"); else out.print("at"); else out.print("up"); out.print("fun");
upfun
substring(x,y)
x to y not including y