0% found this document useful (0 votes)
43 views2 pages

CS213 Quiz # 2

The document is a quiz for a CS213 class that contains two questions. The first question provides code defining functions for structs A and B, creates objects of each type, and calls their functions through a common interface. It asks what output would result. The second question provides code for an Animal class hierarchy with subclasses for Cat and Dog, creates objects of each type, and calls their talk functions through the base Animal class to demonstrate polymorphism. It also asks for the output.

Uploaded by

couplefaith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

CS213 Quiz # 2

The document is a quiz for a CS213 class that contains two questions. The first question provides code defining functions for structs A and B, creates objects of each type, and calls their functions through a common interface. It asks what output would result. The second question provides code for an Animal class hierarchy with subclasses for Cat and Dog, creates objects of each type, and calls their talk functions through the base Animal class to demonstrate polymorphism. It also asks for the output.

Uploaded by

couplefaith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS213 Quiz # 2

March 1st, 2011

Name: ________________________________ ID: _____________________________

Question # 1 [50 Points]

void A_ping() { printf( "A_ping\n"); }


void A_pong() { printf( "A_pong\n"); }

void B_ping() { printf( "B_ping\n" ); }


void B_pong() { printf( "B_pong\n" ); }
void B_wiff() { printf( "B_wiff\n" ); }

struct A{ struct B {
void (*ping)(); void (*wiff)();
void (*pong)(); void (*ping)();
}; void (*pong)();
};

struct A* new_A() {
struct A* a = (struct A*)malloc( sizeof(struct A) );
a->ping = A_ping;
a->pong = A_pong;
return a;
}

struct B* new_B() {
struct B* b = (struct B*)malloc( sizeof(struct B) );
b->ping = B_ping;
b->pong = A_pong;
b->wiff = B_wiff;
return b;
}

void foo( struct A* a ) {


a->ping();
a->pong();
}

void main( void )


{
foo( new_A() );
foo( (struct A*)new_B() );
}

When main() executes, what would the program output be, and why?

Answer:
Question # 2 [50 Points]
class Animal {

void talk() {
System.out.println( "Animal talking!" );
}
}

class Cat extends Animal {

void talk() {
System.out.println( "Meow!!" );
}
}

class Dog extends Animal {

void talk() {
System.out.println( "Woof!!" );
}
}

class PolymorphismApp {

public static void makeTalk( Animal a ) {


a.talk();
}

public static void main( String[] args ) {

Cat tiger = new Cat();


Animal max = new Dog();
Dog ginger = new Dog();

makeTalk( ginger );
makeTalk( tiger );
makeTalk( max );
}
}

What is the program output, and why?

Answer:

You might also like