25 questions on Programming Practices (C++) along with answers:
Questions and Answers:
What is the main difference between C and C++?
- C++ supports object-oriented programming, while C is procedural.
Explain encapsulation in C++.
- Encapsulation restricts access to certain class components, bundling data with functions.
What is inheritance in C++?
- Inheritance allows one class to acquire the properties of another, enabling code reuse.
What is polymorphism in C++?
- Polymorphism enables functions or operators to behave differently based on context (e.g., overloading).
Define constructor and destructor in C++.
- Constructors initialize objects; destructors clean up resources when objects go out of scope.
What is operator overloading?
- Operator overloading allows operators to work with user-defined types.
Explain what a virtual function is in C++.
- A virtual function allows derived classes to override a function defined in the base class.
What is a pure virtual function?
- A pure virtual function is declared in a base class with
= 0
, making the class abstract.
- A pure virtual function is declared in a base class with
Explain the concept of namespaces in C++.
- Namespaces prevent naming conflicts by organizing code into distinct areas.
What is the
this
pointer?this
is a pointer to the current object instance, used within class methods.
Define function overloading.
- Function overloading allows multiple functions with the same name but different parameters.
What is the purpose of
friend
keyword in C++?friend
allows a function or another class to access private members of a class.
Explain the
static
keyword in C++.static
variables retain their value between function calls and are shared by all instances of a class.
What is an exception in C++?
- An exception is an error that occurs during execution; it’s handled with
try
,catch
, andthrow
.
- An exception is an error that occurs during execution; it’s handled with
Define the concept of dynamic memory allocation in C++.
- Dynamic memory is allocated during runtime using
new
and deallocated withdelete
.
- Dynamic memory is allocated during runtime using
What are templates in C++?
- Templates allow generic programming by creating functions or classes to work with any data type.
What is the difference between
struct
andclass
in C++?- By default,
struct
members are public, whileclass
members are private.
- By default,
Explain what a copy constructor is.
- A copy constructor creates a new object as a copy of an existing object.
What is a function pointer?
- A function pointer stores the address of a function, allowing dynamic function calls.
Explain the role of the
const
keyword.const
defines constants, preventing variables from being modified after initialization.
What are standard template library (STL) containers?
- STL containers like
vector
,list
,map
, andset
store collections of data.
- STL containers like
What is the difference between
++i
andi++
?++i
incrementsi
before returning it;i++
increments after returning the originali
.
Explain type casting in C++.
- Type casting converts one data type to another using
static_cast
,dynamic_cast
,const_cast
, andreinterpret_cast
.
- Type casting converts one data type to another using
What is a lambda function in C++?
- Lambda functions are anonymous functions defined with
[]
for use in-line.
- Lambda functions are anonymous functions defined with
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!
0 Comments