CSIS 10A Practice Final Exam Solutions 1) (5 points) What would be the output when the following code block executes? int a=3, b=8, c=2; if (a < b && b < c) b = b + 2; if ( b > 5 a < 3) a = a 1; if ( c!= 8 && b > 5) c = 8; // false // true // true System.out.println(a + " " + b + " " + c); OUTPUT: 2 8 8 2) (5 points) Complete the next 4 lines of the trace table for the following code: int sum = 0, m = 10; for (int k=0; k<10; k=k+2){ sum = sum + m; m = m - 2; sum m k k<10 0 10 0 T 10 8 2 T 18 6 4 T 24 4 6 T 28 2 8 T 3) (10 points) What will be displayed on the terminal by the following nested loop? for (int k = 1; k < 3; k ++ ){ for (int m = 0; m < k ; m ++){ System.out.print( m ); System.out.println(); A trace is a good way to do this. Here is what happens in the loop: k k<3 m m<k Print Therefore the 1 T 0 T 0 output is: 2 T 1 F new line 0 0 T 0 01 1 T 1 2 F new line
4) (20 points) Here is a class for representing Employees at a work place. Complete the constructor and the three methods indicated according to the javadoc comments provided. You don't have to write the javadocs Constructor Summary Employee(String name, double wage) creates a new Employee object with a given name, and hourly wage. Method Summary String getname() returns the name of the employee void setwage(int newwage) changes the wage to newwage double calcpaycheck(double hours) returns the pay check for an employee based on the hourly wage times the number of hours public class Employee { // instance variables private String name; // employee name private double hourlywage; // employee wage // constructor public Employee(String name, double wage){ this.name = name; hourlywage = wage; // getname public String getname(){ return name; // setwage public void setwage(int newwage){ hourlywage = newwage; // calcpaycheck public double calcpaycheck(int hours){ return hours*hourlywage; 4) (5 points) Complete the folowing test app to print out the pay check for Employee e (just 2 lines of code) public class EmployeeApp { public static void main(string [] args){ Employee e = Employee("Ben", 25); double pay; // a)calculate pay given that Ben worked 10 hours and store in pay pay = e.calcpaycheck(10); // b)print the pay for Ben System.out.pritln("Pay = " + pay);
6) (10 points) Draw a map of memory after the following statements execute: Employee a = new Employee("Gil", 25); Employee b = new Employee("Jan",30); Employee c = b; c.setwage(40); 7) (10 points) Suppose that a company has 500 employees and that information about all the employees has already been stored in an array Employee [] workers = new Employee[500]; Write a code segment that will: a) Print the names of all the employees in the company for (int k = 0; k < workers.length; k++) System.out.println( workers[k].getname() ); b) Compute and print the sum of all pay checks for the entire company, assuming everyone worked 40 hours. Print just one number, the sum of all the paychecks. double sum = 0; for (int k = 0; k < workers.length; k++) sum = sum + workers.calcpaycheck(40); System.out.println("total payroll = " + sum); 8) (10 points) Suppose we have an array of int declared with the following values: int [] data = {7, 5, 1, 2, 4, 5, 3, 6; and, we execute the statements: for (int k = 0; k<7 ; k++) { data[k] = data[k+1]; Please draw the data array after the statements have completed: When k = 0 we have data[0] = data[1] // cell 0 gets 5 (value in cell 1) When k = 1 we have data[1] = data[2] // cell 1 gets 1 (value in cell 2) continuing until k = 6 we have data[6] = data[7] // cell 6 gets 6 (value in cell 7) so the array has become: 5 1 2 4 5 3 6 6
9) (10 points) We've created a 2D array of integers int[][] table = new int[3][4]; Fill in the values contained in the table after the following statements execute: for (int row=0; row<3; row++){ for (int col=0; col<4; col++){ if (col == 2) table[row][col] = 7; else table[row][col] = 0; table
10) (15 points) Complete the following short applet. When it starts up, the applet should display the message "Falling" at location 100,100. Whenever the user clicks on the applet, the message should move down the screen 10 pixels. Use the paint() method to display the message. You will need to add an instance variable to keep track of the vertical location of the message. class Falling extends Applet implements MouseListener { private int ymessage; // the y value for the message public void init() { ymessage = 100; // start message with a 100 pixel off from top addmouselistener(this); public void paint(graphics g) { g.drawstring("falling",100,y); ; public void mousepressed(mouseevent evt) { ymessage = ymessage + 10; // moves down 10 pixels repaint(); ; public void mousereleased(mouseevent evt) { // Junk required by public void mouseclicked(mouseevent evt) { // the MouseListener public void mouseentered(mouseevent evt) { // interface. public void mouseexited(mouseevent evt) { // end class Falling