RSS Feed for C/C++ Interview QuestionsCategory: C/C++ Interview Questions

What is a void return type? »

A void return type indicates that a method does not return a value.

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

The == operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory.

What is the difference between a while statement and a do statement? »

A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

Can a for statement loop indefinitely? »

Yes, a for statement can loop indefinitely. For example, consider the following:
for(;;) ;

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

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;
}
}

Can a copy constructor accept an object of the same class as parameter, instead of reference of the object? »

No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

What is a local class? Why can it be useful? »

Local class is a class defined within the scope of a function — any function, whether a member function or a free function. For example:
// Example 2: Local class
//
int f()
{
class LocalClass
{
// …
};
// …
};
Like nested classes, local classes can be a useful tool for managing code dependencies.

What is a nested class? Why can it be useful? »

A nested class is a class enclosed within the scope of another class. For example:
// Example 1: Nested class
//
class OuterClass
{
class NestedClass
{
// …
};
// …
};
Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in Example 1, if Nested Class is [...]