Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 255 - 257 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 255

C & C Plus Plus
Edit

Write in Short

What is the output of the following program?

1. voidmain ()
2. {
3.     staticinti =5;
4.     if (--i)
5.     {
6.         main ();
7.         printf ( “%d” i);
8.     }
9. }

Explanation

In the program

 static int i = 5; The variable “i” is declared as static, hence memory for i will be allocated for only once, when it encounters the statement. if (-i){main () ;printf ( “% d” i) ;} The function main () will be called recursively unless i becomes equal to 0, and since main () is recursively called, so the value of static i i.e.. , 0 will be printed every time the control is returned.

Question 256

C & C Plus Plus
Edit

Write in Short

What is the output of the following program?

main(){ unsignedinti=65000; while(i++!=0); printf("%d",i);}

Explanation

In the program

 unsigned int i = 65000; Define the unsigned integer variable i = 65000. while (i ++ ! = 0) ;printf ( “% d” , i) ; Note the semicolon after the while statement.When the value of i becomes 0 it comes out of while loop.Due to post-increment on i the value of i while printing is 1

Question 257

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Find the output of the following program

1. classbase
2. {
3.     public:
4.     VirtualvoidbaseFun ()
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
 class base{    public:    Virtual void baseFun ()    {        cout ≪ “from base” ≪ endl;    };} In base class defines the virtual baseFun () functionIn a baseFun () cout prints “from base” class deri:Public base{    public:    Void baseFun ()    {        cout ≪ “from derived” ≪ endl;    }}; In deri class define the void baseFun () functionIn a baseFun () cout prints “from derived” void SomeFunc (base ⚹baseObj){    baseObj ⇾ baseFun ();} SomeFunc expects a pointer to a base class. int main (){    base baseObject;    SomeFunc (&baseObject);    deri deriObject;    SomeFunc (&deriObject);} Define the base () function object baseObjectSomeFunc () function pass the baseobject addressDefine the deri () function object deriObjectSomeFunc () function pass the deriobject addressSince a pointer to a derived class object is passed.Remember that baseFunc is a virtual function.That means that it supports run-time polymorphism.So the function corresponding to the derived class object is called

Developed by: