Brush up your c basics part-2

 Hint : see the behavior of output on making some  changes in code.
1.   class Y
     {
              static int num;
              public:
                       Y(){num++;}
                       void show(){cout<<"Debugging "<<endl;}
                       ~Y(){num--;}
     };
     int Y::num=0;
     Y fun(Y y)
     {
              return y;
     }
     int main()
     {        Y y1;
              Y y2=fun(y1);
              y2=fun(y2);
               fun(y2).show();
     }
     Explain the output of this program? It is showing very absurd behavior. Hope you can help me.
2.   int main()
     {
              float f1=5.3;
              float f2=1.3;
              float f3=f1%f2;
     }
     I have not heard that % operator can be used with floats. Tell me if % cannot be used with
     floats, then how will you perform this function with floats?
3. #define cat(x,y) x##y
   int main()
   {
           printf("%d \n",cat(cat(1,2),3));
   }
   I want 123 as the output but it is not coming so. Make some additions in the code to achieve this
   output. You can see the file that results after preprocessing using “gcc -E” command.
4. Read the code carefully.
   class MBase
   {
           public:
                   virtual char* vf() const = 0;
                   virtual ~MBase() {}
   };
   class D1 : public MBase
   {
           public:
                   char* vf() const
                   {
                           return "D1";
                   }
   };
   class D2 : public MBase
   {
           public:
                   char* vf() const
                   {
                           return "D2";
                   }
   };
   class MI : public D1, public D2 {};
   int main() { }
   I thought C++ is not ambiguous, but it is. Can you remove the ambiguity in the above code.
5. int fun(int x,int y)
   {
           int s=2; int r=0;
           while(s<=x && s<=y)
           {
                   r+=(x+y)%s;
                   x-=(x%s);
                    y-=(y%s);
                     s*=2;
            }
            return r;
   }
   What this code snippet is doing?
6. struct s
   {
            int a:16;
            int :0;
            int c:2;
   };
   int main()
   {
            printf("%d\n",sizeof(struct s));
   }
   I was excepting error in the above code because of zero bit-field but there is no error and
   it is giving output 8. Give the reason for it?
7. int main()
   {
            const int i=10;
            int *j=(int *)&i;
            printf("%d %d ",i,*j);
            *j=20; printf("%d %d\n",i,*j);
   }
   This program if compiled using gcc, and run, it is giving output 10 10 20 20. However if
   compiled using g++ and run, it is giving output 10 10 10 20. Explain the reason? Is it some
   compiler dependent thing, I am little bit confused? Explain it?
8. class Y
   {
            public:
                     Y()
                     {
                            cout<<" In Constructor Y"<<endl;
                     }
                     Y(const Y & a)
                     {
                            cout<<"In Copy Constructor Y"<<endl;
                     }
   };
   class X:public Y
   {
            public:
                    X()
                    {
                            cout<<" In Constructor X"<<endl;
                    }
                    X(const X & a)
                    {
                            cout<<"In Copy Constructor X"<<endl;
                    }
   };
   int main()
   {
            X y1; X y2=y1;
   }
   It is giving output -
   In Constructor Y
   In Constructor X
   In Constructor Y
   In Copy Constructor X
   It is not calling the base class copy constructor. Make some addition in the above program so
   that it also call the base class copy constructor in copying the object of derived class.
9. How many process are created in the program given below. And how many times “Hello”
   will be printed in output? Give the number in terms of n.
   int main()
   {
            for(i=0;i<n;i++)
            {
                    fork();
                    printf(Hello);
            }
            printf(Hello);
   }
10. What are re-entrant functions? What are their importance. Are the functions given below
    re-entrant? Why or Why not?
    int g_var = 1;
    int f()
    {
             g_var = g_var + 2;
             return g_var;
    }
    int g()
    {
             return f() + 2;
    }
11. This code is very simple. Somya gave me this code and asked the abnormal behavior for this
    code although she knew the answer but trying to test my IQ. Help me out to understand it.
    main()
    {
             while(1)
             {
                     fprintf(stdout,"hello-out");
                     fprintf(stderr,"hello-err");
                     sleep(1); //waits for 1 unit of time
             }
    }
    This is my observation- The above program seem to print “hello-out” after printing many times
    “hello-err”, not just one after the another .Suggest me reason for that. And also do some
    addition in the above code so that it first prints “hello-out” and then “hello-err” and keeps
    on printing one after the other till you quit the program.
12. Fill in the blank so that code works perfectly-
    #define mysizeof(x)        ______________________
    int main()
    {
             printf("%d\n",sizeof(int));
             printf("%d\n",sizeof(float));
             printf("%d\n",sizeof(double));
    }
    And also for any user defined data type and other data types it should work perfectly.
13. Are the expressions *ptr++ and ++*ptr same ? How are they different?What is the
    difference between p* and *p?
14. How will you place an object at an specific memory location? Hint: Overload new
15. What is the need for pure virtual destructor even though it's definition is provided not in the
    derived class (its definition is provided in the same class but not inline)? Give an
    example?
16. Create a class that cannot be derived i.e. Final class. But its objects can be created on stack
    and on heap both.
17. Create a class for which you can create only single object i.e. Singleton class.
18. Create a class such that its constructor is private but you should be able to create its object
    on stack. For example in the scenario given below, add something so that you are able to
    create its object on stack.
    class A
    {
            private:
                    A()
                    {}
    };
    Write at least two methods.
19. class X
    {
            public:
                    virtual void fun()
                    {
                            cout<<"Hello"<<endl;
                    }
    };
    class Z
    {
            int i;
            public:
                    Z(int a):i(a){}
                    void fun1()
                    {
                            cout<<"hello z "<<i<<endl;
                    }
    };
    int main()
    {
            Z *y=dynamic_cast<Z *>(new X());
            y->fun1();
    }
    Debug the given code.
20. Read the code carefully. In this code I tried my best to make the correct code. But then also
    segmentation fault, I hate this word. I was planning to go to Shubham since he knows much
    about “gdb debugger”, but before going to him I want you to suggest me the reason and
    solution such that Segmentation fault does not result.
    struct Student
    {
            char name[25];
            float CPI;
            int rollnum;
    };
    int main()
    {
            using namespace std;
            Student *p = new Student [3];
            strcpy(p->name, "Sourabh");
            p->CPI = 9.7;
            p->rollnum = 4023;
            p++;
            strcpy(p->name, "Avnindra");
            p->CPI = 9.5;
            p->rollnum = 4048;
            p++;
            strcpy(p->name, "Sandeep");
            p->CPI = 8.5;
            p->rollnum = 4089;
            p -= 2;
            int x;
            for(x=0;x<3;x++)
            {
                     cout << p->name << "\n";
                     cout << p->CPI << "\n";
                     cout << p->rollnum << "\n\n";
                     p++;
            }
            p -= 2;
            delete [] p;
            return 0;
    }
21. Look the code given below, it is so simple. Rajora was saying that it is showing some absurd
    behavior. I have not checked it. Please check it and explain the solution to remove the absurd
    behavior of this program.
    int main()
    {      char str[100];
           int i;
           while(1)
           {       cin>>i;
                   cout<<i;
                   cin.getline(str,100);
           }
    }
22. Following is code for iterative postorder traversal of tree but some part of code is missing.
    Complete it.
    void postorder(struct bst *root)
    {
           while(root)
           {
                   if(root->left)
                   {
                            push(root);
                            root=root->left;
                   }
                   else if(root->right)
                   {
                            push(root);
                            root=root->right;
                   }
                   else
                   {
                            printf("%d\n",root->data);
                            /* Insert code here */
                            if(top())
                                    root=top()->right;
                            else
                                    root=NULL;
                   }
           }
    ## You can use push, pop(delete the topmost value of stack and return it) and top(to get
    the value of top element)
23. The following code is a naive implementation of finding common ancestor of two nodes in a
    Binary Tree. Is it correct? If not please specify and correct the bug.
    Also write all the test cases for the programme.
    tree* commonParent(tree * S,tree* p1,tree* p2)
    {
            if(ser(S->right,p1)&& ser(S->right,p2))
                    return commonParent(S->right,p1,p2);
            if(ser(S->left,p1)&& ser(S->left,p2))
                    return commonParent(S->left,p1,p2);
            return S;
    }
    bool ser(tree *S,tree* ptr)
    {
            if(S!=NULL)
            {
                    if(*ptr==*S)
                    return true;
                    return(ser(S->left,ptr) || ser(S->right,ptr));
            }
            return false;
    }
24.  Avnindra have made a atof function which converts strings to double. Please write down test
    strings which he must check so that he is sure of his function's success.
                                    !!!ALL THE BEST!!!

Comments

Popular posts from this blog