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

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?

#include <stdio.h>
main()
{
typedef union
{
int a;
char b[10];
float c;
}
Union;

Union x,y = {100};


x.a = 50;
strcpy(x.b,\"hello\");
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)

^Back to Top

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.

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.

2. What problems might the following macro bring to the application?

#define sq(x) x*x

3. Consider the following struct declarations:


4. struct A { A(){ cout << \"A\"; } };
5. struct B { B(){ cout << \"B\"; } };
6. struct C { C(){ cout << \"C\"; } };
7. struct D { D(){ cout << \"D\"; } };
8. struct E : D { E(){ cout << \"E\"; } };
9. struct F : A, B
10. {
11. C c;
12. D d;
13. E e;
14. 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.

15. Anything wrong with this code?


16. T *p = new T[10];
17. 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”.

18. Anything wrong with this code?


19. T *p = 0;
20. 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.

21. What’s potentially wrong with the following code?


22.
23. long value;
24. //some stuff
25. 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++.

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

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

---------
What will be the output of the following code?

7. void main ()

8. { int i = 0 , a[3] ;

9. a[i] = i++;

10. printf ("%d",a[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?

#define int char*


main()
{
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}
Answer: sizeof(i)=1
Explanation:
Since the #define replaces the string int by the macro char*
10. What will be the output?
main()*
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}

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>

// main() function with no argument


// and int return value
int main( )
{
std::cout<<"Testing 1..2..3..\n";
// return to system with no error
return 0;
}

Output:

Testing 1..2..3..
Press any key to continue . . .

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