Interview Questions

C++ Questions only (1)

C++ programming on UNIX, C++ Networking,C++ Algorithm Questions and Answers


(Continued from previous question...)

C++ Questions only (1)


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.
Assuming that:
char* p = "abc";
int a = 123;
Fully parenthesize the following expressions and evaluate them:
*p++;
*--p;
*--p++;
a--;
++a;
++a--;


4.
Write a class to store, evaluate and print integer arithmetic expressions. the public interface should look like this:
class myexpr {
public:
myexpr(char*);
int eval();
void print();
};


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<class T = int> class Foo {
public:
template<class T2 = T> class InnerFoo {
T2 t2;
};
static InnerFoo<long> *f;
};
Foo<double> f;

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.

(Continued on next question...)

Other Interview Questions