C++ Questions

Questions which can be used for B.E(First Year), BCA, MCA and IT Placement tests

14 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What does a compiler do?

  1. It translates source code into activities (which may comprise groups of machine instructions) and immediately thereafter executes those activities.
  2. It translates source code directly into assembly language or machine instructions. The eventual end product is a file or files containing machine code.
  3. It combines a list of object modules into an executable program that can be loaded and run by the operating system.
  4. It only enables the programmer to edit the program files.
Question 2 Multiple Choice (Single Answer)

How does the do-while loop different from the while loop?

  1. The do-while loop is faster than the while loop.
  2. The do-while loop is slower than the while loop.
  3. There is no difference.
  4. In a do-while loop, the statement always executes at least once.
Question 3 Multiple Choice (Single Answer)

What does the following C++ statement do?
extern int a;

  1. Declaration of variable 'a' without definition
  2. Definition of variable 'a'
  3. Declaration and definition of variable 'a'
  4. Declaration of function pointer 'a'
Question 4 Multiple Choice (Single Answer)

If a class declares a constructor taking one or more parameters but does not declare a default constructor, then

  1. every class object definition must provide the required arguments
  2. it is a compilation error
  3. it is a runtime error
  4. the compiler automatically inserts the default constructor
Question 5 Multiple Choice (Single Answer)

Assume all the relevant header files are included.

In the following code, which function will be called?
void print(int) {}
void print(int&) {}
int iobj;
int &ri = iobj;

int main() {

           print(iobj);

}

  1. Print(int)
  2. Print(int&)
  3. Compilation error: Ambiguous
  4. Runtime error
Question 6 Multiple Choice (Single Answer)

A private member can be accessed only by the

  1. member functions
  2. member functions and friends of its class
  3. member functions and derived class member functions
  4. static functions
Question 7 Multiple Choice (Single Answer)

Consider the following array definition
int ia[] = { 0, 1, 2, 3, 4, 5 };

What is the equivalent of the following statement?
ia;

  1. ia[0];
  2. *ia;
  3. &ia[0];
  4. Ia[1];
Question 8 Multiple Choice (Single Answer)

Which of the following STL containers is an appropriate choice in a situation where a random access into a container is required?

  1. Vector
  2. List
  3. Both list and vector
  4. Random access is not supported in STL.
Question 9 Multiple Choice (Single Answer)

Which of the following is NOT true for C++ reference?

  1. A reference must be initialised, when it is created.
  2. Once a reference is initialized to an object, it cannot be changed to refer to another object.
  3. NULL references are not allowed.
  4. When a reference is used as a function argument, any modification to the reference inside the function will not cause changes to the argument outside the function.
Question 10 Multiple Choice (Single Answer)

What is the output of the following program?
Assume sizeof(int) = 4

#include <iostream>
using namespace std;

class Base {
static int b;
};
class Derived : public Base {
int d;
};
int main() {
Derived objD;
cout << sizeof(objD) << endl;
}

  1. 4
  2. 8
  3. 5
  4. 12
Question 11 Multiple Choice (Single Answer)

What is the output of the following program?

#include <iostream>
using namespace std;

class Base {
public:
virtual void fun() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
void fun() { cout << “Derived Class” << endl; }
};
int main() {
Derived objD;
Base *ptrB = &objD;
ptrB->fun();
}

  1. Base Class
  2. Derived Class
  3. No output. A Compilation Error
  4. No output. A Runtime Error
Question 12 Multiple Choice (Single Answer)

What is the output of the following program?

#include <iostream>
using namespace std;

                    class Base {
                                public:
                                      Base() { cout &lt;&lt; &#x201C;Base Class&#x201D; &lt;&lt; endl; }
                    };
                    class Derived : public Base {
                                public:
                                Derived() { cout &lt;&lt; &#x201C;Derived Class&#x201D; &lt;&lt; endl; }
                    };
                      int main() {
                                Derived objD;
                    }
  1. Derived Class
  2. Base Class
  3. Derived Class
    Base Class
  4. Base Class
    Derived Class
Question 13 Multiple Choice (Single Answer)

What is the output of the following program?

#include <iostream>
using namespace std;

class Base {
public:
void fun() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
void fun() { cout << “Derived Class” << endl; }
};
int main() {
Derived objD;
Base *ptrB = &objD;
ptrB->fun();
}

  1. Base Class
  2. Derived Class
  3. No output. A Compilation Error
  4. No output. A Runtime Error
Question 14 Multiple Choice (Single Answer)

What is the output of the following program?

Assume sizeof( int ) = 4

#include <iostream>
using namespace std;

class Base {
public:
virtual void fun() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
void fun() { cout << “Derived Class” << endl; }
};
int main() {
Base objBase;
cout << sizeof( objBase ) << endl;
}

  1. 0
  2. 1
  3. 4
  4. 8