IKM C/C++ Test Summury
29 Apr 2023
Post Tags
C/C++1. Container Adapters
Question: Which of the following are container adapters in the STL (Standard Template Library) in C++? A. list B. map C. stack D. queue E. deque
Answer:
Answer: C D
Basically, the queue, stack and priority_queue are derived/trimed from some other basic container: queue–> deque, stack–>deque, priority_queue–>vector. For example, stack is implement as following:
template <class _Ty, class _Container = deque<_Ty>>
class stack {
public:
using value_type = typename _Container::value_type;
using reference = typename _Container::reference;
using const_reference = typename _Container::const_reference;
using size_type = typename _Container::size_type;
using container_type = _Container;
...
stack() = default;
explicit stack(const _Container& _Cont) : c(_Cont) {}
...
private:
_Container c{};
}
So it called container adapter. I think the world adapter
could be some how related with adapter design pattern.
2. getline() VS extraction operator(»)
Question: Which of the following correctly identify benefits of the getline() member function for cin over the extraction operator in C++?
A. The getline() function by default, accepts whitespace, and returns on seeing the ‘\n’ character, whereas the extraction operator returns when it encounters any whitespace character. B. Delimiters indicating end of input can be specified to the getline() function, whereas the extraction operator has no such facility. C. The getline() function can be overloaded to accept different argument types, whereas the extraction operator cannot be overloaded. D. The number of bytes to read can be specified to the getline() function, whereas it cannot be done with the extraction operator. E. The getline() function can be used like a manipulator with cin, whereas the extraction operator cannot be used as a manipulator.
Answer:
Answer: B E
operator» reads till a space (as defined by std::isspace) and is found. Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, ‘\n’, for (2)).
// two prototype of getline are given below:
istream& getline (istream& is, string& str, char delim);// (1)
istream& getline (istream& is, string& str); // (2)
3. Inheritage (1)
Question: Which of the following statements correctly describe the results of executing the code below in C++?
#include <iostream>
using namespace std;
class ExBase
{
private:
static int stat;
public:
static int GetStat(){ return stat; }
};
int ExBase::stat = 25;
class ExDer1 : public ExBase
{
public:
friend int Der1Fn(){ return ExBase::stat; }
};
class ExDer2 : public ExBase{};
class ExDer : public ExDer1, public ExDer2{};
A.
int main()
{
ExDer d;
cout << d.Der1Fn() << endl;
}
will result in an ambiguity error from the compiler.
B.
int main()
{
ExDer d;
cout << d.GetStat() << endl;
}
will display an output as “25”.
C.
int main()
{
cout << ExDer1::GetStat() << endl;
}
will result in an ambiguity error from the compiler.
D.
class ExDer1 : public ExBase
{
public:
friend int Der1Fn(){ return ExBase::stat; }
};
will result in an access error from the compiler.
E.
int main()
{
cout << ExDer1::ExBase::GetStat() << endl;
}
will display an output as “25”.
Answer:
Answer: C D E
D ask for a private member of ExBase directly, which will cause access error. C,D is correct since GetStat() declared as static function.