Question

QUESTION 10 According to the course materials, if we have defined class P, class R, and...

QUESTION 10

  1. According to the course materials, if we have defined class P, class R, and class T as follows:
    class P
    {
    public:
           void set_values (int a, int b);
           int get_width() const;
           int get_height() const;
           virtual int area ();
    private:
           int width;
           int height;
    };
    void P::set_values(int a, int b)
    {
         width = a;
         height = b;
    }
    int P::area()
    {
        return -1;
    }
    int P::get_height() const
    {
         return height;
    }
    int P::get_width() const
    {
         return width;
    }


    class R: public P
    {
    public:
           virtual int area (void);
    };
    int R::area()
    {
          return (get_height() * get_width());
    }


    class T: public P
    {
    public:
            virtual int area (void);
    };
    int T::area()
    {
           return (get_width() * get_height() / 2);
    }


    What is the output when the following main() function is executed?
    int main() {
        R rect;
       T trgl;
        P * ppoly1 = ▭
        P * ppoly2 = &trgl;
        ppoly1->set_values (2, 5);
       ppoly2->set_values (2, 5);
        cout << ppoly1 -> area() ;
        cout << ppoly2 -> area();
        return 0;
    }

    1.

    (a) Nothing would be displayed.

    2.

    (b) 105

    3.

    (c) 1020

    4.

    (d) -1-1

    5.

    (e) 510

    6.

    (f) 501

    7.

    (g) None of (a) through (f) is a correct answer.

How many times is the symbol ’#’ printed by a successful function call foo(4)?
void foo (int i) {
     if (i > 1) {
           foo (i/2);
           foo (i/2);
      }
      cout << "##";
}

1.

(a) 6

2.

(b) 12

3.

(c) 14

4.

(d) 16

5.

(e) 18

6.

(f) None of (a) through (e) is a correct answer.

According to the course materials, given the following declarations:
   class B {
        public:
             virtual void f1();
             void f2();
   };

   class D: public B {
        public:
             virtual void f1();
             void f2();
    };


And the following code:
     B *b = new D;
    b->f1();       // line 1
    b->f2();       // line 2


Which functions are called for line 1 and line 2, respectively?

1.

(a) B::f1() is called for line 1 and B::f2() is called for line 2

2.

(b) B::f1() is called for line 1 and D::f2() is called for line 2

3.

(c) D::f1() is called for line 1 and D::f2() is called for line 2

4.

(d) D::f1() is called for line 1 and B::f2() is called for line 2

5.

(e) None of (a) through (d) is a correct answer.

QUESTION 14

  1. According to the course materials, when a request is made through a base-class pointer or reference to use a virtual function, C++ chooses the correct overridden function in the appropriate derived class associated with the object.

    1.

    (a) True

    2.

    (b) False

According to the course materials, which of the following would not be appropriate to be included in a header file?

1.

(a) definitions of classes.

2.

(b) declarations of constants.

3.

(c) declarations of nonmember functions.

4.

(d) definitions of member functions.

5.

(e) declarations of global variables.

6.

(f) None of (a) through (e) is a correct answer.

According to the course materials, to declare class subclass as a publicly derived class of superclass, one would write:

1.

(a) class subclass : superclass

2.

(b) class subclass :: superclass

3.

(c) class subclass < superclass >

4.

(d) class subclass inherits public superclass

5.

(e) class subclass : public superclass

6.

(f) None of (a) through (e) is a correct answer.

QUESTION 17

  1. According to the course materials, if we have defined class P, class R, and class T as follows:
    class P
    {
    public:
           void set_values (int a, int b);
           int get_width() const;
           int get_height() const;
           int area ();
    private:
           int width;
           int height;
    };
    void P::set_values(int a, int b)
    {
         width = a;
         height = b;
    }
    int P::area()
    {
        return -1;
    }
    int P::get_height() const
    {
         return height;
    }
    int P::get_width() const
    {
         return width;
    }


    class R: public P
    {
    public:
            int area (void);
    };
    int R::area()
    {
          return (get_height() * get_width());
    }


    class T: public P
    {
    public:
             int area (void);
    };
    int T::area()
    {
           return (get_width() * get_height() / 2);
    }


    What is the output when the following main() function is executed?
    int main() {
        R rect;
       T trgl;
        P * ppoly1 = ▭
        P * ppoly2 = &trgl;
        ppoly1->set_values (2, 5);
       ppoly2->set_values (2, 5);
        cout << ppoly1 -> area() ;
        cout << ppoly2 -> area();
        return 0;
    }

    1.

    (a) Nothing would be displayed

    2.

    (b) 501

    3.

    (c) 105

    4.

    (d) 00

    5.

    (e) -1-1

    6.

    (f) 2010

    7.

    (g) None of (a) through (f) is a correct answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

What is the output when the following main() function is executed?
int main() {
    R rect;
   T trgl;
    P * ppoly1 = ▭
    P * ppoly2 = &trgl;
    ppoly1->set_values (2, 5);
   ppoly2->set_values (2, 5);
    cout << ppoly1 -> area() ;
    cout << ppoly2 -> area();
    return 0;
}

Answer is b) 105

Reason: runtime binding cause by virtual function which calculate area first for rectangle that is 2*5 = 10

and then area for polygon = 2*5/2 = 5

so ans is 105

How many times is the symbol ’#’ printed by a successful function call foo(4)?
void foo (int i) {
     if (i > 1) {
           foo (i/2);
           foo (i/2);
      }
      cout << "##";
}

Answer is (c) 14 times

i

/ \

i/2 i/2

4 --- ##

/ \

2 2--------- ## ##

/ \ / \

1 1 1 1--------

## ## ## ##

therefore total 14 times

According to the course materials, given the following declarations:
   class B {
        public:
             virtual void f1();
             void f2();
   };

   class D: public B {
        public:
             virtual void f1();
             void f2();
    };

And the following code:
     B *b = new D;
    b->f1();       // line 1
    b->f2();       // line 2
Answer is: d) (d) D::f1() is called for line 1 and B::f2() is called for line 2

D::f1() due to runtime binding

B::f2() due to compile time binding.

Question 14 a) True

According to the course materials, which of the following would not be appropriate to be included in a header file?

Answer: c)declarations of nonmember functions.

According to the course materials, to declare class subclass as a publicly derived class of superclass, one would write:

Answer: (e) class subclass : public superclass

QUESTION 17

Answer e) -1-1

Reason compile time binding it see the pointer and points to the function belonging to pointer type class.

/*PLEASE UPVOTE (THANK YOU IN ADVANCE) IF YOU SATISFY WITH THE ANSWER IF ANY QUERY ASK ME IN COMMENT SECTION I WILL RE-SOLVE THE QUESTION FOR YOU */

Add a comment
Know the answer?
Add Answer to:
QUESTION 10 According to the course materials, if we have defined class P, class R, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT