C++ Questions and Answers
C++ Questions and Answers
Question 1
a) Define a variable and differentiate between a reserved word and a standard identifier.
Answer:
- A variable in C++ is a named storage location in memory used to store data that can be
modified during program execution.
- Reserved words are predefined keywords in C++ that have specific meanings and cannot
be used as identifiers (e.g., int, float, return).
- Standard identifiers, however, are names defined in the standard library that
programmers can use but should not redefine (e.g., cout, cin).
Answer:
- Single-line comments: Begin with `//` and extend to the end of the line.
- Multi-line comments: Enclosed between `/*` and `*/`, spanning multiple lines.
c) Differentiate between C++ inbuilt functions and user-defined functions and give two
examples and advantages of each.
Answer:
- Inbuilt functions: Predefined functions provided by C++ (e.g., `sqrt()`, `abs()`). Advantages:
Reliable and save time.
- User-defined functions: Created by programmers to perform specific tasks (e.g., `int
add(int a, int b)` or `void display()`). Advantages: Flexibility and code reusability.
Question 2
a) Parse the following program. Identify all the keywords, identifiers, operators,
punctuation, and comments.
Program:
Int main ()
{
Int n;
cin >> n;
n = 3 * n; //
cout << "n = " << n << endl;
}
Parsing:
- Keywords: `int`, `main`, `cin`, `cout`, `endl`
- Identifiers: `n`
- Operators: `>>`, `*`, `=`, `<<`
- Punctuation: `{`, `}`, `;`, `()`
- Comments: `//`
b) Assume that x has the value of 7 and y has the value of 3 before the statement executes.
Give the value of x and y after each of the following statements executes:
- i) x = x + x++; → x = 15, y = 3
- ii) y++ = --x; → Error (assignment to a post-incremented value is invalid in C++).
- iii) ++x = y; → Error (assignment to pre-incremented constant is invalid in C++).
- iv) X%Y error clearing.
Question 3
a) What does a declaration and initialization of a variable do and what is the purpose of the
preprocessor directive #include <iostream>?
Answer:
- Declaration reserves memory for a variable and specifies its data type.
- Initialization assigns an initial value to the variable.
- The `#include <iostream>` directive includes the Input/Output stream library, enabling
functionalities like `cin` and `cout`.
Program:
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum of first " << n << " numbers: " << sum << endl;
return 0;
}
Question 4
Question 5
a) Describe what object-oriented programming is and briefly explain these three basic
concepts of OOP:
- i) Encapsulation: Bundling data and functions that manipulate the data into a single unit
(class).
- ii) Inheritance: Enabling a class (child) to inherit properties and behaviors from another
class (parent).
- iii) Polymorphism: Allowing a single function or operator to operate differently based on
the context.
b) Define any class whose object represents any item (e.g., a ratio) and show its member
function, private function, and public function.
Example:
#include <iostream>
using namespace std;
class Ratio {
private:
int numerator, denominator;
public:
Ratio(int num, int den) {
numerator = num;
denominator = den;
}
void display() {
cout << "Ratio: " << numerator << "/" << denominator << endl;
}
private:
void simplify() {
// Code for simplifying the ratio
}
};
Question 6
Write a program that reads the user's age and determines their category.
Program:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
return 0;
}
Question 7
b) What does "floating point" mean and why is it called that? Write a C++ program that tests
arithmetic assignment operators +, -, *, /, and %.
Answer: Floating point refers to numbers with decimal points that can "float" in terms of
precision and size (e.g., 3.14). It's called floating because the decimal point can move
relative to significant digits.
Program:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Addition: " << (a += b) << endl;
cout << "Subtraction: " << (a -= b) << endl;
cout << "Multiplication: " << (a *= b) << endl;
cout << "Division: " << (a /= b) << endl;
cout << "Modulus: " << (a %= b) << endl;
return 0;
}