INPUT / OUTPUT HANDLING IN C++ Chapter # 03- COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

 COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS

INPUT / OUTPUT HANDLING IN C++
Chapter # 03


Q.1: Describe basic structure of C++ program.

Ans:
BASIC STRUCTURE OF C++ PROGRAM
A C++ program is mainly divided into three parts:

  1. Preprocessor Directives
  2. Main Function Header
  3. Body of program / Function

Basic structure of C++ program is given below:

cpp
#include<iostream> using namespace std; int main() { statements; return 0; }

Q.2: Describe elements of basic structure.

Ans:
EXPLANATION OF BASIC STRUCTURE

  1. #include<iostream>
    The statement starts with # symbol, which is called a preprocessor directive. This statement tells the preprocessor to include the contents of the iostream header file in the program before compilation. This file is required for input-output statements.

  2. using namespace std;
    This statement is used to instruct the compiler to use the standard namespace. A namespace is a declarative location that provides a scope to the identifiers. Namespace std contains all the classes, objects, and functions of the standard C++ library.

  3. int main()
    This statement is a function and is used for the execution of a C++ program. int means it returns an integer type value.

  1. {
    This symbol represents the start of the main function.

  2. statements;
    Statements are instructions that perform a particular task. The statement terminator (;) is used to end every statement in a C++ program.

  3. return 0;
    This statement is used to return the value to the operating system. By default, the main function returns an integer value 0.

  4. }
    This symbol represents the end of the main function.

Q.3: Define comments in C++. Also define its types.

Ans:
COMMENTS IN C++
Comments are special remarks that help to understand different parts of the code in a C++ program. Comments are ignored by the compiler. In C++, there are two types of comments:

  1. Single Line Comment
  2. Multi Line Comment

Q.4: Define single line comment in C++.

Ans:
SINGLE LINE COMMENT
This type is used to write a single line comment. Double slash (//) is used at the start of each single line comment.

Example:

cpp
// This is my first C++ program // This program displays a statement on screen #include<iostream> int main() { puts("My First C++ Program"); return 0; }

Q.5: Define multi-line comment in C++.

Ans:
MULTI LINE COMMENT
This type is used to write multi-line comments. Symbols (/* and */) are used at the start and end of comment statements.

Example:

cpp
/* This is my first C++ program This program displays a statement on screen */ #include<iostream> int main() { puts("My First C++ Program"); return 0; }

Q.6: What do you mean by input/output statement in C++?

Ans:
INPUT/OUTPUT STATEMENTS IN C++
Input and output statements are used to perform input and output operations in C++. These input/output statements are stored in header files like <iostream>. At the beginning of every program, these header files must be included.

Q.7: Define output functions or statements in C++. Define cout statement and puts statement with syntax and example in C++.

Ans:
OUTPUT FUNCTION / STATEMENTS IN C++
cout STATEMENT
cout stands for "Character Output". In C++, cout sends formatted output to standard output devices, such as the screen. The cout object is used along with the insertion operator (<<) for displaying output.

Syntax:

cpp
cout << variable; or cout << expression / string;

Example:

cpp
cout << "This is C++ Programming"; cout << num1;

puts() STATEMENT
This function is used to print the string output. After printing, a new line is automatically inserted.

Syntax:

cpp
puts("string constant");

Example:

cpp
puts("This is C++ Programming");

Q.8: Define input functions or statements in C++.

Ans:
INPUT FUNCTION / STATEMENTS IN C++
The following are the input functions/statements in C++:

  • cin STATEMENT
  • getchar() STATEMENT
  • getch() STATEMENT
  • getche() STATEMENT
  • gets() STATEMENT

Q.9: Define cin statement with syntax and example in C++.

Ans:
cin STATEMENT
cin stands for "Character Input". In C++, cin reads formatted data as input from the keyboard. The cin object is used along with the extraction operator (>>) to accept data from the standard input device.

Syntax:

cpp
cin >> variable;

Example:

cpp
#include<iostream> using namespace std; int main() { int b; cin >> b; // cin takes input in "b" variable return 0; }

Q.10: Define getchar() statement with example in C++.

Ans:
getchar() STATEMENT
The getchar() function reads the available character from the keyboard. This function reads only a single character at a time. When the user presses the key, getchar() requires the Enter key to be pressed. This function is defined in the <stdio.h> header file.

Example:

cpp

#include<iostream> #include<stdio.h> using namespace std; int main() { char b; cout << "\nEnter a character:"; b = getchar(); cout << "\nInput character is " << b; return 0; }

Q.11: Define getch() statement with example in C++.

Ans:
getch() STATEMENT
The getch() function reads the character from the keyboard. This function reads only a single character and does not print it on the screen. This function takes input and does not require the Enter key to be pressed. This function is defined in the <conio.h> header file.

Example:

cpp
#include<iostream> #include<conio.h> using namespace std; int main() { char ch; cout << "\nEnter a character:"; ch = getch(); cout << "\nInput character is " << ch; return 0; }

Q.12: Define getche() statement with example in C++.

Ans:
getche() STATEMENT
The getche() function reads a character from the keyboard and echoes it on the screen. It reads only a single character and displays it on the screen. This function takes input and does not require the Enter key to be pressed. This function is defined in the <conio.h> header file.

Example:

cpp

#include<iostream> #include<conio.h> using namespace std; int main() { char ch; cout << "\nEnter a character:"; ch = getche(); cout << "\nInput character is " << ch; return 0; }

Q.13: Define gets() statement with syntax and example in C++.

Ans:
gets() STATEMENT
This function is used to read characters or a string input and stores them until a newline character is found. This function is defined in the <cstdio> (or <stdio.h>) header file.

Syntax:

cpp

gets("variable");

Example:

cpp

#include<iostream> #include<conio.h> using namespace std; int main() { char name[25]; cout << "\nEnter your name:"; gets(name); cout << "\nYour Name is " << name; return 0; }

Q.14: What is statement terminator?

Ans:
STATEMENT TERMINATOR (;)
In C++, a statement terminator is used to end the statement. Statements are terminated with a semicolon (;) symbol. Every statement in C++ must be terminated; otherwise, an error message will occur.

Q.15: Write down in detail about escape sequences in C++.

Ans:
ESCAPE SEQUENCES
Escape sequences are used to control the cursor movement on the screen by using special codes. An escape sequence is a special non-printing character consisting of the escape character (the backslash \) and a second (code) character. The list of escape sequences is given below:

Escape SequenceExplanation with Example
\nNewline. Positions the cursor at the beginning of the next line.
c

Example: `cout << "\n";` |

| \t | Horizontal tab. Moves the cursor to the next tab stop.
Example: cout << "\t"; | | \\ | Backslash. Inserts a backslash character in a string.
Example: cout << "\\"; | | \a | Alert. Produces a beep sound or visible alert.
Example: cout << "\a"; | | \b | Backspace. Moves the cursor back one position.
Example: cout << "\b"; | | \r | Carriage Return. Moves the cursor to the beginning of the current line.
Example: cout << "\r"; | | \' | Single Quotation. Prints an apostrophe (').
Example: cout << "\'"; | | \" | Double Quotation. Prints a quotation mark (").
Example: cout << "\""; |

Q.16: What are operators? Write down the name of operators used in C++.

Ans:
OPERATORS IN C++
Operators are symbols that tell the computer to execute certain mathematical or logical operations. A mathematical or logical expression is generally formed with the help of an operator. C++ programming offers a number of operators that are classified into the following categories:

  1. Arithmetic Operators
  2. Increment Operators
  3. Decrement Operators
  4. Relational Operators
  5. Logical/Boolean Operators
  6. Assignment Operators
  7. Arithmetic Assignment Operators

Q.17: What are arithmetic operators?

Ans:
ARITHMETIC OPERATORS
Arithmetic operators are used to perform mathematical operations. All operators use integer and floating-point data types except the remainder or modulus operator.

OperatorOperationExample
+Addition: It is used to perform addition.a + b
-Subtraction: It is used to perform subtraction.a - b
*Multiplication: It is used to perform multiplication.a * b
/Division: It is used to perform division.a / b
%Remainder or Modulus: Finds the remainder after integer division.a % b

Q.18: Define all arithmetic operators with examples.

Ans:
SIMPLE CALCULATOR PROGRAM IN C++ USING ARITHMETIC OPERATORS

cpp

#include<iostream> #include<conio.h> #include<stdio.h> using namespace std; int main() { int a, b, add, sub, mul, rem; float div; cout << "\n\t SIMPLE CALCULATOR"; cout << "\n\t Enter the value of a: "; cin >> a; cout << "\n\t Enter the value of b: "; cin >> b; add = a + b; cout << "\n\t Addition of " << a << " and " << b << " is " << add; sub = a - b; cout << "\n\t Subtraction of " << a << " and " << b << " is " << sub; mul = a * b; cout << "\n\t Multiplication of " << a << " and " << b << " is " << mul; div = a / b; cout << "\n\t Division of " << a << " and " << b << " is " << div; rem = a % b; cout << "\n\t Remainder of " << a << " and " << b << " is " << rem; return 0; }

OUTPUT:

swift

SIMPLE CALCULATOR Enter the value of a: 30 Enter the value of b: 20 Addition of 30 and 20 is 50 Subtraction of 30 and 20 is 10 Multiplication of 30 and 20 is 600 Division of 30 and 20 is 1 Remainder of 30 and 20 is 10

Q.19: Define increment operators with example in C++.

Ans:
INCREMENT OPERATORS
C++ provides the unary increment operator. It is used to increment a variable by 1. The increment operator is represented by ++ (double plus sign). The increment operators are used in two ways (postfix and prefix), summarized below:

OperatorsExplanation
++a (Prefix)Increments a by 1, then uses the new value of a in the expression in which a resides.
a++ (Postfix)Uses the current value of a in the expression in which a resides, then increments a by 1.

EXAMPLE (PREFIX):

cpp

#include<iostream> using namespace std; int main() { int ch = 5; cout << "\nValue of ch is: " << ++ch; return 0; }

OUTPUT:

csharp

Value of ch is 6

EXAMPLE (POSTFIX):

cpp

#include<iostream> using namespace std; int main() { int ch = 5; cout << "\nValue of ch is: " << ch++; return 0; }

OUTPUT:

csharp

Value of ch is 5

Q.20: Define decrement operator in C++.

Ans:
DECREMENT OPERATORS
C++ also provides the unary decrement operator. It is used to decrement a variable by 1. The decrement operator is represented by -- (double minus sign). The decrement operators are used in two ways (postfix and prefix), summarized below:

OperatorsExplanation
--a (Prefix)Decrements a by 1, then uses the new value of a in the expression in which a resides.
a-- (Postfix)Uses the current value of a in the expression in which a resides, then decrements a by 1.

Q.21: Define relational operators with example in C++.

Ans:
RELATIONAL OPERATORS
Relational operators are used when we have to make comparisons. It is used to test the relation between two values. The result of comparison is True (1) or False (0). C++ programming offers the following relational operators:

OperatorOperationsExample
<Checks if the value on the left is less than the value on the right.a < b
>Checks if the value on the left is greater than the value on the right.a > b
<=Checks if the value on the left is less than or equal to the value on the right.a <= b
>=Checks if the value on the left is greater than or equal to the value on the right.a >= b
==Checks the equality of two values.a == b
!=Checks if the value on the left is not equal to the value on the right.a != b

PROGRAM USING RELATIONAL OPERATOR IN C++

cpp

#include<iostream> using namespace std; int main() { int x = 20, y = 10; if (x > y) cout << "X is greater than Y"; else cout << "Y is greater than X"; return 0; }

OUTPUT:

csharp

X is greater than Y

Q.22: Define logical operators with example in C++.

Ans:
LOGICAL OPERATORS
Logical operators are used when more than one condition needs to be tested, and based on that result, decisions have to be made. C++ programming offers three logical operators. They are:

OperatorOperationsExpression
&&Logical AND. The condition will be true if both expressions are true.1 if a == b && c == d; else 0
``
!Logical NOT. The condition will be inverted; false becomes true and true becomes false.1 if !(a == 0); else 0

PROGRAM USING LOGICAL OPERATORS IN C++

cpp

#include<iostream> #include<conio.h> using namespace std; int main() { int num1 = 30, num2 = 40; cout << "Logical Operators Example \n"; if (num1 <= 40 && num2 >= 40) { cout << "Num1 is less than and Num2 is greater than or equal to 40 \n"; } if (num1 >= 40 || num2 >= 40) { cout << "Num 1 or Num 2 is greater than or equal to 40 \n"; } getch(); return 0; }

OUTPUT:

vbnet

Logical Operators Example Num1 is less than and Num2 is greater than or equal to 40 Num 1 or Num 2 is greater than or equal to 40

Q.23: Write down the difference between relational and logical operators.

Ans:
DIFFERENCE BETWEEN RELATIONAL OPERATOR AND LOGICAL OPERATOR

RELATIONAL OPERATOR

  • Relational operators compare any values in the form of expressions.
  • Relational operators are binary operators because they require two operands to operate.
  • Relational operators return results either as 1 (TRUE) or 0 (FALSE).

LOGICAL OPERATOR

  • Logical operators perform logical operations on boolean values 1 (TRUE) and 0 (FALSE).
  • Logical operators are usually used to compare one or more relational expressions.
  • Logical operators also return output as 1 (TRUE) or 0 (FALSE).

Q.24: Define assignment operator.

Ans:
ASSIGNMENT OPERATOR
Assignment operators (=) are used to assign the result of an expression or a value to a variable. The associativity of assignment operators is right to left, which means the value or expression on the right is assigned to the left-side variable.

Q.25: Define arithmetic assignment operators with examples.

Ans:
ARITHMETIC ASSIGNMENT OPERATOR
Arithmetic assignment operators are a combination of arithmetic and assignment operators. This operator first performs an arithmetic operation on the current value of the variable on the left with the value on the right and then assigns the result to the variable on the left.

OPERATORDESCRIPTION
+= (Addition-Assignment)Adds the right operand to the left and assigns the result to the left operand.
-= (Subtraction-Assignment)Subtracts the right operand from the left and assigns the result to the left operand.
*= (Multiplication-Assignment)Multiplies the right operand with the left and assigns the result to the left operand.
/= (Division-Assignment)Divides the left operand by the right and assigns the result to the left operand.

PROGRAM USING ASSIGNMENT & ARITHMETIC ASSIGNMENT OPERATORS IN C++

cpp

#include<iostream> #include<conio.h> using namespace std; int main() { int a = 10; cout << "Value of a using assignment operator is " << a << "\n"; a += 10; cout << "Value of a using addition assignment operator is " << a << "\n"; return 0; }
cpp

a -= 10; cout << "Value of a using subtraction assignment operator is " << a << "\n"; a *= 10; cout << "Value of a using multiplication assignment operator is " << a << "\n"; a /= 10; cout << "Value of a using division assignment operator is " << a << "\n"; return 0; }

OUTPUT:

csharp

Value of a using assignment operator is 10 Value of a using addition assignment operator is 20 Value of a using subtraction assignment operator is 10 Value of a using multiplication assignment operator is 100 Value of a using division assignment operator is 10

No comments:

Post a Comment