Question: Q4. Consider this class: #include using namespace std; class HelloGoodbye { public: string name_; HelloGoodbye(const string& s) { name_ = s; cout < < Hi
Q4. Consider this class: #include
What will the following code print to cout? void f() { HelloGoodbye A("A"); { HelloGoodbye B("B"); } HelloGoodbye C("C"); }
Q5. Calulate: 1+2+...+n in three ways using three functios sumI, sumR and sum, They will take one integer paramter as input and return the sum. (a) Iteratively - sumI (b) Recursively - sumR (c) math formula - sum Q6. What Does this Function F Do? int F(unsigned int m, unsigned int n) { int sum = 0; if (odd(m)) // odd(m) is true if and only if m is an odd number sum = n; while (m > 1) { m = m/2; n = 2*n; if (odd(m)) sum += n; } // while return sum; }
Code Printed on cout cout << F(4,7); cout << F(7,4); cout << F(8,2); cout << F(3,8);
Write the code for odd function using ternary syntax.
Q7. Write a class called Adder that stores the sum of all the ints given to it. Your Adder class should allow you to write the following code (and code like it): // sample code in main function Adder sum1; // sum1 is initialized to 0 Adder sum2(2); // sum2 is initialized to 2 cout << um1 is ?<< sum1 << endl; // prints um1 is 0? cout << um2 is ?<< sum2 << endl; // prints um2 is 2? sum1 += 5; // adds 5 to sum1; now sum1 is 5 sum2 += -3; // adds -3 to sum2; now sum2 is -1 if (sum1 == sum2) cout << um1 and sum2 are the same ? else cout << "sum1 and sum2 are NOT the same: " << sum1 <<" " << sum2 << " "; You should only write the functions that are necessary for Adder to be used as in the above program. Use const wherever appropriate, and do not write or use a cast operator.
Q8. Destructors should never be virtual because you don't really override a destructor when you create a subclass.
True False
Q9. Can static methods cannot be virtual? True False
Can they are not polymorphic? True False
Q10. class Super { public: virtual void foo() { cout << "Super::foo()" << endl; } virtual void foo(int i) { cout << "Super::foo(" << i << ")" << endl; } }; class Sub : public Super { public: virtual void foo() { cout << "Sub::foo()" << endl; } };
Since Sub doesn't override the one-argument version of Super::foo(int), the following code will call Super's version, outputting "Super::foo(3)" -
Sub mySub; mySub.foo(3);
True False
Q11. If you overload operator+, you must return an object of the same type as your parameters. Otherwise, it won't compile. For example, the following code will not compile: // I claim that this won't compile Bar operator+(const Foo& lhs, const Foo& rhs); True False
Q12. What will this program print? class Enemy { public: Enemy() { cout << "E constructor" << endl; } Enemy(int i) { cout << "E constructor " << i << endl; } Enemy(const Enemy& src) {cout << "E copy constructor"<< endl;} Enemy& operator=(const Enemy& rhs) {cout<<"E="< Q13. Given the C++ declaration class A {public: int x; protected: int y;}; Which of the following would be rejected by the compiler? i. class B: public A { void f() { x = y; }}; ii. class B { void f() { A a; a.x = a.y; }}; iii. class B: public A{}; class C: public B { void f() { x = y; }}; iv. None of the above Q14. The output of the C++ code, class A { public: void f(int x) { cout << x << " "; }}; class B: public A { public: void f(int y) { A::f(y+1); }}; void g(A a, B b) { a.f(3); b.f(3); } int main() { B p; B q; g(p,q); } would be i. 3 3 ii. 3 4 iii. 4 4 iv. None of the above Q15. The signature of the standard << operator for printing an integer is i. ostream &operator<<(ostream &, const int) ii. ostream &ostream::operator<<(ostream &, const int) iii. int &operator<<(ostream &, const int) iv. int int::&operator<<(ostream &, const int) Q16. Given the C++ code class A { public: A() { a = new int[3]; for(int i=0; i<3; i++) a[i] = i;} ~A() { delete[] a; } private: int *a; }; void init(A &x) //sets elements of x.a to to 0,1,2 { A y; //fresh object x = y; } int main() { A p; ... init(p); ... } You notice that as the program runs following the call init(p), the values stored in p.a start changing unpredictably. What might be happening? Fix this problem. Q17. The C++ copy constructor is invoked when an object is passed by reference to a function True False Q18. Define the terms dynamic overloading (also known as overriding) and static overloading. Q19. Write an expression that sets the value of x to zero, using only the pointer pppppx, as defined here: int x = 7; int *px = &x; int **ppx = &px; int ***pppx = &ppx; int ****ppppx = &pppx; int *****pppppx = &ppppx; Q20. Friend functions in a Class allow the function to access all the public and protected, but not the private data of the Class. T F Q21. In C++, every Class needs to have a copy constructor defined by the user. T F Q22. If ptr2 is set to ptr (a pointer given by the declaration har ptr[10]? then ptr2++ points to the cell ptr[1]; T F Q23. When creating an object of a class derived from a base class, the constructor for the base class is invoked before the one for the derived class. T F Q24. A class member that is to be shared among all objects of a class is called (A) A value member (B) A reference member (C) A const member (D) A static member Q25. Which of the following is true? (A) The delete operator destroys a dynamically allocated object. (B) A reference is a pointer. (C) A constructor can have a return type. (D) none of the above Q26. Which function is used to change the values of private data members of a class? (A) mutator (B) constructor (C) accessor (D) none of the above Q27. Which is used to specify that an object is not modifiable? (A) const (B) static (C) fix (D) none of the above Q28. Consider a class for an animal as follows: class Animal { protected: string name; Animal(std::string strName) : name(strName) {} public: string GetName() { return name; } virtual string Speak() { return "Bark"; } }; (a)Based on the Animal class to create a derived class called Cat which speaks eow? (b)Based on the Animal class to create a derived class called Dog which speaks oof? (c)Write the main function to use Cat and Dog.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
