#include #include using namespace std; class Bob{ public: Bob(); int getA(); private: int A; void Init( int ); }; Bob::Bob() { A = 0; Bob::Init(A); // Line 0 } void Bob::Init(int A) { A = 1; cout << "A is initialized to " << A << endl; } int Bob::getA() { return A; } main () { Bob MyBob; // Line 1 cout << "Value of A is: " << MyBob.getA() << endl; // Line 2 } Q1- Output after Line 1 executes is: a- Nothing. Default constructor gets called which prints nothing. b- Nothing. Syntax error on Line 0. c- A is initialized to 0. d- A is initialized to 1. Q2- Output after Line 2 executes is: a- Segmentation fault b- Value of A is: Segmentation fault c- Value of A is 0 d- Value of A is 1 e- We will get a syntax error on Line 2. Q3- Suppose we had the following lines in main(): Bob BobArray[20]; BobArray = BobArray + 1; When compiled and run, this program will: a- Produce a syntax error because the + operator is not overloaded for Bob. b- Does not give a syntax error, but its behavior is undefined because we did not provide a copy constructor. c- Adds one more element to the Array. d- Shifts the address of the whole array in memory by 1. e- Adds one to BobArray[0].A f- Makes the first element of the array BobArray "disappear". /********************************************************/ #include #include using namespace std; class Bob{ public: Bob(); int getA(); private: int A; void Init( int ); }; Bob::Bob() { A = 0; Bob::Init(A); } void Bob::Init(int A) { A = 1; cout << "A is initialized to " << A << endl; } int Bob::getA() { return A; } class NamedBob { public: NamedBob(); string Name; Bob* thisBob; int getA(); private: int A; }; NamedBob::NamedBob() { Name = ""; A = 123; cout << "NamedBob constructed" << endl; } int NamedBob::getA() { return A; } main() { NamedBob* MyNamedBob; MyNamedBob = new NamedBob[2]; // Line 1 MyNamedBob->thisBob = new NamedBob::Bob; // Line 2 cout << "A is: " << MyNamedBob->thisBob->getA() << endl; // Line 3 } Q3- Output after Line 1 executes is: a- NamedBob constructed NamedBob constructed b- NamedBob constructed A is initialized to * NamedBob constructed A is initialized to * (where * is either 0 or 1) c- NamedBob constructed d- This program will give a syntax error on Line 1 because the [] operator is not overloaded for the NamedBob class. (Can use either Q4 or Q5 -- not both) Q4- (Erase Line 2) Assume any possible syntax errors on Line 1 are fixed such that MyNamedBob is an array of 2 NamedBob's. When the program is run, Line 3 produces a segmentation fault. How can we fix the problem? a- We can't fix this problem because thisBob was not initialized in the constructor. b- On Line 2, put: MyNamedBob->thisBob = new Bob; c- On Line 2, put: MyNamedBob->thisBob = new NamedBob::Bob; d- On Line 2, put: MyNamedBob.thisBob = new Bob; e- On Line 2, put: Bob MyNamedBob->thisBob; Q5- (Leave Line 2 in as it is) After Line 3 executes, the output will be: a- A is * (where * is either 0 or 1) b- A is 123 c- Segmentation fault d- Line 3 will produce a syntax error. /********************************************************/ /****************************** main.cpp ******************************/ #include #include #include "bob.h" // Line 1 using namespace std; template bool operator > (Bob& D1, Bob& D2) { if (D1.Value > D2.Value) return true; else return false; } main() { Bob Bob1(10); Bob Bob2(10); Bob1.Value = 4; Bob2.Value = 40; cout << Bob1.Value << endl; if (Bob2 > Bob1) cout << "Bob2 is greater" << endl; else cout << "It didn't work!" << endl; } /****************************** bob.h ******************************/ #ifndef DYNARRAY_H #define DYNARRAY_H template class Bob{ public: Bob(int initialvalue); T Value; }; #endif /****************************** bob.cpp ******************************/ #include #include #include "bob.h" using namespace std; template Bob::Bob(int initialvalue) { Value = initialvalue; cout << "Bob constructed\n"; } Q6- (Change Line 1 to #include "bob.h") When compiled, we get the following output: % g++ main.cpp bob.cpp /usr/bin/ld: Unsatisfied symbols: Bob::Bob(int)(code) collect2: ld returned 1 exit status % How do we fix this problem (according to Dr. Chuck)? a- Change Line 1 to #include "bob.cpp" b- Change Line 1 to #include c- Make operator > (Bob& , Bob&) a member of the class Bob. d- Make a default constructor for Bob. e- Make Value a private member variable of Bob. f- (a) and (c) g- None of the above. #include #include class Bob{ private: int* Value; public: Bob(); Bob(int); int getValue(); }; Bob::Bob() { Value = new int(10); } Bob::Bob(int A) { Value = new int(A); } int Bob::getValue() { return *Value; } int main() { Bob* Bob1 = new Bob; Bob Bob2(*Bob1); // Line 0 cout << "Bob1 Value: " << (*Bob1).getValue() << endl; // Line 1 delete Bob1; // Line 2 cout << "Bob2 Value: " << Bob2.getValue() << endl; // Line 3 } Q8 - (change Line1 to Bob1.getValue() ) In the above program, Line 1 gives a synatx error. We want this line to print out the value of *Value for Bob1. How do we fix it? a- cout << "Bob1 Value: " << (*Bob1).getValue() << endl; b- cout << "Bob1 Value: " << *Bob1.getValue() << endl; c- cout << "Bob1 Value: " << *Bob1->getValue() << endl; d- cout << "Bob1 Value: " << (*Bob1)->getValue() << endl; Q9- Assume correction to Line 1 is made so that we do not get a syntax error on Line 1. What would this program do at run time? a- output Bob1 Value: 10 Bob2 Value: 10 and will never produce a segmentation fault. b- Will output Bob1 Value: 10, but could produce a segmentation fault on Line 3. c- We will get a syntax error on Line 0 because no copy constructor is defined. d- We will get a syntax error on Line 2 because no destructor is defined. (Could possibly add another answer having to do with the memory leak in the program). Q10- main is modified as follows: main() { Bob Bob1; Bob Bob2[2] = {Bob1, 3}; // Line 1 cout << "Bob2[0] is: " << Bob2[0].getValue() << endl; // Line 2 cout << "Bob2[1] is: " << Bob2[1].getValue() << endl; // Line 3 } The above program when compiled and run will: a- Give a syntax error on Line 1 because we can not mix integers and type Bob in the same initialization. b- Will give a syntax error on Line 2 and Line 3. c- Will compile fine, and output: Bob[0] is: 10 Bob[1] is: 3 d- Will compile fine, and output: Bob[0] is: 10 Bob[1] is: 10 Q1) what is the output of the folowing code: class Bob { Bob() { cout<<"Bob constructor"< c) . d)>> q5) Which of the following is not true about friend function?. a) has access to private members b) Must be named as a friend in the class definition c) is a member function. d) Functions can be friends of several classes e) A member function of one class can be friend of another Q6) class Bob { Bob() { x=0 } private: int x; }; main() { Bob *b; //line1 } which of the following is not wrong to put in line1 a)(*b).x=5; b) b->x=5; c)b.x=5; d)*(b.x) =5; pick one: 1) a, b 2)c,d 3)only c 4)only d Q7)Cosider a class named bob which of the following is true: `+' is an overloaded operator. a)bob.xyx()-Must be a only member function b)xyz(bob) - must be only a friend c)2 + bob -Must be only a friend d)bob + 2 - Must be only a friend Q8) Which is true about stucture? a) structures have member variables as classes do b) structures have meber functions as classes do c) structures can instantiate variables of thier type as classes do d) all the above. Code for questions 1 and 2 class bob { public: bob() { cout<<"Hello"<