If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

6) How to find a substring in a map key(syntax?), the key stores a string.
7) Returning an array from function (syntax of function signature?)
8) Default copy constructor does what

Code:

i) Reference to the original object of the same class
ii) Does nothing by default

9) Assign const pointer to a non-const pointer

Code:

i) Reinterpret
ii) Dynamic_cast
iii) Const_cast
iv) Static_cast

10) If we do not write a constructor, the compiler generates one for us. Is there a default destructor too?
11) How to print vector elements in a separate function having only a print statement
Rough Code

6) How to find a substring in a map key(syntax?), the key stores a string.

Code:

I know how to find a key and then extract the substring from it but is there way to do it in one step? Because the choices I remember were all ONE LINE statements - e-g find_first_of

7) Returning an array from function (syntax of function signature?)

Code:

int* foo(params) - We get a memory address pointing to the 0th element of the Array

8) Default copy constructor does what

Code:

i) Reference to the original object of the same class
ii) Does nothing by default ... FALSE b'coz then why would compiler provide one, I am still not sure of the exact reason

9) Assign const pointer to a non-const pointer

Code:

i) Reinterpret
ii) Dynamic_cast
iii) Const_cast (YES ... const_cast operator is used to add or remove a const or volatile modifier to or from a type)
iv) Static_cast

10) If we do not write a constructor, the compiler generates one for us. Is there a default destructor too? (YES)
11) How to print vector elements in a separate function having only a print statement
Rough Code

Re: Online C++ Test Questions

2) Can we call pure virtual function from constructor? NO (Read this answer somewhere but I don't know the reason)

You can call a function, but the "virtualness" will not be exhibited in the constructor. When an object is constructed, any call to a virtual function will just invoke the function as if it is non-virtual.

3) Benefits of Templates over overloading

Code:

i) One copy of code in memory
ii) Fast (YES as it avoids pointers/polymorphism)

The simple answer is that you can write one set of generic code, and "overload" the code on a compile-time type. You can't do that with regular overloaded functions.

4) [code]Obj1 = obj2 ........... points to the same memory location?

More information is needed. Of course if Obj1 and obj2 are pointers to a type T, then they will point to the same memory location. If Obj1 and obj2 are different objects, then they will be two distinct objects.

Re: Online C++ Test Questions

4) without knowing a whole lot more about obj1 and obj2, the question cannot be answered as the operator = could be overloaded.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: Online C++ Test Questions

Thank You Very Much Paul. Please have a look at the questions/answers below

4)

Code:

Obj1 and Obj2 are the Objects of the same class (type)
e-g if Object is the class name:
Object obj1 = new Object();
Object obj2;
Obj2 = obj1 ........... do these objects point to the same memory location?

5) Rough Code Below

Code:

Object1 and Object2 are the names of two different classes.
The Class Object2 has one Object and the Class Object1 does not have any objects
Object2 obj2 = new Object2();
cout << dynamic_cast< Object1>(obj2) << endl; ....... prints what?

7) Returning an array from function syntax

Code:

int * foo(int arr[]) this was one of the choices given

What I learnt is that NO we cannot return an array but a pointer to the first element of the array

Re: Online C++ Test Questions

4)
Code:

Obj1 and Obj2 are the Objects of the same class (type)
e-g if “Object” is the class name:
Object obj1 = new Object();
Object obj2;
Obj2 = obj1 ........... do

This is not valid c++ code. new returns a pointer which is tried to be assigned to a non-pointer instance of a class. The variable Obj2 does not exist (it should be obj2) and assuming obj1 is a pointer to an Object class, then obj2 also must be defined as a pointer variable.

then obj1 and obj2 do indeed point to the same memory address and hence to the same object.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: Online C++ Test Questions

Re: Online C++ Test Questions

Originally Posted by 2kaud

This is not valid c++ code. new returns a pointer which is tried to be assigned to a non-pointer instance of a class. The variable Obj2 does not exist (it should be obj2) and assuming obj1 is a pointer to an Object class, then obj2 also must be defined as a pointer variable.

This is actually perfectly valid C++ assuming there is a constructor for Object that takes a Object* as parameter and that constructor isn't declared as explicit.
Granted, it's rare you'd write such a constructor (and not declare it explicit if you do, it's accidents waiting to happen).

Re: Online C++ Test Questions

Originally Posted by OReubens

This is actually perfectly valid C++ assuming there is a constructor for Object that takes a Object* as parameter and that constructor isn't declared as explicit.
Granted, it's rare you'd write such a constructor (and not declare it explicit if you do, it's accidents waiting to happen).

Yep, those assignments can be made to be legal. I never figured on having an Object constructor that takes as argument Object *! I'll remember that one next time I ask some newbie c++ questions

which shows that obj1 and obj2 point to the same object and obj3 and obj4 point to different objects.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: Online C++ Test Questions

Re: Online C++ Test Questions

Hello everyone

Could anyone please help me with this online interview question?

Code:

Object1 and Object2 are the names of two different classes.
The Class Object2 has one Object and the Class Object1 does not have any objects
Object2 obj2 = new Object2();
cout << dynamic_cast< Object1>(obj2) << endl; ....... what does this line print?