Академический Документы
Профессиональный Документы
Культура Документы
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.
#include <stdio.h>
main()
{
typedef union
{
int a;
char b[10];
float c;
}
Union;
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)
^Back to Top
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.
1. Explain which of the following declarations will compile and what will be
constant - a pointer or the value pointed at:
o const char *
o char const *
o 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.
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”.
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.
26. What does the following code do and why would anyone write something like
that?
27. void send (int *to, int * from, int count)
28. {
29. int n = (count + 7) / 8;
30. switch ( count % 8)
31. {
32. case 0: do { *to++ = *from++;
33. case 7: *to++ = *from++;
34. case 6: *to++ = *from++;
35. case 5: *to++ = *from++;
36. case 4: *to++ = *from++;
37. case 3: *to++ = *from++;
38. case 2: *to++ = *from++;
39. case 1: *to++ = *from++;
40. } while ( --n > 0 );
41. }
42. }
43. In the H file you see the following declaration:
44. class Foo {
45. void Bar( void ) const ;
46. };
---------
What will be the output of the following code?
7. void main ()
8. { int i = 0 , a[3] ;
9. a[i] = i++;
}
The output for the above code would be a garbage value. In the statement a[i] = i++; the
value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would
get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage
value.
-------------
7. Find the output :
void main()
{
int const * p=5;
cout<<++(*p);
}
Answer:Compiler error: Cannot modify a constant value.*
Explanation:
p is a pointer to a "constant integer". But wae tried to change the value of the
"constant integer".*
8. Find the output :
main()*
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
cout<<"\n"<<s[i]<<*(s+i)<<*(i+s)<<i[s]);}
Answer:
mmmm
aaaa
nnnn
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same
dea.*Generally*array name is the base*address*for that array. Here s is the base
address. i is the index number/displacement from the base address. So, indirecting it
with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].*
9. What will be the output?
Answer:
45545
Explanation:The arguments in a function call are pushed into the stack from left to
right. The evaluation is by popping out from the stack. and the evaluation is from
right to left, hence the result.*
-----
/ using C++ namespace std
#include <iostream>
Output:
Testing 1..2..3..
Press any key to continue . . .