Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 39 - 40 of 354

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 39

C & C Plus Plus
Edit

Describe in Detail

Essay▾
  1. main ()
  2. {
  3.     statici =3;
  4.     printf ( “%d” ,i--);
  5.     returni>0? main ():0;
  6. }

Explanation

  • In the program
Table Showing the Program
static i = 3;
  • Define the static variable i = 3
printf ( “% d” , i-) ;
  • Printf prints the value of I and is then decreased by one
return i > 0? main () : 0;
  • Check the “I” value is greater than 0 than print the value and if value of “i” is less than 0 than call main () .
  • So the answer is 321
  • Then 0 is returned

Question 40

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Find the output of the following program

  1. classbase
  2. {
  3.     public:
  4.     VoidbaseFun ()
  5.     {
  6.         cout≪ “frombase”≪endl;
  7.     }
  8. };
  9. classderi:Publicbase
  10. {
  11.     public:
  12.     VoidbaseFun ()
  13.     {
  14.         cout≪ “fromderived” ≪endl;
  15.     }
  16. };
  17. voidSomeFunc (base ⚹baseObj)
  18. {
  19.     baseObj ⇾ baseFun ();
  20. }
  21. intmain ()
  22. {
  23.     basebaseObject;
  24.     SomeFunc (&baseObject);
  25.     derideriObject;
  26.     SomeFunc (&deriObject);
  27. }

Explanation

  • In the program
Table Showing the Program
  1. classbase
  2. {
  3.     public:
  4.     VoidbaseFun ()
  5.     {
  6.         cout ≪ “frombase” ≪ endl;
  7.     }
  8. };
  • In base class define the void baseFun () function
  • In a baseFun () cout prints “from base”
  1. classderi:Publicbase
  2. {
  3.     public:
  4.     VoidbaseFun ()
  5.     {
  6.         cout ≪ “fromderived” ≪ endl;
  7.     }
  8. };
  • In deri class define the void baseFun () function
  • In a baseFun () cout prints “from derived”
  1. voidSomeFunc (base ⚹baseObj)
  2. {
  3.     baseObj ⇾ baseFun ();
  4. }
SomeFunc expects a pointer to a base class.
  1. intmain ()
  2. {
  3.     basebaseObject;
  4.     SomeFunc (&baseObject);
  5.     derideriObject;
  6.     SomeFunc (&deriObject);
  7. }
  • Define the base () function object baseObject
  • SomeFunc () function pass the baseobject address
  • Define the deri () function object deriObject
  • SomeFunc () function pass the deriobject address
  • A pointer to a derived class object is passed but SomeFunc treats it as a base class pointer and the corresponding base function is called.
  • Remember that baseFunc is not a virtual function and does not support polymorphism.

Developed by: