Interview Question?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Interview Question?

  1. #1
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567

    Question Interview Question?

    Dear Friends
    i have four question please answer these questions..........

    Q1>
    ...
    .
    .
    .
    .
    switch(iVar)
    {
    case 0:cout<<"Zero";
    case 1:cout<<"One";
    case 2:cout<<"Two";
    }

    if suppose now iVar value is 0
    than what will be the output................

    i think it should be Zero

    though i know that i have not used "break" in each case

    what i know about break is that after excuting some match case if no break is present than the execution fall through next case if the case matches with the value for test........

    i know that i am wrong as output coming is different,please clarify my doubt.......

    Q2>
    what is the following code represent

    template<class T>
    T Run(T Process);

    choices were
    (a)template class declaration
    (b)template class definition
    (c)template function declaration
    (d)template function definition

    my answer was template function declaration

    but the interviewer said i am wrong.........
    how so?

    Q3>
    suppose there are two class
    class A
    {
    private:
    int num;
    public:
    .
    .
    .
    .
    /*other things*/
    };

    class B
    {
    private:
    .
    .
    .
    public:
    void UsenumOfA();//function which uses "num" of "class A"
    };

    now the question was without using inheritance how i can initialise "num" of "class A" in my class B function UsenumOfA()


    my answer was
    by using aggregation
    we would declare "A" object inside my class B and than we use inside the UsenumOfA() as shown below

    class A{ . . . . .};//same as above
    class B
    {
    private:
    A obja;
    public:
    void UsenumOfA()
    {
    obja.num = 20;//some integer value
    }

    }

    but i am getting error how so?

    Q4>
    last question
    if i initialise the memory with "malloc" and free it using "delete"
    what will happen
    my answer was either application will crash or some exception will occur or soemthing else but the application would not work correctly.......

    the interview said u r wrong how so?


    for the Q4 i am trying right now to see the result but atleast answer the first question...

    thanks
    vishal

  2. #2
    Join Date
    Feb 2002
    Posts
    69
    #include <iostream.h>

    int main()
    {
    int iVar=0;

    switch(iVar)
    {
    case 0:cout<<"Zero";
    case 1:cout<<"One";
    case 2:cout<<"Two";
    }
    return 0;
    }


    Output

    Zero one two

    There is no break given in the switch.So it will execute till the end.

    --------------------------------------------------------------
    template<class T>
    T Run(T Process);

    I guess it is

    (a)template class declaration

    --------------------------------
    I guess Friend Function should do teh trick if I am wrong correct me.

    #include <iostream.h>
    class A
    {
    private:
    int num;
    public:
    A()
    {
    num=10;
    }

    friend class B;
    };
    class B
    {
    public:
    void show(A &a)
    {
    a.num=40;
    cout<<a.num;
    }
    };

    int main()
    {
    A a;
    B b;

    b.show(a);
    return 0;
    }


    -----------------------------------------------------------------------------------

    Can I delete pointers allocated with malloc()?

    It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program

    Beware! Sometimes people say, "But I'm just working with an array of char." Nonetheless do not mix malloc() and delete on the same pointer, or new and free() on the same pointer! If you allocated via p = new char[n], you must use delete[] p; you must not use free(p). Or if you allocated via p = malloc(n), you must use free(p); you must not use delete[] p or delete p! Mixing these up could cause a catastrophic failure at runtime if the code was ported to a new machine, a new compiler, or even a new version of the same compiler.
    Last edited by shivabharat; October 31st, 2003 at 01:03 AM.

  3. #3
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567
    thanks SB
    i agree with u on answer of Q3 but i want to know how can i do the same thing using aggregation......

    also for Q2 mine answer was same as urs but it was incorrect according to interviewer....

    and about Q4
    i infer that we can mix malloc and delete or new or free only if they are not an array initialisation.

    correct me if i am wrong

    thanks
    vishal

  4. #4
    Join Date
    Apr 1999
    Posts
    27,446
    Answers

    Q1:

    ZeroOneTwo

    Q2:

    The answer that I would give is:

    Prototype for a template function called Run() that takes a template argument of T, has a function argument of T, and returns a T.

    The trick is to not have the interviewer force an answer on you by giving you multiple choice. Answer the question correctly in your own words if you believe the interviewer is forcing you to answer using multiple choice. This would give the interviewer the impression that you do know what it is, but don't entirely agree on the choices given.

    Q3:
    class A must have some sort of accessor function to change the num member variable, since num is private. I see no public accessor function for A in your question, so I can't answer it.

    Q4:
    The correct answer is "Undefined behavior".

    Your answer was incorrect. Anything can happen if you mix malloc with delete. Anything from the program working correctly to your machine rebooting, to formatting your hard drive can happen. When you specified what will happen, then your answer was incorrect.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 31st, 2003 at 05:04 AM.

  5. #5
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567
    paul thanks
    but for my Q3 i want to know whether i can use aggregation for initialising num of A in class B
    Thanks
    Vishal

    special thanks for your answer and guidance for question no 2

  6. #6
    Join Date
    Apr 1999
    Posts
    27,446
    Originally posted by VishalNikiSharma
    paul thanks
    but for my Q3 i want to know whether i can use aggregation for initialising num of A in class B
    Thanks
    Vishal

    special thanks for your answer and guidance for question no 2
    Yes, you can use aggregation, but class A must have a member function.
    Code:
    class A
    {
       int num;
    
       public:
           void SetNum(int n) { num = n; }
    };
    
    class B
    {
            A ThisA;
            void UseNumOfA(int n) { ThisA.SetNum(n); }
           
       public:
           B(int num = 20) { UseNumOfA(num); }
    };
    Also, is that all the interviewer gave you for class A and class B, or are you leaving out some details of each class?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Feb 2002
    Posts
    69
    Whats the output this program should give?

    Code:
    #include <iostream.h> 
    class A
    {
       int num;
    
       public:
           void SetNum(int n) 
    	   { 
    		   num = n; 
    		   cout<<"Inside A :"<<num<<endl;
    		   
    	   }
    
    	   void show()
    	   {
    		   cout<<num;
    	   }
    };
    
    class B
    {
            A ThisA;
            void UseNumOfA(int n) { ThisA.SetNum(n); }
           
       public:
           B(int num = 20) { UseNumOfA(num); }
    
    };
    
    int main()
    {
    	A aa;
    	B bb;
    	aa.show();
    	return 0;
    }
    I got this as output

    [QOUTE]
    Inside A :20
    -858993460Press any key to continue
    [/QOUTE]

    Also in my earlier posting I have used freind function can anyone say if that answer falls to the problem stated?

  8. #8
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567
    thanks paul
    yes interviewer give me this much only

    cant we use num of A without a function like SetNum as u have stated

    please explain in detail
    vishal

  9. #9
    Join Date
    Aug 1999
    Posts
    586
    Originally posted by VishalNikiSharma cant we use num of A without a function like SetNum as u have stated
    No you can't. "private" means nobody can access the member except the class itself or friends of that class. There is no accessor function (like "SetNum") nor "friend" relationship established here so the member is off-limits to the outside world. You should have simply explained this situation and given an example of how it could be done using an accessor function or friend (and in the case of a friend, it's best to restrict that friendship as much as possible by making "UsenumOfA()" a friend and nobody else - you can look up the syntax). In any event, multiple choice questions aren't always cut-and-dry so demonstrate your knowledge of the topic by explaining things in English if you must (as Paul did with the template question). Note that answer C is correct BTW (for the template question) without getting pedantic about it. That is, it's really a "function template" (declaration), not a "template function", but few in the real world really care (excluding members of the C++ committee, compiler writers, authors, etc.). What does the interviewer think the right answer is anyway?

  10. #10
    Join Date
    Apr 1999
    Posts
    27,446
    Originally posted by VishalNikiSharma
    thanks paul
    yes interviewer give me this much only

    cant we use num of A without a function like SetNum as u have stated

    please explain in detail
    vishal
    Explain this to the interviewer. As Sef pointed out, you cannot access the private variable from the outside unless there is a public member function that changes the variable, or B is made a friend of A.

    If this is a trick question of some sort, then an interview shouldn't be asking trick questions or questions that rarely, if ever, come up in real world C++ programming. If the interviewer is asking you this question as a trick instead of asking questions that test a knowledge of C++ and OOP from a real-world standpoint, the interviewer is not doing a good job.

    And I agree with Sef -- the answer for question 2 that closely fits the description is C, but an explanation in English by you may force the interviewer to explain why his/her answer is correct. Too many times, the interviewer just has a crib sheet of questions, has some one-letter answers written down, and is not ready for the interviewer who will give complete answers -- not just say "A", "B", "C", or "D". Who knows, maybe the interviewer was looking for you to provide good explanations for your answers.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    Well you can access it, IF by someway u can get hold of the address of that var. Either by providing the get function that returns the address (it seems you can modify class A :() or by minuplulating the address of Object of class A and moving to the private mem-var memory location (but thats not in any way standard C++ approach and is compiler dependant).
    Regards,
    Usman.
    Last edited by usman999_1; October 31st, 2003 at 12:39 PM.

  12. #12
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    637
    Strange, I was under an impression that interviewers only ask a few simple questions about language (to determine whether a person is familiar with it) and then give a task to solve.

    May I ask you, did they take you if some of your answers regarding to C/C++ knowledge was subjectively/objectively wrong?
    Thanks.

  13. #13
    Join Date
    Aug 1999
    Posts
    586
    Originally posted by usman999_1
    Well you can access it, IF by someway u can get hold of the address of that var. Either by providing the get function that returns the address (it seems you can modify class A ) or by minuplulating the address of Object of class A and moving to the private mem-var memory location (but thats not in any way standard C++ approach and is compiler dependant).
    Regards,
    Usman.
    Of course you can if you're willing to hijack the language's rules (at your own peril)

  14. #14
    Join Date
    Jul 2011
    Posts
    7

    Re: Interview Question?

    Hi,

    Good ideal, pls try to keep posting. I like this topic very much and I will digged this one. Tks again.

    If you want to get more materials that related to this topic, you can visit: http://interviewquestionsandanswers....s-and-answers/

    Best regards.
    Last edited by hambim336; December 8th, 2011 at 06:28 AM. Reason: Update

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Interview Question?

    This thread is over 8 years old.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center