Interview Question: Topper
Interview Question: Topper
Interview Question: Topper
Interview Question
Ans : The main difference between C and C++ are provided in the table below:
C C++
©Topperworld
C C++
Ans : A class is a user-defined data type that has data members and
member functions. Data members are the data variables and member
functions are the functions that are used to perform operations on these
variables.
class A{
private:
Int data;
public:
void fun()
{
}
};
©Topperworld
©Topperworld
Ans : In C++ a structure is the same as a class except for a few differences
like security. The difference between struct and class are given below:
Structure Class
For example -
The following code is for adding two complex number using operator
overloading-
©Topperworld
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
// Overloading the '+' operator to add two complex numbers
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
// Overloading the '<<' operator for easy printing
friend std::ostream& operator<<(std::ostream& out, const
Complex& c) {
out << c.real << " + " << c.imag << "i";
return out;
}
};
int main() {
Complex c1(2.5, 3.5);
Complex c2(1.5, 2.5);
Complex result = c1 + c2;
std::cout << "c1 = " << c1 << std::endl;
std::cout << "c2 = " << c2 << std::endl;
std::cout << "Sum = " << result << std::endl;
return 0;
}
©Topperworld
Q 6. What is polymorphism in C++?
For example, think of a base class called a car that has a method called car
brand(). Derived classes of cars could be Mercedes, BMW, Audi - And they
also have their own implementation of a cars
©Topperworld
©Topperworld
#include <iostream>
class MyClass {
private:
int myInt;
public:
// Constructor with parameter
MyClass(int val) {
myInt = val;
std::cout << "Constructor called with value: " << val << std::endl;
} // Function to display the value of myInt
void display() {
std::cout << "myInt = " << myInt << std::endl;
}
};
int main() {
MyClass obj1(10); // Constructor is called with value 10
MyClass obj2(20); // Constructor is called with value 20
// Displaying the values of myInt for both objects
obj1.display(); // Output: myInt = 10
obj2.display(); // Output: myInt = 20
return 0;
}
©Topperworld
©Topperworld
Ans : Virtual function is a member function in the base class that you
redefine in a derived class. A virtual function is declared using the virtual
keyword. When the function is made virtual, C++ determines which function
is to be invoked at the runtime based on the type of the object pointed by the
base class pointer.
Q 10. What do you know about friend class and friend function?
Ans : A friend class can access private, protected, and public members of
other classes in which it is declared as friends.
©Topperworld
©Topperworld
Like friend class, friend function can also access private, protected, and
public members. But, Friend functions are not member functions.
#include <iostream>
©Topperworld
// Declare FriendClass as a friend class
friend class FriendClass;
public:
void display() {
std::cout << "Data: " << data << std::endl;
}
};
// Definition of friend function
void friendFunction(const MyClass& obj) {
std::cout << "Friend function accessing private data: " <<
obj.data << std::endl;
}
// Definition of friend class method
void FriendClass::friendMethod(const MyClass& obj) {
std::cout << "Friend class method accessing private data: "
<< obj.data << std::endl;
}
int main() {
MyClass obj;
// Accessing private data using friend function
friendFunction(obj);
©Topperworld
Output:
Ans : If a function is inline, the compiler places a copy of the code of that
function at each point where the function is called at compile time.
©Topperworld
Q 13. What is a reference in C++?
Ans : Abstraction is the process of showing the essential details to the user
and hiding the details which we don’t want to show to the user or hiding
the details which are irrelevant to a particular user.
For these copied values a new memory is assigned and changes made to
these values do not reflect the variable in the main function.
In call by reference method, we pass the address of the variable and the
address is used to access the actual argument used in the function call. So
changes made in the parameter alter the passing argument.
©Topperworld
Q 17. What is an abstract class and when do you use it?
Ans : A class is called an abstract class whose objects can never be created.
Such a class exists as a parent for the derived classes.
We can make a class abstract by placing a pure virtual function in the class.
A destructor has the same name as the constructor (which is the same as the
class name) but is preceded by a tilde.
©Topperworld
Example:
#include <iostream>
class MyClass {
public:
// Constructor
MyClass() {
std::cout << "Constructor called." << std::endl;
}
// Destructor
~MyClass() {
std::cout << "Destructor called." << std::endl;
}
};
int main() {
MyClass obj;
// Destructor will be called automatically when obj goes out
of scope
return 0;
}
Output:
Constructor called.
Destructor called.
©Topperworld
Q 19. What are the static members and static member functions?
When a variable in a class is declared static, space for it is allocated for the
lifetime of the program. No matter how many objects of that class have been
created, there is only one copy of the static member. So same static member
can be accessed by all the objects of that class.
A static member function can be called even if no objects of the class exist
and the static function are accessed using only the class name and the scope
resolution operator ::
©Topperworld
Q 21. What is a copy constructor?
Example-
#include <iostream>
class MyClass {
private:
int data;
public:
// Constructor
MyClass(int value) : data(value) {
std::cout << "Constructor called." << std::endl;
}
// Copy constructor
MyClass(const MyClass& other) : data(other.data) {
std::cout << "Copy constructor called." << std::endl;
}
// Display function
void display() {
std::cout << "Data: " << data << std::endl;
}
};
©Topperworld
©Topperworld
int main() {
// Creating an object
MyClass obj1(42);
obj1.display(); // Output: Data: 42
return 0;
}
Output:
Constructor called.
Data: 42
Copy constructor called.
Data: 42
Q 22. What is the difference between shallow copy and deep copy?
Ans : The difference between shallow copy and a deep copy is given below:
Shallow copy stores the references Deep copy makes a new and separate
of objects to the original memory copy of an entire object with its unique
address. memory address.
©Topperworld
Shallow Copy Deep Copy
Shallow copy reflects changes made Deep copy doesn ’ t reflect changes
to the new/copied object in the made to the new/copied object in the
original object. original object
Ans : A virtual function is a member function in the base class that you
redefine in a derived class. It is declared using the virtual keyword.
Classes having virtual functions are Base class containing pure virtual
not abstract. function becomes abstract.
Syntax:
Syntax:
virtual<func_type><func_name>()
{ virtual<func_type><func_name>()
// code = 0;
}
©Topperworld
©Topperworld
Base class having virtual function Base class having pure virtual
can be instantiated i.e. its object can function becomes abstract i.e. it
be made. cannot be instantiated.
Ans : The derived class has two parts, a base part, and a derived part. When
C++ constructs derived objects, it does so in phases. First, the most-base
class(at the top of the inheritance tree) is constructed.
©Topperworld
©Topperworld
Then each child class is constructed in order until the most-child class is
constructed last.
So the first Constructor of class B will be called and then the constructor of
class D will be called.
Ans : Yes, we can call a virtual function from a constructor. But the behavior
is a little different in this case. When a virtual function is called, the virtual
call is resolved at runtime. It is always the member function of the current
class that gets called. That is the virtual machine doesn’t work within the
constructor.
©Topperworld
©Topperworld
For example-
#include <iostream>
class Base {
public:
Base() {
std::cout << "Base constructor called." << std::endl;
// Call virtual function from constructor
printMessage();
}
virtual void printMessage() {
std::cout << "Message from Base class." << std::endl;
}
};
class Derived : public Base {
public:
Derived() {
std::cout << "Derived constructor called." << std::endl;
}
void printMessage() override {
std::cout << "Message from Derived class." << std::endl;
}
};
int main() {
Derived d;
return 0;
}
©Topperworld
Output:
For example-
#include <iostream>
int main() {
int x = 10;
float y = 3.14;
void* ptr;
// Assigning address of integer variable to void pointer
ptr = &x;
std::cout << "Value of x: " << *(static_cast<int*>(ptr)) <<
std::endl;
// Assigning address of float variable to void pointer
ptr = &y;
std::cout << "Value of y: " << *(static_cast<float*>(ptr)) <<
std::endl;
return 0;
}
©Topperworld
Output:
Value of x: 10
Value of y: 3.14
Ans : The member functions of every object have a pointer named this,
which points to the object itself.
The value of this is set to the address of the object for which it is called. It
can be used to access the data in the object it points to.
Example :
#include <iostream>
class MyClass {
private:
int data;
public:
// Constructor
MyClass(int value) : data(value) {}
©Topperworld
// Member function to compare two objects
bool isEqual(const MyClass& other) {
return this == &other;
}
};
int main() {
MyClass obj1(10);
MyClass obj2(20);
if (obj1.isEqual(obj2)) {
std::cout << "obj1 and obj2 are the same object." << std::endl;
} else {
std::cout << "obj1 and obj2 are different objects." <<
std::endl;
}
return 0;
}
©Topperworld
Output:
Data: 10
Data: 20
obj1 and obj2 are different objects.
Ans : A scope resolution operator is denoted by a ‘::‘ symbol. Just like its
name this operator resolves the barrier of scope in a program. A scope
resolution operator is used to reference a member function or a global
variable out of their scope furthermore to which it can also access the
concealed variable or function in a program.
Scope Resolution is used for numerous amounts of tasks:
1) To access a global variable when there is a local variable with the same
name
2) To define the function outside the class
3) In case of multiple inheritances
4) For namespace
©Topperworld
would have no implementation for any members whatsoever. In simple
words, Abstract Classes are a good fit if you want to provide implementation
details to your children but don’t want to allow an instance of your class to
be directly instantiated.
Ans : Just like its name, things can change suddenly and unexpectantly; So it
is used to inform the compiler that the value may change anytime. Also, the
volatile keyword prevents the compiler from performing optimization on the
code. It was intended to be used when interfacing with memory-mapped
hardware, signal handlers, and machine code instruction.
ABOUT US
At TopperWorld, we are on a mission to empower college students with the
knowledge, tools, and resources they need to succeed in their academic
journey and beyond.
➢ Our Vision
❖ Our vision is to create a world where every college student can easily
access high-quality educational content, connect with peers, and achieve
their academic goals.
❖ We believe that education should be accessible, affordable, and engaging,
and that's exactly what we strive to offer through our platform.
©Topperworld
➢ Unleash Your Potential
❖ Education is not just about textbooks and lectures; it's also about forming
connections and growing together.
❖ TopperWorld encourages you to engage with your fellow students, ask
questions, and share your knowledge.
❖ We believe that collaborative learning is the key to academic success.
©Topperworld
“Unlock Your
Potential”
With- Topper World
Explore More
topperworld.in
Follow Us On
E-mail
[email protected]