C-Plus-Plus Questions

0 votes, 0 avg
159

C-Plus-Plus Questions

1 / 15

1. What is virtual inheritance in C++?

2 / 15

2. What will be the output of the following C++ code?

        #include 

        using namespace std;

        void Sum(int a, int b, int & c)

        {

            a = b + c;

            b = a + c;

            c = a + b;

        }

        int main()

        {

            int x = 2, y =3;

            Sum(x, y, y);

            cout << x << " " << y;

            return 0; 

        }

                            

3 / 15

3. Which of the following constructors are provided by the C++ compiler if not defined in a class?

4 / 15

4. What is C++?

5 / 15

5. What will be the output of the following C++ code?

#include 
using namespace std; 
template 
T max (T &a, T &b)
{
	cout << "Template Called ";
    return (a > b)? a : b;
}
 
template <>
int max  (int &a, int &b)
{
    cout << "Called ";
    return (a > b)? a : b;
}
 
int main ()
{
    int a = 10, b = 20;
    cout << max  (a, b);
}

6 / 15

6. What will be the output of the following C++ code?

#include 
using namespace std; 
template 
int arrMin(T arr[], int n)
{
   int m = max;
   for (int i = 0; i < n; i++)
      if (arr[i] < m)
         m = arr[i];
 
   return m;
}
 
int main()
{
   int arr1[]  = {10, 20, 15, 12};
   int n1 = sizeof(arr1)/sizeof(arr1[0]);
 
   char arr2[] = {1, 2, 3};
   int n2 = sizeof(arr2)/sizeof(arr2[0]);
 
   cout << arrMin(arr1, n1) << endl;
   cout << arrMin(arr2, n2);
   return 0;
}

7 / 15

7. What is the use of the indentation in c++?

8 / 15

8. What will be the output of the following C++ code?

        #include 

        using namespace std;

        int main()

        {

            int a, b;

            int* c;

            c = &a;

            a = 200;

            b = 200;

            *c = 100;

            b = *c;

            cout << *c << " " << b;

            return 0;

        }

9 / 15

9. Which is more effective while calling the C++ functions?

10 / 15

10. What is the output of this C++ program in the “test.txt” file?

        #include 

        using namespace std;

        int main ()

        {

            long pos;

            ofstream outfile;

            outfile.open ("test.txt");

            outfile.write ("This is an apple",16);

            pos = outfile.tellp();

            outfile.seekp (pos - 7);

            outfile.write (" sam", 4);

            outfile.close();

            return 0;

        }

11 / 15

11. What will be the output of the following C++ code?

        #include 

        using namespace std;

        int gcd (int a, int b)

        {

            int temp;

            while (b != 0) 

            {

                temp = a % b;

                a = b;

                b = temp;

            }

            return(a);

        }

        int main ()

        {

            int x = 15, y = 25;

            cout << gcd(x, y);

            return(0);

        }

12 / 15

12. Predict the output of following C++ program.

#include
using namespace std;
 
union A {
  int a;
  unsigned int b;
  A() { a = 10; }
  unsigned int getb() {return b;}
};
 
int main()
{
    A obj;
    cout << obj.getb();
    return 0;
}

13 / 15

13. Which of the following statements is correct about the formal parameters in C++?

14 / 15

14. Find the output of below program.

int main()
{
for(int i=1;i<=2;i++)
{
for(int j=i;j<=2;j++)
cout<

15 / 15

15. What will be the output of the following C++ code?

#include 
    using namespace std;
    void swap(int &a, int &b);
    int main()
    {
        int a = 5, b = 10;
        swap(a, b);
        cout << "In main " << a << b;
        return 0;
    }
    void swap(int &a, int &b)
    {
        int temp;
        temp = a;
        a = b;
        b = temp;
        cout << "In swap " << a << b;
    }

Your score is

The average score is 29%

0%