0% found this document useful (0 votes)
3 views

CS UNIT 2

The document provides a comprehensive guide to C++ programming, covering 1-Mark, 2-Mark, and 3-Mark questions with concise answers. It includes topics such as the C++ character set, data types, operators, and the structure of a C++ program. Additionally, it explains concepts like variable declaration, initialization, and the compilation process.

Uploaded by

roughuse161
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)
3 views

CS UNIT 2

The document provides a comprehensive guide to C++ programming, covering 1-Mark, 2-Mark, and 3-Mark questions with concise answers. It includes topics such as the C++ character set, data types, operators, and the structure of a C++ program. Additionally, it explains concepts like variable declaration, initialization, and the compilation process.

Uploaded by

roughuse161
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/ 8

Thought for 35s

Expand for details

Below is a complete and self-contained response to your query, providing answers to all the
questions listed under the 1-Mark, 2-Mark, and 3-Mark categories. Each question is clearly stated,
followed by its answer, ensuring the content is concise, informative, and formatted for easy copying
into a Word document.

1-Mark Questions

These are short, straightforward questions requiring brief answers, such as definitions, examples, or
simple identifications.

1. What is the C++ character set?


The C++ character set includes letters (A-Z, a-z), digits (0-9), special characters (e.g., @, #, $,
%), and punctuation (e.g., commas, semicolons) used in a program.

2. Name any two C++ tokens.


Two C++ tokens are identifiers (e.g., variable names) and keywords (e.g., int, if).

3. What is the purpose of the main function in a C++ program?


The main function serves as the entry point where program execution begins.

4. Which header file is used for input and output operations in C++?
The header file iostream.h is used for input and output operations.

5. What does the cout object do?


The cout object displays output on the console.

6. What is the use of the endl manipulator?


endl inserts a new line in the output and flushes the output buffer.

7. What does cascading of I/O operators mean?


Cascading means chaining multiple I/O operators (e.g., << or >>) in one statement, like cout
<< "Hello" << " World";.

8. What is the difference between compilation and linking in C++?


Compilation converts source code into object code; linking combines object code with
libraries to create an executable.

9. What is a data type in C++?


A data type specifies the type of data a variable can hold, such as int, float, or char.

10. Name the built-in data types in C++.


Built-in data types are char, int, float, and double.

11. What is a constant in C++?


A constant is a fixed value that cannot change during execution, e.g., 5 or 'a'.

12. Give an example of a character constant.


An example is 'A'.
13. What is the use of the const keyword?
The const keyword defines constants that cannot be modified.

14. What is variable declaration in C++?


Variable declaration specifies a variable’s type and name, e.g., int x;.

15. What is an assignment statement?


An assignment statement assigns a value to a variable, e.g., x = 5;.

16. What does the signed type modifier do?


The signed modifier allows a variable to store both positive and negative values.

17. What are arithmetic operators in C++?


Arithmetic operators are +, -, *, /, and %.

18. What is the unary minus operator?


The unary minus (-) negates a value, e.g., -x.

19. What do the increment and decrement operators do?


Increment (++) increases a value by 1; decrement (--) decreases it by 1.

20. Name the relational operators in C++.


Relational operators are >, <, >=, <=, ==, and !=.

21. What is the logical NOT operator?


The logical NOT operator (!) reverses a truth value.

22. What is the conditional operator in C++?


The conditional operator (?:) evaluates a condition and returns one of two values.

23. What is operator precedence?


Operator precedence determines the order in which operators are evaluated in an
expression.

24. What is type casting in C++?


Type casting converts a value from one data type to another, e.g., (int)3.14.

2-Mark Questions

These questions require slightly more detailed answers, such as explanations with examples or
comparisons.

1. Explain the structure of a C++ program with an example.


A C++ program includes:

o Include directives: Header files like #include <iostream.h>.

o Main function: Execution starts here with int main().

o Statements: Code like cout << "Hello, World!";.

o Return: Ends with return 0;.


Example:

cpp
CollapseWrapCopy

#include <iostream.h>

int main() {

cout << "Hello, World!";

return 0;

2. What are header files? Name two header files used in C++.
Header files declare functions and objects for use in programs. Two examples are iostream.h
(I/O operations) and iomanip.h (formatting manipulators).

3. Describe the use of cin and cout with an example.

o cout: Outputs data to the console.

o cin: Inputs data from the user.


Example:

cpp

CollapseWrapCopy

int x;

cout << "Enter a number: ";

cin >> x;

cout << "You entered: " << x;

4. What is the significance of the setw() manipulator? Give an example.


setw() sets the output field width for alignment.
Example:

cpp

CollapseWrapCopy

#include <iomanip.h>

cout << setw(10) << "Hello"; // Outputs " Hello"

5. Explain the concept of error messages in C++.


Error messages indicate code issues:

o Syntax errors: Structural mistakes caught during compilation.

o Logical errors: Incorrect logic causing wrong results during execution.

6. What are the basic commands used in a C++ editor?

o Save: Stores the source code.

o Compile: Converts code to object code.


o Run: Executes the program.

7. Explain the difference between float and double data types.

o float: 4 bytes, single precision.

o double: 8 bytes, double precision.


Example:

cpp

CollapseWrapCopy

float f = 3.14f;

double d = 3.14159;

8. Describe the different types of constants in C++ with examples.

o Integer: 5

o Character: 'a'

o Floating-point: 3.14

o String: "Hello"

9. What is the purpose of type modifiers in C++? Give examples.


Type modifiers change data type properties:

o signed: Allows negatives.

o unsigned: Only positives.

o long: Increases size.


Example: unsigned int x;

10. Explain the concept of variable initialization with an example.


Initialization assigns a value during declaration.
Example:

cpp

CollapseWrapCopy

int x = 10;

11. What is the difference between declaration and initialization of variables?

o Declaration: Defines type and name, e.g., int x;.

o Initialization: Assigns a value, e.g., x = 5;.

12. Explain the difference between prefix and postfix increment operators.

o Prefix (++x): Increments first, then uses the value.

o Postfix (x++): Uses the value, then increments.


Example:
cpp

CollapseWrapCopy

int x = 5;

cout << ++x; // 6

cout << x++; // 6, then x is 7

13. Describe the logical operators in C++ with their truth tables.

o AND (&&): True if both are true.

text

CollapseWrapCopy

T && T → T

T && F → F

F && T → F

F && F → F

o OR (||): True if either is true.

text

CollapseWrapCopy

T || T → T

T || F → T

F || T → T

F || F → F

o NOT (!): Reverses value.

text

CollapseWrapCopy

!T → F

!F → T

14. What is automatic type conversion in expressions? Give an example.


Automatic type conversion adjusts types in expressions to a common type.
Example:

cpp

CollapseWrapCopy

int x = 5;

float y = x + 2.5; // x becomes float


15. Explain the use of shorthand operators in C++ with examples.
Shorthand operators combine operations with assignment.
Example:

cpp

CollapseWrapCopy

int x = 5;

x += 3; // x = x + 3, now 8

16. What is the precedence of arithmetic operators in C++?


*, /, % are evaluated before +, -.
Example:

cpp

CollapseWrapCopy

int x = 5 + 3 * 2; // 11 (3*2 first)

3-Mark Questions

These questions demand detailed explanations, often with examples, comparisons, or processes.

1. Discuss the process of compilation, linking, and execution in C++.

o Compilation: Translates source code to object code, checks syntax.

o Linking: Combines object code with libraries to form an executable.

o Execution: Runs the executable in memory, performing tasks.

2. Explain the different types of C++ tokens with examples.

o Identifiers: Variable names, e.g., myVar.

o Keywords: Reserved words, e.g., int.

o Constants: Fixed values, e.g., 5.

o Operators: Symbols, e.g., +.

3. Describe the use of I/O operators (<< and >>) with examples.

o <<: Outputs data with cout.

o >>: Inputs data with cin.


Example:

cpp

CollapseWrapCopy

int x;

cout << "Enter a number: ";


cin >> x;

cout << "You entered: " << x;

4. What is the role of the iomanip.h header file? Provide examples of its use.
iomanip.h provides formatting manipulators like setw().
Example:

cpp

CollapseWrapCopy

#include <iomanip.h>

cout << setw(10) << "Hello"; // " Hello"

5. Discuss the concept of data types in C++ with examples.


Data types define variable data:

o char: Characters, e.g., char c = 'A';.

o int: Integers, e.g., int x = 5;.

o float: Floats, e.g., float f = 3.14f;.

o double: Doubles, e.g., double d = 3.14159;.

6. Explain the use of access modifiers with constants and variables.


The const keyword prevents modification.
Example:

cpp

CollapseWrapCopy

const int x = 10; // Unchangeable

7. Describe the process of declaring and initializing variables of different data types.
Declaration specifies type; initialization assigns a value.
Examples:

cpp

CollapseWrapCopy

int a = 5;

float b = 3.14f;

char c = 'A';

double d = 2.718;

8. What are the rules for naming variables in C++? Provide examples.

o Start with a letter or underscore.

o Use letters, digits, underscores.


o Case-sensitive.

o Not a keyword.
Examples:

o Valid: myVar, _count

o Invalid: 1var, int

9. Discuss the different types of operators in C++ with examples.

o Arithmetic: +, e.g., 5 + 3.

o Unary: -, e.g., -x.

o Increment/Decrement: ++, e.g., x++.

o Relational: >, e.g., x > y.

o Logical: &&, e.g., x && y.

o Conditional: ?:, e.g., x > y ? x : y.

10. Explain the concept of operator precedence with examples.


Precedence dictates operator order (e.g., * before +).
Example:

cpp

CollapseWrapCopy

int x = 5 + 3 * 2; // 11 (3*2 first)

11. Describe how type casting works in C++ with examples.


Type casting converts types explicitly.
Example:

cpp

CollapseWrapCopy

float f = 3.14;

int i = (int)f; // 3

12. What are expressions in C++? Provide examples of different types of expressions.
Expressions combine operands and operators:

o Arithmetic: x + y

o Relational: x > y

o Logical: x &&

You might also like