Sample Final Exam 1. Evaluate each of the following expressions and show the result and data type of each: Expression Value Data Type 14 % 5 1 / 2 + 1 / 3 + 1 / 4 4.0 / 2.0 Math.pow(2.0, 3.0) (double)(2 / 5) 15 + 3 4 * 5 5.0e2 + 6 Math.sqrt(100) 5 / 2 * 2.0 2.0 * 5 / 2 2. Examine each line of code in the program below and correct the syntax errors by rewriting the line correctly on the right. If a line does not contain an error, write NO ERROR. Code Import java.util.random; public class Errors ; public attics void main (String [] args) int x; y = 0; Random r = new Random; x = r; y += x; System.out.println( y= y); ) Correction Sheller Page 1
1. (10 points) Identify and correct the errors in each snippet of code below. Each section of the table is selfcontained. All the snippets are supposed to print x=0 if and only if variable x equals 0; however, the actual value of x doesn t really matter in terms of identifying the errors: if (x == 0); if (x = 0) if (x!= 0) else System.out.println( x is not 0 ); if (x == 0) System.out.print ( x ); System.out.print( = ); System.out.print( 0 ); 2. (15 points) Show the truth value of each expression a through e given the declarations below: int a = 2, b = 0, c = -3; a) (a + b) > c b) a > b && b < c c) (b > c)!= (a > b) d) a == b (c < a && b < c) e) ((b c) > (a b) ) && ((a + c) < (a c)) Sheller Page 2
3. Show the output of the following program using the grid below. You will only be graded on the output, but if it is incorrect you can get partial credit by filling the other columns of the grid: public class Exam2Q public static void main (String [] args) int x = 0, y = 1; while (x < 10) x++; if (x % 2 == 1) System.out.println("x="+x); y++; if (y % 2 == 0) System.out.println("y="+y); Iteration x at start x at end y at start y at end Output 1 2 3 4 5 6 7 8 9 10 4. Show the truth value of each expression a through e given the code below: String s1 = abc ; String s2 = bbc ; a) s1.charat(1) == s2.charat(1) b) s1.compareto(s2) > 0 c) s1.substring(1).equalsignorecase(s2.substring(1)) d) s1.length() < s2.length() e) s1.indexof( b ) == s2.indexof( b ) Sheller Page 3
5. Use the class below to determine if instructions a through i are valid; if so, write valid in the space next to the instruction and if not, explain what is wrong or correct the error. Each subsequent instruction can assume that previous instructions were valid: public class Albatross private int wings; private String bellow; public Albatross() wings=2; bellow= awk ; public void setfire(int match) wings = (int)(math.random()*match); public String getaway() return bellow; Code (in main method, different class) Error/Correction a) Albatross a = new Albatross(-4, whoop ); b) System.out.println(a); c) Albatross.getAway(); d) int x = a.setfire(100); e) a.getout( of here ); f) String shout = a.getaway(); g) a.setfire(a.getaway()); h) a.setfire(wings); i) a.getaway( eh? ); Sheller Page 4
6. Write a Java class that models a Pixel (one of the many colored dots that makes up the display on a computer screen). Instance variables must include: red, green and blue: int values (in the range 0..255) that represent the saturation of each of the colors represented by a pixel size: an int value between 1 and 50, representing the size of a pixel; we can think of a pixel as a size x size solid square in the designated color Instance methods (these must enforce the integer limits described above): 3 constructors: o default constructor creates a 1 x 1 white pixel (NOTE: white is full color saturation; all 3 color values are 255); o constructor with 4 int arguments, representing red, green, blue and size; o constructor with an int argument representing size, and default color of white Mutator methods to set size, set individual color values, and set all 3 color values Accessor methods to return values for colors & size a tostring method that returns a String with this format: red:xxx, green:xxx, blue:xxx, size:xx (where the x s are the actual int values) an equals method that returns true if the calling object and a Pixel argument are the same size, false otherwise You do not need to write a main method just the Pixel class itself, with the elements described above. Sheller Page 5