Some C++ interview questions

  1. What is a void return type?
  2. How is it possible for two String objects with identical values not to be equal under the == operator?
  3. What is the difference between a while statement and a do statement?
  4. Can a for statement loop indefinitely?
  5. How do you link a C++ program to C functions?
  6. How can you tell what shell you are running on UNIX system?
  7. How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
  8. How do you write a function that can reverse a linked-list?
  9. Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
  10. What is a local class?
  11. What is a nested class?
  12. What are the access privileges in C++? What is the default access level?
  13. What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?
  14. How do you access the static member of a class?
  15. What does extern int func(int *, Foo) accomplish?
This entry was posted in C++. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

28 Comments on Some C++ interview questions

  1. Nirmal
    Posted 10/4/2007 at 9:30 am | Permalink

    for question no 6:

    we can find out the shell which we are currently working is in terminal we have like[bash/abc/home] here bash is the shell. so that you can find out like tat.

  2. Nirmal
    Posted 10/4/2007 at 9:32 am | Permalink

    question 12:

    you have 3 access modifiers in C++ 1. private 2. public 3. protected. defaultly c++ variable are Private. Java variables arr Public. C# varibles are Virtual

  3. Nirmal
    Posted 10/4/2007 at 9:34 am | Permalink

    14:

    you can use the static memeber or method of a class in Main function jus call class name. method or member name. tats enough

  4. Nirmal
    Posted 10/4/2007 at 9:35 am | Permalink

    15:

    we can use the Extern keyword function in any other file. we can call that function indepedently

  5. Harunya
    Posted 10/15/2007 at 11:44 pm | Permalink

    Question:1

    void doesn’t return any value

    Question:2
    By using an operator overloading we find the strings are same or not

    Question:3
    while check the condition in entry,do-while check the condition at exit of the loop statement ie., do-while works atleast once

  6. Bakkialakshmi
    Posted 10/26/2007 at 3:45 am | Permalink

    12. What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?

  7. Jeetendra
    Posted 10/30/2007 at 6:03 am | Permalink

    What is a local class?

    If a class is defined within a function that class is a local class which is local to the function and it can not be accessed outside the class.

  8. Jeetendra
    Posted 10/30/2007 at 6:04 am | Permalink

    Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
    Yes it can accept but it will go on infinite loop.

  9. Jeetendra
    Posted 10/30/2007 at 6:07 am | Permalink

    What is a nested class?
    Class within a class.

    How do you access the static member of a class?

    static data member -you can access thru member function or thru class name using scope resolution operator.
    static member function:-you can access thru class name using scope resolution operator.

  10. Abhishek
    Posted 11/5/2007 at 4:29 am | Permalink

    4. Can a for statement loop indefinitely?
    YES, leave conditional expression blank ie. in the format
    for(i=0;;i++)

  11. Abhishek Singh
    Posted 11/5/2007 at 5:08 am | Permalink

    How do you write a function that can reverse a linked-list?
    Ans:
    void reverselist(void)
    {
    if(head==0)
    return;
    if(head->next==0)
    return;
    if(head->next==tail)
    {
    head->next = 0;
    tail->next = head;
    }
    else
    {
    node* pre = head;
    node* cur = head->next;
    node* curnext = cur->next;
    head->next = 0;
    cur->next = head;

    for(; curnext!=0; )
    {
    cur->next = pre;
    pre = cur;
    cur = curnext;
    curnext = curnext->next;
    }

    curnext->next = cur;
    }
    }

  12. Posted 11/29/2007 at 3:25 am | Permalink

    Q. #5
    You can do that by means of the extern "C" { } construct.

    Q. #6
    Easy, you could use the getenv function, like this:

    #include
    #include

    int main(){
    std::cout

    Q. #7
    Many ways to implement it, but you could basically add a counter to each node with initial value of 0, then after each node read increase its value, so before reading you compare if it has the same value or higher than the previous node then we’ve found a loop, if not (value = prev - 1), then increase the value counter + 1.

    Q. #9
    Depends on the compiler, whoever, the definition of copy constructor explicitly says that it can only have an const-reference of the same class as argument.

    Q. #12
    The access privileges are public, private and protected. public can be accessed from any subclass and even outside the scope of the class, protected can only be used from within the scope of the class and its subclasses, private can only be used within the scope of that class but is forbidden for any subclass. The default access privilege is private.

  13. Ramon Zamora
    Posted 12/12/2007 at 8:57 pm | Permalink

    #13
    In a diamond hierarchy, virtual inheritance allows a derive class “D” to share only one copy of the common base case “A”. This is done by using the keyword virtual on the declaration.
    For example
    class A{…}
    class B : virtual public A {…}
    class C : virtual public A {…}
    class D : public B, public C {} // class D only contains one copy of A.
    Advantage: The derived class contains only one copy of the base class.
    Disadvantage: The runtime cost is similar to the one of a virtual function. The compiler inserts a pointer (in D) that points to the location of the base “A”. And it has to dereference it, whenever is needed.

  14. Zabin
    Posted 12/13/2007 at 2:04 am | Permalink

    What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?
    Multiple Inheritance means there is only one base class and multiple no. of derived class are inheriting it.
    Virtual Inheritance: Suppose class A is inherited by class B and class C. Another class D is inheriting class B and C. Now class D is indirectly inheriting class A that to two times. This is an ambiguity. To avoid this we have amke the class A as Virtual base class and class B and C will have inherit is virtually. Now when D is inheriting both B and C, it will get only one copy of A rather than 2 copies(as in the earlier case). This virtual Inheritance.

  15. PORCHELVI
    Posted 12/28/2007 at 2:52 am | Permalink

    3.What is the difference between a while statement and a do statement?
    Ans:
    while is first check the condition and execute the statements
    but do first execute the statements then check the condition

  16. Manish
    Posted 1/14/2008 at 4:34 am | Permalink

    Q: What is a void return type?
    A: void return type indicates that the function returns nothing ( no value at all ). Although this should not be confused with the ” void * ” return type which is altogather different thing.
    Q: How is it possible for two String objects with identical values not to be equal under the == operator?
    A :
    Q : What is the difference between a while statement and a do statement?
    A : While checks the condition statement before the iteration whereas do checks the conditon statement after iteration.Thus do ensures that the loop block is executed at least once.
    Q : Can a for statement loop indefinitely?
    A : Yes . for (int i=0;i!=1;) cout

  17. vidhya
    Posted 2/29/2008 at 6:58 am | Permalink

    How do you find out if a linked-list has an end?

    Check the next(pointer) of each node, if it is NULL that is the end node. Linked list will be a loop if there is no NULL value in any of the node’s pointer.

  18. dina nassif
    Posted 3/11/2008 at 3:07 pm | Permalink

    Hi
    i wanna to ask aquistion , i’m try to do a programe to read from file expretion like this(create x [10,20,30]) and simulate  it in c++ cod here is my cod ,i’d be gratiful if any body help me to solve this problem.
    //******************************************/
    #include<iostream>
    #include<fstream>
    #include<vector>
    using namespace std;

    class MemoryTableEntry{
    public :
     char vn;
     vector* list;  //eh da?
    };class parser(){ MemoryTableEntry memroyTable[100];
     
     static int variable_index = 0; void parse(string str){
      if(c==create){  char vn=’x';  vector* v=new vector();  MemoryTableEntry entry;
      
      entry.vn = vn;
      enrty.list = v;
      
      memroyTable[variable_index] = entry;
      variable_index++;
      }
     }
     
    };
    void main(){
     fstream f1("F://f1.txt");
      char c;
     while(!f1.eof()){
        f1>>c;
     cout<<c;
     }
    //**************************************/

  19. vidya
    Posted 3/28/2008 at 9:43 pm | Permalink

    question 14.
    Those Class which inherits the properties of one or more base class is called multiplae inheritance.
    The class C inherits the properties of base class A & B. That means C is the derived class which inherits the properties of base class A & A.
    Advantages: It inherits all properties of each & every base class which it can inherit.

  20. KT
    Posted 4/7/2008 at 4:23 am | Permalink

    #4
    for(;;)

  21. matt
    Posted 4/7/2008 at 10:31 am | Permalink

    answer to:

    How is it possible for two String objects with identical values not to be equal under the == operator?

    the == operator looks for string variables in the same memory LOCATION on your hard drive, not for the same values (ie same letters, spaces, etc)

  22. praveen
    Posted 5/8/2008 at 6:07 am | Permalink

    question #6 :

    by using the echo $SHELL command

  23. Anuj Tripathi
    Posted 10/7/2008 at 3:32 pm | Permalink

    ANSWER 1
    Void is a empty data type , which is used as an access specifier but not as a variable declaration. :)

    ANSWER 2
    Use operator Overloading

    ANSWER 3
    while First check the condition then execute Statement
    but Do-While atleast one time runs the loop, then check the condition . :)

    ANSWER 4
    of course
    for(int i=0;;i++)

    ANSWER 5
    Use Extern c .

    ANSWER 6
    :( sorry dont no !

    IF U HAVE NE MORE QUESTION < SEND ME ON MY MAIL anuj_2cute@yahoo.co.in

    BREAK;

  24. bla
    Posted 11/11/2008 at 1:05 am | Permalink

    Question 7.
    Checking to see if a linked list ends:
    - Check to see if there are loops in the linked list: Have one pointer incremented 1 node at a time, another one 2 nodes at a time. If the 2 node pointed passes the 1 node pointer, it has looped. If the pointer that increments 2 at a time reaches the end, there is no loop.

  25. Posted 12/6/2008 at 9:50 pm | Permalink

    Question 8:
    ———–
    Function to reverse a list:

    #include

    typedef struct Node *Node_ptr;

    struct Node{
    int _data;
    Node_ptr _next;
    };

    //given list head, returns the head of the reversed list
    Node_ptr reverseList(Node_ptr head){
    if (!head){ //if head is NULL
    return head;
    }
    Node_ptr prev, curr;
    curr = head;
    prev = NULL;
    while (curr->_next){ //if next element is not null
    head = curr;
    curr = curr->_next;
    head->_next = prev;
    prev = head;
    }
    curr->_next = prev;
    return curr;
    }

  26. sreenu
    Posted 1/7/2009 at 1:13 am | Permalink

    3.difference between while and do is ,in while statement , first it will check the condition ,if condition is true it will enter into the loop,false it will exit.but in do if condition is false ,at first iteration it will enter into loop.after it will check the condition.

  27. sreenu
    Posted 1/7/2009 at 1:16 am | Permalink

    12.nested class means ,which is the class inside of the class .

  28. brazil42
    Posted 1/14/2009 at 11:36 am | Permalink

    #7: How do you find out if a linked-list has an end?

    You can’t simply follow the next-pointers until you find a NULL-pointer. This would result in an infinite loop if you have a ring!

    Linked lists can have three forms:
    l1 -> l2 -> l3 -> l4 -> NULL Linear
    l1 -> l2 -> l3 -> l4 -> l1 Closed ring
    l1 -> l2 -> l3 -> l4 -> l2 Open ring

    You must compare the pointer with the start-pointer to identify a Closed ring. This funktion also identifies Open rings:

    int getListForm(ListElem *p0)
    {
    if (!p0 || !po->next)
    return 0; // Linear

    ListElem *p1=p0, *p2=p1->next;
    bool bEven=true;
    for (;;)
    {
    p2=p2->next;
    if (p2==NULL)
    return 1; // Linear
    else if (p2==p0)
    return 2; // Closed ring
    else if (p2==p1)
    return 3; // Open ring

    bEven = !bEven;
    if (bEven)
    p1=p1->next; // Step every 2nd loop
    }
    }

    p2 “moves twice as fast” as p1.
    In an open ring, p2 will surely hit p1 after a finite number of loops because both will enter the closed loop in finite time.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*