Saturday, October 4, 2008

C, C++ Interview Q & A

Download link:
Download link

C++ code examples for job interviews

 Write a short code using C++ to print out all odd number from 1 to 100 using a for loop(Asked by people)

for( unsigned int i = 1; i < = 100; i++ )
    if( i & 0x00000001 )
        cout <<>

ISO layers and what layer is the IP operated from?( Asked by Cisco system people)

cation, Presentation, Session, Transport, Network, Data link and Physical. The IP is operated in the Network layer.

 Write a program that ask for user input from 5 to 9 then calculate the average( Asked by Cisco system people) main()
int MAX=4;
int total =0;
int average=0;
int numb;
cout<<"Please enter your input from 5 to 9"; cin>>numb;
if((numb <5)&&(numb>9))
cout<<"please re type your input"; else for(i=0;i<=MAX; i++) { total = total + numb; average= total /MAX; } cout<<"The average number is"<<

return 0;

Can you be bale to identify between Straight- through and Cross- over cable wiring? and in what case do you use Straight- through and Cross-over? (Asked by Cisco system people)

A. Straight-through is type of wiring that is one to to one connection Cross- over is type of wiring which those wires are got switched

We use Straight-through cable when we connect between NIC Adapter and Hub. Using Cross-over cable when connect between two NIC Adapters or sometime between two hubs.

If you hear the CPU fan is running and the monitor power is still on, but you did not see any thing show up in the monitor screen. What would you do to find out what is going wrong? (Asked by WNI people)

A. I would use the ping command to check whether the machine is still alive(connect to the network) or it is dead.


C++ object-oriented interview questions

How do you write a function that can reverse a linked-list? (Cisco System)

void reverselist(void)
                        head->next = 0;
                        tail->next = head;
                        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;

What is polymorphism?

Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.

Example: function overloading, function overriding, virtual functions.

How do you find out if a linked-list has an end? (i.e. the list is not a cycle)

You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle.

How can you tell what shell you are running on UNIX system?

You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.

What is Boyce Codd Normal form?

A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a->b, where a and b is a subset of R, at least one of the following holds:

  • a->b is a trivial functional dependency (b is a subset of a)
  • a is a superkey for schema R


Interview questions on C/C++

A reader submitted the interview questions he was asked. More C/C++ questions will be added here, as people keep sending us a set of 2-3 questions they got on their job itnerview.

Tell how to check whether a linked list is circular.

A: Create two pointers, each set to the start of the list. Update each as follows:

while (pointer1) {
 pointer1 = pointer1->next;
 pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;
 if (pointer1 == pointer2) {
   print (\"circular\n\");

OK, why does this work?

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet.

 How can you quickly find the number of elements stored in a a) static array b) dynamic array ?

Why is it difficult to store linked list in an array?

 How can you find the nodes with repetetive data in a linked list?

Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable but aac or bcd are unacceptable.

This is a C question that I had for an intern position at Microsoft: Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. After I wrote out my function, he asked me to figure out from the code how many times the printf statement is run, and also questions on optimizing my algorithm.

 What’s the output of the following program? Why?

            typedef union
                        int a;
                        char b[10];
                        float c;
            Union x,y = {100};
            x.a = 50;
            x.c = 21.50;
            printf(\"Union x : %d %s %f \n\",x.a,x.b,x.c );
            printf(\"Union y :%d %s%f \n\",y.a,y.b,y.c);

Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively)

 What is output equal to in

output = (X & Y) | (X & Z) | (Y & Z)

C++ gamedev interview questions

This set of questions came from a prominent gaming company. As you can see, the answers are not given (the interviews are typically conducted by senior developers), but there’s a set of notes with common mistakes to avoid.

Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at:

const char *

char const *

char * const

Note: Ask the candidate whether the first declaration is pointing to a string or a single character. Both explanations are correct, but if he says that it’s a single character pointer, ask why a whole string is initialized as char* in C++. If he says this is a string declaration, ask him to declare a pointer to a single character. Competent candidates should not have problems pointing out why const char* can be both a character and a string declaration, incompetent ones will come up with invalid reasons.

You’re given a simple code for the class BankCustomer. Write the following functions:

Copy constructor

= operator overload

== operator overload

+ operator overload (customers’ balances should be added up, as an example of joint account between husband and wife)

Note:Anyone confusing assignment and equality operators should be dismissed from the interview. The applicant might make a mistake of passing by value, not by reference. The candidate might also want to return a pointer, not a new object, from the addition operator. Slightly hint that you’d like the value to be changed outside the function, too, in the first case. Ask him whether the statement customer3 = customer1 + customer2 would work in the second case.

What problems might the following macro bring to the application?

#define sq(x) x*x

Consider the following struct declarations:

struct A { A(){ cout << \"A\"; } };
struct B { B(){ cout << \"B\"; } };
struct C { C(){ cout << \"C\"; } };
struct D { D(){ cout << \"D\"; } };
struct E : D { E(){ cout << \"E\"; } };
struct F : A, B
       C c;
       D d;
       E e;
       F() : B(), A(),d(),c(),e() { cout << \"F\"; }

What constructors will be called when an instance of F is initialized? Produce the program output when this happens.

Anything wrong with this code?

T *p = new T[10];
delete p;

Note: Incorrect replies: “No, everything is correct”, “Only the first element of the array will be deleted”, “The entire array will be deleted, but only the first element destructor will be called”.

Anything wrong with this code?

T *p = 0;
delete p;

Note: Typical wrong answer: Yes, the program will crash in an attempt to delete a null pointer. The candidate does not understand pointers. A very smart candidate will ask whether delete is overloaded for the class T.

Explain virtual inheritance. Draw the diagram explaining the initialization of the base class when virtual inheritance is used.
Note: Typical mistake for applicant is to draw an inheritance diagram, where a single base class is inherited with virtual methods. Explain to the candidate that this is not virtual inheritance. Ask them for the classic definition of virtual inheritance. Such question might be too complex for a beginning or even intermediate developer, but any applicant with advanced C++ experience should be somewhat familiar with the concept, even though he’ll probably say he’d avoid using it in a real project. Moreover, even the experienced developers, who know about virtual inheritance, cannot coherently explain the initialization process. If you find a candidate that knows both the concept and the initialization process well, he’s hired.

What’s potentially wrong with the following code?

long value;
//some stuff
value &= 0xFFFF;

Note: Hint to the candidate about the base platform they’re developing for. If the person still doesn’t find anything wrong with the code, they are not experienced with C++.

What does the following code do and why would anyone write something like that?

void send (int *to, int * from, int count)
       int n = (count + 7) / 8;
       switch ( count  %  8)
                    case 0: do { *to++ = *from++;
                    case 7: *to++ = *from++;
                    case 6: *to++ = *from++;
                    case 5: *to++ = *from++;
                    case 4: *to++ = *from++;
                    case 3: *to++ = *from++;
                    case 2: *to++ = *from++;
                    case 1: *to++ = *from++;
                    } while ( --n > 0 );

In the H file you see the following declaration:

class Foo {
void Bar( void ) const ;

Tell me all you know about the Bar() function.



Courtesy  Akhila J S,  MCA, S N IT,  Kollam

No comments:

Post a Comment