1. What are the main types of operators in terms of number of arguments?
Operators are classified based on the number of operands they require.
- Unary Operator
- Takes one operand
- Example:
++a,--a
- Binary Operator
- Takes two operands
- Example:
a + b,a * b
- Ternary Operator
- Takes three operands
- Example:
condition ? value1 : value2
2. What is the “this” pointer?
The this pointer is a special pointer available inside member functions that points to the current object of the class.
Example
class Test{
int x;
public:
void setX(int x){
this->x = x;
}
};
3. What are manipulators?
Manipulators are special functions used with input/output streams to format data.
Example
endl
setw()
setprecision()
Example:
cout << setw(10) << 100;
4. If requested memory is not available what do malloc/calloc/new return?
- malloc / calloc → return NULL
- new → throws bad_alloc exception (or returns NULL in older compilers)
5. What is memory leak?
A memory leak occurs when dynamically allocated memory is not released using delete or free, causing memory wastage.
Example:
int *p = new int;
If delete p; is not used → memory leak occurs.
6. What does code optimization mean?
Code optimization means improving program efficiency without changing output.
Goals:
- Reduce execution time
- Reduce memory usage
- Improve performance
7. Difference between structure and class
| Structure | Class |
| Members are public by default | Members are private by default |
| Mostly used for simple data | Used for full OOP programming |
8. Difference between endl and \n
| endl | \n |
| Inserts new line and flushes buffer | Only inserts new line |
| Slower | Faster |
Example:
cout << "Hello" << endl;
cout << "Hello\n";
9. When does an object get destroyed?
An object is destroyed when:
- It goes out of scope
- Program ends
deleteis called for dynamic object
At that time destructor is executed.
10. What is static variable and its lifetime?
A static variable is initialized only once and retains its value between function calls.
Lifetime = entire program execution
Example
void func(){
static int x = 0;
x++;
}
11. Output of the following functions
Function 1
void func1(){
int x = 0;
x++;
cout << x << endl;
}
Output if called 3 times
1
1
1
Because x is created again every time.
Function 2
void func2(){
static int x = 0;
x++;
cout << x << endl;
}
Output
1
2
3
Because static variable retains value.
12. What is friendship in classes?
A friend function can access private and protected members of a class.
Example
class A{
private:
int x;
friend void show(A);
};
13. Limitations of friendship
- Friendship is not inherited
- Friendship is not mutual
- Friendship is not transitive
14. What are disadvantages of templates?
- Code becomes difficult to read
- Compiler errors become complex
- Debugging becomes harder
15. What is assignment operator?
The assignment operator (=) is used to assign the value of one object to another.
Example
a = b;
16. Constructor and Destructor call order
If object of class A is inside class B
class B{
A obj;
};
Constructor order
- A constructor
- B constructor
Destructor order
- B destructor
- A destructor
17. Unary vs Binary operator
| Unary Operator | Binary Operator |
| Works on one operand | Works on two operands |
Example: ++a | Example: a + b |
Overloading Syntax
return_type operator symbol(parameters);
Example
Vector operator+(Vector v);
18. What are steps to design a good program?
- Problem analysis
- Algorithm design
- Coding
- Testing
- Debugging
- Documentation
19. What is truth table?
A truth table shows the output of a logical expression for all possible combinations of inputs.
Example
| A | B | A AND B |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
20. What is default constructor?
A constructor without parameters is called a default constructor.
Example
class A{
public:
A(){
cout<<"Default Constructor";
}
};
21. What is conversion constructor?
A constructor that converts one data type into class type.
Example
class Test{
int x;
public:
Test(int a){
x = a;
}
};
22. Why are utility functions kept private?
Utility functions are kept private so they:
- cannot be accessed outside the class
- are used only internally
- improve security
23. What does free format language mean?
C and C++ are free format languages, meaning:
- spaces
- tabs
- line breaks
do not affect program logic.
24. Types of user defined conversions
- Conversion Constructor
- Operator Conversion Function
25. What is STL?
STL = Standard Template Library
Provides ready-made:
- containers (vector, list)
- algorithms
- iterators
Advantages:
- Code reuse
- Efficiency
- Type safety