Wednesday 14 September 2022

UNIT 03: INPUT/OUTPUT HANDLING IN C++ - Question Answers - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 03: Input / Output Handling In C++
Question Answers



Q.1: Describe basic structure of C++ program.
Ans: BASIC STRUCTURE OF C++ PROGRAM:
C++ is mainly divided in three parts:
  1. Preprocessor directive
  2. Main Function Header
  3. Body of program / Function

Basic Structure of C++ program is given below:


Q.2: Describe elements of basic structure.
Ans: EXPLANATION OF BASIC STRUCTURE:
1. #include<iostream>:
The statement starts with # symbol is called preprocessor directives. This statement tells preprocessor to include contents of "iostrearm" 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 compiler to use standard namespace. A namespace is a declarative location that provides a space to the Namespace std contains all the classes, objects and functions of the standard C++ library.

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

4. {:
This symbol represents the start of main function.

5. statements;:
Statements are instructions that performs particular task. Statement terminator (;) is used to end every statement in C++ program.
6. return0;:
This statement is used to return the value to the operating system. By default, main function returns integer value 0.

7. }:
This symbol represents the end of main function.

Q.3: Define comments in C++. Also define its types.
Ans: COMMENTS IN C++:
Comments are special remarks that helps to understand different parts of the code in C++ program. Comments are ignored by the compiler. In C++, there are two types of comments statement.

  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 single line comment. Double slash (//) symbol is used at the start of each single line comment.

Example:

  // 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 comment. Symbols (/*and*) are used at the start and end of comment statements.

Example:

 /*This is my first C++ program
 This program display a statement on screen.*/
 #include<iostream>
 int main()
 {
 puts("My First C++ Program");
 return0:
 }



Q.6: What do you mean input / output statement in C++.
Ans: INPUT / OUTPUT STATEMENTS IN C++:
Input and output statements are used to perform input & 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 / STATEMENT IN C++:
cout STATEMENT:
cout stands for "Character Output". In C++, cout sends formatted output to standard outputdevices, such as the screen. cout bject is used along with the insertion operator (<<) for displaying output.

Syntax

   cout << variable; or cout << exp. / string;

Example:

 cout << This is C++ Programming';
 cout << num1;



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

Syntax:

 puts("string constant");


Example:

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


Q.8: Define input functions or statements in C++.
Ans: INPUT FUNCTIONS / STATEMENT IN C++:
Following are the input functions / statement 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 keyboard. cin object is used along with the extraction operator (>>) to accept data from standard input device.

Syntax:

 cin >>variable;


Example:

 #include<iostream>
 using namespaces std;
 int main()
 {
 int b;
 cin >> // cin takes input in "b" variable
 return0;
 }



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 single character at a time. When the user presses the key. getchar() requires Enter key to be pressed. This function is defined in <stdio.h> header file.

Example:

 #include<iostream>
 using namespace std;
 int main()
 {
 char b;
 cout << "\n Enter a character,";
 b = getchar();
 cout << "\n Input character is" << b;
 return 0;
 }



Q.11: Define getch() statement with example in C++.
Ans: getc() STATEMENT:
The getch() function reads the character from the keyboard. This function reads only single character and not printed on the screen. This function takes input and does not requires Enter key to pressed. This function is defined in <conio.h> header file.

Example:

 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main()
 {
 char ch;
 cout << "\n Enter a character";
 ch = getch();
 cout << "\n Input character is "<<ch;
 return0;
 }



Q.12: Define getche() statement with example in C++.
Ans: getche() STATEMENT:
The getche() function reads the character from the keyboard and echoes on the screen. It reads only single character and displays on the screen. This function takes input and does not require Enter key to be pressed. This function is defined in <conio.h> header flle.

Example:

 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main()
 {
 char ch;
 cout << "\n Enter a character:";
 ch = getche();
 cout << "\n Input character is" << ch;
 return0;
 }



Q.13: Define gets() statement with syntax and example in C++.
Ans: gets() STATEMENT:
This function is used to reads characters or the string input and stored them until a newline character found. This function is defined in <conio.h> header file.

Syntax:

 gets("variable");


Example:

 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main()
 {
 char name {25};
 cout << "\n Enter your name:";
 gets(name);
 cout << "\n Your Name is"<< name;
 return0;
 }



Q.14: What is statement terminator?
Ans: STATEMENT TERMINATOR (;):
In C++, statement terminator used to end the statement. Statements are terminated with 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 moves on screen by using special codes. An escape sequence is a special non-printing characters consists of the escape character (the backslash "\") and a second (code) character. The list of the escape sequences is given below:

Escape Sequence Explanation with Example
\n Newline. Position the cursor at the beginning of the next line.
Example: cout << "\n";
\t Horizontal tab. It move the cursor to the next tab stop.
Example: cout << "\t";
\\ Backslash. Insert a backslash character in a string.
Example: cout << "\\";
\a Alert. Produces a beep sound or visible alert.
Example: cout << "\a";
\b Backspace. It moves the cursor backspace.
Example: cout << "\b";
\r Carriage Return. Moves the cursor to the beginning of the current line. Example: cout << "\r";
\' Single Quotation. It is used to print apostrophe sign (').
Example: cout << "\'";
\" Double Quotation. It is used to print quotation mark (").
Example: cout << "\" ";

Q.16: What are operators? Write down the name of operators used in C++.
Ans: OPERATORS IN C++:
Operators are the symbols which 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 which 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 used integer & floating.point data type except remainder or modulas operator.

Operator Operation Example
+ 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 Modulas: Find remainder after integer division.a%b

Q.18: Define all arithmetic operators with examples.
Ans: SIMPLE CALCULATOR PROGRAMMING IN C++ USING ARITHMETIC OPERATORS:


Q.19: Define increment operators with example in C++.
Ans: C++ provides the unary increment operator. It is used to be incremented a variable by 1. Increment operator represented by ++ (double plus sign). The increment operators are used in two ways (Postfix & Prefix) summarized below:

Operator Explanation
++a
(Prefix)
 Increment a by 1. then use the new value of a in the expression in which a resides. i.e. x = ++a;

a++
(Postfix)
 Use the current value of a in the expression in which a resides, then increment a by 1.




Q.20: Define decrement operator in C++.
Ans: DECREMENT OPERATORS:
C++ also provides the unary decrement operator. It is used to be decremented a variable by 1. The decrement operator represented by -- (Double minus sign). are used in The decrement operators are used in two ways (Postfix & Prefix) summarize below:

Operators Explanation
--a
(Prefix)
 Decrement a by 1, then Use the new value of a in the expression in which a resides.

a--
(Postfix)
 Use the current value of a in the expression in which a resides, then decrement 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 following relational operators:

Operators Operations Example
< It checks the value on left is less than value on right. a<b
> It checks the value on left is greater than value on right. a>b
<= It checks the value on left is less than or equal to value on right.a<=b
>= It checks the value on left is greater than or equal to tbe value on right.a>=b
== It checks the equality of two values.a==b
!= It checks the value on left is not equal to value on right.a!=b

PROGRAM USING RELATIONAL OPERATOR IN C++

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

 OUTPUT
 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 conditions are to be tested and based on that result, decisions have to be made. C++ programming offers three logical operators. They are:
Operators Operations Explanation
&& Logical AND. The condition will be true if both expressions are true. 1 if a==b && c==d; else 0
|| Logical OR. The condition will be true if any of the expressions ere true. 1 if a==b || c>d; else 0
! Logic NOT. The condition, will be inverted, False becomes true & true becomes false. 1 if !(a==0); else 0

PROGRAM USING LOGICAL OPERATOR IN C++:

 #include<iostream>
 #include<conio.h>
 using namespace std;

 int main()

 {
 int num1 = 30, num2 = 40;
 cout<< "Logical Operators Example \n";
 if (nun1<=40 && num2>=40)
 {
 cout<< "Numl is less than and Num2 is greater than or equal to 40 \n";
 }
 if (nun1>=40 || num2>=40)
 {
 cout<< "Numl or Num2 is greater than or equal to 40 \n";
 }
 getch();
 return 0;
 }

 OUTPUT
 Logical Operators Example
 Numl is less than and Num2 is greater than or equal to 40
 Numl or Num2 is greater than or equal to 40


Q.23: write down the difference between relational end logical operators.
Ans: DIFFERENTIATE 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 1 (TRUE) or 0 (FALSE).

LOGICAL OPERATOR :
  • Logical operators perform logical operations on boolean values 1 (TRUE) and 0 (FALSE).
  • Logioal operator is usually used te compare one or more relational expressions.
  • Logical operator also return output as 1 (TRUE) and 0 (FALSE).

Q.24: Define assignment operator.
Ans: ASSIGNMENT OPERATOR:
Assignment operator (=) are used to assign result of an expression or a value to a variable. The associativity of assignment operators is right to left means value or expressi at the right is assigned to the left side variable.

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

OPERATOR DESCRIPTION
 +=(Addition-Assignment) Adds the right operand to the left and assigns the result to the left operand.
 -=(Subtraction-Assignment) Subtracts the right operand to the left and assigns the result to the left. operand.
 *=(Multipication-Assignment) Multiplies the right operand to the left and assigns the result to the left operand.
 /=(Division-Assignment) Divides the right operand to the left and assign the result to the left operand.

PROGRAM USING ASSIGNMENT & ARITHMETIC OPERATORS IN C++:

 #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";
 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
 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 100

Value of a using division assignment operator is 10






No comments:

Post a Comment