ANSWERS – QUESTIONS 3.
What is C++?
C++ is an object-oriented programming language developed by Bjarne Stroustrup as
an extension of C.
It supports both procedural and object-oriented programming features.
What are tokens in C++?
Tokens are the smallest individual units of a C++ program.
Examples:
Keywords
Identifiers
Constants
Operators
Punctuators
Strings
State features of C++:
Object-Oriented Language
Reusability through classes and objects
Supports Function Overloading
Supports Operator Overloading
High performance like C
Portable and Extensible
Supports Inheritance and Polymorphism
Write rules for writing identifiers in C++:
Must begin with a letter or underscore (_)
Can contain letters, digits, or underscore
No spaces or special characters allowed
Case-sensitive
Cannot use C++ keywords
Example: studentName, _totalMarks, roll_no
What are data types in C++?
Basic Data Types: int, float, char, double, bool
Derived Data Types: array, pointer, function
User-defined Data Types: struct, union, class, enum
What is constant in C++?
A constant is a fixed value that cannot be changed during program execution.
Example:
const int age = 18;
What is dynamic initialization of variables? How is it achieved in C++?
Dynamic initialization means initializing variables at runtime using expressions.
Example:
int a = 5;
int b = a * 2; // initialized dynamically
What is reference variable in C++?
A reference variable acts as an alias for another variable.
Example:
int a = 10;
int &ref = a; // ref refers to a
What is scope resolution operator in C++?
The scope resolution operator (::) is used to access a global variable or a class
member when there is a name conflict.
Example:
int x = 10;
void show() {
int x = 20;
cout << ::x; // prints 10
What is use of insertion and extraction operator in C++?
Insertion operator (<<) – used with cout to output data
Extraction operator (>>) – used with cin to input data
Example:
cin >> a;
cout << a;
1 Describe operator precedence in C++:
Operator precedence defines the order in which operators are evaluated.
Example:
* and / have higher precedence than + and -.
So, in a + b * c, multiplication is done first.
1 What are memory management operators in C++?
new – allocates memory dynamically
delete – frees memory allocated by new
Example:
int *p = new int;
delete p;
1 What are manipulators?
Manipulators are used to format output in C++.
Common manipulators: endl, setw, setprecision
Example:
cout << setw(10) << a << endl;
1 Describe general structure of C++ program:
#include <iostream>
using namespace std;
int main() {
// variable declaration
// statements
return 0;
1 Write syntax of if statement in C++:
if (condition) {
statement;
1 Write syntax for switch statement in C++:
switch(expression) {
case value1: statement1; break;
case value2: statement2; break;
default: statementN;
}
1 What are loop control statements in C++?
for loop
while loop
do-while loop
Used to execute a block repeatedly.
1 What is function prototyping in C++?
A function prototype tells the compiler about the function name, return type, and
parameters before it is used.
Example:
int add(int, int);
1 What is advantage of inline function?
Inline functions save function call overhead and make execution faster.
Example:
inline int square(int x) { return x*x; }
2 What is function overloading?
When multiple functions have the same name but di erent parameters, it is called
function overloading.
Example:
int add(int a, int b);
float add(float a, float b);
2 What is default argument? How it can be achieved?
Default arguments provide default values to parameters when no value is passed.
Example:
int sum(int a, int b = 10) { return a + b; }
2 What is argument in functions?
An argument is the value passed to a function when it is called.
Example:
sum(5, 6); // 5 and 6 are arguments
2 Program to find sum of first 100 odd numbers:
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for(int i = 1; i <= 200; i += 2)
sum += i;
cout << "Sum = " << sum;
return 0;
2 Program to check if a number is prime:
#include <iostream>
using namespace std;
int main() {
int n, i, flag = 0;
cout << "Enter number: ";
cin >> n;
for(i = 2; i <= n/2; i++) {
if(n % i == 0) {
flag = 1;
break;
}
}
if(flag == 0)
cout << "Prime number";
else
cout << "Not prime";
2 Program to find sum of digits of a number:
#include <iostream>
using namespace std;
int main() {
int n, sum = 0, r;
cout << "Enter number: ";
cin >> n;
while(n > 0) {
r = n % 10;
sum += r;
n = n / 10;
cout << "Sum of digits = " << sum;
}
ANSWERS – QUESTIONS 3.2 (ARRAYS & POINTERS)
What is array? Explain how array is declared.
Array is a collection of elements of same data type stored in contiguous memory
locations.
Declaration:
int marks[5];
Explain how array can be passed onto a function.
An array can be passed by passing its base address to the function.
Example:
void display(int arr[], int n);
Here, arr refers to the base address of the array.
What are pointers?
Pointers are variables that store the address of another variable.
Example:
int a = 10;
int *ptr = &a;
What are advantages of pointers?
Allows dynamic memory allocation
Supports e icient array and structure handling
Enables function arguments to be passed by reference
Used in data structures like linked lists, trees, etc.
6. What operations are permitted in pointer arithmetic?
Addition, subtraction, increment (++), decrement (--), and comparison operations.
7. What is 'call by value' and 'call by reference'?
Call by Value: Copy of the variable is passed to the function (changes don’t
a ect original).
Call by Reference: Address of the variable is passed (changes a ect original
value).
8. Describe close correspondence between array and pointer.
The name of an array acts as a pointer to its first element.
Example: int arr[5]; int *p = arr; → p points to arr[0].
9. What are strings?
Strings are sequences of characters terminated by a null character '\0' in C++.
10. What is object-oriented programming?
It is a programming paradigm based on objects that contain both data and
functions.
Example: C++, Java, Python (OOP).
11. State features of object-oriented programming.
Major features:
1. Class and Object
2. Inheritance
3. Encapsulation
4. Polymorphism
5. Abstraction
12. What are objects in C++?
Objects are instances of a class that represent real-world entities and have data
members and member functions.
13. What is class in C++?
Class is a user-defined data type that groups data and functions together.
14. What is inheritance?
Inheritance allows one class (derived) to acquire properties and behaviors of
another class (base).
15. What is polymorphism?
The ability of one function or operator to behave di erently based on the type of
objects.
Example: Function Overloading, Operator Overloading.
16. What is data abstraction?
Showing only essential features and hiding internal details of an object.
17. What is data hiding?
Restricting access to internal data of a class using access specifiers like private and
protected.
18. What is data encapsulation?
Wrapping data and functions into a single unit called a class.
19. How member functions are defined?
Member functions can be defined:
1. Inside the class definition.
2. Outside the class using scope resolution operator ::.
20. Explain how to form an array of object.
Declare multiple objects of the same class using array syntax:
class Student { ... };
Student s[5]; // Array of 5 objects
21. What is friendly function? What are advantages of it?
A function declared with keyword friend can access private and protected members
of a class.
Advantage: Allows controlled access without inheritance.
22. What is constructor?
Special function of a class that initializes objects automatically when created.
23. What is destructor?
Special function used to destroy objects and free resources.
It has the same name as the class preceded by ~.
24. What are syntax rules for writing constructor function?
Rules:
1. Same name as class
2. No return type (not even void)
3. Automatically invoked when object is created.
25. What is default constructor?
Constructor with no parameters, automatically provided if no other constructor
exists.
26. How dynamic initialization of object is achieved?
By passing arguments to the constructor at runtime.
Example:
Student s1(roll, marks);
27. What is copy constructor? When is it used?
Constructor that creates a copy of an existing object.
Used during:
1. Object initialization from another object
2. Passing object by value to a function.
28. Write syntax rules for writing destructor function.
Syntax:
~ClassName() {
// cleanup code
No arguments, no return type.
29. Write a program in C++ for addition of two matrices.
#include<iostream>
using namespace std;
int main() {
int a[2][2] = {{1,2},{3,4}};
int b[2][2] = {{5,6},{7,8}};
int c[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c[i][j] = a[i][j] + b[i][j];
cout<<"Sum of matrices:\n";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cout<<c[i][j]<<" ";
cout<<endl;
30. Write a program in C++ for addition of diagonal elements of matrix.
#include<iostream>
using namespace std;
int main(){
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int sum=0;
for(int i=0;i<3;i++)
sum += a[i][i];
cout<<"Sum of diagonal elements = "<<sum;
31. Write a program in C++ to find transpose of a given matrix.
#include<iostream>
using namespace std;
int main(){
int a[2][3]={{1,2,3},{4,5,6}}, t[3][2];
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
t[j][i]=a[i][j];
cout<<"Transpose:\n";
for(int i=0;i<3;i++){
for(int j=0;j<2;j++)
cout<<t[i][j]<<" ";
cout<<endl;
32. Write a program in C++ for concatenation of two strings.
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char s1[20]="Techno ";
char s2[20]="Generation";
strcat(s1, s2);
cout<<"Concatenated String: "<<s1;
33. Write a program in C++ to find a substring in the given string.
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str[50]="Welcome to Techno Generation";
char sub[10]="Techno";
if(strstr(str, sub))
cout<<"Substring found!";
else
cout<<"Substring not found!";
34. Write an object-oriented program to perform trigonometric operations on
complex numbers.
#include<iostream>
#include<cmath>
using namespace std;
class Complex {
double real, imag;
public:
void getdata(){ cout<<"Enter real and imaginary parts: "; cin>>real>>imag; }
void showTrig(){
cout<<"sin(real)="<<sin(real)<<"\ncos(real)="<<cos(real);
};
int main(){
Complex c;
c.getdata();
c.showTrig();
35. Select the correct alternative and rewrite the following:
a) A pointer is a variable that holds (ii) Memory address of another variable.
b) "Prakash" is a string of length (ii) 8 (7 letters + null \0).
c) Object is a variable of type (iv) Class.
Type Conversion & Classes (Q1–5)
1. What is type conversion?
Type conversion is the process of converting data from one data type to another,
such as from int to float or from a basic type to a class type.
2. How conversion from built-in type to class type is achieved?
By using a constructor that takes a built-in type as an argument.
Example:
3. class Example {
4. int x;
5. public:
6. Example(int a) { x = a; }
7. };
8. Explain class to basic type conversion.
This is done using a casting operator function inside the class.
Example:
9. class Example {
10. int x;
11. public:
12. operator int() { return x; }
13. };
14. Explain conversion from one class to another class.
This can be done using:
o Constructor of destination class that takes source class object, or
o Casting operator in source class converting it to destination class.
15. What is inheritance?
Inheritance allows one class (derived class) to acquire properties and behavior
(data & functions) of another class (base class).
Inheritance (Q6–15)
6. Di erent types of inheritance:
o Single inheritance
o Multiple inheritance
o Multilevel inheritance
o Hierarchical inheritance
o Hybrid inheritance
7. Advantage of inheritance:
o Code reusability
o Easier maintenance
o Reduces redundancy
o Supports polymorphism
8. What is single inheritance?
A derived class inherits from one base class only.
9. What is multilevel inheritance?
A class is derived from another derived class (a chain of inheritance).
10. What is multiple inheritance?
A derived class inherits from more than one base class.
11. What is hierarchical inheritance?
Multiple derived classes inherit from a single base class.
12. What are virtual base classes?
Used in multiple inheritance to avoid duplication of base class members.
Declared using the keyword virtual.
Polymorphism & Virtual Functions (Q17–23)
17. What is polymorphism?
It means "many forms". In C++, it allows a function or operator to behave
di erently based on the type of object.
18. What are virtual functions?
A member function in the base class that is redefined in a derived class and
accessed via a base class pointer using the virtual keyword.
19. What is early binding?
When a function call is linked to the function definition at compile time.
20. What is late binding?
When a function call is linked to the function definition at runtime using virtual
functions.
21. Write rules for virtual functions.
o Declared using the virtual keyword in the base class.
o Must be members of a class.
o Cannot be static.
o Accessed through base class pointers.
o Constructors can’t be virtual; destructors can.
22. What is pure virtual function?
A function declared as virtual and assigned to 0. It makes the class abstract.
23. virtual void display() = 0;
File Handling (Q24–35)
24. What is a file?
A file is a collection of data stored permanently on a storage device.
25. What is input stream and output stream?
o Input stream: Used to read data from file (ifstream).
o Output stream: Used to write data into file (ofstream).
26. What are classes for file stream operations?
o ifstream – input file stream
o ofstream – output file stream
o fstream – both input and output file operations
27. How to open a file?
28. ifstream fin("data.txt");
29. ofstream fout("data.txt");
30. What are di erent file modes?
o ios::in → read
o ios::out → write
o ios::app → append
o ios::binary → binary mode
o ios::ate → open and move to end of file
31. What is file pointer?
It is an internal pointer that keeps track of the read/write position in a file.
32. How to perform sequential input/output operations on file?
o Read or write data in sequence using >>, <<, or getline() for text files.
o Use read() and write() for binary files.
33. How binary file operations are performed?
34. fout.write((char*)&obj, sizeof(obj));
35. fin.read((char*)&obj, sizeof(obj));
36. How to write class object in file?
37. fout.write((char*)&object, sizeof(object));
38. Describe random access of file.
Allows direct access to any part of a file using file pointers:
o seekg() → move get pointer (read)
o seekp() → move put pointer (write)
39. What are command line arguments?
Parameters passed to main() function from command line.
Example:
40. int main(int argc, char *argv[]) { ... }
41. Write a program for overloading of unary + operator.
42. #include<iostream>
43. using namespace std;
44. class Test {
45. int a;
46. public:
47. Test(int x){ a=x; }
48. void operator +() { a = +a; cout<<"Value: "<<a; }
49. };
50. int main() {
51. Test t(-5);
52. +t;
53. }
Type Conversion Program (Q36)
36. Program for basic to class data conversion
37. #include<iostream>
38. using namespace std;
39. class Sample {
40. int x;
41. public:
42. Sample(int a) { x = a; }
43. void show() { cout<<"x = "<<x; }
44. };
45. int main() {
46. int a = 10;
47. Sample s = a; // basic to class
48. s.show();
49. }
MCQ (Q37)
a) :: operator cannot be overloaded → Answer: (iii) scope resolution
b) Derived class with several base classes → Answer: (ii) multiple inheritance
c) Binding of an object to its function at compile time → Answer: (i) early binding
d) Late binding is also called → Answer: (i) dynamic binding
e) Class used to write a stream of objects into a file → Answer: (i) ofstream