Object Oriented Programming Flashcards

1
Q

What is an object?

A

An object is an instance of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain abstraction

A

Abstraction is a good feature of OOPS, and it shows only the necessary details to the client of an object. Means, it shows only required details for an object, not the inner constructors, of an object. Example – When you want to switch On television, it not necessary to show all the functions of TV. Whatever is required to switch on TV will be showed by using abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the use of default constructor?

A
  • It is a constructor that does not accept any parameters.
  • If there is no user-defined constructor for a class, the compiler declares a default parameterless constructor called default constructor.
  • It is an inline public member of its class.
  • When the compiler uses this constructor to create an object – the constructor will have no constructor initializer and a null body.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Whether static method can use nonstatic members?

A

False. Static methods can only access the static part of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the various types of constructors?

A

There are three various types of constructors, and they are as follows:

– Default Constructor – With no parameters.

– Parametric Constructor – With Parameters. Create a new instance of a class and also passing arguments simultaneously.

– Copy Constructor – Which creates a new object as a copy of an existing object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain deep copy and a shallow copy.

A

a. Deep copy :
It involves using the contents of one object to create another instance of the same class. Here, the two objects may contain the same information but the target object will have its own buffers and resources. The destruction of either object will not affect the remaining objects.

b. Shallow copy :
It involves copying the contents of one object into another instance of the same class. This creates a mirror image. The two objects share the same externally contained contents of the other object to be unpredictable.This happens because of the straight copying of references and pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an abstract class?

A

An abstract class is a class which cannot be instantiated. Creation of an object is not possible with an abstract class, but it can be inherited. An abstract class can contain only Abstract method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are all the operators that cannot be overloaded?

A

Following are the operators that cannot be overloaded -.

Scope Resolution (:: )
Member Selection (.)
Member selection through a pointer to function (.*)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When do I need necessarily to use the initialization list to initialize some parameters?

A

When the class has static members

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Inheritance? What is the purpose?

A
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class.
The purpose of inheritance is Code Reuse.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Inheritance: definition, use, advantages

A
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class.
The purpose of inheritance is Code Reuse.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is encapsulation?

A

Encapsulation is referred to one of the following two notions.

1) Data hiding: A language feature to restrict access to members of an object. For example, private and protected members in C++.
2) Bundling of data and methods together: Data and methods that operate on that data are bundled together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the type of “this” pointer? When does it get created?

A

It is a constant pointer type. It gets created when a non-static member function of a class is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Difference between overloading and overriding?

A

1) Overloading is static binding whereas Overriding is dynamic binding.
2) Overloading is nothing but the same method with different arguments, and it may or may not return the same value in the same class itself.

Overriding is the same method names with same arguments and return types associated with the class and its child class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is function overloading?

A

Function overloading an as a normal function, but it can perform different tasks. It allows the creation of several methods with the same name which differ from each other by the type of input and output of the function.

Example
void add(int& a, int& b);
void add(double& a, double& b);
void add(struct bob& a, struct bob& b);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Difference between class and struct

A
  • Structure default access type is public, but class access type is private.
  • A structure is used for grouping data whereas class can be used for grouping data and methods. Structures are exclusively used for data, and it doesn’t require strict validation, but classes are used to encapsulates and inherit data which requires strict validation.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Define Destructor?

A

A destructor is a method which is automatically called when the object is made of scope or destroyed. Destructor name is also same as class name but with the tilde symbol before the name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Explain the virtual inheritance in C++.

A

Virtual inheritance is used when a single base class is inherited with virtual methods. It can be achieved by the virtual keyword in the program. In this, the object that belongs to virtual class becomes common to the base class. It is used for multiple inheritance, as it creates multiple sub objects and gives the feature where a class can inherit from more than one classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is ‘this’ pointer?

A

THIS pointer refers to the current object of a class. THIS keyword is used as a pointer which differentiates between the current object with the global object. Basically, it refers to the current object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Which keyword can be used for overloading?

A

Operator keyword is used for overloading.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Explain virtual class and friend class.

A

Virtual Base Class :

  • It is used in context of multiple inheritance in C++.
  • If you want to derive two classes from a class, and further derive one class from the two classes in the second level, you need to declare the uppermost base class as ‘virtual’ in the inherited classes.
  • This prevents multiple copies of the uppermost base class data members when an object of the class at the third level of hierarchy is created.

b. ) Friend class:
- When a class declares another class as its friend, it is giving complete access to all its data and methods including private and protected data and methods to the friend class member methods.
- Friendship is not necessarily bi-directional. If A declares B as its friend it does not imply that A can access private data of B. It only means that B can access all data of A.

22
Q

What are main features of OOP?

A

Encapsulation
Polymorphism
Inheritance

23
Q

What are the advantages of using friend classes?

A
  • Friend classes are useful when a class wants to hide features from users which are needed only by another, tightly coupled class.
  • Implementation details can be kept safe by providing friend status to a tightly cohesive class.
24
Q

Polymorphism: definition, use, advantages

A

In object-oriented programming, polymorphism is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

25
Q

What happens when you make call “delete this;”?

A

The two pit-falls exist here,

  1. If it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement gets executed.
  2. When an object finishes like this, the using program might not know about this end. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object is finished. Subsequent dereferencing of the pointer can lead to disaster.
26
Q

What is a pure virtual function?

A

A pure virtual function is a function which can be overridden in the derived class but cannot be defined. A virtual function can be declared as Pure by using the operator =0.

Example -.

Virtual void function1() // Virtual, Not pure

Virtual void function2() = 0 //Pure virtual

27
Q

Differentiate between a copy constructor and an overloaded assignment operator.

A
  • A copy constructor constructs a new object by using the content of the argument object while an overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
  • Copy Constructor invoke in cases, such as:
    i. Creation and initialization of an object simultaneously.
    ii. When an object is passed to a function by value.
    iii. When an object is returned from a function by value.
28
Q

Define a constructor?

A

A constructor is a method used to initialize the state of an object, and it gets invoked at the time of object creation. Rules forconstructor are:

Constructor Name should be same as class name.
A constructor must have no return type.
29
Q

What is the default access modifier in a class?

A

The default access modifier of a class is Private by default.

30
Q

Explain public, protected, private in C++

A

These are three access specifiers in C++.

  1. Public : Here the data members and functions are accessible outside the class.
  2. Protected : Data members and functions are available to derived classes only.
  3. Private : Data members and functions are not accessible outside the class.
31
Q

What are virtual functions?

A

A virtual function is a member function of a class, and its functionality can be overridden in its derived class. This function can be implemented by using a keyword called virtual, and it can be given during function declaration.

32
Q

What is a class?

A

A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describes the details of an object.

33
Q

What is encapsulation?

A

Encapsulation is basically information hiding. It describes the idea of bundling data and methods that work on that data within one unit. In here access to data need to be controlled using access modifiers (public, private, protected etc.) and expose them to the outside world using getters and setters.

34
Q

Which OOPS concept exposes only necessary information to the calling functions?

A

Encapsulation

35
Q

Class: definition, syntax

A
class className: $INHERITANCE_TYPE $MOTHER_CLASS {
private:
....
public:
default_constructor;
destructor;
...
protected:
...
}
36
Q

What is dynamic or run time polymorphism?

A

Dynamic or Run time polymorphism is also known as method overriding in which call to an overridden function is resolved during run time, not at the compile time. It means having two or more methods with the same name, same signature but with different implementation.

37
Q

What is a friend function?

A

A friend function is a friend of a class that is allowed to access to Public, private or protected data in that same class. If the function is defined outside the class cannot access such information.

Friend can be declared anywhere in the class declaration, and it cannot be affected by access control keywords like private, public or protected.

38
Q

What is Polymorphism? How is it supported by C++?

A

Polymorphism means that some code or operations or objects behave differently in different contexts. In C++, following features support polymorphism.

Compile Time Polymorphism: Compile time polymorphism means compiler knows which function should be called when a polymorphic call is made. C++ supports compiler time polymorphism by supporting features like templates, function overloading and default arguments.

Run Time Polymorphism: Run time polymorphism is supported by virtual functions. The idea is, virtual functions are called according to the type of object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime.

39
Q

When does a class need a virtual destructor?

A

If your class has at least one virtual function, you should have a virtual destructor.

40
Q

Explain Copy Constructor

A

This is a special constructor for creating a new object as a copy of an existing object. There will always be only one copy constructor that can be either defined by the user or the system.

41
Q

What is an Inline function?

A

An inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code

42
Q

List the advantages of inheritance

A
  • Inheritence permits code reusability.
  • Reusability saves time in program development.
  • It encourages the reuse of proven and debugged high-quality software which reduces the problems after a system becomes functional.
43
Q

Explain Overriding

A

Method overriding is a feature that allows a subclass to provide the implementation of a method that overrides in the main class. This will overrides the implementation in the superclass by providing the same method name, same parameter and same return type.

44
Q

How can we call the base method without creating an instance?

A

Yes, it is possible to call the base method without creating an instance. And that method should be “Static method”.

Doing inheritance from that class.-Use Base Keyword from a derived class.

void Update()
{
    BaseClassName::Update();
    std::cout << "called from derived class" << std::endl;
}
45
Q

How many instances can be created for an abstract class?

A

Zero instances will be created for an abstract class.

46
Q

What does the keyword virtual represented in the method definition?

A

It means, we can override the method.

47
Q

What is the need of a destructor?

A
  • During construction of an object, resources may be allocated for use.
  • For example, a constructor may have opened a file and memory area may be allocated to it. These allocated resources must be deallocated before the object is destroyed.
  • A destructor performs all clean-up tasks like closing a file, deallocating and releasing memory area etc. automatically.
48
Q

Name the implicit member functions of a class

A
  1. default constructor
  2. copy constructor
  3. assignment operator
  4. default destructor
  5. address operator
49
Q

List the special characteristics of constructor.

A
  • A constructor has the same name as that of class.
  • It is automatically invoked when an object of the class is declared.
  • Constructor obeys the usual access rule. Private & protected constructor can only be accessed by the member function and friend function of the class, Public constructor is available for all the function. Only that function can create the object that has access to the constructor.
  • No return type is specified for the constructor.
  • These cannot be inherited but a derived class can invoke base class constructor.
  • A constructor can also have default arguments.
  • A constructor can invoke the member functions.
  • The default constructor and copy constructor are provided by the compiler only if these are not defined by the programmer.
50
Q

When do you call copy constructors?

A
When do you call copy constructors?
Copy constructors are called in these situations : 
1. When compiler generates a temporary object. 
2. When a function returns an object of that class by value .
3. When the object of that class is passed by value as an argument to a function .
4. When you construct an object based on another object of the same class.