Skip to main content

General Programming Interview Questions

1.

How many problems can you spot in the following class definition?

class MyString()

{

MyString()

{

}

~MyString()

{

delete m_pText;

}

void Set(char* pText)

{

size_t length = strlen(pText);

m_pText = new char[length];

}

private:

m_pText;

}

int _tmain(int argc, _TCHAR* argv[])

{

return 0;

}

  1. Class declaration is wrong ended with ().
  2. Type for m_pText is not specified.
  3. Class declaration closing brace is not ended with semicolon
  4. m_pText should be deleted as delete[] and not just delete.
  5. you cant take the strlen of pointer as if it is not null terminated one it will give wrong results.

2.

Explain the difference between the following...

- public - These members can be accessed any where, inside class outside class in derived class.

- protected – can be used within the class and inside derived classes.

- private - can be used only inside the class,

- mutable – if a const method is declared which ensure that the state of object or member variable will not be modified inside such const method . one can declare the variable as mutable and it modified by const method. Basically used for const ojbects.

3.

Write a template function to swap two values of the same type.

template <class type> void swap( type & t1, type & t2)

{

type t3 = t1;

t1= t2;

t2 = t3;

}

4.

Declare a structure which could be used as a single node in a double linked list. The node will store one integer and a pointer to a long.

struct NODE {

Node * next;

long * ptr;

int data;

};

5.

Given the following definitions:

#define STYLE_MINIMIZE 0x01

#define STYLE_MAXIMIZE 0x02

#define STYLE_CLOSE 0x04

#define STYLE_ON 0x08

1. Write a function which takes an unsigned integer and a set of style flags. The function should return true if all flags are set.

Struct {

Bool Func( unsigned int arg1, int style)

{

Return ( (arg1 & STYLE_MINIMIZE) && (arg1 & 0x02) &&(arg1 & 0x03)&& (arg1 & 0x04))? True : false;

}

2. Write a function which takes a reference to an unsigned integer and a set of style flags. The function should add the flags if they don't exist and remove them if they do.

3. Define an unsigned integer variable with both the minimize and maximize style.

4. Add the close style.

5. Use the method from #2 t o add and remove the ON style.

6.

How would you reverse the words in a string? E.g., “the quick brown fox” -> “fox brown quick the”

Void StringReverse( char * ptr)

{

Int I =0;

Whil(ptr[i] != ‘\0’) i++;

Char *temp = new char[i];

Int j =I;

While( i>0)

{

Temp[j-i] = ptr[i];

}

Strcpy(ptr, temp);

}

7.

How would you count the number of bits switched on in a number?

1. operate with ‘&’ operation on the specific numbered bit.

int bitcount (unsigned int n)

{

int count=0;

int counter =0;

while (n)

{

count += n & 0x1u ;

n >>= 1 ;

counter++;

}

return count ;

}

8.

What is wrong with the following code?

void CopyTheString(TCHAR *sourceString, TCHAR *destString)
{
size_t lengthOfSource = _tcslen(sourceString);
TCHAR *tempPtr = (TCHAR *) malloc(lengthOfSource);
memmove(tempPtr, sourceString, lengthOfSource);
destString = tempPtr;
}

9.

How would you reverse a singly linked list?

You need to keep two extra pointer to hold the pointers

10.

There is an array of sorted items. An arbitrary element is picked in this array and the portion of the array starting at that element is moved to the front and the portion of the array before this element is pushed further down. What is the best algorithm (asymptotically) for finding an element in this new modified array?

Binary search

11.

X=a, Y=b. How would you swap the values in X and Y without using additional variables?

x= x+y;

y = x-y;

x= x - y

12.

Why would you not want to throw an exception from a destructor?

The memory allocated after the throw statement will not be freed forever.

Comments

Popular posts from this blog

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.