I am practicing some quizz questions on c++ and there are a few I am not very sure about. I wondered if you could help me answer them since I am still a rookie who is learning. I marked with an arrow the answer which i thought was correct for every question with a brief reason. Thank you in advance.
QUESTION 1 For some class Value, which of the following fragments is the preferred way of writing the declaration of the equality operator?
1 2 3 4
|
a)extern bool operator==(const Value &lhs, const Value &rhs);<--
b)class Value{public: bool operator==(const Value &other);};
c)class Value{public: bool operator==(const Value *other);};
d)class Value{public: bool operator==(const Value &other) const;};
|
I think its a),from what i read on the c++ primer book on overloading operators, however from what i've researched on the web i think its missing the keyword friend.
QUESTION 2 Considering the following program, what will be the output?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream.h>
class record
{
char * bmfName;
int bmfAge;
public:
record(char *iName="", int iAge=0){
bmfName = iName;
bmfAge = iAge;};
char *getName(){
return bmfName;};
int getAge(){
return bmfName;};
void changeName(char *iName){
bmfName=iName;};
void changeAge (int iAge){
bmfAge=iAge;};
};
void main()
{
record one("One",29);
record two("Two",32);
cout<<one.getName()<<","<<one.getAge()<<endl;
cout<<two.getName()<<","<<two.getAge()<<endl;
two=one;
one.changeName("Three");
two.changeAge(26);
cout<<one.getName()<<","<<one.getAge()<<endl;
cout<<two.getName()<<","<<two.getAge<<endl;
}
a) one,29 <--
two,32
three,29
one,26
b) one,29
two,32
three,26
one,26
c) one,29
two,32
three,29
two,26
d) one,29
two,32
three,29
three,26
|
First two are easy, then, two has ("one",29); one's name is changed to ("three",29), and two has agge changed to ("one",26). Correct?
QUESTION 3 What will be the output of the following:
1 2 3 4 5 6 7
|
unsigned inf bmf = 48;
cout<<(bmf==38)?15:10;
a) 15
b) 10<-- (condition)?iftrue:iffalse
c) 0
d) 1
|
QUESTION 4 What s the meaning of the declaration "A * const p"?
1 2 3 4
|
a) p is a pointer to an A that is const
b) p is a const pointer to A <--
c) the object cant be changed via p
d) all the above
|
b) since the const is after the asterik its the pointer that is constant, not A.
QUESTION 5 Consider the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
struct A
{
int i;
};
class B
{
int j;
}
a) A::i is publicly accessible <--Structs are by default public members.
b) B:i is publicly accessible
c) both of the above are true
d) Neither the above are true
|
QUESTION 6 Given the following code fragment, what output would be generated?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//suitable #includes
typedef std::map Lookup;
void FindAndPrint(const Lookup &lookup, const std::string &key)
{
std::cout<<""<<lookup[key]<<std::endl;
}
int main(int, char *[]){
Lookup textLookup;
textLookup["apple"]="fruit";
textLookup["pear"]="fruit";
textLookup["cat"]="animal";
FindAndPrint(textLookup, "apple");
FindAndPrint(textLookup; "dog");
return 0;
}
a) "apple fruit" "dog"
b) "fruit" ""
c) "fruit" followed by a run-time exception
d) none of the above <--
|
I think it would be "fruit" "aminal" since apple key is mapped to fruit and dog key is mapped to animal
QUESTION 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class Coordinate{
int x;
int y;
public:
Coordinate(int a=0, int b=0)
{
x=a;
y=b;
};
};
What are the values for the following object:
Coordinate point(10);
a) x=0,y=10
b) x=10,y=0 <-- x is initialized to 10, and y to default which is 0.
c) x=10,y=10
d) x=0,y=0
|
QUESTION 8 The following line appears in the header file of class Node, what does it give?:
1 2 3 4 5
|
friend class Queue;
a) an instance of Queue the ability to construct a Node object
b) an instance of Node the ability to construct a Queue object
c) Queue member functions unrestricted access to private members of Node<--
d) Node member functions unrestricted access to private memebers of Queue
|
A friend class has access to the private and protected parts of the class that made it a friend