25 questions on Programming Practices (C++) along with answers:

Questions and Answers:

  1. What is the main difference between C and C++?

    • C++ supports object-oriented programming, while C is procedural.
  2. Explain encapsulation in C++.

    • Encapsulation restricts access to certain class components, bundling data with functions.
  3. What is inheritance in C++?

    • Inheritance allows one class to acquire the properties of another, enabling code reuse.
  4. What is polymorphism in C++?

    • Polymorphism enables functions or operators to behave differently based on context (e.g., overloading).
  5. Define constructor and destructor in C++.

    • Constructors initialize objects; destructors clean up resources when objects go out of scope.
  6. What is operator overloading?

    • Operator overloading allows operators to work with user-defined types.
  7. Explain what a virtual function is in C++.

    • A virtual function allows derived classes to override a function defined in the base class.
  8. What is a pure virtual function?

    • A pure virtual function is declared in a base class with = 0, making the class abstract.
  9. Explain the concept of namespaces in C++.

    • Namespaces prevent naming conflicts by organizing code into distinct areas.
  10. What is the this pointer?

    • this is a pointer to the current object instance, used within class methods.
  11. Define function overloading.

    • Function overloading allows multiple functions with the same name but different parameters.
  12. What is the purpose of friend keyword in C++?

    • friend allows a function or another class to access private members of a class.
  13. Explain the static keyword in C++.

    • static variables retain their value between function calls and are shared by all instances of a class.
  14. What is an exception in C++?

    • An exception is an error that occurs during execution; it’s handled with trycatch, and throw.
  15. Define the concept of dynamic memory allocation in C++.

    • Dynamic memory is allocated during runtime using new and deallocated with delete.
  16. What are templates in C++?

    • Templates allow generic programming by creating functions or classes to work with any data type.
  17. What is the difference between struct and class in C++?

    • By default, struct members are public, while class members are private.
  18. Explain what a copy constructor is.

    • A copy constructor creates a new object as a copy of an existing object.
  19. What is a function pointer?

    • A function pointer stores the address of a function, allowing dynamic function calls.
  20. Explain the role of the const keyword.

    • const defines constants, preventing variables from being modified after initialization.
  21. What are standard template library (STL) containers?

    • STL containers like vectorlistmap, and set store collections of data.
  22. What is the difference between ++i and i++?

    • ++i increments i before returning it; i++ increments after returning the original i.
  23. Explain type casting in C++.

    • Type casting converts one data type to another using static_castdynamic_castconst_cast, and reinterpret_cast.
  24. What is a lambda function in C++?

    • Lambda functions are anonymous functions defined with [] for use in-line.
  25. What is the purpose of a move constructor?

    • A move constructor transfers resources from a temporary object to a new object, avoiding deep copies.

Let me know if you need more in-depth explanations or examples for any of these!