Vous êtes sur la page 1sur 10

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A

Name: First __________________ Last ___________________________

Student ID # _________________________________

 Please leave at least one seat between yourself and the person next to you.
 No books, notes, or electronic equipment can be used during the exam.
 Place your answers inside the provided boxes.
 Any CHEATING will result in an F as well as being written-up on academic dishonesty.

Points
Question # Points Earned
Available
1 6
2 6
3 6
4 6
5 6
6 6
7 6
8 6
9 6
10 6
11 6
12 6
13 6
14 6
15 6
16 10
Total: 100

ATTENTION: Make sure your exam has all 16 questions.

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 1


NOTE:

The Rect, ColoredRect, and RoundedColoredRect classes are defined below and compile
without error. They will be used in many of the questions to follow.

public class Rect


{
public int x;
public int y;
public int width;
public int height;

public void clear()


{
x = 0;
y = 0;
width = 0;
height = 0;
}

public String toString()


{
return "(" + x + "," + y + ")";
}
}

public class ColoredRect extends Rect


{
public int rgb = 255;

public void clear()


{
rgb = 0;
}

public String toString()


{
return "RGB: " + rgb;
}
}
public class RoundedColoredRect extends ColoredRect
{
public static int maxAngle = 90;
public int angle = 45;

public String toString()


{
return angle + " degrees";
}
}

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 2


Question 1 (6 Points)

Using the classes define on the previous page, how many bytes of memory are reserved on the heap:

a) When a Rect object is constructed?

b) When a ColoredRect object is constructed?

c) When a RoundedColoredRect object is constructed?

Question 2 (6 Points)

The ConstructorTester class below does not compile due to syntax errors inside the main
method. Circle the line numbers, if any, next to those lines of code inside that method that
produce errors.

public class ConstructorTester


{
public static void main(String[] args)
{
Rect r; // 1
ColoredRect cr; // 2
RoundedColoredRect rcr; // 3
r = new Rect(); // 4
r = new ColoredRect(); // 5
r = new RoundedColoredRect(); // 6
cr = new Rect(); // 7
cr = new ColoredRect(); // 8
cr = new RoundedColoredRect(); // 9
rcr = new Rect(); // 10
rcr = new ColoredRect(); // 11
rcr = new RoundedColoredRect(); // 12
}
}

Question 3 (6 Points)

a) What is the term used to describe GET methods in Object Oriented Programming?

b) What is the term used to describe SET methods in Object Oriented Programming?

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 3


Question 4 (6 Points)

The code below compiles and runs without error. What output would result when executed?

RoundedColoredRect rcr = new RoundedColoredRect();


ColoredRect cr = rcr;
Rect r = rcr;
rcr.x = 5;
cr.x = 6;
r.x = 7;
System.out.println(rcr.x);
System.out.println(cr.x);
rcr.x = 8;
System.out.println(r.x);

Question 5 (6 Points)

The CBV program below compiles and executes without error. What output would result from
running this program?

public class CBV


{
public static void main(String[] args)
{
Rect r1 = new Rect();
r1.x = 5;
Rect r2 = new Rect();
r2.x = 10;
int sumX = r1.x + r2.x;
change(r1, r2, sumX);
System.out.println(r1.x);
System.out.println(r2.x);
System.out.println(sumX);
}

public static void change(Rect rect1, Rect rect2, int n)


{
rect1 = new Rect();
rect1.x = 20;
rect2.x = 25;
n = rect1.x + rect2.x;
}
}

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 4


Question 6 (6 Points)

In lab we did bitwise-AND (‘&’) operations. A bitwise-OR (‘|’) operation works similarly, except
that it performs OR operations between bits rather than AND operations. The code below
compiles and runs without error and uses both. What output would result from running it?

int num = 11;


int mask1 = 4;
int mask2 = 8;
int mask3 = 16;
int answer = num & mask1;
System.out.println(answer);
answer = num & mask2;
System.out.println(answer);
answer = num | mask3;
System.out.println(answer);

Question 7 (6 Points)

The RectPrinter program below compiles and executes without error. What output results
when running it?

public class RectPrinter


{
public static void main(String[] args)
{
Rect r = new Rect();
r.x = 1;
r.y = 2;
System.out.println(r.toString());

Rect r2 = new ColoredRect();


r2.x = 3;
r2.y = 4;
r = r2;
System.out.println(r.toString());

Rect r3 = new RoundedColoredRect();


r3.x = 5;
r3.y = 6;
r = r3;
System.out.println(r.toString());
}
}

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 5


Question 8 (6 Points)

The ATCDriver program below compiles and executes without error. What output results when
running it?

public class ATCDriver


{
public static void main(String[] args)
{
Rect r = new Rect();
r.x = 1;
r.y = 2;
clearAndPrint(r);

Rect r2 = new ColoredRect();


r2.x = 3;
r2.y = 4;
clearAndPrint(r2);

RoundedColoredRect rcr = new RoundedColoredRect();


rcr.x = 5;
rcr.y = 6;
clearAndPrint(rcr);
}

public static void clearAndPrint(Rect rect)


{
rect.clear();
System.out.println(rect.toString());
}
}

Question 9 (6 Points)

Fill in the blanks in the statements below.

The _______________________________ only cares about an object’s apparent type, which is

the type it was declared as.

The _______________________________ only cares about an object’s actual type, which is the

type it was _____________________________ as.

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 6


Question 10 (6 Points)

In the diagram below we are using the eclipse IDE’s debugger and execution has stopped at a breakpoint
on the highlighted line of code. If one were to press the “Step Over” button, what would the debugger do?

a) Skip execution of the clearAndPrint method and pause on the next line of code.
b) Skip execution of the clearAndPrint method and all following statements until another
breakpoint is reached.
c) Execute the clearAndPrint method and pause on the next line of code.
d) Execute the clearAndPrint method and all following statements until another breakpoint is
reached.

Question 11 (6 Points)

The code below compiles and runs without error. What output would result when executed?

Rect r = new Rect();


r.x = 5;
r.y = 6;
r.width = 7;
int num = r.x;
num++;
r.y += r.y;
r.width++;
System.out.println(r.x);
System.out.println(r.y);
System.out.println(r.width);

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 7


Question 12 (6 Points)

The Op and OpTester classes below compile and run without error. What output would result
from running the OpTester program?

public class Op
{
public static int counterA = 0;
public int counterB = 0;

public Op() {}

public void update()


{
counterA++;
counterB++;
}

public void opPrint()


{
System.out.println(counterA);
System.out.println(counterB);
}
}

public class OpTester


{
public static void main(String[] args)
{
Op op1 = new Op();
op1.update();
Op op2 = new Op();
op2.update();
op2.update();
Op op3 = new Op();
op3.update();
op3.update();
op3.update();
op1.opPrint();
op2.opPrint();
op3.opPrint();
}
}

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 8


Question 13 (6 Points)

What is the name of the java construct (it’s also a Java keyword) that we used in HW 3 for representing
a fixed number of textual choices for a given variable

a) assert
b) const
c) enum
d) interface
e) protected

Question 14 (6 Points)

The Num class below compiles and run without error. What output would result from running the
program?

public class Num


{
private int num;

public Num(int initNum) { num = initNum; }

public int getNum() { return num; }

public int test(Num n)


{
return this.num;
}

public static void main(String[] args)


{
Num n1 = new Num(1);
Num n2 = new Num(2);
Num n3 = new Num(3);
System.out.println(n1.test(n2));
System.out.println(n2.test(n3));
System.out.println(n3.test(n1));
}
}

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 9


Question 15 (6 Points)

The What class below compiles, but it has a problem. Upon running the program experiences a
runtime error. Circle the line of code where this error originates, meaning where the
exception error is first thrown.

public class What


{
public String name;

public What(String initName){}

public static void main(String[] args)


{
What w1 = new What("Joe"); // 1
String n = w1.name; // 2
int c = n.charAt(0); // 3
Object obj1 = w1; // 4
System.out.println(obj1); // 5
What w2 = new What("Jane"); // 6
w2.name = "Joe"; // 7
Object obj2 = (Object)w2; // 8
System.out.println(obj2); // 9
}
}

Question 16 (10 Points)

Below is an equals method that we wish to add to the Num class. It should compare the
equivalence of the num instance variable between objects, but it is missing a few pieces. Fill in
the blanks to complete the method such that it works properly.

public _________ equals(Object obj)


{
_________ n = _________ obj;
return _________ == __________;
}

CSE 114 – Fall 2010 – Midterm Exam 2 – Group A 10

Vous aimerez peut-être aussi