C++ Questions
Questions which can be used for B.E(First Year), BCA, MCA and IT Placement tests
Questions
What does a compiler do?
- It translates source code into activities (which may comprise groups of machine instructions) and immediately thereafter executes those activities.
- It translates source code directly into assembly language or machine instructions. The eventual end product is a file or files containing machine code.
- It combines a list of object modules into an executable program that can be loaded and run by the operating system.
- It only enables the programmer to edit the program files.
How does the do-while loop different from the while loop?
- The do-while loop is faster than the while loop.
- The do-while loop is slower than the while loop.
- There is no difference.
- In a do-while loop, the statement always executes at least once.
What does the following C++ statement do?
extern int a;
- Declaration of variable 'a' without definition
- Definition of variable 'a'
- Declaration and definition of variable 'a'
- Declaration of function pointer 'a'
If a class declares a constructor taking one or more parameters but does not declare a default constructor, then
- every class object definition must provide the required arguments
- it is a compilation error
- it is a runtime error
- the compiler automatically inserts the default constructor
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);
}
- Print(int)
- Print(int&)
- Compilation error: Ambiguous
- Runtime error
A private member can be accessed only by the
- member functions
- member functions and friends of its class
- member functions and derived class member functions
- static functions
Consider the following array definition
int ia[] = { 0, 1, 2, 3, 4, 5 };
What is the equivalent of the following statement?
ia;
- ia[0];
- *ia;
- &ia[0];
- Ia[1];
Which of the following STL containers is an appropriate choice in a situation where a random access into a container is required?
- Vector
- List
- Both list and vector
- Random access is not supported in STL.
Which of the following is NOT true for C++ reference?
- A reference must be initialised, when it is created.
- Once a reference is initialized to an object, it cannot be changed to refer to another object.
- NULL references are not allowed.
- 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.
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;
}
- 4
- 8
- 5
- 12
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();
}
- Base Class
- Derived Class
- No output. A Compilation Error
- No output. A Runtime Error
What is the output of the following program?
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
Derived() { cout << “Derived Class” << endl; }
};
int main() {
Derived objD;
}
- Derived Class
- Base Class
- Derived Class
Base Class - Base Class
Derived Class
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();
}
- Base Class
- Derived Class
- No output. A Compilation Error
- No output. A Runtime Error
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;
}
- 0
- 1
- 4
- 8