Monday, 13 December 2021

UNIT 03. INPUT/OUTPUT HANDLING in C++ - Text Book Exercise And SLOs Question Answers - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 03: Input / Output Handling In C++
Text Book Exercise & SLOs Question Answers

By Mrs. Ayesha Arif
Vice Principal
(Jauhar Progressive School)

SLOs Question-Answers

Q No.1: What is the basic structure of C++. Explain basic structure of C++ program.
Ans: BASIC STRUCTURE OF C++:
A C++ program is structured in a specific and particular manner. In C++, a program is divided into the following three basic structures or sections:
  1. Preprocessor Directives
  2. Main Function Header
  3. Body of Program /Function

1. PREPROCESSOR DIRECTIVES:
a. #include:
The # symbol is called preprocessor directive. #include is a specific preprocessor command that effectively copies and pastes the entire text of the file. It links the external header files / libraries which may be required in program.

b. <iostream>:
The file <iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams. This command contains code for displaying and getting an input from the user.

c. namespace std:
It is a prefix that is applied to all the names in a certain set. This instruction tells the compiler to use standard namespace. It is a collection of identifiers . It is used for variables, functions, class and objects . All these elements of the Standard library of C++ are declared within standard “std”.

2. MAIN FUNCTION HEADERS:
a. int main( ):
The starting point of all C++ programs is the int main( ) function. This function is called by the operating system when your program is executed by the computer .int main ( ) return value zero.
(int main(void) is used for execution of C++ program. void main(void) nothing to return.)

b. { :
It signifies the start of a block of code or indicates the beginning of the main function. It is also known as opening curly braces.

c. } :
This signifies the end of a block of code or indicates the ending of the main function. It is also known as closing curly braces.

3. BODY OF PROGRAM / FUNCTION:
The body of the function is enclosed between curly braces. All instructions are executed within opening "{" and closing "}" curly braces.

a. Statements:
Instructions that perform a particular task are called statements . Statement terminator (;) is used to end a statement. The symbol is called semi-colon.

b. cout :
The name cout is short for character output and displays whatever is between the << brackets.

c. << :
Symbols such as << can also behave like functions and are used with the keyword cout.
For example:
cout << "Pakistan Zindabad";
The output of the given example is that
"Pakistan Zindabad" will print on the screen.

d. The return:
The return keyword tells the program to return a value to the function int main.

e. After the return statement, execution control returns to the operating system component that launched this program.
(The return value; is the exit code of our program. By defualt, main( ) in C++ return an int data type value to the operating system.)


Q.2: Introduce the use of Preprocessor directives in C++ program.
Ans: Use of Preprocessor Directives in C++ Program:
The # symbol is called preprocessor directive. It gives instructions to the compiler to preprocess the information before actual compilation starts.
#include is a specific preprocessor command that effectively copies and pastes the entire text of the file. It is used to links the external header files / libraries which may be required in program. #define is used define constants in program.

Q No.3: What do you know about comment statement in C++?
Ans: COMMENT STATEMENTS:
A comment statement is where the programmer place a remark in the source code. The content of the comment is ignored by the compiler.These statements do not execute. Through comments, the programmers give special remarks to the statements for their convenience.
In CAT, there are two types of comment statements.
  1. Single Line Comment
  2. Multi Line Comment

1. Single Line Comment:
It is used for a single-line explanation with the help of a double slash (//) symbol. If the programmer wants to use a single line comment on more than one line, this may need to put a double slash on each line at the start. These comments are ignored by the compiler, which means comments are not executable.

For example:
 // Single line comment
 // This is my first program
 #include<iostream>
 int main( )
 {
 Statements…….;
 return 0;
 }


2. Multi Line Comment:
It is used for multiple-line explanations. Symbols (/* and */) are needed at the start and end of the statements. These comments are ignored by the compiler, which means comments are not executable.

For Example:
 /* Multi line comment
 This is my first program
 #include<iostream>
 int main( )
 Statements…...
 return 0;
 }


Q.4: Define I/O Streams or input/output handling in C++. OR Differentiate between input and output functions or I/O streams.
Ans: I/O STREAMS OR INPUT/OUTPUT HANDLING:
In C++ input and output (I/O) are performed in the form of a sequence of bytes, commonly known as streams. These I/O streams perform operation and are stored in header file. Such as <iostream>. These header files must be mentioned at the beginning of the program.

Difference Between Input And Output Functions Or I/O Streams:

INPUT FUNCTION OR STREAM:
  • If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.
  • Input function means to write or provide the program with some data to be used in it.

OUTPUT FUNCTION OR STREAM:
  • If the direction of flow of bytes is opposite, i.e. from main memory to device ( display screen like monitor) then this process is called output.
  • Output function means to display the output or data to the screen or write the data to a printer or a file.

Q.5: Use input and output function in a program.
Ans: For Answer see below Question No. 7.

Q.6: Use escape sequence in any C++ program.
Ans: Escape sequence in any C++ program:

 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main()
 {
 cout << "\n\t Escape Sequence";
 cout << " \n\t Escape sequences are \"special non-printing\" characters. \\ They can be used with cout in the program.";
 cout << " \n\t \'Examples of escape Sequence";
 cout << " \n\t \a Alert Sound:";
 cout << " \n\t Pakistan \r ";
 cout << " \n\t if \\b place after the word in code, it will remove last letter as:\n\t \"Back is display as Back\b\" ";
 return 0;
 }



Q No.7: List all the input and output functions of I/O streams with descriptions.
Ans: I/O stream uses multiple input/output channels.

OUT PUT FUNCTIONS:
a. cout statement:
  • cout stands for "Character Output". Here 'C' menas "character" and 'Out' means "output".
  • Cout is a predefined object in C++. It is used to produce output on the standard output device which is usually the display screen or monitor.
  • The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator (<<).
  • Syntax: cout << variable or cout <<exp./string << variable

For Example:
cout << "MY FIRST PROGRAM";


b. puts():
  • It is used to print the string to the output stream.
  • The new line is automatically inserted after printing the string.
  • This is defined in <cstdio> header file. This file must be included at the beginning of the program.
  • Syntax: int puts(const char*str);

 For Example:
  #include<iostream>
  using namespace std;
  int main(void)
  {
  puts("MY FIRST PROGRAM");
  return 0;
 }


 OUTPUT:
MY FIRST PROGRAM



INPUT FUNCTIONS:
a. cin statement:
  • cin is a pre defined object that reads data from the keyboard with the extraction operator(>>).
  • It allows to accept data from standard input devices.
  • Syntax: cin >> varaible;

For Example:
 #include<iostream>
 using namespace std;
 int main(void)
 {
 int a;
 cin >> a; //cin takes input in "a" variable
 return 0;
 }




b. getch ():
  • It is a predefined function.
  • It is defined in conio.h.(Console input and output header file).
  • It stands for get character.
  • It is used to get a single character input from user (keyboard) during execution of program.
  • The entered character it is not displayed or printed on the screen.
  • It is used to hold the output screen until the user press any key from the keyboard.

For Example:
 #include<iostream>
 using namespace std;
 int main(void)
 {
 char ch = getch();
c out <<"X Class";
 cout << ch;

 return 0;
 }




c. getche ():
  • It stands for get character echo.
  • The function of "getche()" is similar to getch() function.
  • It is also predefined function in "conio.h" header file.
  • It is used to take a character from user and these characters are displayed on screen.
  • Syntax: character variable = getche();

For Example:
 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main(void)
 {
 char ch0;
 int a=10,b=10;
 cout <<"\n Do you want to continue (Y/N)...";
 ch=getche();
 cout << "\n the addition is...";
 <<a+b;

 return 0;
 }

OUTPUT:

Do you want to continue (Y/N)... Y
the addition is...20



d. getchar ():
  • It is function in C++ that reads the charcter from standard input stream . It is defined in <stdio.h> header file.
  • It needs to press enter key after entering the character.

For Example:
 #include<iostream.h>
 #include<stdio.h>
 using namespace std;
 int main(void)
 {
 char ch;
 cout << "\n Use of getchar function";
 ch=getchar();
 cout << "\n getchar is" << ch;

 return 0;
 }

OUTPUT:

Use of getchar function ......a
getchar is ........a



e. gets():
  • It is a predefined function in C++ and it reads characters from stdin and stores them until a newline character found.
  • It is defined in <cstdio> header file.
  • Program shows how to apply this function in C++.
  • Syntax: gets(variable);

For Example:
 #include<iostream>
 #include<stdio.h>
 using namespace std;
 int main(void)
 {
 char ch[20];
 cout << "\n Enter the message .....";
 get(ch);
 cout << " Your message is .... . .; << ch;

 return 0;
 }

OUTPUT

Enter the message .....
Pakistan
Your message is ......
Pakistan



Q No.8: Define statement terminators.
Ans:STATEMENT TERMINATORS:
Statement terminators are semicolon (;) that indicates the end of the statement. Every statement in C++ must be terminated with semi colon (;). If the terminator is missing an error message will occur.

Q No.9: What are escape sequences?
Ans: ESCAPE SEQUENCES:
The escape sequences are special non-printing characters that can be used with the “cout” in C++. It starts with a backslash (\) and a code character.

Some commonly used escape sequences are as follows:

Q No.10: What are operators? Discuss in detail.
Ans: OPERATORS:
In programming, an operator is a symbol that operates on a value or a variable.
Operators are special symbols that perform operations on variables and values. They are used for specific purposes. They perform mathematical operations on operands.
For example: x + y.
Here
  • "x" and "y" are operand and
  • "+" is an operator used for addition.

Similarly "-" is also an operator used for subtraction.

TYPES OF OPERATORS:
Operators in C++ can be classified into 7 types:
  1. Arithmetic Operators
  2. Increment Operators
  3. Decrement Operators
  4. Relational Operators
  5. Logical Operators
  6. Assignment Operators
  7. Arithmetic assignment operators

1. Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations on variables and data. There are five different arithmetic operators. They are:
  1. Addition (+):
    It is used to perform arithmetic addition.
    Example: a + b;

  2. Subtraction (-):
    It’s used to perform arithmetic subtraction.
    Example: a - b;

  3. Multiplication (*):
    It used to perform arithmetic multiplication.
    Example: a * b;

  4. Division (/):
    It performs the arithmetic division of two numbers.
    In the division of integers, numbers show only whole number in result.
    Example: a/ b.

  5. Remainder / Modulus (%):
    It is used to find remainder of a division. It returns the remainder of an integer value. Remainder operator is also known as Modulus operator. This operator is used only with integral data types.
    Example: 5/2 = 2 and 1 is remainder. We can write in this form 5%2

All arithmetic operators except Remainder or Modulus operator can be used in integer and float data type.

2. Increment Operators:
Increment operators are used to add 1 to the value of a variable. They can be used with any type of variable and are represented by ++ (double plus ) sign. It can be applied to a single variable.
There are two ways to use increment operators:

  1. Pefix Increment operator:
    This operator is applied before variable name.
    It can be written like ++a.
    i.e. x=++a;

    In prefix increment operation value of 'a' is printed as 11 because 1 is added in 'a' before printing.

  2. Postfix Increment Operator:
    This operator is applied after variable name.
    It is written like a++.


    In Postfix increment operation value of 'a' is printen as 10 because 1 is added in 'a' after printing. So the value of 'a' will change to 11 after printing.

3. Decrement Operators:
Decrement operators are same as increment operators but they are used to subtract 1 from the value of a variable. They can be used with any variable and are represented by -- (double minus ) sign.
There are two ways to use decrement operators:
  1. Prefix Decrement operator:
    This operator is applied before variable name.
    Example --a

  2. Post fix Decrement Operator:
    This operator is applied after variable name.
    Example a—

4.Relational Operators:
A relational operator is used to test the relationship between two values (operands). All relational operators are binary operators. They are also called comparison operators.
These operators must require two operands. The result of the comparison is True (1) or False(0).
The following are some relational operators:


5. Logical Operator:
Logical Operators are used to determine two relational expression. They operators can be used in many conditional and relational expressions.
There are three logical operators that are used in C++ programming:


6. Assignment Operators:
The assignment operator (=) is used for assigning a variable to a value. This operator assigns the value of right side expression to left side variable.
Such as x=10;

7. Arithmetic Assignment Operators:
In arithmetic assignment operators , the arithmetic operator is combined with the assignment operator. The assignment operator comes to the right of an arithmetic operator. This is also called compound assignment operator.
The following are the arithmetic assignment operators:
S.NO. Words Meanings
1. += (Addition–Assignment)
 It adds the right operand to the left operand and assigns result to the left operand
 Example: a+2 means a=a+2

2. -= (Subtraction-assignment)
 It subtracts the right operand from the left operand and assigns result to the left operand.
 Example: a -= 3 means a=a-3

3. *= (Multiplication–Assignment)
 It multiplies the right operand with the left operand and assigns result to the left operand.
 Example: a *=4 means a=a*4

4. /= (Division–Assignment)
 It divides the left operand with the right operand and assigns result to the left operand.
 Example: a /=4 means a=a/4



Q No.11: Differentiate between relational and logical operators.
Ans: Difference Between Relational And Logical Operators
S.NO. Relational Operators Logical Operators
1. A relational operator is used to check the relationship between two given variables (operands). Logical Operators are used to determine two relational expression.
2. All relational operators are binary operators. They require two operands. They are used to combine one and more than one relational expression
3. The result of the comparison is True (1) or False (0). The result is True (1) or False(0).


Q No.12: Differentiate between assignment operator and equal to operator.
Ans: Difference Between Assignment Operator And Equal To Operator.
S.NO. Assignment Operator(=) Equal to operator (= =)
1.
 The assignment operator (=) is used for assigning a variable to a value.


 The equal to (= =) operator is used to check the equality of two operands values.

2.
 This operator assigns the value of right-side expression to left-side variable.
 Such as x=10

 It compares the value of left-side and right-side expression.
 Such as x=10 and y=10 than x= = y
 If condition true otherwise false.


TEXT BOOK EXERCISE
B. RESPOND THE FOLLOWING:

1. Use \a and \r both escape sequences in a program.
Ans: \a:
"a" means Alert or alarm. It cause a beep sound in the computer.
Example: cout << "\a";

\r:
Carriage return "r". It is used to position the cursor to the beginning of the cursor line.
Example: cout << "\r";

Use of \a and \r In Escape Sequences In A Program:
EXAMPLE:

 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main( )
 {
 cout << " \a Alert Sound:";
 cout << " \n Pakistan \r ";
 return 0;
 }


2. How many types of comment statements are used in C++?
Ans: Ans. There are two types of comment statements used in C++.
  1. Single line comment (//)
  2. Multi line Comment (*/)
These statements do not execute.

1. Single Line Comment:
It is used for a single-line explanation with the help of a double slash (//) symbol. Double slash (//) symbols are used at the start of each single line comment. These comments are ignored by the compiler, which means comments are not executable.

For Example:
 // Single line comment
 // This is my first program
 #include<iostream>
 int main( )
 { 
 Statements…….;
 return 0;
 }


2. Multi Line Comment:
It is used for multiple-line explanations. Symbols (/* and */) are needed at the start and end of the statements. These comments are ignored by the compiler, which means comments are not executable.

For Example:
 /* Multi line comment
 This is my first program
 #include<iostream>
 int main( )
 Statements…...
 return 0;
 }


3. Differentiate between Arithmetic operators and Relational operators.
Ans: Difference Between Arithmetic Operators And Relational Operators:
S.NO. Arithmetic Operator Relational Operator
1. Arithmetic operators are used to perform mathematical operations. The relational operators are used to compare any two values.
2. All operators use integer & floating- point data type except remainder or modulas operator. All operators use integer & floating-point data type.
3. The result of arithmetic operation is a value. The result of comparison is True (1) or False (0).
4. In Arithmetic operators, five different operators are used to perform an arithmetic operation. These operators are used to perform logical operations on two given variables.
5. Five different operators are Addition, Subtraction, Multiplication, Division, Remainder /Modulus. Equal to ==, Not equal to != , greater than > , less than < , greater than equal to >= and Less than Equal to <= are the Relational operators.


4. Write a program in C++ and use all arithmetic assignment operators.
Ans: A program In C++ With All Arithmetic Assignment Operators:

RESULT:

5. What is basic difference between Assignment operator and Equal to operator.
Ans: Difference Between Assignment Operator And Equal To Operator.
S.NO. Assignment Operator(=) Equal to operator (= =)
1.
 Assignment operator (=) is used for assigning a variable to a value.


 The equal to (= =) operator is used to check the equality of two operands values.

2.
 This operator assigns the value of right-side expression to left-side variable.
 Such as x=10

 It compares the value of left-side and right-side expression.
 Such as x=10 and y=10 than x= = y
 If condition true otherwise false.

6. What is the basic difference between \n and \t?
Ans: Difference Between \n and \t:
S.NO. \t \n
1.
 "\t" stands for Horizontal tab.

 "\n" stands for New line or line feed.
2.
 It is used to shift the cursor to a couple of spaces to the right in the same line.

 It insert a new line and cursor moves to the beginning of the next line.
3.
 Example: cout << "\t";


 Example: cout << "\n";


7. Get the output of following program.


Ans: Output:



Arithmetic Operators

OR




11 comments:

  1. Hi,
    sir g
    sir g Question NO 04. is wrong so please correct it again
    because in this question you must use ARITHMATIC ASSIGNMENT OPERATOR
    sir g ap nay sirf Arithmatic operator ka use kia ha jo k galat ha


    aska Wright Answer ha :

    #include
    using namespace std;
    int main()
    {
    int m;

    cout <<"\n\t Arithmatic Assignment Operator"<>m;
    cout<<"\t";
    m+=2;
    cout <<m<<endl;
    cout<<"\t";
    m-=3;
    cout <<m<<endl;
    cout<<"\t";

    m*=4;
    cout <<m<<endl;
    cout<<"\t";

    m/=5;
    cout <<m<<endl;
    cout<<"\t";

    m%=6;
    cout <<m<<endl;


    return ;

    }

    ReplyDelete
    Replies
    1. Education is the key to success19 October 2024 at 11:43

      That answer is giving in text book well, we have made a slight change in your codes as it wasn't compiling
      We have uploaded image under heading request in comment by anonymous
      You can check it now
      JAZAKALLAH

      Delete
    2. sir g apnay wo Q.NO :04
      Correct nahe kiya q
      sir please recorrect it

      Delete
    3. sir g book ma ha lekin wo sirf Arithmetic operator ka ha
      but ya question Q NO : 04 kehta ha k (Arithmetic and Assignment) ka use Karen

      Delete
    4. 4. Write a program in C++ and use all arithmetic assignment operators.

      please check the above Question

      Delete
    5. Education Is the Key To Success28 October 2024 at 13:19

      Thanks for your feed back. We have made correction of it.
      JAZAKALLAH

      Delete

  2. #include
    using namespace std;
    int main()
    {
    int m;

    cout <<"\n\t Arithmatic Assignment Operator"<>m;
    cout<<"\t";
    m+=2;
    cout <<m<<endl;
    cout<<"\t";
    m-=3;
    cout <<m<<endl;
    cout<<"\t";

    m*=4;
    cout <<m<<endl;
    cout<<"\t";

    m/=5;
    cout <<m<<endl;
    cout<<"\t";

    m%=6;
    cout <<m<<endl;


    return ;

    }

    ReplyDelete
  3. sir g please ,kindly use these following operators to make a program

    a+=2
    a-=3
    a*=4
    a/=5
    a%=6

    ReplyDelete
    Replies
    1. Education I s The Key To Success19 October 2024 at 09:49

      Ok let me check it, I will make correction. JAZAKALLAH

      Delete
  4. Thanks you so much sir

    ReplyDelete
    Replies
    1. Education is the key to success21 October 2024 at 09:33

      JAZAKALLAH

      Delete