Section A — 1 Mark Questions
1. Define a class in C++. [1 Mark]
Answer: A class is a user-defined data type that serves as a blueprint for creating objects, combining data and functions into a single unit.
2. What is an object? [1 Mark]
Answer: An object is an instance of a class that occupies memory and has a set of data values.
3. Name the three access specifiers in C++. [1 Mark]
Answer: The access specifiers are public, private, and protected.
4. What is the default access specifier in a C++ class? [1 Mark]
Answer: The default access specifier is private.
5. What keyword is used to declare a class in C++? [1 Mark]
Answer: The keyword used is 'class'.
6. How do you terminate a class definition? [1 Mark]
Answer: A class definition is terminated by a semicolon (;) after the closing brace.
7. Can a class contain both data and functions? [1 Mark]
Answer: Yes, a class contains data members and member functions.
8. What is the purpose of the scope resolution operator (::)? [1 Mark]
Answer: It is used to define a member function outside of the class definition.
9. Define member functions. [1 Mark]
Answer: Member functions are functions declared inside a class that operate on the class's data members.
10. What is 'nesting of member functions'? [1 Mark]
Answer: It is the process of calling one member function from another member function of the same class.
11. Can objects be created in the main() function? [1 Mark]
Answer: Yes, objects are typically instantiated within the main() function or other functions.
12. What does a public access specifier allow? [1 Mark]
Answer: It allows members to be accessed from outside the class.
13. Which operator is used to access class members using an object? [1 Mark]
Answer: The dot operator (.) is used.
14. Where are member functions typically defined? [1 Mark]
Answer: They can be defined inside the class body or outside using the scope resolution operator.
15. What is encapsulation in the context of classes? [1 Mark]
Answer: It is the wrapping of data and functions into a single unit called a class.
16. Are class data members automatically initialized? [1 Mark]
Answer: No, they are not automatically initialized to zero; they contain garbage values unless specified.
17. How is an object of a class declared? [1 Mark]
Answer: It is declared using the syntax: ClassName objectName;
18. What is the significance of the private access specifier? [1 Mark]
Answer: It restricts access to members, making them accessible only within the class itself.
19. Can a class exist without any member functions? [1 Mark]
Answer: Yes, a class can contain only data members, though it is less common.
20. What does 'instantiation' mean? [1 Mark]
Answer: It is the process of creating an object from a class definition.
Section B — 2/3 Mark Questions
1. Differentiate between public and private access specifiers. [2 Marks]
Answer: Public members are accessible from anywhere in the program outside the class. Private members are only accessible to functions within the class, ensuring data hiding.
2. Write the basic structure of a C++ class. [2 Marks]
Answer: class ClassName { access_specifier: data_members; member_functions; };
3. Explain the concept of nesting of member functions with an example. [3 Marks]
Answer: A member function can call another member function of the same class. For example, a function display() can internally call a private function calculate() to perform an operation before printing the result.
4. Why is a semicolon required after the closing brace of a class? [2 Marks]
Answer: The semicolon is a syntactic requirement in C++ that indicates the end of the class declaration, similar to how statements end with a semicolon.
5. How do you define a member function outside a class? [3 Marks]
Answer: To define it outside, use the scope resolution operator (::) in the format: ReturnType ClassName::FunctionName() { // code }.
6. Describe the role of data members in a class. [2 Marks]
Answer: Data members are variables declared within the class that represent the state or properties of the objects created from that class.
7. Can multiple objects of the same class share the same member functions? [2 Marks]
Answer: Yes, member functions are defined once in the class, and all objects of that class share the same code logic while operating on their individual data.
8. What is the purpose of access specifiers? [3 Marks]
Answer: Access specifiers control the visibility of class members, enabling data abstraction, protecting sensitive data (via private), and defining the interface of the class (via public).
9. How is an object accessed after creation? [2 Marks]
Answer: An object is accessed by using the dot operator (.) followed by the name of the public member, e.g., objectName.memberFunction().
10. Why is it recommended to make data members private? [3 Marks]
Answer: Making data members private prevents unauthorized direct modification, promotes encapsulation, and allows the class designer to control how data is accessed or changed through public functions.
Section C — 5 Mark Questions
1. Explain the process of specifying a class in C++ with a detailed example. [5 Marks]
Introduction
A class acts as a blueprint for objects. It groups data and the methods that operate on that data.
Example
class Rectangle { private: int width, height; public: void setDimensions(int w, int h) { width = w; height = h; } int area() { return width * height; } };
Explanation
- The 'class' keyword defines the class.
- The body is enclosed in braces and ends with a semicolon.
- 'private' members are hidden from outside access.
- 'public' members provide a functional interface.
- The functions define the behavior of the Rectangle object.
2. Discuss the importance and usage of access specifiers in C++. [5 Marks]
Explanation
Access specifiers define how members of a class can be accessed. They are the backbone of encapsulation.
Key Points
- private: Members are accessible only within the class. This protects data integrity.
- public: Members are accessible from outside the class. This is used for the class interface.
- protected: Similar to private, but allows access in derived classes (relevant for inheritance).
- Default: In a class, the default is private, while in a struct, the default is public.
- Usage: Proper use ensures that the internal representation of data can change without breaking the external code that uses the class.
3. Describe the two ways to define member functions and their differences. [5 Marks]
Inside the class
Functions defined inside the class are treated as inline functions by default. This is suitable for small functions.
Outside the class
Functions defined outside use the scope resolution operator (::). The syntax is ReturnType ClassName::FunctionName() { ... }. This is cleaner for large codebases.
Comparison
- Inside: Faster execution (inline), but makes the class definition bulky.
- Outside: Separates declaration from implementation, improving readability and maintenance for large programs.
4. Explain nesting of member functions with a practical C++ approach. [5 Marks]
Introduction
Nesting of member functions occurs when a member function calls another member function of the same class.
Application
It is often used when a high-level function requires an internal calculation. For example, a function 'calculateTotal' might call a private function 'getTax' to compute the final amount.
Steps
- Define the primary function in the class.
- Identify a sub-task that can be encapsulated in another function.
- Call the sub-task function within the primary function body.
- Ensure the calling function is accessible to the current scope.
5. Write a complete C++ program to demonstrate a class with private data and public member functions. [5 Marks]
Code Structure
#include <iostream> using namespace std; class Student { private: int id; public: void setId(int i) { id = i; } void display() { cout << "Student ID: " << id; } }; int main() { Student s1; s1.setId(101); s1.display(); return 0; }
Explanation
- The class 'Student' contains one private data member 'id'.
- 'setId' is a public function used to set the private data.
- 'display' is a public function used to output the data.
- The object 's1' is instantiated in 'main'.
- Accessing 'id' directly from 'main' would cause a compiler error, demonstrating encapsulation.