All Exams  >   Class 10  >   C++ Programming for Beginners  >   All Questions

All questions of C++ Classes for Class 10 Exam

How access specifiers in Class helps in Abstraction?
  • a)
    They does not helps in any way
  • b)
    They allows us to show only required things to outer world
  • c)
    They help in keeping things together
  • d)
    Abstraction concept is not used in classes
Correct answer is option 'B'. Can you explain this answer?

Access specifiers in classes play a crucial role in achieving abstraction. Abstraction is one of the fundamental concepts in object-oriented programming that allows us to focus on the essential features of an object while hiding the unnecessary details. Access specifiers help in achieving this abstraction by controlling the visibility of class members (variables and methods) to the outer world.

Here's how access specifiers in classes help in abstraction:

1. Encapsulation and Information Hiding:
- Access specifiers like private and protected allow us to encapsulate the implementation details of a class.
- By making certain members private, we hide them from the outer world, preventing direct access and modification.
- This ensures that the internal implementation of the class remains hidden, promoting information hiding and abstraction.

2. Controlling Visibility:
- Access specifiers like public and protected allow us to control the visibility of class members.
- Only the public members are visible to the outer world, while private and protected members are hidden.
- By selectively exposing only the required members, we can present a simplified and abstracted view of the class to the users.

3. Interface Design:
- Access specifiers help in designing clean and intuitive interfaces for classes.
- By making certain members public, we define the contract or interface of the class, which represents the essential features available to the users.
- The interface acts as a high-level abstraction, hiding the internal implementation details and providing a simplified way to interact with the class.

4. Encouraging Modularity and Reusability:
- Access specifiers promote modularity by allowing us to define separate modules within a class.
- By grouping related variables and methods together and using different access specifiers, we can create modular and reusable code.
- This modularity helps in achieving abstraction as it allows us to focus on specific functionalities without being overwhelmed by the entire class implementation.

In summary, access specifiers in classes help in achieving abstraction by encapsulating implementation details, controlling visibility, designing intuitive interfaces, and promoting modularity. They allow us to present a simplified and abstracted view of the class to the outer world, hiding unnecessary details and focusing on the essential features.

Which of the following approach is used by C++?
  • a)
    Top-down
  • b)
    Bottom-up
  • c)
    Left-right
  • d)
    Right-left
Correct answer is option 'B'. Can you explain this answer?

Bhaskar Gupta answered
Bottom-up approach is used by C.

The bottom-up approach is a programming methodology where the program development starts with the implementation of lower-level modules and gradually builds up to higher-level modules. In this approach, the focus is on developing individual components or functions and then integrating them to create a complete program.

Advantages of Bottom-up approach:
1. Modularization: The bottom-up approach promotes modularization, as it allows developers to focus on building smaller, manageable components first. This makes the development process more organized and easier to maintain.

2. Easy Debugging: As the program is developed in small modules, debugging becomes easier. If any errors or issues arise, they can be easily identified and resolved within the specific module, without affecting the entire program.

3. Reusability: Bottom-up approach encourages the development of reusable modules. Once a module is created and tested, it can be reused in multiple programs, saving time and effort in future projects.

4. Incremental Development: This approach allows for incremental development, where each module can be tested and validated independently before integration. This reduces the risk of errors and ensures a more reliable and robust final product.

Example of Bottom-up approach in C:
Let's consider a simple example where we need to create a program to calculate the average of three numbers.

1. First, we would start by creating a function to input three numbers.
2. Then, we would create a function to calculate the sum of three numbers.
3. Next, we would create a function to calculate the average by dividing the sum by 3.
4. Finally, we would integrate these functions and create a main function to execute the program.

In this example, we start with developing the lower-level modules (input function, sum function) and gradually build up to the higher-level module (average calculation). This follows the bottom-up approach as we focus on developing individual components and then integrate them to create the complete program.

Overall, the bottom-up approach is beneficial in terms of modularization, easy debugging, reusability, and incremental development. It allows for better organization and efficiency in program development.

What does polymorphism in OOPs mean?
  • a)
    Concept of allowing overiding of functions
  • b)
    Concept of hiding data
  • c)
    Concept of keeping things in differnt modules/files
  • d)
    Concept of wrapping things into a single unit
Correct answer is option 'A'. Can you explain this answer?

Shamita Desai answered
Polymorphism in OOPs

Polymorphism is one of the fundamental concepts in Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It provides a way to perform a single action in different ways by changing the method implementation in the derived classes.

Concept of allowing overriding of functions

Polymorphism in OOPs primarily refers to the concept of allowing the overriding of functions. Overriding is the process of redefining a method in the derived class that already exists in its superclass. By doing so, the derived class can provide its own implementation of the method, which may differ from the implementation in the superclass.

Polymorphism allows objects of different classes that inherit from the same superclass to be treated as objects of that superclass. This means that even though the objects may have different data and behavior, they can still be accessed and used using a common interface provided by the superclass. This promotes code reusability and flexibility in programming.

Example:
Consider a simple OOP scenario where we have a superclass called "Shape" and two subclasses called "Circle" and "Rectangle" that inherit from the Shape class. The Shape class has a method called "calculateArea" that calculates the area of the shape.

In the Circle class, we can override the "calculateArea" method to calculate the area of a circle using the formula specific to circles. Similarly, in the Rectangle class, we can override the same method to calculate the area of a rectangle using the formula specific to rectangles.

Now, if we create objects of both the Circle and Rectangle classes and store them in a list of Shape objects, we can iterate over the list and call the "calculateArea" method on each object without knowing the specific type of the object. This is possible because of polymorphism - the objects are treated as Shape objects, even though they are instances of different classes.

Conclusion
Polymorphism in OOPs allows objects of different classes to be treated as objects of a common superclass, enabling the use of a common interface for accessing and using these objects. This concept is primarily achieved through the overriding of functions in derived classes, providing different implementations for the same method defined in the superclass. Polymorphism promotes code reusability, flexibility, and abstraction in object-oriented programming.

When struct is used instead of the keyword class means, what will happen in the program?
  • a)
    access is public by default
  • b)
    access is private by default
  • c)
    access is protected by default
  • d)
    access is denied
Correct answer is option 'A'. Can you explain this answer?

Anirudh Iyer answered
Explanation:
When the keyword "struct" is used instead of the keyword "class" in C++, it affects the default access level of its members. By default, the access level of members in a struct is public, whereas in a class, it is private.

Access Levels:
Access levels determine how the members (variables and functions) of a class or struct can be accessed by other parts of the program.

There are three access levels in C++:
1. Public: Members can be accessed from anywhere in the program.
2. Private: Members can only be accessed within the same class or struct.
3. Protected: Members can be accessed within the same class or struct, or by derived classes.

Default Access Level with "struct":
When the keyword "struct" is used instead of "class", the default access level of the members is set to public. This means that all the variables and functions declared within the struct are accessible from anywhere in the program.

Example:
```cpp
struct MyStruct {
int x; // Public member variable
void display() { // Public member function
cout < "hello,="" world!"="" />< />
}
};

int main() {
MyStruct obj;
obj.x = 5; // Accessing public member variable
obj.display(); // Accessing public member function
return 0;
}
```

In the above example, the struct "MyStruct" has a public member variable "x" and a public member function "display". These members can be accessed directly from the main function without any restrictions.

Conclusion:
When the keyword "struct" is used instead of "class" in C++, the default access level of the members is public. This means that all the variables and functions declared within the struct can be accessed from anywhere in the program.

Which of the following explains the overloading of functions?
  • a)
    Virtual polymorphism
  • b)
    Transient polymorphism
  • c)
    Ad-hoc polymorphism
  • d)
    Pseudo polymorphism
Correct answer is option 'C'. Can you explain this answer?

Explanation:

Overloading of functions is a feature in programming languages that allows multiple functions with the same name but different parameters to be defined. It is a form of ad-hoc polymorphism, where a function can have different implementations depending on the types or number of arguments passed to it.

Ad-hoc Polymorphism:

Ad-hoc polymorphism, also known as function overloading, is a type of polymorphism where a function can be defined with the same name but different parameters. This allows the same function name to be used for different purposes, depending on the arguments passed to it. Ad-hoc polymorphism is a key feature of many programming languages and is used to improve code readability and reusability.

Virtual Polymorphism:

Virtual polymorphism, also known as dynamic polymorphism, is a feature that allows a function to be overridden in a derived class. It is achieved through the use of virtual functions and allows the correct function to be called at runtime based on the actual type of the object. Virtual polymorphism is commonly used in object-oriented programming to achieve runtime polymorphism.

Transient Polymorphism:

Transient polymorphism is not a recognized term in the context of programming languages. It is possible that the term was used incorrectly or is not commonly used in the field.

Pseudo Polymorphism:

Pseudo polymorphism is also not a recognized term in the context of programming languages. It is possible that the term was used incorrectly or is not commonly used in the field.

Conclusion:

In summary, the correct answer to the question is option 'C', which is ad-hoc polymorphism. Ad-hoc polymorphism, or function overloading, allows multiple functions with the same name but different parameters to be defined, improving code readability and reusability. Virtual polymorphism and transient polymorphism are not related to the concept of function overloading, and pseudo polymorphism is not a recognized term in programming languages.

Why references are different from pointers?
  • a)
    A reference cannot be made null
  • b)
    A reference cannot be changed once initialized
  • c)
    No extra operator is needed for dereferencing of a reference
  • d)
    All of the mentioned
Correct answer is option 'D'. Can you explain this answer?

References and Pointers:
References and pointers are both used in C++ to indirectly access and manipulate variables. However, there are some key differences between them.

A) A reference cannot be made null:
- A reference in C++ must always be initialized with a valid object.
- It cannot be left uninitialized or set to null.
- This ensures that a reference always refers to a valid object, eliminating the possibility of a null reference error.

B) A reference cannot be changed once initialized:
- Once a reference is initialized with an object, it cannot be changed to refer to a different object.
- It remains bound to the same object throughout its lifetime.
- This provides a level of safety, as it prevents accidental reassignment and ensures that the reference consistently refers to the same object.

C) No extra operator is needed for dereferencing of a reference:
- In C++, accessing the value of a reference does not require an extra operator like the * operator used with pointers.
- The reference itself can be treated as if it were the object it refers to.
- This simplifies the syntax and makes code more readable.

Summary:
In summary, references and pointers serve similar purposes in C++, but there are some important distinctions between them. References cannot be null, they cannot be changed once initialized, and they do not require an extra operator for dereferencing. These characteristics make references safer and more convenient to use in certain situations. However, pointers still have their own advantages, such as the ability to be null and to be reassigned to different objects. Therefore, the choice between references and pointers depends on the specific requirements of the program and the desired behavior.

How compile-time polymorphisms are implemented in C++?
  • a)
    Using Inheritance
  • b)
    Using Virtual functions
  • c)
    Using Templates
  • d)
    Using Inheritance and Virtual functions
Correct answer is option 'C'. Can you explain this answer?

Kiran Reddy answered
Compile-time polymorphism is implemented using templates in which the types(which can be checked during compile-time) are used decides which function to be called.

What does a mutable member of a class mean?
  • a)
    A member that can never be changed
  • b)
    A member that can be updated only if it not a member of constant object
  • c)
    A member that can be updated even if it a member of constant object
  • d)
    A member that is global throughout the class
Correct answer is option 'C'. Can you explain this answer?

Samarth Roy answered
Explanation:

Mutable Member of a Class:
A mutable member of a class refers to a member variable that can be updated or modified even if it is a part of a constant object.

Rules for Mutable Members:
- Mutable members can be modified regardless of whether they are part of a constant object or not.
- Constant objects may restrict the modification of their members, but mutable members can still be updated.

Example:
cpp
class MyClass {
public:
mutable int mutableMember; // Mutable member variable
int regularMember; // Non-mutable member variable
};
int main() {
const MyClass obj1; // Constant object
obj1.mutableMember = 10; // Allowed since mutable
// obj1.regularMember = 20; // Not allowed as obj1 is constant
MyClass obj2; // Non-constant object
obj2.mutableMember = 30; // Allowed
obj2.regularMember = 40; // Allowed
}
In the above example, `mutableMember` is a mutable member of the class `MyClass`. It can be modified even in a constant object like `obj1`. On the other hand, `regularMember` cannot be modified in a constant object.
Therefore, a mutable member of a class allows for flexibility in updating member variables, even in situations where object constancy is enforced.

What is the other name used for functions inside a class?
  • a)
    Member variables
  • b)
    Member functions
  • c)
    Class functions
  • d)
    Class variables
Correct answer is option 'B'. Can you explain this answer?

Alpana reddy answered
Member Functions are the other name used for functions inside a class.

Explanation:
- In object-oriented programming, a class is a blueprint for creating objects (instances of the class).
- A class can consist of both data members and member functions.
- Member functions are the functions defined inside a class that operate on the data members of the class.
- These functions are also known as methods.
- Member functions have access to all the data members and other member functions of the class.
- They can be used to perform various operations on the data members, manipulate the data, and provide the desired functionality.
- Member functions can be defined within the class definition or outside the class definition.
- When a member function is defined inside the class definition, it is implicitly inline.
- Member functions can be declared in the public, private, or protected section of the class.
- The public member functions can be accessed by objects of the class and external functions, whereas private member functions can only be accessed by other member functions of the class.
- Member functions are called using the object of the class.
- They can be invoked by using the dot operator (.) followed by the function name and parentheses.

Example:
```cpp
class Rectangle {
private:
int length;
int width;

public:
// Member function to calculate area
int calculateArea() {
return length * width;
}

// Member function to set length and width
void setDimensions(int l, int w) {
length = l;
width = w;
}
};

int main() {
// Create an object of the class
Rectangle rect;

// Set the dimensions
rect.setDimensions(5, 3);

// Calculate and print the area
int area = rect.calculateArea();
cout < "area:="" "="" />< area="" />< />

return 0;
}
```

In the above example, we have a class named Rectangle with two member variables (length and width) and two member functions (calculateArea and setDimensions). The member function calculateArea calculates the area of the rectangle based on the length and width, and the member function setDimensions sets the values of length and width. These member functions can be accessed using the object of the class (rect).

Which of the following is a valid class declaration?
  • a)
    class A { int x; };
  • b)
    class B { }
  • c)
    public class A { }
  • d)
    object A { int x; };
Correct answer is option 'A'. Can you explain this answer?

Mohit Yadav answered
Valid class declaration in Java:
A valid class declaration in Java consists of the keyword "class" followed by the class name and a pair of curly braces. Inside the curly braces, we can define variables, methods, and other members of the class.

Explanation of options:
a) class A { int x; };
This is a valid class declaration. It declares a class named A with a single variable x of type int.

b) class B { }
This is also a valid class declaration. It declares a class named B with no members inside the class.

c) public class A { }
This is a valid class declaration. It declares a public class named A with no members inside the class.

d) object A { int x; };
This is not a valid class declaration. The keyword "object" is not used to declare a class in Java. Instead, the keyword "class" is used.

Explanation of the correct answer:
The correct answer is option 'A' because it follows the syntax of a valid class declaration in Java. It declares a class named A with a single variable x of type int. The class declaration ends with a semicolon after the closing curly brace.

The other options (B, C, and D) are not valid class declarations for the following reasons:
- Option B is a valid class declaration but does not have any members inside the class.
- Option C is a valid class declaration but does not have any members inside the class and is declared as public.
- Option D is not a valid class declaration because it uses the keyword "object" instead of "class" to declare the class.

In summary, the valid class declaration is option 'A' because it follows the correct syntax and declares a class with a variable inside it.

What does modularity mean?
  • a)
    Hiding part of program
  • b)
    Subdividing program into small independent parts
  • c)
    Overriding parts of program
  • d)
    Wrapping things into single unit
Correct answer is option 'B'. Can you explain this answer?

Vidhi bajaj answered
Modularity refers to the practice of subdividing a program into small, independent parts. Each of these parts, also known as modules, is designed to perform a specific function or task within the program. These modules can be developed and tested independently, making it easier to understand and maintain the overall program.

Modularity is an essential concept in programming and software development as it brings several benefits to the development process. Let's explore some key points to understand the significance of modularity:

Benefits of Modularity:

1. Simplicity: Breaking down a program into smaller modules makes it easier to understand and manage. Each module focuses on a specific functionality, which enhances code readability and simplifies troubleshooting.

2. Reusability: Modular programming allows modules to be reused in different programs. Once a module is developed, it can be easily imported into other projects, saving time and effort in writing the same code repeatedly.

3. Maintainability: When a program is divided into modules, it becomes easier to maintain and update. If a bug is identified, it can be isolated and fixed within the respective module, without affecting other parts of the program.

4. Collaboration: Modularity promotes collaboration among developers working on the same project. Different team members can work on separate modules simultaneously, leading to more efficient development and faster project completion.

5. Testing: With modular programming, each module can be tested individually, ensuring that it functions correctly before integrating it into the larger program. This reduces the complexity of testing the entire program at once.

Implementation of Modularity:

To implement modularity in programming, the following techniques can be used:

- Functions: Functions are self-contained blocks of code that perform a specific task. They can be called multiple times from different parts of the program, promoting code reuse and modularity.

- Classes and Objects: Object-oriented programming languages provide classes and objects as a way to achieve modularity. Classes encapsulate data and functions into a single unit, allowing for better organization and reusability.

- Libraries and Modules: Libraries and modules provide pre-written code that can be imported into a program, eliminating the need to reinvent the wheel. They offer a collection of functions and classes that can be used to enhance modularity.

Conclusion:

Modularity plays a crucial role in software development by breaking down complex programs into smaller, manageable parts. By subdividing a program into independent modules, it becomes easier to understand, maintain, and reuse code. Modularity promotes collaboration, simplifies testing, and enhances the overall efficiency of the development process.

Out of the following, which is not a member of the class?
  • a)
    Static function
  • b)
    Friend function
  • c)
    Constant function
  • d)
    Virtual function
Correct answer is option 'B'. Can you explain this answer?

Kiran Reddy answered
Friend function is not a member of the class. They are given the same access rights as the class member function have but they are not actual members of the class.

How many types of polymorphism are there in C++?
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'B'. Can you explain this answer?

Kiran Reddy answered
There are two types of polymorphism in C++ namely run-time and compile-time polymorphisms.

Which of the following class allows to declare only one object of it?
  • a)
    Abstract class
  • b)
    Virtual class
  • c)
    Singleton class
  • d)
    Friend class
Correct answer is option 'C'. Can you explain this answer?

Kiran Reddy answered
Singleton class allows the programmer to declare only one object of it, If one tries to declare more than one object the program results into error.

Which category of data type a class belongs to?
  • a)
    Fundamental data type
  • b)
    Derived data type
  • c)
    User defined derived data type
  • d)
    Atomic data type
Correct answer is option 'C'. Can you explain this answer?

Kiran Reddy answered
Fundamental/Atomic data type includes int, char, float, double and void. Derived data type includes arrays, pointers, references, function and constants. User defined derived data type includes class, structure, union and enumeration.

Chapter doubts & questions for C++ Classes - C++ Programming for Beginners 2025 is part of Class 10 exam preparation. The chapters have been prepared according to the Class 10 exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Class 10 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of C++ Classes - C++ Programming for Beginners in English & Hindi are available as part of Class 10 exam. Download more important topics, notes, lectures and mock test series for Class 10 Exam by signing up for free.

Signup to see your scores go up within 7 days!

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