15-100 Sections S-V / Fall 2008
Quiz 4 / 4 Questions / 30 Minutes
Quiz Date:  Tue 30-Sep-2008

1.      What will the following code snippets print?

a.    System.out.println(Math.log(1));
System.out.println(Math.log(-1));
 

b.    System.out.println("ABCDBC".lastIndexOf("BC",4));
System.out.println("ABCDBC".lastIndexOf("BC",0));
 

c.    System.out.println("ABCD".substring(1,3));
System.out.println("ABCD".substring(2));
 

d.    String s1 = "   ab   cd   ";
String s2 = s1.trim();
System.out.println("[" + s1 + "]");
System.out.println("[" + s2 + "]");
 

e.    System.out.println(Math.round(-2.50));
System.out.println(Math.floor(2.99));
 

2.      What will the following code snippets paint?Draw your answer inside the 100x100 window provided for you to the left of each problem.  Do not worry about being pixel-perfect.  Within a few pixels is fine.
 

a. page.drawRect(10, 60, 40, 40);
   page.drawLine(10, 60, 40, 40);




 

b. int startAngle = -90;
   int extentAngle = 180;
   page.fillArc(0, 50, 100, 50, startAngle, extentAngle);



 

c. Polygon p = new Polygon();
   p.addPoint(50, 0);
   p.addPoint(0, 50);
   p.addPoint(50, 100);
   page.drawPolygon(p);
   p.addPoint(100,50);
   page.drawPolygon(p);


 

d. Polygon p = new Polygon();
   p.addPoint(0, 0);
   p.addPoint(50, 50);
   p.addPoint(100, 0);
   page.fillPolygon(p);
   p.translate(0, 50);
   page.drawPolygon(p);

3.      In general, for non-null String s, when would the following expressions evaluate to true?

a.    (s.indexOf("abc") == s.lastIndexOf("abc"))
 

b.    (s.replace("abc","").equals("abc"))
 

c.    (s.toUpperCase().substring(3).equals(s.toLowerCase().substring(0,3)))
 

d.    ((s+s).length() == (int)Math.log10(Color.red.getRed()))
 

4.    Write a method that takes an integer and returns true if that number is triangular, and false otherwise, where a number is triangular if it is the sum of the first K positive integers (for any K>0).  The first few triangular numbers are:  1,   3 (1+2),    6  (1+2+3),  and  10  (1+2+3+4).


carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem