Online C++ Test Questions
Hello Everyone, I recently gave an online C++ test. Below are few of the questions I need help with. Could anyone please explain to me the correct answers?
1) Can we call virtual function from non-virtual function?
2) Can we call pure virtual function from constructor?
3) Benefits of Templates over overriding
Code:
i) One copy of code in memory
ii) Fast
4)
Code:
Obj1 = obj2 ........... points to the same memory location?
5) Rough Code below
Code:
Class Object1; (no object created)
Class Object2 obj2; (one object created)
cout << dynamic_cast< Object1>(obj2) << endl; ....... prints what?
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
Code:
For_each(syntax for iteratring, printFunc) – syntax?????
For() {} – syntax?????
int printFunc(int i)
{cout << i << endl;}
12) What does double ( ## ) between two variables and single ( # ) before one variable in macros mean?
e-g ...13) Rough Code
Code:
Pointer1 ptr1 = new Pointer;
Pointer2 ptr2;
ptr2 = ptr1; ..... how to assign? Syntax
Re: Online C++ Test Questions
Quote:
Originally Posted by
gulHK
Hello Everyone, I recently gave an online C++ test. Below are few of the questions I need help with. Could anyone please explain to me the correct answers?
First, let's see your answers.
I know that some of the questions are worded badly, but others can be solved by just writing a simple program and testing your solution here:
http://www.compileonline.com/compile_cpp_online.php
Regards,
Paul McKenzie
Re: Online C++ Test Questions
Hello Paul. Below are my answers.
Thanks and Regards
1) Can we call virtual function from non-virtual function? YES
2) Can we call pure virtual function from constructor? NO (Read this answer somewhere but I don't know the reason)
3) Benefits of Templates over overloading
Code:
i) One copy of code in memory
ii) Fast (YES as it avoids pointers/polymorphism)
4)
Code:
Obj1 = obj2 ........... points to the same memory location?
5) Rough Code below
Code:
Class Object1; (no object created)
Class Object2 obj2; (one object created)
cout << dynamic_cast< Object1>(obj2) << endl; ....... prints what?
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
Code:
For_each(syntax for iteratring, printFunc) – syntax?????
For() {} – syntax?????
int printFunc(int i)
{cout << i << endl;}
12) What does double ( ## ) between two variables and single ( # ) before one variable in macros mean?
e-g ...13) Rough Code
Code:
Pointer1 ptr1 = new Pointer;
Pointer2 ptr2;
ptr2 = ptr1; ..... how to assign? Syntax
Re: Online C++ Test Questions
Quote:
Originally Posted by
gulHK
Hello Paul. Below are my answers.
Thanks and Regards
1) Can we call virtual function from non-virtual function? YES
OK. That's right.
Quote:
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.
Quote:
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.
Quote:
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.
Quote:
5) Rough Code below
[code]Class Object1; (no object created)
Class Object2 obj2; (one object created)
There is no such keyword in C++ as Class. So the question cannot be answered.
Quote:
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
Don't know if this is a trick question. Your answer returns a pointer, not an array. Can you actually return an array in C++? I ask you.
Quote:
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
You need to know this one, or you can be rejected by any interviewer right from the start.
The default copy constructor does a member-wise, "shallow" copy on all the individual members of the class or struct.
Quote:
11) How to print vector elements in a separate function having only a print statement
Well, this one uses a "std::copy", not "print", as there is no such function as "print" in standard C++.
Code:
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
//...
template <typename T>
void PrintVector(const std::vector<T>& tVect)
{
std::copy(tVect.begin(), tVect.end(), std::ostream_iterator<T>(std::cout, " "));
}
int main()
{
std::vector<int> IntVector;
IntVector.push_back(10);
IntVector.push_back(20);
IntVector.push_back(30);
IntVector.push_back(40);
PrintVector(IntVector);
}
That's all I'll contribute for now.
Regards,
Paul McKenzie
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.
Re: Online C++ Test Questions
Quote:
Originally Posted by
2kaud
4) without knowing a whole lot more about obj1 and obj2, the question cannot be answered as the operator = could be overloaded.
Or they could be pointers. The questions aren't worded well at all.
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
Code:
int * foo(int arr[]){
// populate arr
return arr;
}
int main()
{
int arrA [10];
int *arrB = foo(arrA);
for(int i=0; i<10; i++)
{
cout << arrB[i] << endl;
}
}
8) Default copy constructor does what
Code:
i) Reference to the original object of the same class
ii) Does nothing by default ... FALSE used for Object instantiation (one reason I can think of)
11) Print vector elements in a different function having only a print statement … is this correct?
Code:
for_each (myvector.begin(), myvector.end(),printVect);… this was one of the choices given
Code:
void printVect(int i) { // function for printing vector elements
std::cout << ' ' << i;
}
int main () {
std::vector<int> myvector;
// populate vector;
for_each (myvector.begin(), myvector.end(),printVect);
std::cout << '\n';
}
Can we do it with FOR LOOP?
Thanks and Regards
Re: Online C++ Test Questions
Quote:
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.
Given the revised code
Code:
#include <iostream>
class Object {
public:
int a;
};
int main()
{
Object *obj1 = new Object();
Object *obj2;
obj2 = obj1;
std::cout << "address of obj1: " << obj1 << std::endl;
std::cout << "address of obj2: " << obj2 << std::endl;
}
then obj1 and obj2 do indeed point to the same memory address and hence to the same object.
Re: Online C++ Test Questions
Quote:
Originally Posted by
gulHK
[/code]
8) Default copy constructor does what
Code:
i) Reference to the original object of the same class
ii) Does nothing by default ... FALSE used for Object instantiation (one reason I can think of)
That is not what the answer should be. There is one universal answer, and that is to state what the default copy constructor does. I stated the answer in my previous post.
Regards,
Paul McKenzie
Re: Online C++ Test Questions
Thank You Very Much 2Kaud.
Re: Online C++ Test Questions
Thank You Very Much Paul.
Re: Online C++ Test Questions
Quote:
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
Quote:
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.:o 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:D
Code:
#include <iostream>
class Object {
public:
Object()
{
}
Object(Object*)
{
}
};
int main()
{
Object *obj1 = new Object();
Object *obj2;
Object obj3 = new Object();
Object obj4;
obj2 = obj1;
obj4 = obj3;
std::cout << "address of obj1: " << obj1 << std::endl;
std::cout << "address of obj2: " << obj2 << std::endl;
std::cout << "address of obj3: " << &obj3 << std::endl;
std::cout << "address of obj4: " << &obj4 << std::endl;
}
In this case the output is
Code:
address of obj1: 00323FF0
address of obj2: 00323FF0
address of obj3: 0012FEE0
address of obj4: 0012FEE4
which shows that obj1 and obj2 point to the same object and obj3 and obj4 point to different objects.
Re: Online C++ Test Questions
That's great. Thank you very much for explaining it with a practical example.
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?
Many Thanks