Вы находитесь на странице: 1из 3

1. What is the most efficient way to reverse a linklist?

2. How to sort & search a single linklist?


3. Which is more convenient - single or double-linked linklist? Discuss the t
rade-offs? What about XOR-linked linklist?
4. How does indexing work?
5. char s[10];
s="Hello";
printf(s);
What will be the output? Is there any error with this code?
6. What is the difference between
char s[]="Hello";
char *s="Hello";
Please give a clear idea on this?
7. Why do we pass a reference for copy constructors? If it does shallow copy
for pass by value (user defined object), how will it do the deep cop
y?
8. What is the difference between shallow copy & deep copy?
9. What is the difference between strcpy and memcpy? What rule should we foll
ow when choosing between these two?
10. If we declare two variable and two applications are using the same variabl
e, then what will its value be, will it be the same?
-------------------------------------------------------------------------------
--------------------------------------
Some Answers
5. The declaration char s[10] implies that s has a fixed adrress in memory i.e
it is a constant pointer. The right hand operand produces a pointer to a m
emory location that holds the character string Hello . Thus, it is clear from
this code that there is an attempt to modify a constant pointer by assigning it
an address. This will give compilation error.
6.
char s[]= Hello ;
char *s= Hello ;
The only difference between the two statements is that the former is decla
ring a constant pointer to a variable string while the latter is declaring a
variable pointer to constant string .
i) char s[] = Hello ; s is constant pointer
we can change its contents like
s[0] = b , s[1]= y', s[2] = e ..so on
ii) char *s = Hello ; s is a variable pointer.
we cannot change its contents like above. Doing so produces run time e
rrors
--------------------------------------------------------------------------------
-------------------------------------
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 c
ycle)
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, in
stead 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?

Implementing itoa function is a popular interview question. Here s one implementat


ion from SAP.
char *itoa(int value)
{
int count, /* number of characters in string */
i, /* loop control variable */
sign; /* determine if the value is negative */
char *ptr, /* temporary pointer, index into string */
*string, /* return value */
*temp; /* temporary string array */
count = 0;
if ((sign = value) < 0) /* assign value to sign, if negative */
{ /* keep track and invert value */
value = -value;
count++; /* increment count */
}
/* allocate INTSIZE plus 2 bytes (sign and NULL) */
temp = (char *) malloc(INTSIZE + 2);
if (temp == NULL)
{
return(NULL);
}
memset(temp,'\0', INTSIZE + 2);
string = (char *) malloc(INTSIZE + 2);
if (string == NULL)
{
return(NULL);
}
memset(string,'\0', INTSIZE + 2);
ptr = string; /* set temporary ptr to string */
/*--------------------------------------------------------------------+
| NOTE: This process reverses the order of an integer, ie: |
| value = -1234 equates to: char [4321-] |
| Reorder the values using for {} loop below |
+--------------------------------------------------------------------*/
do {
*temp++ = value % 10 + '0'; /* obtain modulus and or with '0' */
count++; /* increment count, track iterations*/
} while (( value /= 10) >0);
if (sign < 0) /* add '-' when sign is negative */
*temp++ = '-';
*temp-- = '\0'; /* ensure null terminated and point */
/* to last char in array */
/*--------------------------------------------------------------------+
| reorder the resulting char *string: |
| temp - points to the last char in the temporary array |
| ptr - points to the first element in the string array |
+--------------------------------------------------------------------*/
for (i = 0; i < count; i++, temp--, ptr++)
{
memcpy(ptr,temp,sizeof(char));
}
return(string);
}

Вам также может понравиться