1.Write a short code using C++ to print out all odd number from 1 to 100 using a for loop
for( unsigned int i = 1; i < = 100; i++ )
if( i & 0x00000001 )
cout << i<<",";
2. Design and implement a String class that satisfies the following:
* Supports embedded nulls
* Provide the following methods (at least)
o Constructor
o Destructor
o Copy constructor
o Assignment operator
o Addition operator (concatenation)
o Return character at location
o Return substring at location
o Find substring
* Provide versions of methods for String and for char* arguments
3.
Which one of the following statements about operator delete is TRUE?
1) delete is equivalent to dealloc()
2) delete can be overloaded
3) delete is always more efficient than free()
4) delete is not as efficient as dealloc()
5) A delete call can be made on a variable of any type
4.
You have a class whose internal representation is not exposed to the user and can change transparently. What is that an example of?
1) Polymorphism
2) Abstraction
3) Derivation
4) Encapsulation
5) Inheritance
5.
char a;
char ca[] = {'a','e','i','o','u','y'};
char* pca = ca;
pca += 2;
a = *pca;
What values are be stored in 'a' after the above sample code
is run if it is run on a system where pointers are two bytes long?
1) a = 'u'
2) a = 'e'
3) a = 'i'
4) The code is illegal. Only a pointer can be added to a pointer,
not an integer.
5) The value cannot be determined without also knowing the size of an int on the system.
6.
template
public:
template
T2 t2;
};
static InnerFoo
};
Foo
Referring to the sample code above, what is the type of T2 in the object ::f?
1) double
2) t2
3) char
4) int
5) long
7.
int I, j;
string s;
cin >> I >> j >> s >> s >> I;
cout << I << " " << j << " "<< s << " "<< I;
Referring to the sample code above, what is the displayed
output if the input string given were: "5 10 Sample Word 15 20"?
1) 5 10 Sample Word 15 20
2) 15 20 Sample Word 15
3) 5 10 Sample Word 15
4) 15 10 Word 15
5) 15 20 Sample Word 20
8.
String::~String() {
cout << " String() " << endl;
}
Assuming that all the necessary using-directives have been made,
in the sample code above, which one of the following statements
pertaining to the destructor is TRUE?
1) The destructor should be ~String::String().
2) The destructor is incorrect because it is declared outside the class definition.
3) The destructor has been defined correctly.
4) The destructor is incorrect because
of the scope resolution operator ::
5) The destructor has been defined incorrectly since it contains code.
9.
Which type conditional expression is used when a developer wants
to execute a statement only once when a given condition is met?
1) switch-case
2) for
3) if
4) do-while
5) while
10.
const int MaxEntries = 10;
extern int entries[MaxEntries];
If this is a complete translation unit, what can be said about the variable "MaxEntries"?
1) It can only be used within the translation unit shown in the sample.
2) It cannot be used as a case value in a switch statement.
3) It becomes accessible to other translation units that reference it
by using the extern specifier.
4) It can be used in preprocessor directives.
5) It cannot be used to specify bounds in an array declaration.
C++ Questions only (2)
10.
int count=0;
class obj
{
public: obj(){
count++;
}
~obj() {
count--;
}
};
main()
{
obj A,B,C,D ;
return 0 ;
}
What is the value of "count" immediately before
the return statement above is executed?
1) 0
2) 1
3) 2
4) 3
5) 4
11.
class Foo;
What is the above sample code an example of?
1) A class constructor
2) A class object instantiation
3) A class definition
4) A class declaration
5) A syntax error
12.
1: class Foo {
2: int size; string name; double value;
3:
4: public:
5: Foo() : size(0), name("unknown"), value(0.0) {
6: }
7: Foo(int s) {
8: size = s; name = "unknown"; value = 0.0;
9: }
10: Foo(int s = 0, string n = "unknown", double v=0) {
11: size = s; name = n; value = v;
12: }
13: };
Referring to the sample code above, on which one of
the following lines does an initializer list occur?
1) Line 2
2) Line 5
3) Line 8
4) Line 10
5) Line 11
13.
Scenario:-
Your manager has asked you to integrate an existing project
with an external timer. Her requirements include adding a global
variable whose contents can be only read by the software
but whose value will be continuously updated by the hardware clock.
Referring to the above scenario, which one of the following
variable declarations satisfies all the requirements?
1) extern volatile clock;
2) extern const volatile long clock;
3) extern long clock;
4) extern const mutable long clock;
5) extern mutable long clock;
14.
class Grandpa
{
} ;
class Ma : virtual public Grandpa
{
} ;
class Pa : virtual public Grandpa
{
} ;
class Me : public Ma, public Pa, virtual public Grandpa
{
} ;
Each object of type Me will contain how many
sub-objects of type Grandpa?
1) 1
2) 2
3) 3
4) 4
5) 5
15.
class Foo {
int i;
public:
Foo(int x) : i(x) { }
};
1: Foo *f = new Foo;
2: Foo &f = new Foo(1);
3: int i = Foo::i;
4: Foo *af = new Foo[10];
5: const Foo &af = Foo(1);
Referring to the sample code above, which one
of the numbered lines of code is legal C++?
1) Line 1
2) Line 2
3) Line 3
4) Line 4
5) Line 5
16.
Which set of preprocessor directives is used
to prevent multiple inclusions of header files?
1 #ifndef, #define and #endif
2 #ifdefined and #enddefine
3 #define and #endif only
4 #$if and #endif
5 #if and #define
17.
Which one of the following statements is TRUE
in regard to overloading the ++ operator?
1 You cannot define a post-increment operator.
2 You cannot define a pre-increment operator
3 You cannot define both a pre-increment
and post increment operator for a class.
4 You must use a dummy parameter to
define a post increment operator.
5 You should always create a pre-increment
operator when overloading ++.
C++ Questions only (3)
18.
Which one of the following correctly shows
that class d is a sub-class of class b?
1 class b : public d {};
2 class d : public b {};
3 class b public b {};
4 class d, public b {};
5 class b, public d {};
19.
class numbers {
int I;
public:
void set(int v) {I = v;}
int read() {return I;}
} ;
Referring to the sample code above, how do you declare
a numbers object called "A" and assign its "I" member the value "2"?
1) A : numbers;
A.set(2);
2) numbers set(2);
numbers A;
3) numbers A;
set(2);
4) A.set(2), B.set(3), C.set(5);
numbers A, B, C;
5) numbers A;
A.set(2);
20.
char s[] = {'a','b','c',0,'d','e','f',0};
int I = sizeof(s);
Referring to the sample code above,
what value does I contain?
1 3
2 6
3 7
4 8
5 9
21.
std::cout << count_if(
strVec.begin(),
strVec.end(),
bind2nd(greater(), "Baz")
);
What will the above code output if strVec is
a std::vector
1 Unknown, those functions are non-standard.
2 2
3 "Bee"
4 "Baz"
5 4
22.
class Parent {
public:
Parent () { Status () ; }
virtual ~Parent () { Status () ; }
virtual void Status () { cout << "Parent "; }
} ;
class Child : public Parent {
public:
Child () { Status () ; }
virtual ~Child () { Status () ; }
virtual void Status () { cout << "Child " ; }
} ;
int main () {
Child c ;
return 0 ;
}
Assuming that all the necessary standard headers have been
included and the necessary using-directives have been made,
what will be the output of the sample code above?
1 Child Child Child Child
2 Parent Parent
3 Child Child
4 Parent Child Child Parent
5 Child Parent Parent Child
23.
using namespace std;
string s("abcdefghijk");
string::size_type I = s.find_first_of("a-c");
s = string(s, I, string::npos);
I = s.find_first_not_of("a-e");
cout << string(s, I, string::npos);
Assuming that all the necessary standard headers
have been included, what is the output produced
be the sample code above?
1 abcfghijk
2 abc
3 abcdefghijk
4 fghijk
5 bcdefghijk
24.
Which one of the following operators can only be
overridden when implemented as a member function
of the class it is being implemented for?
1 == (double equal)
2 + (plus)
3 ! (exclamation point)
4 = (equal)
5 & (ampersand)
25.
Sample Code
class HasStatic {
static int I;
};
Referring to the sample code above, what is the
appropriate method of defining the member variable "I",
and assigning it the value 10, outside of the class declaration?
1 int static I = 10;
2 int HasStatic::I = 10;
3 HasStatic I = 10;
4 static I(10);
5 static I = 10;
26.
class A {
public:
A() {
if(x > 1)
throw "x overflow";
else
x++;
}
private:
static int x;
};
int A::x;
Referring to the sample code above, which
one of the following statements is correct?
1 The class can be instantiated without throwing an exception two times.
2 It cannot be determined how many times the class can be instantiated (unknown value for x).
3 The class can be instantiated without throwing an exception one time.
4 Class A can be instantiated as many times as desired without throwing an exception.
5 Class A can never be instantiated without throwing an exception.
C++ gamedev interview questions (1)
1. Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at:
* const char *
* char const *
* 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. You’re given a simple code for the class BankCustomer. Write the following functions:
* Copy constructor
* = operator overload
* == operator overload
* + operator overload (customers’ balances should be added up, as an example of joint account between husband and wife)
Note:Anyone confusing assignment and equality operators should be dismissed from the interview. The applicant might make a mistake of passing by value, not by reference. The candidate might also want to return a pointer, not a new object, from the addition operator. Slightly hint that you’d like the value to be changed outside the function, too, in the first case. Ask him whether the statement customer3 = customer1 + customer2 would work in the second case.
3. What problems might the following macro bring to the application?
#define sq(x) x*x
4. Consider the following struct declarations:
struct A { A(){ cout << "A"; } };
struct B { B(){ cout << "B"; } };
struct C { C(){ cout << "C"; } };
struct D { D(){ cout << "D"; } };
struct E : D { E(){ cout << "E"; } };
struct F : A, B
{
C c;
D d;
E e;
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.
C++ gamedev interview questions (2)
5. Anything wrong with this code?
T *p = new T[10];
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”.
6. Anything wrong with this code?
T *p = 0;
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.
7. Explain virtual inheritance. Draw the diagram explaining the initialization of the base class when virtual inheritance is used. Note: Typical mistake for applicant is to draw an inheritance diagram, where a single base class is inherited with virtual methods. Explain to the candidate that this is not virtual inheritance. Ask them for the classic definition of virtual inheritance. Such question might be too complex for a beginning or even intermediate developer, but any applicant with advanced C++ experience should be somewhat familiar with the concept, even though he’ll probably say he’d avoid using it in a real project. Moreover, even the experienced developers, who know about virtual inheritance, cannot coherently explain the initialization process. If you find a candidate that knows both the concept and the initialization process well, he’s hired.
8. What’s potentially wrong with the following code? long value; //some stuff 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++.
9. What does the following code do and why would anyone write something like that?
void send (int *to, int * from, int count)
{
int n = (count + 7) / 8;
switch ( count % 8)
{
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while ( --n > 0 );
}
}
10. In the H file you see the following declaration: class Foo { void Bar( void ) const ; }; Tell me all you know about the Bar() function.
C++ coding interview questions
Given the following classes
class Fruit {
// …
}
class Apple : public Fruit {
// …
}
class Peach : public Fruit {
// …
}
// Container of fruit
class BasketOfFruit {
BasketOfFruit() ;
void insert( Fruit & f ) ;
// …
}
// Container of apples
class BasketOfApples /* ??? */ {
// …
}
Should BasketOfApples derive from BasketOfFruit?
Why or why not?
What is the general principle
that determines the answer?
----------------------------------------------
Describe briefly what the following function does.
What standard function is it most like ?
int f( char *p ) {
int n = 0 ;
while ( *p != 0 ) n = 10*n + *p++ - ‘0? ;
return n ;
}
-------------------------------------------
Describe briefly what function ‘a’ does in
the following code fragment.
struct s {
struct s *next ;
}
a( struct s *p, struct s *x ) {
while ( p->next != 0 ) p = p->next ;
p->next = x ;
x->next = 0 ;
}
--------------------------------------------
What default methods are declared implicitly
by the C++ compiler for the class below:
class Empty
{
};
No comments:
Post a Comment