exam example questions. makefile data.o: data.c data.h gcc -c data.c driver.o: driver.c data.h gcc...

24
Exam Example Questions

Post on 19-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Exam Example Questions

Makefile

data.o: data.c data.hgcc -c data.c

driver.o: driver.c data.h gcc -c driver.c

io.o: io.c gcc -c io.c

driver: io.o data.o driver.ogcc -o driver io.o driver.o data.o

-rw-r--r-- 1 plab courses 673 Feb 4 18:10 data.c-rw-r--r-- 1 plab courses 673 Feb 4 16:59 data.h-rw-r--r-- 1 plab courses 673 Feb 4 17:00 data.o-rwxr-xr-x 1 plab courses 673 Feb 4 18:01 driver-rw-r--r-- 1 plab courses 673 Feb 4 17:30 driver.c-rw-r--r-- 1 plab courses 673 Feb 4 17:31 driver.o-rw-r--r-- 1 plab courses 673 Feb 4 17:40 io.c-rw-r--r-- 1 plab courses 673 Feb 4 17:41 io.o

Which files will be updated ?1. driver 2. driver.o 3. data.h 4. data.o 5. io.o

1, 4

Macro

#define MAX(a,b) ( ( (a) > (b) ) ? (a) : (b) )

int i=2;

int j=3;

int x = MAX(i++,j);

printf("%d %d\n",i,x);

What will be the output ? 3 3

C string

char s[] = {'p','l','a','b'};

What will be returned by calling strcmp(s,"plab")?

1. a negative number

2. 2. 0

3. 3. a positive number

4. 4. We cannot know for sure

4

structstruct Flight {

char source[20]; char * destination;

}; Flight ticket1; Flight ticket2; ticket1.destination =(char*)malloc( 20*sizeof(char)); strcpy(ticket1.source, "florence");strcpy(ticket1.destination,"london");ticket2=ticket1; *(ticket2.destination + 2) = 'k'; ticket2.source[1] = 'm';

printf("%s %s",ticket1.source,ticket2.destination); The output of the code will be: florence lokdon

Friend Function

(1) class Movie {

(2) public:

(3) friend void print(Movie const& m);

(4) private:

(5) int m_length;

(6) };

(7) void Movie::print( Movie const & m ) {

(8) cout << m.m_length;

(9) }

Friend Function

בחר את התשובה הנכונה )תשובה אחת בלבד(:

m_length( private memberניסיון הגישה למשתנה הפרטי )1. יגרום לשגיאת קומפילציה.8בשורה

-runהקוד יתקמפל ללא שגיאות ולא יגרום לשגיאות זמן-ריצה )2.time errors.)

חייבת להיות מוגדרת כפונקציה ולא כמתודה " print, "7בשורה 3.(method ) של המחלקהMovie.

תגרום לשגיאות זמן-ריצה." printהרצת הקוד של "4.

3תשובה -

Operator Overloading

מהי הצורה הנכונה להכריז על אופרטור ההשמה (assignment operator) להשמת אובייקטים ,

כלומר, מתודה . X למחלקה Y( classמהמחלקה )המאפשרת את שורות הקוד הבאות:

X a;

Y b;

a = b;

Operator Overloading

1. void Y::operator=(Y const &from);2. void

X::operator=(X& to, Y const &from);3. Y& X::operator=( Y const &from);4. X& X::operator=( Y const &from);5. Y& Y::operator=( X const &to);

4תשובה

Copy Constructorclass Person {public: Person ( int id, int friendId = 0) : m_id( id ) { init( friendId ); } ˜Person( ) { if (m_bestFriend) delete m_bestFriend; }

void init( int friendId ) { if( friendId > 0 ) m_bestFriend = new Person( friendId ); else

m_bestFriend = NULL;}

void show () {cout<<"id "<< m_id << endl; cout<<“best friend “<< m_bestFriend->m_id<<endl; }

protected: int m_id; Person * m_bestFriend;};

Copy Constructorint func4() { Person p( 1234,789); foo(p); p.show();

}void foo( Person p ) { p.show();}

(.run-time errorsריצה )-עלול לגרום לשגיאות זמן” func4הקוד של הפונקציה "1.

יגרום לשגיאות בקומפילציה.” func4הקוד של הפונקציה "2.

.ירוץ ויתקמפל ללא שגיאות” func4הקוד של הפונקציה "3.

כמתודה סטטית, הקוד יתקמפל וירוץ ללא שגיאות.show( method)אם נגדיר את המתודה 4.

1תשובה

Studentclass Student : public Person {public: Student( int id, int friendId, int grade) : Person( id, friendId) , m_grade(grade) { } void show() { cout <<"id "<<m_id <<“ grade "<<m_grade<< endl; } protected: int m_grade;};

Exceptions//assuming Student : public Personint bar( int id) {

if (id < 0) throw Student(1234, 567, 89);

else return 0;}int func5() { try { bar(-3); } catch(Person p) { p.show(); } catch(Student s) { s.show(); }}

id 1234

Animalclass Animal {public:

Animal(string name = string(“something”) ) {m_name = name;cout << "animal ctor" << endl;

}virtual ˜Animal() { cout << "animal dtor" << endl; }virtual void print() { cout << m_name << endl;}

private:string m_name;

};

Dog

class Dog : public Animal {public:Dog ( string name ) : Animal(name)

{ cout << "dog ctor" << endl; }virtual ˜Dog()

{ cout << "dog dtor" << endl; }virtual void print() {

Animal::print();cout << “how how” << endl;

}};

Construction and Destruction

void foo( Dog& d ) {cout << "how how" << endl;

}int main() {Dog d("rambo");foo(d);return 0;

}What will be the output ?

animal ctordog ctorhow howdog dtoranimal dtor

Frogclass Frog : public Animal{public:Frog ( string name ) : Animal(name)

{ cout << "frog ctor" << endl; }virtual ˜Frog ()

{ cout << "frog dtor" << endl; }

virtual void print() {Animal::print();cout << “quack” << endl;

}private:Frog ( Frog const& f ) : Animal( f ) {}

};

Dynamic Casting

כלומר, )d1=NULLלאחר ריצת שורות הקוד הנ"ל, 1.(.NULL יקבל את הערך d1הפוינטר

d2=NULLלאחר ריצת שורות הקוד הנ"ל, 2.

run-timeבאחת השורות עלולה להיות שגיאת זמן ריצה )3.error.)

1תשובה

(1) Frog * f = new Frog("foo");(2) Animal * a = new Dog("humi");(3) Dog * d1 = dynamic_cast<Dog*>( f );(4) Dog * d2 = dynamic_cast<Dog*>( a );

Type Checking!

Dog d1("humi");

Dog d2("muki");

(1) Dog& d3 = d2;

(2) Dog* d4 = &d3;

(3) *d4 = d2;

(4) Dog& d5 = new Dog("rambo");

סמנו את כל השורות שיגרמו לשגיאת קומפילציה

)4( Trying to convert Dog* to Dog&

Hidden Constructor

האם השורות הבאות מתקמפלות ללא שגיאה?

Frog f(“kermit”);

vector<Frog> fv;

fv.push_back(f);

fv[0].print();

Will not compile – Frog’s copy c-tor is private

Conversionvoid foo(string s) { cout << s << endl; }void bar(char* s) { cout << s << endl; }void apple(int i) { cout << i << endl; }int main(){

char c=’z’;char *str = "plab";string s(“easy”);

foo(str); //1bar(s); //2

apple(c); //3return 0;

}

2 – trying to convert string to char* , no implicit cast available

Conststruct foo { int* p; };class bar {

foo m_foo;public:

bar(int *q) { m_foo.p = q; }foo const& getFoo() const { return m_foo; }//1foo & getApple() const { return m_foo; }//2

};int main() {

int i, j;bar mybar(&i);*(mybar.getFoo().p) = 7; //3mybar.getFoo().p = &j; //4

}Which lines will not compile ?

)2( converting foo const& to foo&.)4( assigning to a const pointer

Memory allocation

void func(){double a;int* b = new int;double* c = &a;set<int> d;double* e = new double[10];string words[10];// ...

}Write code to release memory for this section:delete b;delete [] e;