C++ Quiz Part2

Continuing the questions I had on part 1:
QUESTION 9 What is the output of the snippet below?
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
//suitable #includes
class A
{
    public:
    void name() const{std::cout<<"A::name"<<std::endl;}
    virtual ~A(){}
};

class B : public virtual A
{
    public:
    void name() const {std::cout<<"B::name"<<std::endl;}
};

int main(int, char*[])
{
    const A *x=new B;
    x->name();
    return 0;
}

a) A::name
b) B::name <-- 
c) A::name B::name
d) none of the above

x is a pointer to a class A object, but since it was created from B. The virtual is just used so that other subclasses of A can override B.

QUESTION 10 For some class Data, what does the following code snippet do?
1
2
3
4
5
6
7
Data *p =new Data[10];
delete p;

a) destructs all 10 elements in the array pointed at by p, and releases all the memory
b) destructs just the element pointed at by p, and releases all the memory 
c) it compiles, but the run-time behaviour is not well defined <-- 
d) it fails to compile

in order to destroy the elements properly you would need delete []p, otherwise the heap might be corrupted.

QUESTION 16 Consider the following program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>
void main(void)
{
    const int aSize=5;
    int c[aSize]={1};
    cout<<"The answer is: "<<c[4]<<endl;
}

Which of the following is true?
a) The program will execute, output = "The answer is: 0"
b) The program will execute, output = "The answer is: 1"
c) The program will execute, but yield inpredictable results since c[4] 
    has not been explicitly initialised <-- 
d) The program will not execute since it is not legal to declare an array
    using a const variage for the number of elements

Only the first element was initialized, d) is false as far as i know.

QUESTION 17 Consider the following program:
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
53
54
55
56
57
58
59
60
61
62
#include <iostream.h>
#include <string.h>
class AString{
    public:
    AString(){Buf=0;length=0;}
    AString(const char*);
    void display() const {cout<<buf<<endl;}
    ~AString(){delete buf;}
    //<..another function to be defined
    private:
    int length;
    char *buf;
}

AString::AString( const char *s){
    length = strlen(s);
    buf=new char[length+1];
    strcpy(buf,s);
}

void main(){
    AString first, second;
    first = second = "Hello world";
    first.display();
    second.display();
}

Which of the following function needs to be added for the following statement
to work as intended: first=second="Hello world";

a) AString & operator=(const AString &other)
{
    if(&other==this) return *this;
    length=other.length;
    delete buf;
    buf = new char[length+1];
    stcpy(buf, other.buf);
    return *this;
}

b)AString (const AString &other){
    length = other.length;
    buf=new char [length+1];
    strcpy(buf,other.buf);
}

c)void AString::operator=(const AString &other){
    if(&other==this) return;
    length=other.length;
    delete buf;
    buf=new char [length +1];
    strcpy(buf, other, buf);
    return;
}

d) AString(const AString &other){ 
    if (&other==this) return;
    length=other.length;
    buf=new char[length +1];
    strcpy(buf,other.buf);
    return;
}

I get compilation errors on all 4 of them, so I don't know if im doing something wrong here :S


QUESTION 18 Can you name the special functions a C++ compiler creates implicitly on a class?
1
2
3
4
a) default constructor and default destructor
b) default constructor, copy constructor and assignment operator
c) default constructor, copy constructor and destructor
d) all of the above <--

as far as i found, its all 4, constructor, destructor, copy and assignment

QUESTION 19 Members of a class declared as protected are:
1
2
3
4
a) accessible only by the compiler
b) accessible only by the members of its own class
c) accessible only by the members of its own class and classes inherited from it <-- 
d) accessible by anything in the same scope as the class

I think its this one since the children of a class know its public and protected members. However,shouldn't it include friend classes?

QUESTION 20 Consider the following program
1
2
3
4
5
6
7
8
9
10
11
12
13
int symmetricTrans(int x, const char c);
float symmetricTrans(const char x, float f);
Which of the following correctly declares and initialises function pointers 
the overloaded function?

a)void *fp1=symmetriTrans(int, const, char);
  void *fp2=symmetricTrans(const char, float);
b)void *fp1=&symmetricTrans;
  void *fp2=&symmetricTrans;
c)int(*fp1)(int, const char)=&symmetricTrans;<-- 
  float(*fp2)(const char, float)=&symmetricTrans;
d)int(*fp1)=&symmetricTrans(int, const char);
  float(*fp2)=&symmetricTrans(const char, float);

fp1 points to a function which takes int and const char, and returns int. This is initialised to the address of &symmetricTrans
9) void A::name() const is not a virtual function so it will simply call A::name() because that is the type of the pointer.

10) Correct.

16) When you initialize an array with ={...}; all values in the array will be initialized. The elements that have no value listed will be initialized to 0 so the answer is a.

17) The code is very old (pre-standard) C++ so it will probably not compile on a modern C++ compiler. main() should have return type int, the standard headers should not have the suffix .h, etc. If you still use an old compiler maybe it has to be like that but there are also a few other problems that are probably just mistakes like the missing semicolon after the class definition, the uppercase B in buf and misspelt strcpy as stcpy. I don't think I should give you the correct answer without you trying first so I will just say that line 23 is equivalent to
 
first.operator=(second.operator=(AString("Hello world")));
Hope it helps you.

18) Correct, but note that if you define a constructor you will not get the implicit default constructor.

19) Correct. Yes protected members are also accessible from friends.

20) Correct.
Last edited on
Thank you very much! :-) I'll try 17 without the typos
Last edited on
Topic archived. No new replies allowed.