C++ Namespace Aptitude Questions and Answers

C++ Namespace Aptitude: This section contains C++ Namespace Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 20, 2021

1) There are the following statements that are given below, which of them are correct about namespace in C++?

  1. A namespace is used to group similar types of classes.
  2. Two classes with the same name can be residing in two different namespaces.
  3. A namespace can contain functions.
  4. All of the above

Options:

  1. A
  2. B
  3. A and B
  4. D

2) Which of the following keywords are used for the namespace in C++?

  1. namespace
  2. namespaces
  3. using
  4. use

Options:

  1. A and B
  2. A and C
  3. A, B, and C
  4. A, B, C, and D

3) Which of the following operator is used to access elements of the namespace?

  1. .
  2. :
  3. ::
  4. ->

4) What is the correct output of the given code snippets?

#include <iostream>

using namespace std;

namespace mynamespace {
    private int a = 10;
}

int main()
{
    cout << mynamespace::a << endl;
    return 0;
}

Options:

  1. 10
  2. Garbage value
  3. Syntax error
  4. Runtime error

5) What is the correct output of the given code snippets?

#include <iostream>

using namespace std;

namespace mynamespace {
int a = 10;
}

int main()
{
    cout << mynamespace::a << endl;
    return 0;
}

Options:

  1. 10
  2. Garbage value
  3. Syntax error
  4. Runtime error

6) What is the correct output of the given code snippets?

#include <iostream>

using namespace std;

int a = 10;

namespace mynamespace {
int* ptr;
}

int main()
{
    mynamespace::ptr = &a;

    cout << *(mynamespace::ptr) << endl;
    return 0;
}

Options:

  1. 10
  2. Garbage value
  3. Syntax error
  4. Runtime error

7) What is the correct output of the given code snippets?

#include <iostream>

using namespace std;

int a = 10;

namespace mynamespace {
int& fun()
{
    return a;
}
}

int main()
{
    mynamespace::fun() = 20;

    cout << a << endl;
    return 0;
}

Options:

  1. 10
  2. 20
  3. Garbage values
  4. Syntax error

8) What is the correct output of the given code snippets?

#include <iostream>

using namespace std;

namespace mynamespace {
class Sample1 {
public:
    void fun()
    {
        cout << "Sample1 ";
    }
};

class Sample2 {
public:
    void fun()
    {
        cout << "Sample2 ";
    }
};
}

int main()
{
    mynamespace::Sample1 S1;
    mynamespace::Sample2 S2;

    S1.fun();
    S2.fun();

    return 0;
}

Options:

  1. Sample1 Sample2
  2. Runtime error
  3. Garbage values
  4. Syntax error

9) What is the correct output of the given code snippets?

#include <iostream>

using namespace std;

namespace mynamespace {
public
class Sample1 {
public:
    void fun()
    {
        cout << "sample1 ";
    }
};

public
class Sample2 {
public:
    void fun()
    {
        cout << "sample2 ";
    }
};
}

int main()
{
    mynamespace::Sample1 S1;
    mynamespace::Sample2 S2;

    S1.fun();
    S2.fun();

    return 0;
}

Options:

  1. Sample1 Sample2
  2. Garbage value
  3. Syntax error
  4. Runtime error

10) What is the correct output of the given code snippets?

#include <iostream>

using namespace std;

namespace mynamespace {
class Sample1 {
public:
    void fun()
    {
        cout << "Sample1 ";
    }
};

struct Sample2 {
public:
    void fun()
    {
        cout << "Sample2 ";
    }
};
}

int main()
{
    mynamespace::Sample1 S1;
    mynamespace::Sample2 S2;

    S1.fun();
    S2.fun();

    return 0;
}

Options:

  1. Sample1 Sample2
  2. Garbage values
  3. Syntax error
  4. Runtime exception





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.