0% found this document useful (0 votes)
155 views5 pages

C++ Questions and Answers

Uploaded by

aleeyuyunus8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views5 pages

C++ Questions and Answers

Uploaded by

aleeyuyunus8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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).

b) Describe the two ways to include comments in a C++ program.

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`.

b) Write a program to sum the first n numbers, where n is user input.

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

a) Explain the following:


- i) Pointer and Array: A pointer stores the memory address of another variable. An array is
a collection of elements stored in contiguous memory locations. A pointer can be used to
traverse an array.
- ii) Object and Class: A class is a blueprint for creating objects (instances) in object-oriented
programming. An object is an instance of a class.
- iii) Public member and Private member of a Class: Public members can be accessed from
outside the class, while private members are only accessible within the class.
- iv) A Constructor and Destructor: A constructor is a special function that initializes an
object when it is created. A destructor cleans up resources when an object is destroyed.

b) State the differences between Passing by value and Passing by reference.


- Passing by value: A copy of the actual parameter is passed to the function. Changes made
in the function do not affect the original variable.
- Passing by reference: The actual parameter is passed, and changes made in the function
directly affect the original variable.

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;

if (age < 18)


cout << "You are a child." << endl;
else if (age < 60)
cout << "You are an adult." << endl;
else
cout << "You are a senior citizen." << endl;

return 0;
}
Question 7

a) Identify and correct the error in each of the following:


i) cout >> float;
Correction: cout << float; // Correct syntax for output stream.

ii) int double = 77.10;


Correction: double value = 77.10; // "double" is a reserved keyword.

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;
}

You might also like