正文

C++ tests and answers (1)

(2024-01-18 05:41:52) 下一个

C AND C++ TESTING  (BASIC)

 

1.      What's precedence?  What's associativity?     

2.      What's lvalue?  What's rvalue?  Why (x=y)=z is illegal in C? 

3.      What's signature of function?  What's difference of signatures

        in C and C++?

4.      What's overloading of function?  What's mangling?

5.      What are static variable and static function in C ?

        What are their scopes?

6.      What's difference between reference and pointer?  Why reference

        is needed in C++?

7.      How to avoid multiple definition error in compiling time?

        in linking time?

8.      What's memory leak?

9.      What's friend function?  Why friend function is needed in C++?

10.     When we need to use volatile?

11.     Explain:

        (1)     for (x=0;x!=10;) {...}

        (2)     for (;x<10;) {...}

12.     What's difference between  a[++x] and a[x++] ?

13.     Explain:

        (1)     *++p;

        (2)     ++*p;

14.     Why X::X(X) is illegal in C++?

15.     What's difference between malloc(), calloc(), free(),

        new, and delete?

16.     In the function with a variable number of arguments such as

        f(first,...)..., what's meaning: va_start(args,first); ?

17.     What's shallow copy?  How to avoid the shallow copy?

18.     What's order in which the class members are initialized?

19.     Explain the meaning of const in the following:

        (1)     func1(const class1 & c1) {...}

        (2)     int func2(...) const;

        (3)     const String s("Hello");

20.     What are public, private, and protected inheritances?

21.     What are pure virtual functions and abstract classes?

        What are virtual classes?

22.     What's "canonical form" for class?

23.     What's different between NULL, 0, '0', and '' ?

24.     What's generic pointer?  What's generic function pointer?

25.     Why we prefer initialization to assignment in the class

        constructors?

26.     When we use C++ iostreams like:

        ostream& operator << (ostream& output, const String& string){...}

        why we use reference "&" ?

        why iostream library uses << and >> as stream operator?

27.     What are static data member and static member function in C++?

28.     What's meaning of (int(*)(int)) in the following:

 

        void f1(int);

        int (*f2)(int);

        f2 = (int(*)(int)) f1;

29.     What's static object?  What's the static initialization problem?

30.     What's STL?

C AND C++ TESTING  (BASIC)  with answers

 

1.      What's precedence?  What's associativity?    Precedence is C’s version of the “order of operations” in mathematics. Associativity states where the operator lies in relation to the operand 

2.      What's lvalue?  What's rvalue?  Why (x=y)=z is illegal in C?  lvalue is on the left side of assignment statement and is a variable. The rvalue is on the right side of the assignment statement, and it can be a constant value of a variable. The statement is illegal because the left side doesn’t evaluate to anything non-void.

3.      What's signature of function?  What's difference of signatures

        in C and C++?

4.      What's overloading of function?  What's mangling? Overloading is the use of functions with the same names but different parameters. Mangling is C++’s way of coping with overloading. It is the addition of special characters, etc. to the internal representation of the function so  the linker can differentiate between the various overloaded versions

5.      What are static variable and static function in C ? What are their scopes? Static variables are allocated at the beginning of the program and remain in memory until the program terminates. Static functions are functions that use static variables. Scopes are simply how long variables are valid in memory.

 

6.      What's difference between reference and pointer?  Why reference

        is needed in C++? A reference behaves syntactically like a regular object, a pointer requires special operators. We can use references when we need to pass a parameter directly to a function without copying.

7.      How to avoid multiple definition error in compiling time?

        in linking time?

8.      What's memory leak? Allocating memory and then setting its pointer to NULL without using “delete”, “GlobalFree()”, “free()”, etc.

9.      What's friend function?  Why friend function is needed in C++? Friend functions are functions outside a class that have access to all that class’s members. It is needed because a class’s private members can’t be accessed outside the class any other way.

10.     When we need to use volatile? When one of our variables may be changed independently of our own code.

11.     Explain:

        (1)     for (x=0;x!=10;) {...}infinite loop unless x is modified within the loop

        (2)     for (;x<10;) {...}depends on what value x was initialized with

12.     What's difference between  a[++x] and a[x++] ? the left expression will be one higher when it’s evaluated

13.     Explain:

        (1)     *++p; dereferencing of p(incremented)

        (2)     ++*p; incrementing of the value of p

14.     Why X::X(X) is illegal in C++? it’s the copy constructor, which should not be called like this; it will be called forever.  Should be  X::X(const X&).

15.     What's difference between malloc(), calloc(), free(),

        new, and delete? Calloc() calls malloc() for the allocation of an array. Malloc() allows a specific number of bytes to be allocated, but new operator uses data types as parameters. Free() and delete are the deallocation function/operator.

16.     In the function with a variable number of arguments such as

        f(first,...)..., what's meaning: va_start(args,first); ?

17.     What's shallow copy?  How to avoid the shallow copy?

18.     What's order in which the class members are initialized? Private, protected, then public

19.     Explain the meaning of const in the following:

        (1)     func1(const class1 & c1) {...} function cannot modify the parameter preceded by const

        (2)     int func2(...) const; does not modify the object declared for

        (3)     const String s("Hello"); the value must always be “Hello”

20.     What are public, private, and protected inheritances? Public: all public and protected members of the base class remain the same in the derived class. Protected: all public and protected members are protected members of the derived class. Private: all public and protected members of the base class become private members of the derived class

21.     What are pure virtual functions and abstract classes?

        What are virtual classes? Pure virtual functions and abstract classes can do nothing on their own, but they form a basis for other classes to inherit from them. Virtual classes can be modified by descendant classes to fit the programmer’s needs more closely.

22.     What's "canonical form" for class?

23.     What's different between NULL, 0, '0', and '' ? NULL is a pointer that doesn’t point to any data. 0 is the integer value 0. ‘0’ is a string. ‘’ is an escape sequence.

24.     What's generic pointer?  What's generic function pointer? Assuming that this means void pointer, it can point to any variable that is not const or volatile. It cannot be dereferenced without being recast. The generic function pointer can point to any function except C++ class members.

25.     Why we prefer initialization to assignment in the class

        constructors? Avoid copying

26.     When we use C++ iostreams like:

        ostream& operator << (ostream& output, const String& string){...}

        why we use reference "&" ? I believe that these operators need to modify the address of their operands in order to do the insertion/extractions

        why iostream library uses << and >> as stream operator? The stream must be shifted to accomodate for the incoming/outgoing data.

27.     What are static data member and static member function in C++? A static data member has one copy that is shared by all instances of the class. A static member function uses only static data members.

28.     What's meaning of (int(*)(int)) in the following:

 

        void f1(int);

        int (*f2)(int);

        f2 = (int(*)(int)) f1; f2 equals the value of f1 typecast to integer pointer

29.     What's static object?  What's the static initialization problem? A static object is created once and used by everything. The problem is that they are destroyed in a reverse order from initialization.

30.     What's STL? Standard 

[ 打印 ]
阅读 ()评论 (0)
评论
目前还没有任何评论
登录后才可评论.