IKM c++ misc points

——–
Q: is increment operator synthesized by the compiler?
——–
Portable file paths? Relative path OK. For absolute paths, use boost::filesystem
——–
terminate(void)
——–
default args must follow non-default args.
——–
(obscure syntax) int *(*p)[5] = new int *[5][5];
——–
(obscure syntax) redefinition error:
const int Mon = 1, Tue =2;
enum DOW {Mon=11, Tue = 12};
——–
Pointer to private field? legal
Pointer to ctor? Illegal
——–
Templatize operator<< then specialize it? legal
——–
Causing ambiguity error —
class base{};
class der: public base{};
void SomeFunc(base& b){ };
void SomeFunc(base b){ };
void SomeFunc(der& b){ };
void SomeFunc(der b){ };

some IKM java questions

Q:
Base aa = new Sub(); //both Base and Sub defines STATIC method m1()
aa.m1();
(Sub)aa.m1(); // which one?

Q: Deep copy  a java array?
– clone()?
– serialization?

Q: a base class method without access modifier is callable by subclass?

I think java default method access level is “package, not subclass”. In contrast, c# (and c++) default is private — http://msdn.microsoft.com/en-us/library/ms173121.aspx.

Q: if interface1 declares method2 returning Set, can an implementing class’s method return SortedSet?

c++ stream — IKM findings

——tellg and tellp?
http://answers.yahoo.com/question/index?qid=20110507033952AAXZBz2 is a short explanation.

tellg(void) and tellp(void) both return their pointer’s position
——
ostream& endl (ostream& os);
——
stream::rdbuf() changes the filebuf… http://www.cplusplus.com/reference/ios/ios/rdbuf/
——

endl – flush() implicitly
——
If your variable to “populate” is an int, then extraction operator stops when hitting ….”any character that couldn’t be part of an int” which is not limited to white space.

c++ template – IKM findings

template<  > void f<char>(char c){…} //to specialize an existing func template

——–
to specialize a func template,
template void fn(T a){}
template void fn(char* a){}
——–
Some is distinct from Some or Some —

template class Some{
public: static int stat;
};
template int Some::stat = 10;
int main()
{
Some::stat = 5;
cout<<Some::stat<<endl;
cout<<Some::stat<<endl;
cout<<Some::stat<<endl;
cout<<Some::stat<<endl;

IKM c++ 16Q (no Answer)

http://cboard.cprogramming.com/cplusplus-programming/125239-some-interview-questions-please-help.html
———–Question 1 (hit me):

//Someone else’s code, e.g. library
class IGlyph {
public:
    virtual ~IGlyph(){}
    virtual std::string Text()=0;
    virtual IIcon*      Icon()=0;
    //…
};
class IWidgetSelector {
public:
    virtual ~IWidgetSelector(){}
 
    virtual void    AddItem(IGlyph*)=0;
    virtual IGlyph* Selection()=0;
};
//Your code
class MyItem : public IGlyph {
public:
    virtual std::string Text(){
        return this->text;
    }
    virtual IIcon* Icon() {
        return this->icon.get();
    }
    void Activate() {
        std::cout < < "My Item Activated" < < std::endl;
    }
    std::string          text;
    std::auto_ptr icon;
};
void SpiffyForm::OnDoubleClick(IWidgetSelector* ws) {
    IGlyph* gylph = ws->Selection();
    MyItem* item  = dynamic_cast (gylph);
    if(item) item->Activate();
}
A. The dynamic_cast is necessary since we cannot know for certain what concrete type is returned by IWidgetSelector::Selection().
B. The dynamic_cast is unnecessary since we know that the concrete type returned by IWidgetSelector::Selection() must be a MyItem object.
C. The dynamic_cast ought to be a reinterpret_cast since the concrete type is unknown.
D. The dynamic_cast is redundant, the programmer can invoke Activate directly, e.g. ws->Selection()->Activate();
E. A polymorphic_cast should be used in place of the dynamic_cast.
———–2. (hit me) Which of the following declarations of function main are standard or standard conforming extensions?
(Please note that some compilers accept ill-formed main declarations, these should be considered incorrect).
A. void main(char* argv[], int argc)
B. int main()
C. void main()
D. int main(int argc, char* argv[])
E. int main(int argc, char* argv[], char* arge[])
———–3. Which of the following statements accurately describe the condition that can be used for conditional compilation in C++?
A. The condition can depend on the value of program variables.
B. The condition can depend on the values of any const variables.
C. The condition can use the sizeof operator to make decisions about compiler-dependent operations, based on the size of standard data types.”
D. The condition can depend on the value of environmental variables.
E. The condition must evaluate to either a “0” or a “1” during pre-processing.
———–4. In C++, which of the following are valid uses of the std::auto_ptr template considering the class definition below?
class Object {
public: virtual ~Object() {}
  //…
};
A. std::auto_ptr pObj(new Object);
B. std::vector <std::auto_ptr > object_vector;
C. std::auto_ptr pObj(new Object);
D. std::vector <std::auto_ptr > object_vector;
E. std::auto_ptr source() {
    return new Object;
}
———–5. Which of the following statements correctly describe C preprocessor directives in C++?
A. The #pragma directive is machine-independent.
B. Preprocessor directives are processed before macros are expanded.
C. The #import directive is used to copy code from a library into the program’s source code.
D. Any number of #else directives can be used between an #if and an #endif.
E. The #include directive is used to identify binary files that will be linked to the program.
———–6. Which of the following statements describe the results of executing the code snippet below in C++?
int var = 1;
void main() {
    int i = i;
}
A. The i within main will have an undefined value.
B. The i within main will have a value of 1.
C. The compiler will not allow this statement.
D. The compiler will allow this statement, but the linker will not be able to resolve the declaration of i.
E. The result is compiler-dependent.
———–7. (hit me) Which of the following statements regarding the benefits of using template functions over preprocessor #define macros are correct?
A. A preprocessor macro expansion cannot work when user-defined types are passed to it as arguments.
B. While expanding #define macros, the preprocessor does no type checking on the arguments to the macro.
C. Since the preprocessor does the macro expansion and not the compiler, the build process takes a longer period of time.
D. A preprocessor macro expansion incurs a performance overhead at runtime.
E. It is simple to step into a template function code during the debugging process.
———–8. In a hierarchy of exception classes in C++, which of the following represent possible orders of catch blocks when a C++ developer wishes to catch exceptions of more than one class from a hierarchy of exception classes?
A. Classes belonging to the same hierarchy cannot be part of a common set of catch blocks.
B. The most derived classes must appear first in the catch order, and the parent classes must follow the child classes.
C. The most derived classes must appear last in the catch order, and the parent classes must precede the child classes.
D. The order is unimportant as exception handling is built into the language.
E. Multiple classes can be caught in a single catch clause as multiple arguments.
———–9. (hit me) Which of the following statements provide a valid reason NOT to use RTTI for distributed (i.e. networked between different platforms) applications in C++?
A. RTTI is too slow.
B. RTTI does not have standardized run-time behavior.
C. RTTI uses too much memory.
D. RTTI’s performance is unpredictable/non-deterministic.
E. RTTI frequently fails to function correctly at run-time.
———–10. (hit me) Which of the following options describe the expected overhead for a class that has 5 virtual functions?
A. Every object of the class holds the address of the first virtual function, and each function in turn holds the address of the next virtual function.
B. Every object of the class holds the address of a link list object that holds the addresses of the virtual functions.
C. Every object of the class holds the addresses of the 5 virtual functions.
D. Every object of the class holds the address of a structure holding the addresses of the 5 virtual functions.
E. Every object of the class holds the address of the class declaration in memory, through which the virtual function calls are resolved.
———–11. (hit me) A C++ developer wants to handle a static_cast () operation for the class String shown below. Which of the following options are valid declarations that will accomplish this task?
class String {
public:
//…
//declaration goes here
};
A. char* operator char*();
B. operator char*();
C. char* operator();
D. String operator char*();
E. char* operator String();
———–12. Which of the following options describe the functions of an overridden terminate() function?
A. It performs the desired cleanup and shutdown processing, and then throws a termination_exception.
B. It performs the desired cleanup and shutdown processing, and then returns an error status value to the calling function.
C. It performs the desired cleanup and shutdown processing, and then calls abort() or exit().
D. It performs the desired cleanup and shutdown processing, and if it has restored the system to a stable state, it returns a value of “-1” to indicate successful recovery.
E. It performs the desired cleanup and shutdown processing, and then calls the unexpected() handler.
———–13. Which of the following options are returned by the typeid operator in C++?
A. A reference to a std::type_info object
B. A const reference to a const std::type_info object
C. A const std::type_info object
D. A reference to a const std::type_info object
E. A const reference to a std::type_info object
———–14. Which of the following statements accurately describe unary operator overloading in C++?
A. A unary operator can be overloaded with no parameters when the operator function is a class member.
B. A unary operator can be overloaded with one parameter when the operator function is a class member.
C. A unary operator can be overloaded with one parameter when the operator function is free standing function (not a class member).
D. A unary operator can only be overloaded if the operator function is a class member.
E. A unary operator can be overloaded with no parameters when the operator function is a free standing function (not a class member).
———–15. (hit me) What is the correct syntax for portable fstream file paths?
(e.g. the string you would pass to std::fstreamen() to open a file.)
A. “::directory:file.bin”
B. “C:/Directory/File.bin”
C. “/directory/file.bin”
D. “C:\Directory\File.bin”
E. std:fstream file paths are not portable.
———–16. (hit me) When a Copy Constructor is not written for a class, the C++ compiler generates one. Which of the following statements correctly describe the actions of this compiler-generated Copy Constructor when invoked?
A. The compiler-generated Copy Constructor makes the object being constructed, a reference to the object passed to it as an argument.
B. The compiler-generated Copy Constructor does not do anything by default.
C. The compiler-generated Copy Constructor performs a member-wise copy of the object passed to it as an argument, into the object being constructed.
D. The compiler-generated Copy Constructor tags the object as having been Copy-Constructed by the compiler.
E. The compiler-generated Copy Constructor invokes the assignment operator of the class.