CS160

Final

Spring 2005

 

 

Question number

Points

Points Earned

1

16

 

2

16

 

3

8

 

4

12

 

5

12

 

6

16

 

7

20

 

Total Points

100

 

 

 

 

 

Name__________________________________

Lab Section Number______________________

email__________________________________

  1. Make sure you answer all 7 questions. Some questions have several parts.
  2. You have 110 minutes to complete the test.
  3. Make sure your answers are easy to read, if I can't read them I will have to subtract the points.
  4. This exam contributes 25% of the total grade for this course.


  1. The following code has two classes, Animal and Driver. Give the output of the following code:

 

public class Animal

{

private String myType;

public Animal(String type)

{

myType = type;

}

public String getType()

{

return myType;

}

public void setType(String type)

{

myType = type;

}

public Animal changeType(Animal furry, Animal hairy)

{

Animal p1 = new Animal("Elephant");

furry = hairy;

System.out.println(furry.getType()); //************* print 1***********

System.out.println(hairy.getType()); //********** print 2************

furry.setType("Pig");

System.out.println(furry.getType()); //************print 3*************

System.out.println(hairy.getType());//************print 4*************

furry = p1;

System.out.println(furry.getType()); //************print 5*************

System.out.println(hairy.getType());//************print 6*************

return furry;

}}

/*******************************************/

public class Driver

{

public static void main(String [] args)

{

Animal animalOne = new Animal("Monkey");

Animal animalTwo = new Animal("Dog");

Animal big = animalOne;

animalOne = animalTwo.changeType(big, animalTwo);

System.out.println(animalOne.getType()); //***********print 7

System.out.println(animalTwo.getType()); //***********print 8

}

}

 

What is the out put at each print statement

 

Print 1: Print 5:

Print 2: Print 6:

Print 3: Print 7:

Print 4: Print 8:


  1. Answer with very short answers the following questions:

 

A . How many parameters can a Constructor receive?

B. How many constructors can a class have?

 

C. How many values can a method return?

 

D. Can a method return an instance of a Class (an object)?

 

E. Write the method definition for the method call getBook (the definition should only be one line of code):

 

String name = library.getBook(new Title("Harvey "));

 

 

 

F. What is the answer to num=4%5;

 

G . Name a data type that allows a fractional part?

H. What is the keyword final used for?

 


  1. Given the following code, give the output (all the output) of each print statement. If there is a loop that executes five times, and there is a print statement in that loop I want all five outputs of the print statement with the proper line by line output.

 

 

public class Seven

{

public static void main(String [] args)

{

int [] num = new int[7];

int [] temp = new int[4];

//first for loop

for (int i = 0; i<num.length; i++)

{

if( i%3 == 0)

num[i] = i;

else

num[i] = i+5;

System.out.println(i + " is " + num[i]); //Print statement one

}

//Second for loop

for(int j=temp.length-1; j >= 0; j--)

{

temp[j] = num[j*2];

System.out.println(j +" is " + temp[j]); //Print statement two

}

char letters[] = {'F', 'I', 'N', 'A', 'L'};

String name = new String(letters); //this will initialize name to a string with the value of the array

aMethod(letters, name);

for(int i=0; i<name.length(); i++)

System.out.println(letters[i] + " - "+ name.charAt(i)); //Print statement four

}

 

public static void aMethod(char sub[], String n)

{

n = n.replace('A', 'E');

sub[0] = 'B';

for(int i=0; i<sub.length; i++)

System.out.println(sub[i] + " - "+ n.charAt(i)); //Print statement three

}

}

 


 

Print statement one

 

 

Print statement two

 

Print statement three

 

Print statement four

 


  1. Give the output of the following code in the proper order (Assume all proper imports have been declared):

 

public class ExceptionExample

{

public static void main(String [] args)

{

Test p1 = new Test();

try{

p1.sampleMethod();

}

catch(RuntimeException es)

{

System.out.println("First");

}

catch(IOException es)

{

System.out.println("Second");

}

catch(Exception e)

{

System.out.println("Third");

}

finally{

System.out.println("Fourth");

}

}

}

/*********************************************************/

public class Test

{

public Test()

{

}

public void sampleMethod() throws Exception

{

try{

throw new Exception("Fifth");

}

catch(IOException err)

{

System.out.println("Sixth");

throw new IOException("Seventh");

}

catch(Exception e)

{

System.out.println("Eighth");

throw new Exception("Nineth");

}

finally{

System.out.println("Tenth");

}

}

}

 

  1. Given the following code, four classes TheMain, Mammal, Bear , and Whale , follow the rules of inheritance and give the output of the six print statements.

 

public class TheMain

{

public static void main()

{

Mammal m1 = new Mammal("black", 200);

Bear b1 = new Bear(500);

Whale w1 = new Whale(1200);

//Question one

System.out.println(b1.getWeight());

//Question two

System.out.println(m1.getWeight());

//Question three

System.out.println(w1.getColor());

//Question four

System.out.println(b1.getColor());

//Question five

System.out.println(w1.getWeight());

//Question six

System.out.println("The amount of count = " + Mammal.count);

}

}

 

/*******************************************************/

 

public class Mammal

{

protected int x;

protected String y;

public static int count = 1;

public Mammal()

{

y = "Tan";

x = 450;

count++;

}

public Mammal(String a)

{

y = a;

}

public Mammal(String a, int b)

{

y = a;

x = b;

}

public Mammal(int b)

{

x = b;

y = "White";

}

public int getWeight()

{

x++;

return x;

}

public String getColor()

{

return y;

}

public String getColor(String s)

{

return s;

}

}

 

 

 

 

 

 

 

/***********Bear Class*************/

 

public class Bear extends Mammal

{

private int x;

private String y;

public Bear(String a, int b)

{

y = a;

x = b;

}

public Bear( int b)

{

x = b;

y = "Brown";

}

public String getColor()

{

return super.getColor(y);

}

}

 


/****************** Whale Class ***************/

 

public class Whale extends Mammal

{

public static int count;

protected int x;

protected String y;

public Whale(int a)

{

super(a);

x = a;

y = "White";

count++;

}

public int getWeight(int a)

{

return x;

}

}

 

 

 

 

Question 1:

 

Question 2:

 

Question 3:

 

Question 4:

 

Question 5:

 

Question 6:

 

 


  1. Given the code on the next page (I had to take out the import calls to fit it all on one page, assume the proper imports have been listed), and the mouse actions sketched below draw what the frames look like at each stage.

 

2.&3.

1.

 

4.

 

 

 

        1. Mouse Starts here (The graphics are lost, so start out to the right of the applet)
        2. Mouse moves to here (inside the applet frame)
        3. Mouse clicks at location 2
        4. Mouse Moves to here (back outside the frame).

 

 

 


public class myApplet extends Applet implements MouseListener

{

private String phrase;

private int flag;

public void init()

{

phrase = "CS 160 Complete";

flag =0;

addMouseListener(this);

}

public void paint(Graphics g)

{

if(flag==1){

g.setColor(Color.black);

g.drawString(phrase, 5, 15);

g.drawRect(10, 40, 20, 30);

}

else if(flag==2)

{

g.setColor(Color.blue);

g.drawString(phrase, 10, 5);

g.drawOval(30, 30, 10, 40);

}

else

{

g.setColor(Color.red);

g.drawString(phrase, 5, 25);

g.fillRect(10, 25, 30, 20);

}

}

public void mouseClicked(MouseEvent e)

{

flag=1;

repaint();

}

public void mousePressed(MouseEvent e)

{

phrase = "Here Comes CS221";

}

public void mouseReleased(MouseEvent e)

{

}

public void mouseEntered(MouseEvent e)

{

flag=2;

phrase = "Finally CS160 is over";

repaint();

}

public void mouseExited(MouseEvent e)

{

flag=1;

phrase = "Goodbye";

}

}

 

7. Short Answer:

 

 

for(int a = 2; a < 11; a++){

System.out.println(a);

}

•  How many times will the print statement be excuted?

 

•  What is the final value for a?

 

 

***********************************************************************

if (a < b && b > c)

System.out.print("print statement one\n");

else if( b < c || c < a)

System.out.print("print statement two\n");

else if( b == 3)

System.out.print("print statement three\n");

else

System.out.print("print statement four\n");

 

•  If a, b, and c were declared and initialized properly is it possible that a, b or c could have values that would result in none of the four print statements to print?

 

 

•  Given the following values a = 5; b = 3; c = 4, what would be printed to the screen?

 

 

•  Given the following values a = 3; b = 3; c = 3, what would be printed to the screen?

 

 

•  The if/else clause above would be an example of a "conditional statement"?

 

 

 

************************************************************************

int stop = 0;

do{

stop = 1;

System.out.print("print statement\n");

stop++;

}while(stop == 1) ;

 

•  How many times will the print statement be excuted?


**** ***************************************************************  

int stop = 0;

while(stop != 1){

stop = 1;

System.out.print("print statement\n");

stop++;

}

 

•  How many times will the print statement be excuted?

 

 

 

 

 

count = 0;

while(count<20)

System.out.println(count);

count=count+2;

 

 

•  How many times will the print statement be excuted?

 

 

**************************************************************

count=0;

while(count<20);

System.out.println(count++);

 

 

How many times will the print statement be excuted?

Answers