OOPSvivaquestion(SnapED)
OOPSvivaquestion(SnapED)
Questions
Subject Object Orientd Programming Using Code CIC-211
C++
Answer: Object-Oriented Programming (OOP) is a programming paradigm that revolves around the
concept of "objects," which can contain data and code to manipulate that data.
Answer: A class in C++ is a user-defined data type that defines the properties (data members) and
behaviors (member functions) of objects.
Answer: An object in C++ is an instance of a class. It is a variable of the class type and can hold data
and call member functions defined in the class.
Answer: The four fundamental principles of OOP are encapsulation, inheritance, polymorphism, and
abstraction.
Answer: Encapsulation is the bundling of data (attributes) and methods (functions) that operate on
the data within a class, protecting the data from unauthorized access.
Answer: Inheritance is the mechanism by which a class can inherit the properties (data members and
member functions) of another class. It promotes code reuse and establishes a relationship between
classes.
Answer: In C++, inheritance can be of four types: single inheritance, multiple inheritance, hierarchical
inheritance, and multilevel inheritance.
Answer: Polymorphism allows objects to be treated as instances of their parent class, enabling
different implementations for methods in derived classes.
Answer: Polymorphism is achieved through function overloading and function overriding. Function
overloading involves defining multiple functions with the same name but different parameters, while
function overriding involves redefining a function in a derived class.
Answer: Function overloading is when multiple functions have the same name but different
parameters (different types or a different number of parameters).
Answer: Function overriding is when a function in a derived class has the same signature (name and
parameters) as a function in its base class. The function in the derived class replaces the function in
the base class during runtime polymorphism.
Answer: The 'virtual' keyword is used to declare a function as virtual in a base class. Virtual functions
can be overridden in derived classes, enabling runtime polymorphism.
Answer: A pure virtual function is a virtual function that has no implementation in the base class and
is marked with '= 0'. It acts as a placeholder for the derived classes to implement.
Answer: An abstract class is a class that contains one or more pure virtual functions. Instances of an
abstract class cannot be created, and it is meant to be subclassed.
Answer: Constructors are special member functions of a class that are automatically called when an
object is created. They initialize the object's state and allocate resources if needed.
Answer: A destructor is a special member function of a class that is called when an object is destroyed
or goes out of scope. It is used to release resources held by the object.
Answer: A default constructor is one that can be called without any arguments, while a parameterized
constructor accepts parameters that are used to initialize the object's state.
Answer: A copy constructor is a constructor that initializes an object using another object of the same
class. It creates a new object with the same values as the existing object.
Answer: Operator overloading allows operators (such as +, -, *, /) to be used with user-defined data
types (classes). It enables custom operations for these operators with class objects.
Answer: The 'this' pointer is a pointer that holds the memory address of the current object. It is used
to refer to the current instance of the class within its member functions.
Answer: Static members (data members or functions) in a class are shared among all instances of the
class. They belong to the class itself, not to individual objects.
Answer: A 'friend' function is a function that is not a member of a class but is granted access to the
class's private and protected members. It is declared using the 'friend' keyword.
Answer: The 'const' keyword in C++ is used to declare variables or functions as constant, indicating
that they cannot be modified.
Answer: Dynamic memory allocation in C++ is the process of allocating memory at runtime using
operators like 'new' and deallocating it using 'delete'. It is used for creating objects with a lifetime
determined during runtime.
Answer: A destructor is a special member function of a class that is automatically called when an
object is destroyed. Its purpose is to release resources held by the object, ensuring proper cleanup
and preventing memory leaks.
Answer: To prevent object copying, you can make the copy constructor and the assignment operator
private or delete them in the class definition, or you can inherit privately from a non-copyable base
class.
Answer: Function templates in C++ allow you to write a single function that can work with different
data types. The data type is determined by the type of arguments passed during function invocation.
Answer: Member initializer lists are used in constructors to initialize class members before the
constructor body executes. They follow a colon and a comma-separated list of member initializations.
Answer: Operator precedence determines the order in which operators are evaluated in an
expression. It is crucial to understand this order to avoid unexpected behavior in expressions.
Answer: The 'typeid' operator in C++ allows you to obtain type information about an expression or a
type. It is often used for runtime type identification.
Answer: The 'const_cast' operator is used to add or remove the 'const' qualifier from a variable. It
allows you to modify a 'const' object.
Answer: The 'dynamic_cast' operator is used for safe downcasting of pointers or references to classes
in an inheritance hierarchy. It performs a runtime check to ensure the conversion is valid.
Answer: The 'reinterpret_cast' operator is used for type casting that allows casting between unrelated
types, like converting a pointer to an integer type. It's a low-level cast and should be used with
caution.
Answer: 'static_cast' is used for safe and explicit type conversions at compile time, while
'dynamic_cast' is used for safe type conversions during runtime and is primarily used for polymorphic
base-to-derived conversions.
Answer: Function hiding occurs when a derived class defines a function with the same name as a
function in its base class, making the base class function inaccessible in the derived class.
Answer: Method overriding in C++ involves redefining a virtual function in a derived class with the
same signature as the virtual function in the base class. This enables the derived class to provide its
implementation.
Answer: Access specifiers (public, private, protected) in C++ classes determine the visibility and
accessibility of class members. Public members are accessible from outside the class, private members
are accessible only within the class, and protected members are accessible within the class and
derived classes.
Answer: 'const' member functions are member functions that do not modify the object's state. They
are used to read data members and ensure the object's integrity is maintained.
Answer: A friend function is a function that is not a member of a class but has access to its private and
protected members. It is used when a function needs to access class internals without being a
member of the class.
Answer: The 'inline' function is a function declared with the 'inline' keyword, suggesting the compiler
to insert the function's code at the point of its call, saving the overhead of function invocation.
Answer: A const reference is a reference that is bound to a variable and cannot be used to modify the
variable's value. It is often used to pass variables to functions without allowing modification.
Answer: C++ supports multiple inheritances by allowing a class to inherit from multiple base classes.
This means a class can have multiple parent classes.
Answer: A virtual destructor is a destructor declared as virtual in a base class. It ensures that the
destructor of the most derived class is called when deleting a pointer to a base class type.
Answer: Early binding, also known as static binding, occurs during compile time and involves resolving
the method calls based on the declared type. Late binding, also known as dynamic binding, occurs at
runtime and involves resolving method calls based on the actual type of the object.