Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exam Example Questions

Similar presentations


Presentation on theme: "Exam Example Questions"— Presentation transcript:

1 Exam Example Questions

2 Makefile data.o: data.c data.h gcc -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.o gcc -o driver io.o driver.o data.o

3 Which files will be updated ? 1. driver 2. driver.o 3. data.h
-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

4 Macro What will be the output ?
#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

5 C string char s[] = {'p','l','a','b'};
What will be returned by calling strcmp(s,"plab")? a negative number 2. 0 3. a positive number 4. We cannot know for sure 4

6 struct struct 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

7 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) }

8 Friend Function בחר את התשובה הנכונה (תשובה אחת בלבד):
ניסיון הגישה למשתנה הפרטי (private member) m_length בשורה 8 יגרום לשגיאת קומפילציה. הקוד יתקמפל ללא שגיאות ולא יגרום לשגיאות זמן-ריצה (run-time errors). בשורה 7, "print" חייבת להיות מוגדרת כפונקציה ולא כמתודה (method) של המחלקה Movie. הרצת הקוד של "print" תגרום לשגיאות זמן-ריצה. תשובה - 3

9 Operator Overloading מהי הצורה הנכונה להכריז על אופרטור ההשמה (assignment operator), להשמת אובייקטים מהמחלקה (class) Y למחלקה X. כלומר, מתודה המאפשרת את שורות הקוד הבאות: X a; Y b; a = b;

10 Operator Overloading void Y::operator=(Y const &from);
void X::operator=(X& to, Y const &from); Y& X::operator=( Y const &from); X& X::operator=( Y const &from); Y& Y::operator=( X const &to); תשובה 4

11 Copy Constructor class 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; };

12 Copy Constructor תשובה 1
int func4() { Person p( 1234,789); foo(p); p.show(); } void foo( Person p ) { p.show();} הקוד של הפונקציה "func4” עלול לגרום לשגיאות זמן-ריצה (run-time errors). הקוד של הפונקציה "func4” יגרום לשגיאות בקומפילציה. הקוד של הפונקציה "func4” ירוץ ויתקמפל ללא שגיאות. אם נגדיר את המתודה (method) show כמתודה סטטית, הקוד יתקמפל וירוץ ללא שגיאות. תשובה 1

13 Student class 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; };

14 Exceptions id 1234 //assuming Student : public Person
int 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

15 Animal class 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; };

16 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; } };

17 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 ctor dog ctor how how dog dtor animal dtor

18 Frog class 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 ) {} };

19 Dynamic Casting (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 ); לאחר ריצת שורות הקוד הנ"ל, d1=NULL (כלומר, הפוינטר d1 יקבל את הערך NULL). לאחר ריצת שורות הקוד הנ"ל, d2=NULL באחת השורות עלולה להיות שגיאת זמן ריצה (run-time error). תשובה 1

20 Type Checking! Dog d1("humi"); Dog d2("muki"); (1) Dog& d3 = d2;
(4) Dog& d5 = new Dog("rambo"); סמנו את כל השורות שיגרמו לשגיאת קומפילציה (4) Trying to convert Dog* to Dog&

21 Hidden Constructor האם השורות הבאות מתקמפלות ללא שגיאה?
Frog f(“kermit”); vector<Frog> fv; fv.push_back(f); fv[0].print(); instantiated from `vector<_Tp, _Alloc>::push_back (const _Tp &) [with _Tp = Frog, _Alloc = allocator<Frog>]' `Frog::Frog (const Frog &)' is private Will not compile – Frog’s copy c-tor is private

22 Conversion void 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); //1 bar(s); //2 apple(c); //3 return 0; } 2 – trying to convert string to char* , no implicit cast available

23 Const (2) converting foo const& to foo&.
struct foo { int* p; }; class bar { foo m_foo; public: bar(int *q) { m_foo.p = q; } foo const& getFoo() const { return m_foo; }//1 foo & getApple() const { return m_foo; }//2 }; int main() { int i, j; bar mybar(&i); *(mybar.getFoo().p) = 7; //3 mybar.getFoo().p = &j; //4 } Which lines will not compile ? (2) converting foo const& to foo&. (4) assigning to a const pointer

24 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;


Download ppt "Exam Example Questions"

Similar presentations


Ads by Google