Made with ScholarlyMake flashcards like these from your own notes
Question:What is a class in C++?
Answer:A class is a user-defined data type that defines the properties and behaviors of a set of objects. It acts as a blueprint for creating objects.
Question:What is an object in C++?
Answer:An object is an instance of a class, a concrete entity created from a class that can have a color and shape, for example.
Question:What are data members and member functions in a class?
Answer:Data members are variables that store the properties of the class, while member functions are functions that define the behaviors of the class.
Question:What is encapsulation in C++?
Answer:Encapsulation is the mechanism of hiding the internal details of an object and providing access only to what's necessary through public interfaces.
Question:What is polymorphism in C++?
Answer:Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling functions to operate on objects without knowing their exact class.
Question:Explain the concept of a Student class with data members like name, id, and gpa.
Answer:The Student class encapsulates data members such as name (string), id (int), and gpa (float). Each object of the Student class will copy these data members.
Question:What is an abstract data type?
Answer:An abstract data type is a self-contained entity composed of both data attributes stored in data members and functions defined by member functions that operate on the data.
Question:What is a normal member function in C++?
Answer:A normal member function is defined outside the class and can only access private members of the class it is defined for.
Question:What is an inline member function?
Answer:An inline member function is defined inside the class declaration and can directly access the data members.
Question:What are the primitive data types in C++?
Answer:int, short, long, long long, float, double, char, bool.
Question:What is data hiding?
Answer:Data hiding is the practice of making data members private to protect them from external access and modification.
Question:What is encapsulation?
Answer:Encapsulation is the bundling of data members and member functions that operate on that data within a class.
Question:What is a static data member?
Answer:A static data member is a class variable that is shared among all objects of the class and belongs to the class itself, not to any individual object.
Question:What are the different ways of defining data members?
Answer:Data members can be defined as private, protected, or public based on access specifiers.
Question:What is the syntax for declaring a static data member?
Answer:Static data members are declared inside the class with the static keyword and defined outside with the class scope resolution operator.
Question:What is the difference between static and non-static data members?
Answer:Static members are shared across all objects and belong to the class, while non-static (instance) members are unique to each object.
Question:What is a constructor?
Answer:A constructor is a special member function that initializes the data members of an object when it is created.
Question:What is a constant data member?
Answer:A constant data member is a data member declared with const that cannot be modified after initialization.
Question:What are access specifiers?
Answer:Access specifiers are keywords (public, private, protected) that control the visibility and access to class members.
Question:What is private access?
Answer:Private members are accessible only by member functions of the same class.
Question:What is protected access?
Answer:Protected members are accessible by member functions of the same class and derived classes.
Question:What is public access?
Answer:Public members are accessible from anywhere.
Question:What is mutability for objects?
Answer:Mutability refers to whether an object can be modified after creation; const objects cannot.
Question:How do you declare a class with a constructor?
Answer:Use the class keyword, declare members, and define the constructor inside or outside the class.
Question:What is the purpose of a constructor?
Answer:To initialize the object's data members upon creation.
Question:What are the types of constructors?
Answer:Default constructor (no parameters), parameterized constructor (with parameters), copy constructor (copies another object).
Question:What is a default constructor?
Answer:A constructor that takes no arguments and provides default initialization.
Question:What is a parameterized constructor?
Answer:A constructor that accepts arguments to initialize specific values for data members.
Question:What is a copy constructor?
Answer:A constructor that initializes an object using another object of the same class.
Question:How would you define a Triangle class with width and height parameters?
Answer:class Triangle { public: Triangle(float w, float h) : width(w), height(h) {} private: float width, height; };
Question:Explain how to implement a parameterized constructor.
Answer:Define the constructor with parameters and use them to set data members, either in the body or initializer list.
Question:What is the difference between a constructor and a destructor?
Answer:Constructor initializes the object when created; destructor cleans up resources when the object is destroyed.
Question:What is the purpose of a destructor?
Answer:To release resources like memory allocated by the object before it is destroyed.
Question:What is the name of a destructor?
Answer:The class name preceded by a tilde (~).
Question:What are new and delete operators?
Answer:New allocates memory for objects dynamically; delete deallocates it.
Question:What is dynamic memory allocation?
Answer:Allocating memory at runtime using new, as opposed to static allocation at compile time.
Question:What is the delete operator?
Answer:Used to deallocate memory that was dynamically allocated with new.
Question:What is dynamic memory in C++?
Answer:Memory allocated at runtime using new and delete, managed manually unlike automatic variables.
Question:What is memory leak?
Answer:When dynamically allocated memory is not deallocated, leading to wasted memory.
Question:What is a pointer?
Answer:A variable that stores the memory address of another variable.
Question:What is the address of operator (&)?
Answer:Returns the memory address of its operand.
Question:What is the dereference operator (*)?
Answer:Accesses the value at the address stored in a pointer.
Question:How do you use the dot operator with an object?
Answer:obj.member accesses members directly with .
Question:How do you use the arrow operator with a pointer to an object?
Answer:ptr->member accesses members through the pointer.
Question:What is this pointer?
Answer:A pointer that points to the current object inside a member function.
Question:What is an implicit call?
Answer:When the compiler automatically calls a function, like a default constructor.
Question:What is an explicit call?
Answer:When you directly call a function in your code.
Question:What happens when you pass an object by value?
Answer:A copy of the object is made and passed, invoking the copy constructor if defined.
Question:What is an implicit call to the copy constructor?
Answer:When the compiler calls it automatically, like when passing by value or initializing.
Question:What is a default constructor?
Answer:A constructor with no parameters that initializes objects with default values.
Question:What are the initialization lists?
Answer:A way to initialize data members in the constructor header using : member(value).
Question:What is a delegating constructor?
Answer:A constructor that calls another constructor in the same class to handle initialization.
Question:What are data members?
Answer:Variables declared inside a class that hold the state of objects.
Question:What is an initializer list?
Answer:A list after the constructor parameters that initializes members before the body executes.
Question:What is the new operator?
Answer:Used to dynamically allocate memory for objects or arrays.
Question:What is the unary + operator?
Answer:Performs no operation but can be overloaded; for example, +x for a number.
Question:What is the unary - operator?
Answer:Negates the value; for example, -x for a number.
Question:What is the logical NOT operator (!)?
Answer:Returns true if the operand is false, and vice versa.
Question:What are binary operators like + , - , * , / ?
Answer:Perform arithmetic operations on two operands.
Question:What is the [] operator?
Answer:Used for array indexing, overloaded for custom containers.
Question:What is the () operator?
Answer:Used for function calls, overloaded for functors.
Question:What is operator overloading?
Answer:Defining how operators work with user-defined types, like + for strings.
Question:What is the >> operator overloading?
Answer:Overloaded for input streams to read into objects.
Question:What is the << operator overloading?
Answer:Overloaded for output streams to print objects.
Question:What is single inheritance?
Answer:A class inherits from one base class.
Question:What is multiple inheritance?
Answer:A class inherits from more than one base class.
Question:What is multilevel inheritance?
Answer:Inheritance in a chain, like A -> B -> C.
Question:What is hierarchical inheritance?
Answer:Multiple classes inherit from one base class.
Question:What is hybrid inheritance?
Answer:A combination of two or more types of inheritance.
Question:What is virtual inheritance?
Answer:Used to avoid multiple copies of base class in diamond inheritance problems.
Question:What is an exception?
Answer:An event that occurs during program execution that disrupts normal flow.
Question:What is exception handling in C++?
Answer:Using try, catch, and throw to manage errors gracefully.
Question:What is a throw keyword?
Answer:Used to raise an exception when an error occurs.
Question:What is a catch block?
Answer:Catches and handles the thrown exception.
Question:What is a try block?
Answer:Encloses code that might generate an exception.
Question:What is a catch block?
Answer:Handles the exception thrown from the try block.
Question:What are compile-time errors?
Answer:Errors detected by the compiler before runtime, like syntax errors.
Question:What are runtime errors?
Answer:Errors that occur during program execution, like division by zero.
Question:What are logical errors?
Answer:Errors where the program runs but produces incorrect results.
Question:What is generic programming?
Answer:Writing code that works with any data type using templates.
Question:What is a class template?
Answer:A blueprint for a generic class that can work with different data types.
Question:What is a function template?
Answer:A blueprint for a generic function that can operate on different types.
Question:What are the advantages of templates?
Answer:Code reusability, type safety, and efficiency without runtime overhead.
Question:How do you write a function template to add two values?
Answer:template T add(T a, T b) { return a + b; }
Question:What is a class template?
Answer:A class definition with type parameters, instantiated with specific types.
Question:What is a function template?
Answer:A function definition with type parameters for generic behavior.