All Exams  >   Software Development  >   DSA in C++  >   All Questions

All questions of Oops for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

Which keyword is used to create an object in C++?
  • a)
    create
  • b)
    new
  • c)
    object
  • d)
    instantiate
Correct answer is option 'B'. Can you explain this answer?

Ananya Rane answered
Understanding Object Creation in C++
In C++, the process of creating an object is facilitated by the `new` keyword. This keyword plays a crucial role in dynamic memory allocation.
What is the `new` keyword?
- The `new` keyword allocates memory for an object on the heap.
- It returns a pointer to the newly created object, allowing access to the instance.
- This is different from stack allocation, where memory is allocated automatically and managed by the scope.
Usage of `new` in C++
- When you create an object using `new`, you can specify the class type, which is essential for defining its properties and methods.
- Example:
cpp
MyClass* obj = new MyClass();
Here, `obj` is a pointer to a `MyClass` object created on the heap.
Benefits of Using `new`
- Dynamic Allocation: Objects can be created at runtime based on conditions, allowing for more flexible code.
- Lifetime Management: Objects remain in memory until explicitly deleted using the `delete` keyword, providing control over their lifespan.
Comparison with Other Options
- Option A (create): Not a valid keyword in C++ for object creation.
- Option C (object): This is not a keyword; it is a term used to describe an instance of a class.
- Option D (instantiate): While the term "instantiate" describes the process of creating an object, it is not a keyword in C++.
Conclusion
In summary, the `new` keyword is essential for creating objects dynamically in C++, providing flexibility and control over memory management. Understanding its usage is fundamental for effective programming in C++.

Which of the following concepts in OOP allows a class to inherit properties and behaviors from another class?
  • a)
    Polymorphism
  • b)
    Encapsulation
  • c)
    Abstraction
  • d)
    Inheritance
Correct answer is option 'D'. Can you explain this answer?

Inheritance is the concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors from another class. It is one of the fundamental principles of OOP and plays a crucial role in code reuse and creating relationships between classes.

Inheritance is represented by an "is-a" relationship, where one class is considered to be a specialized version of another class. The class that inherits properties and behaviors is called the subclass or derived class, and the class from which it inherits is called the superclass or base class.

Key Points:
- Inheritance allows the subclass to inherit all the non-private members (properties and methods) of the superclass.
- The subclass can then extend or override these inherited members, as well as add new members specific to itself.
- Inheritance promotes code reuse and modularity by allowing common functionality to be defined in a base class and shared among multiple subclasses.
- It also helps in creating a hierarchy of classes, where subclasses can inherit from other subclasses, forming a tree-like structure.

Benefits of Inheritance:
1. Code Reusability: Inheritance allows subclasses to reuse the code defined in the superclass, reducing code duplication and promoting modular design.
2. Modularity and Maintainability: Inheritance helps in creating a hierarchical structure of classes, making the code more organized and easier to maintain.
3. Polymorphism: Inheritance is closely related to polymorphism, another important concept in OOP. Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling more flexible and extensible code.

Example:
Let's consider an example where we have a superclass called "Animal" and two subclasses called "Cat" and "Dog". The superclass "Animal" may have properties such as "name" and "age", as well as methods like "eat()" and "sleep()". The subclasses "Cat" and "Dog" can inherit these properties and methods from the superclass, along with any additional properties or methods specific to each subclass.

In this example, the inheritance relationship can be represented as follows:

Animal
- Properties: name, age
- Methods: eat(), sleep()

Cat (subclass of Animal)
- Inherits: name, age, eat(), sleep()
- Additional properties: breed
- Additional methods: meow()

Dog (subclass of Animal)
- Inherits: name, age, eat(), sleep()
- Additional properties: breed
- Additional methods: bark()

By using inheritance, we can create specialized classes like "Cat" and "Dog" that inherit common properties and behaviors from the superclass "Animal", while also adding their own unique characteristics. This promotes code reuse, modularity, and flexibility in our codebase.

Which of the following is not a principle of Object-Oriented Programming (OOP)?
  • a)
    Inheritance
  • b)
    Encapsulation
  • c)
    Abstraction
  • d)
    Normalization
Correct answer is option 'D'. Can you explain this answer?

Normalization is not a principle of Object-Oriented Programming (OOP). This principle is actually a concept in database design and is not directly related to OOP.

To understand why normalization is not a principle of OOP, let's take a closer look at the other principles of OOP:

1. Inheritance: Inheritance is a fundamental principle in OOP that allows classes to inherit properties and behaviors from other classes. It promotes code reuse and helps to create a hierarchical relationship between classes. Through inheritance, classes can be organized into a hierarchy, with more general classes at the top and more specific classes at the bottom.

2. Encapsulation: Encapsulation refers to the bundling of data and methods within a class. It hides the internal details of an object and provides a public interface for interacting with the object. Encapsulation helps to achieve data abstraction and protects the integrity of an object's data by preventing direct access to it.

3. Abstraction: Abstraction is the process of simplifying complex systems by breaking them down into smaller, more manageable parts. In OOP, abstraction is achieved through the use of classes, which encapsulate data and behavior into objects. It allows developers to focus on the essential features of an object while hiding unnecessary implementation details.

Normalization, on the other hand, is a process used in database design to eliminate redundancy and improve data integrity. It involves organizing data into multiple tables and applying specific rules to ensure data consistency. Normalization is not specific to OOP and can be applied in any database system.

In summary, normalization is not a principle of OOP because it is a concept related to database design, not programming paradigms. OOP principles, such as inheritance, encapsulation, and abstraction, focus on code organization, reusability, and abstraction of complex systems.

Which of the following statements about function overloading in C++ is true?
  • a)
    Function overloading allows the creation of multiple functions with different return types.
  • b)
    Function overloading can only be achieved by changing the function name.
  • c)
    Function overloading is determined by the number and types of function arguments.
  • d)
    Function overloading is not supported in C++.
Correct answer is option 'C'. Can you explain this answer?

Gopal Shah answered
Function Overloading in C

Function overloading is a feature in C that allows multiple functions with the same name but different parameter lists to be defined. It provides a way to create functions that perform similar operations but with different inputs.

Statement Analysis

Let's analyze each statement and determine if it is true or false:

a) Function overloading allows the creation of multiple functions with different return types.

This statement is false. Function overloading does not consider the return type when determining which function to call. The return type alone cannot differentiate between overloaded functions. The compiler uses the number and types of function arguments to resolve the function call.

b) Function overloading can only be achieved by changing the function name.

This statement is false. Function overloading is achieved by using the same function name but with different parameter lists. The function name remains the same, but the number and types of parameters are different.

c) Function overloading is determined by the number and types of function arguments.

This statement is true. Function overloading is determined solely by the number and types of function arguments. When a function call is made, the compiler matches the function call with the correct overloaded function based on the number and types of arguments. If an exact match is not found, the compiler looks for the best match by considering implicit conversions.

d) Function overloading is not supported in C.

This statement is false. Function overloading is supported in C++. However, in the C programming language, function overloading is not directly supported. C requires each function to have a unique name, and the programmer must define different functions with different names to achieve similar behavior as function overloading in C++.

Conclusion

The correct statement about function overloading in C is option 'c'. Function overloading in C is determined by the number and types of function arguments. The return type alone does not differentiate between overloaded functions, and function overloading is achieved by using the same function name but different parameter lists.

Which statement is true about pure virtual functions in C++?
  • a)
    A pure virtual function has a definition in the base class
  • b)
    A class containing a pure virtual function cannot be instantiated
  • c)
    A pure virtual function cannot have any parameters
  • d)
    A pure virtual function can be overridden with a non-pure virtual function in a derived class
Correct answer is option 'B'. Can you explain this answer?

Hackers World answered
A class containing a pure virtual function is called an abstract class. An abstract class cannot be instantiated, meaning objects of the abstract class cannot be created. However, a derived class can inherit from the abstract class and provide implementations for the pure virtual functions.

Which statement is true about function overriding in C++?
  • a)
    Overriding can only be done in derived classes
  • b)
    Overriding can only be done for private member functions
  • c)
    Overriding changes the return type of the base class function
  • d)
    Overriding allows a derived class to provide a different implementation of a base class function
Correct answer is option 'D'. Can you explain this answer?

Naina Chauhan answered
Function overriding in C

Function overriding is a feature in object-oriented programming that allows a derived class to provide a different implementation of a method that is already defined in its base class. When a derived class overrides a function, it provides its own implementation of the function, which is used instead of the base class implementation when the function is called on an object of the derived class.

Understanding the given options:

a) Overriding can only be done in derived classes: This statement is incorrect. Overriding can be done in both derived classes and the base class.

b) Overriding can only be done for private member functions: This statement is incorrect. Overriding can be done for both private and public member functions.

c) Overriding changes the return type of the base class function: This statement is incorrect. Overriding does not allow changing the return type of the base class function. The return type of the overridden function in the derived class must be the same as or covariant with the return type of the base class function.

d) Overriding allows a derived class to provide a different implementation of a base class function: This statement is correct. Overriding allows a derived class to provide its own implementation of a method that is already defined in the base class. When the function is called on an object of the derived class, the overridden implementation is executed instead of the base class implementation.

Conclusion:

The correct statement about function overriding in C is that overriding allows a derived class to provide a different implementation of a base class function. This feature enables polymorphism and allows objects of different classes to be treated interchangeably based on their common base class type. Overriding is a powerful mechanism in object-oriented programming that promotes code reuse and flexibility.

Which concept is illustrated in the following code?
class Base {
public:
    virtual void foo() = 0;
};
class Derived : public Base {
public:
    void foo() {
        std::cout << "Derived" << std::endl;
    }
};
int main() {
    Base* ptr = new Derived();
    ptr->foo();
    return 0;
}
  • a)
    Inheritance
  • b)
    Encapsulation
  • c)
    Polymorphism
  • d)
    Abstraction
Correct answer is option 'C'. Can you explain this answer?

Hackers World answered
The code demonstrates polymorphism by using a base class pointer to invoke a function that is overridden in the derived class. This allows the program to exhibit different behaviors based on the actual object type at runtime. In this case, the "foo" function is invoked through the base class pointer, and it prints "Derived" because the object is of the derived class type.

In C++, which access specifier provides the highest level of encapsulation?
  • a)
    public
  • b)
    protected
  • c)
    private
  • d)
    friend
Correct answer is option 'C'. Can you explain this answer?

In C++, the private access specifier provides the highest level of encapsulation. Private members are accessible only within the class and are not accessible from outside the class or its derived classes.

What is the output of the following code?
#include <iostream>
class A {
public:
    virtual void foo() {
        std::cout << "A" << std::endl;
    }
};
class B : public A {
public:
    void foo() {
        std::cout << "B" << std::endl;
    }
};
int main() {
    A* ptr = new B();
    B* bptr = dynamic_cast<B*>(ptr);
    bptr->foo();
    return 0;
}
  • a)
    A
  • b)
    B
  • c)
    Compile-time error
  • d)
    Runtime error
Correct answer is option 'B'. Can you explain this answer?

Hackers World answered
The code creates a pointer of type "A*" and assigns the address of a dynamically allocated object of the derived class "B" to it. The pointer is then cast to a "B*" using dynamic_cast. When the "foo" function is called using the derived class pointer, it invokes the function from the derived class, printing "B".

In C++, a class is a:
  • a)
    blueprint for an object
  • b)
    type of data structure
  • c)
    function to define member variables
  • d)
    library of built-in functions
Correct answer is option 'A'. Can you explain this answer?

CodeNation answered
A class in C++ serves as a blueprint for creating objects. It defines the properties (member variables) and behaviors (member functions) that an object of that class can have.

Chapter doubts & questions for Oops - DSA in C++ 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Oops - DSA in C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

DSA in C++

153 videos|115 docs|24 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev