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

OUTPUT HANDLING in C++ - Text Book Exercise and Question Answers - Computer Science For Class XSSC Part 1 and 2

The document discusses input/output handling in C++. It provides details on the basic structure of a C++ program, including preprocessor directives, the main function header, and the program body. It also discusses comment statements, input/output streams, and various input and output functions like cout, cin, getch(), getche(), getchar(), and gets(). Statement terminators are defined as semicolons (;) that indicate the end of a statement in C++.

Uploaded by

Sajid Alvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

OUTPUT HANDLING in C++ - Text Book Exercise and Question Answers - Computer Science For Class XSSC Part 1 and 2

The document discusses input/output handling in C++. It provides details on the basic structure of a C++ program, including preprocessor directives, the main function header, and the program body. It also discusses comment statements, input/output streams, and various input and output functions like cout, cin, getch(), getche(), getchar(), and gets(). Statement terminators are defined as semicolons (;) that indicate the end of a statement in C++.

Uploaded by

Sajid Alvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Education Is The Key To Success

Home ▼

Monday, 13 December 2021

UNIT 03. INPUT/OUTPUT HANDLING in C++ - Text Book Exercise And


Question Answers - Computer Science For Class X

GO TO INDEX
Computer Science For Class X
Unit 03: Input / Output Handling In C++

Question Answers
By Mrs. Ayesha Arif

Vice Principal

(Jauhar Progressive School)

DETAILED NOTES

Q No.1: What is the basic structure of C++.

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
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 No.2: 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 No.3: Write a note on I/O streams or input/output handling in C++. List all the input and output functions of I/O
streams with descriptions.

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.

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

Output Stream:

If the direction of flow of bytes is opposite, i.e. from main memory to device ( display screen ) then this process is called
output.

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();

cout >>"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.4: 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.5: 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.6: 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:

i. Addition (+):
It is used to perform arithmetic addition.

Example: a + b;

ii. Subtraction (-):

It’s used to perform arithmetic subtraction.

Example: a - b;

iii. Multiplication (*):

It used to perform arithmetic multiplication.

Example: a * b;

iv. Division (/):

It performs the arithmetic division of two numbers.

In the division of integers, numbers show only whole number in result.

Example: a/ b.

v. 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:

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

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

 It adds the right operand to the left operand and assigns result to
1.  += (Addition–Assignment) the left operand

 Example: a+2 means a=a+2

 It subtracts the right operand from the left operand and assigns
2.  -= (Subtraction-assignment) result to the left operand.

 Example: a -= 3 means a=a-3

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

 Example: a *=4 means a=a*4

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

 Example: a /=4 means a=a/4

Q No.7: Differentiate between relational and logical operators.

Ans: Difference Between Relational And Logical Operators

S.NO. Relational Operators Logical Operators


 A relational operator is used to check the  Logical Operators are used to determine two
1.
relationship between two given variables (operands). relational expression.
 All relational operators are binary operators. They  They are used to combine one and more than one
2.
require two operands. relational expression
3.  The result of the comparison is True (1) or False (0).  The result is True (1) or False(0).

Q No.8: Differentiate between assignment operator and equal to operator.

Ans: Difference Between Assignment Operator And Equal To Operator.

S.NO. Assignment Operator(=) Equal to operator (= =)

 The assignment operator (=) is used for assigning a  The equal to (= =) operator is used to check the
1.
variable to a value.
equality of two operands values.

 It compares the value of left-side and right-side


 This operator assigns the value of right-side
2. expression.

expression to left-side variable.

 Such as x=10 and y=10 than x= = y

 Such as x=10

 If condition true otherwise false.

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

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

3. Differentiate between Arithmetic operators and Relational operators.

Ans: Difference Between Arithmetic Operators And Relational Operators:

S.NO. Arithmetic Operator Relational Operator


 In Arithmetic operators, five different operators are  These operators are used to perform logical
1.
used to perform an arithmetic operation. operations on two given variables.
 The relational operators are used to compare any
 Five different operators are Addition, Subtraction, two values. Equal to ==, Not equal to != , greater than
2.
Multiplication, Division, Remainder /Modulus. > , 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:

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 (= =)

 The assignment operator (=) is used for assigning a  The equal to (= =) operator is used to check the
1.
variable to a value.
equality of two operands values.

 It compares the value of left-side and right-side


 This operator assigns the value of right-side
2. expression.

expression to left-side variable.

 Such as x=10 and y=10 than x= = y

 Such as x=10

 If condition true otherwise false.

6. What is the basic difference between \n and \t?

Ans: Difference Between \n and \t:

S.NO. \n \t

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  It insert a new line and cursor moves to the
the right in the same line. beginning of the next line.

3.  Example: cout << "\t";


 Example: cout << "\n";

7. Get the output of following program.

Ans: Output:

on
December 13, 2021

Share

No comments:

Post a Comment

‹ Home ›
View web version

Powered by Blogger.

You might also like