Q1. Main features of C++ with explanation.
Ans: The main features of C++ are:
1. Object-Oriented: Supports classes, objects, inheritance, polymorphism.
2. Simple: Easier syntax compared to C, but with powerful features.
3. Portable: Platform-independent.
4. Middle-Level Language: Combines features of high-level and low-level languages.
5. Rich Library Support: STL for data structures and algorithms.
6. Memory Management: new/delete operators.
7. Reusability: Through inheritance.
8. Encapsulation: Provides data hiding and abstraction.
Q2. Differences between C and C++.
Ans:
- C is procedural, C++ is object-oriented.
- C has no classes/objects; C++ supports them.
- Functions are central in C, Classes are central in C++.
- C does not support polymorphism/inheritance; C++ does.
- C uses stdio.h, C++ uses iostream.
- C uses malloc/free; C++ uses new/delete.
- C has no function overloading; C++ supports it.
Q3. Concept of OOP and its advantages.
Ans: OOP organizes programs into classes/objects.
Concepts: Class, Object, Encapsulation, Inheritance, Polymorphism, Abstraction.
Advantages:
- Code reusability
- Data security
- Easier maintenance
- Real-world modeling
- Flexibility with polymorphism
Q4. Encapsulation, Abstraction, Inheritance, and Polymorphism with examples.
Ans:
- Encapsulation: Wrapping data and code (class with private data).
- Abstraction: Hiding implementation, showing only functionality.
- Inheritance: Deriving new classes from existing ones.
- Polymorphism: Same function/operator behaves differently (function/operator overloading).
Q5. Definition and examples of header files in C++.
Ans: Header files contain declarations for functions, classes, objects.
Examples: iostream, cmath, string, vector.
User-defined header files can be made using #include "filename.h".
Q6. Difference between compile-time error and runtime error.
Ans:
- Compile-time: Occurs during compilation (syntax error, type mismatch).
- Runtime: Occurs during execution (division by zero, null pointer).
Q7. Procedure-oriented vs Object-oriented programming.
Ans:
- Procedure-oriented: Focus on functions, data is global.
- Object-oriented: Focus on objects, data is encapsulated in classes.
- OOP supports inheritance, polymorphism; POP does not.
Q8. Structure of a simple C++ program with example.
Ans:
#include
using namespace std;
int main() { cout << "Hello"; return 0; }
Q9. Difference between keyword and identifier.
Ans:
- Keyword: Reserved word (int, class, return).
- Identifier: User-defined name for variables, functions, classes.
Q10. Difference between iostream.h and iostream.
Ans:
- iostream.h: Old header in pre-standard C++.
- iostream: Standard C++ header (without .h).
Q11. Definition and syntax of functions in C++.
Ans:
Syntax: return_type function_name(parameters) { // body }
Q12. Difference between built-in and user-defined functions.
Ans:
- Built-in: Predefined (sqrt(), strlen()).
- User-defined: Defined by programmer.
Q13. Difference between call by value and call by reference.
Ans:
- Call by Value: Copy of variable passed.
- Call by Reference: Address/reference passed.
Q14. Inline functions with example.
Ans: Inline functions replace function call with code at compile time.
Example:
inline int square(int x) { return x*x; }
Q15. Default arguments in functions with example.
Ans: Default arguments are values provided if none are passed.
Example: int sum(int a, int b=5)
Q16. Difference between function overloading and default arguments.
Ans:
- Overloading: Multiple functions with same name but diff. parameters.
- Default Args: One function with default parameter values.
Q17. Use of if, if-else, and nested if in C++.
Ans:
if: single condition.
if-else: condition true/false.
nested if: if inside another if.
Q18. Difference between if-else-if ladder and nested if.
Ans:
- if-else-if: Sequential multiple conditions.
- Nested if: if inside another if block.
Q19. Switch-case statement with syntax and example.
Ans:
switch(expression) { case val: break; default: }
Q20. Role of default case in switch statement.
Ans: Executes when no case matches.
Q21. Difference between switch and if-else statements.
Ans:
- switch works with constants, if-else works with ranges/conditions.
- switch is cleaner for multiple choices.
Q22. Types of loops: for, while, do-while with examples.
Ans:
for: entry-controlled.
while: entry-controlled.
do-while: exit-controlled.
Q23. Difference between entry-controlled and exit-controlled loops.
Ans:
- Entry-controlled: Condition checked before execution (for, while).
- Exit-controlled: Condition checked after execution (do-while).
Q24. Difference between for loop and while loop.
Ans:
- for: initialization, condition, increment in one line.
- while: condition only, initialization before loop.
Q25. Use of break and continue statements.
Ans:
- break: exits loop.
- continue: skips to next iteration.
Q26. Nested loops with example.
Ans:
Example: for(i=0;i<3;i++){ for(j=0;j<3;j++){...}}
Q27. Definition of array and its types: single dimensional and multi-dimensional.
Ans:
Array: Collection of elements of same type.
- 1D: int arr[5].
- 2D: int arr[3][3].
Q28. How arrays are stored in memory.
Ans: Stored in contiguous memory locations.
Q29. Difference between array and pointer.
Ans:
- Array: Fixed size.
- Pointer: Can point to different memory locations.
Q30. Limitations of arrays.
Ans:
- Fixed size.
- Homogeneous data only.
- No built-in bounds checking.
Q31. Initialization of arrays with example.
Ans:
int arr[3]={1,2,3};
Q32. Concept of 2D arrays with example.
Ans:
int arr[2][2]={{1,2},{3,4}};
Q1. Definition and syntax of a class in C++.
Ans: A class is a user-defined data type that contains data members and member functions.
Syntax:
class ClassName { access_specifier: data_members; member_functions; };
Q2. Difference between class and object.
Ans:
- Class: Blueprint, logical entity.
- Object: Instance of class, real entity.
Q3. How objects are created in C++.
Ans:
ClassName obj;
Q4. Meaning and importance of data hiding in a class.
Ans: Restricting direct access to data by making it private. Ensures security and encapsulation.
Q5. Difference between public, private, and protected access specifiers.
Ans:
- public: Accessible everywhere.
- private: Accessible only within class.
- protected: Accessible in class and derived classes.
Q6. Difference between member function and data member.
Ans:
- Data member: Variable inside class.
- Member function: Function inside class.
Q7. Defining member functions inside and outside a class.
Ans:
- Inside: Defined directly.
- Outside: Using scope resolution operator.
Q8. Concept of inline functions in relation to classes.
Ans: Inline functions can be defined inside class. They expand at compile time.
Q9. Static data members and static member functions in a class.
Ans:
- Static data members: Shared by all objects.
- Static functions: Access only static data.
Q10. Importance of classes in object-oriented programming.
Ans: Classes provide encapsulation, reusability, modularity, and blueprint for objects.
Q1. Use of scope resolution operator (::) in C++.
Ans: Used to define/resolve scope of identifiers, global variables, or functions.
Q2. Difference between local and global variables.
Ans:
- Local: Declared inside function, limited scope.
- Global: Declared outside all functions, accessible everywhere.
Q3. Using scope resolution operator to access global variables.
Ans: ::varName
Q4. Defining member functions outside a class using scope resolution operator.
Ans:
void ClassName::functionName() { // body }
Q5. Default scope of functions and variables in C++.
Ans:
- Functions: global by default.
- Variables: local if declared inside function.
Q6. Difference between local, global, and class scope.
Ans:
- Local: Inside function.
- Global: Outside functions.
- Class: Inside class.
Q7. Importance of scope resolution operator with example.
Ans: Resolves ambiguity between global and local variables.
Example:
int x=10; void f(){ int x=20; cout<<::x; }
Q1. Definition and need of constructors in C++.
Ans: Constructor is a special member function with same name as class, used to initialize objects
automatically.
Q2. Difference between constructors and normal member functions.
Ans:
- Constructor has same name as class, no return type.
- Normal functions have return type, defined separately.
Q3. Types of constructors: Default, Parameterized, and Copy with examples.
Ans:
- Default: No arguments.
- Parameterized: Takes arguments.
- Copy: Initializes object from another object.
Q4. Constructor overloading with example.
Ans: Multiple constructors in same class with different parameters.
Q5. Characteristics and rules of constructors in C++.
Ans:
- Same name as class.
- No return type.
- Invoked automatically.
- Can be overloaded.
Q6. Difference between constructor and destructor.
Ans:
- Constructor: Initializes object.
- Destructor: Destroys object, name prefixed with ~.
Q7. When copy constructor is called with example.
Ans: Called when object is initialized from another object.
Example: Class A obj2(obj1);
Q8. Overloading constructors in C++.
Ans: Defining multiple constructors with different parameters.
Q9. Difference between default and user-defined constructors.
Ans:
- Default: Created automatically if none defined.
- User-defined: Explicitly defined by programmer.
Q10. Why constructors cannot be virtual in C++.
Ans: Virtual mechanism requires object creation, but constructors are invoked at creation, hence not possible.