Interview Questions

C++ Questions only (3)

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


(Continued from previous question...)

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<std::string> and contains the strings "Foo", "Bar," "Baz", and "Bee"?

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.

(Continued on next question...)

Other Interview Questions