Thread: Some Interview questions, please help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Some Interview questions, please help

    The following questions have less than 3 correct answers.

    1. Given the following program snippet, what can we conclude about the use of dynamic_cast in C++?

    C/C++ code
    Code:
    #include <iostream> 
    #include <memory> 
    
    //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 <IIcon> icon; 
    }; 
    
    void SpiffyForm::OnDoubleClick(IWidgetSelector* ws) 
    { 
        IGlyph* gylph = ws->Selection(); 
        MyItem* item  = dynamic_cast <MyItem*>(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. 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?
    Code:
    class Object 
    { 
    public: 
      virtual ~Object() {} 
      //... 
    };
    A. std::auto_ptr <Object> pObj(new Object);
    B. std::vector <std::auto_ptr <Object*> > object_vector;
    C. std::auto_ptr <Object*> pObj(new Object);
    D. std::vector <std::auto_ptr <Object> > object_vector;
    E.
    std::auto_ptr <Object> 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++?

    C/C++ code
    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. 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. 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. 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. A C++ developer wants to handle a static_cast <char*>() 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. 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. 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.

    Here are mine:
    1.a
    2.d
    3.e
    4.a
    5.a,d
    6.a
    7.b,d
    8.b
    9.b
    10.d
    11.b
    12.c
    13.e
    14.b
    15.d
    16.c

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I only looked at 9 of the questions. On those 9, I would give you a grade of 44.4%. I would like to know why you think "C:\\Directory\\File.bin" is portable, for sure, and how you think macro expansion can cause a penalty at runtime. I'm not a fan of your answers to 14 and 11.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    The macro may cause code bloat which might incurs a performance issue at runtime. What do you think?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by higaea View Post
    The macro may cause code bloat which might incurs a performance issue at runtime. What do you think?
    TBH, I think that's one of the silliest things I've heard this month.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think this is much different from extensive inlining which may cause code bloat. However, d implies that the macro is expanded at runtime.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    a macro could conceivably expand to create an extremely inefficient code construct, thus causing runtime performance penalties.
    But that's not the fault of the macro, it's the fault of the construct created using those macros (which might be hidden from view by the macro in code, making it harder to detect reading that code, but again that's merely a side effect of using macros).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. Microsoft Interview Questions
    By Kohatian 3279 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-29-2006, 03:00 PM
  3. Replies: 1
    Last Post: 06-17-2005, 07:29 AM
  4. Companies and their stupid interview questions...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-30-2003, 06:00 PM
  5. a list of interview questions...
    By BrianK in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-29-2002, 02:29 PM