1 / 2100%
First Name (print): ____________________ Last Name (print):_________________________
Write Output Here
Figure
Rectangle
Box
1. Given two reference variables named obj and str, write Compiles” if the code will compile or
“Errorif there will be a compile time error. (3 pts)
Object obj = new Object( );
String str = new String( );
a _Compiles____ obj = str; d _Error________ int e = obj.length( );
b _Error_______ str = obj; e _Compiles_____ int f = str.length( );
c _Compiles_____ str = (String)obj; f _Compiles_____ obj = "New String";
3. What will be printed by the following code? Consider the polymorphic invocation. (2 pts)
public class Inherit
{
class Figure
{
void display()
{
System.out.println("Figure");
}
}
class Rectangle extends Figure
{
void display()
{
System.out.println("Rectangle");
}
}
class Box extends Figure
{
void display()
{
System.out.println("Box");
}
}
Inherit()
{
Figure f = new Figure();
Rectangle r = new Rectangle();
Box b = new Box();
f.display();
f = r;
f.display();
f = b;
f.display();
}
public static void main(String[] args)
{
new Inherit();
}
}
CSE205 OOP and Data Structure Quiz #3 Solution
CSE205 OOP and Data Structure Quiz #3 Solution
First Name (print): ____________________ Last Name (print):_________________________
3. Circle True or False for the following statements (2 pts)
True False a) In Object-Oriented programming, if a reference variable refers to different types of objects at
running time, we call this polymorphism.
True False b) If no visibility modifier is defined for a method, by default the method will be treated as
public and it can be accessed from anywhere.
True False c) An aggregation relationship defines a is arelationship, such as the relationship between
class Animal and Dog shown in class.
True False d) A child class will inherit all instance variables and methods from its parent class.
4. Assume a class Triangle has been defined. It has three instance data, Point p1, p2, p3. The class also has a
constructor that receives the 3 Points and initializes p1, p2, and p3, a toString method that outputs the 3 Points,
and a computeSurfaceArea method that calculates the surface area of the Triangle (as an int value). We want to
extend this class to represent a 3-dimensional Pyramid, which consists of 4 Points. Since the Pyramid will
consist of in essence, 4 triangles, computeSurfaceArea for the Pyramid will compute 4 * the surface area of the
Triangle made up of 3 of those 4 Points. Write the Pyramid class to handle the difference(s) between a Pyramid
and the previously defined Triangle. Assume the instance data of Triangle are protected and all methods are
public. Also assume that the class Point has a toString method. (3 pts)
. public class Pyramid extends Triangle
{
private Point p4;
public Pyramid(Point a1, Point a2, Point a3, Point a4)
{
super(a1, a2, a3);
p4 = a4;
}
public String toString( )
{
return super.toString( ) + p4.toString( );
}
public int computeSurfaceArea( )
{
return 4 * super.computeSurfaceArea( );
}
}
Powered by TCPDF (www.tcpdf.org)
Students also viewed