C++ Quiz Part1

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
Last edited on
question 1, i'd say b. and it has nothing to do with friends.
Why not d) which has the const keyword? looking here its how they do it: http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html
i believe the 2nd const in line d to be incorrect.

that link you provided agrees with me too by the looks of it. although i've been coding in COM all day and my brain's died :)

nope you're correct. it doesnt agree with me, and my brain has died.

although that second const is only saying to the compiler it wont change that object's *this.
Last edited on
1) friend is only needed if you need to have access to the private members of the class. Using extern is a bit unnecessary because it doesn't makes any difference in this case. I think you are correct that the answer is a but if I had to pick another one I would have picked d (not b).

2) Haven't tried it but looks correct (assuming the code actually compiles).

3) Not sure what inf is doing on line 1 but if you remove that it will print 0. The reason is because << has higher precedence than ?: so the code will be interpreted as (cout<<(bmf==38))?15:10; (notice the extra parentheses).
http://en.cppreference.com/w/cpp/language/operator_precedence

4) Correct.

5) Correct.

6) The code contains a few errors so it wouldn't compile at all so you could be correct but not for the reason you said but simply because it didn't gave any output at all.

If you have just copy pasted incorrectly and the code indeed compiles and runs I would say the correct answer is b because "dog" is not mapped to anything (It is "cat" that is mapped to "animal").

7) Correct.

8) c is correct but a could also be true if the Node constructors are private.
Thank you :) Sorry that was a typo, its unsigned int nof unsigned inf :P
For Q2, none of the answers are correct. Look at the definition of record::getAge().

Topic archived. No new replies allowed.