1.
Review
of C++ Programming
.
1. What is
Character Set?
A character set is a set of
characters a language can identify . Most C++ Compiler follows
ASCII character set. They include
letters, digits, special symbols, white spaces etc.
2. What are token?
What are different types of tokens used in C++?
Tokens are the basic building
blocks of a C++ program. There are five types of tokens in C++.
1. Key words: Key words are tokens
that carry a specific meaning to the language compiler. They
also known as reserved words
because they are reserved by the language for special purpose and
not be redefined for any other
purpose.
Eg. int, switch
etc..
2. Identifiers: Identifiers are
user defined words that are used to name different program elements
such as memory locations,
statements, functions, classes etc.
Identifiers used for naming
memory location is known as variables.
Identifiers assigned to
statements are known as labels.
Identifiers used for set of
statements are known as functions.
3. Literal : Literals are data items
that never change their values during the program running. They
are also known as constants.
There are 4 types of literals
a) Integer literal: tokens
formed only by digits. It must have at least one digit and must
not have decimal point. It may
contain +ve or _ve sign as first character. If there is
no sign it is treated as
positive. Other characters are not allowed.
Eg. 15, -20, +40
b) Floating Point Literal (Real
constants): They have fractional part. They can be
written in two forms: Fractional
and exponential form.
They have at least one digit and
a decimal point. They have either +ve or –ve sign.
If there is no sign it is treated
as positive.
c) Character Literal : Single
character enclosed with single quotes and that never
changes its value during program
run. Non graphical symbols can be represented by
using escape sequence which
consists of a back slash(\) followed by one or more
characters. (eg. \n, \a..). (
‘,”, ? etc are also written with escape sequence because
they carry special purpose).
Eg. Valid Character Literal: ‘s’,
‘$’,’\n’
Invalid Character Literal: ‘82’, “k”, ‘\8’
d). String Literals: Sequence
of one or more characters enclosed within a pair of
double quotes.
Eg. “computer”. Every string in
C++ terminated by a null (\o) character.
4. Punctuators: Special symbols that have syntactic or semantic
meaning to the compiler.
Eg: #, : , ’ , ” ,() ,[]
5. Operators: Operators are the
tokens that trigger some kind of operations. The operations applied
on a set of data called operands.
Based on the number of operands,
operators are classified into three, Unary operator, Binary operator and
Ternary operator.
A unary operator acts on a
single operand. A binary operator acts on two operands. A ternary
operator(? :) acts on
three operands.
Based on the nature of operation
operators are classified into three Arithmetic, Relational and
Logical operator.
a). Arithmetic operator are used
for addition, subtraction, multiplication and division(+, - , * , / ).C++
provides a special operator
modulus operator(%) for getting remainder of division operation. For
example 10 % 2 returns 0.
b) Relational operators
Relational operators are used to
compare values. They produces only ‘True’ of ‘False’ values.
The following are the relational
operators in C++.
c). Logical operator
Logical operators allows us to
combine two or more conditions. They are often referred to as
Boolean operators. The logical
operators are &&(logical AND),||(logical OR),and !(logical NOT).
There are some other operators
such as,
d). Input-Output operators
The Input-Output operators are
used to take input and display output. The operator(>>) is used
for taking input, and the
operator(<<) is used to display the output. The >> operator
is called
extraction or get from operator.
The operator << is called insertion or put to operator.
These are
binary operators.
The input operator is used with
standard input stream, cin and the output operator is used with
standard output stream, cout. The
input operator takes the value through cin and stores in a variable.
cin >> number;
cout << number+2;
Cascading of I/O
operators
The multiple use of input or
output operators in a single statement is called cascading of I/O
operators.
Eg. cin>>a>>b;
e). Assignment operator (=)
The assignment operator is used
to assign a value to a variable.
For example a=10 Assigns the value 10 to a
The symbol = assigns a value,
where as the symbol == compare two values(Relational Operator).
3. What are rules
in naming of identifiers?
a). It may contain an arbitery
long sequence of letters, digits and _(under score)
b). First character must be
letter or _
c). White space and special
characters are not allowed.
d). Key words can not be used.
e). Upper and Lower case letters
are treated separately(case sensitive).
Eg.
Valid identifiers: _dob, FOR,
Count
Invalid identifiers: sum of,
1styear, for…..
4. What are Data
Types?
These are means to identify the
type of data and associated operations handling these data. Data
types are classified into fundamental
and user-defined data types.
Fundamental data types represent
atomic values and they include int, char, float, double
and
void.
5. What are type
modifiers?
Type modifiers are used to modify
the size of memory space and range o data supported by the
basic data types.
Eg. long, short, signed, unsigned
6. What are
expressions?
Expressions are constitutes by
operators and operands to perform an operation. Based on the operators
used, there are different types
of expressions like ,
i). Arithmetic expressions Arithmetic
expressions are also divided into
a). Integer expression :All operands in
the expressions are integers. An integer expression yields
an integer result.
b). Floating point (decimal
) expression: All
operands in the expression are floating
points(decimals).A floating point
expression yields a floating point result.
ii). Relational expression ; The relational
expression consists of relational operators. They are also called conditions.
iii). Logical expression : A logical
expression is an expression whose value is either True or False.
For example x>y is a
logical expression, since it can be either ‘True’ or ‘False’.
7. What is type
conversion?
Type conversion means converting
one data type to another data type. There are two types of type
conversion, Implicit type
conversion (Type Promotion) and Implicit type conversion(Type
Casting)
a). Implicit type conversion also known as
automatic type conversion is performed by
the compiler. The conversion is
always from lower type to higher type, it is also known as
type promotion.
b) Explicit type conversion Type casting
refers to conversion that is performed explicitly
using cast operator. The operator
used for this purpose is known as cast operator. The cast
operator takes on the format cast
type (expression)
eg. int a = (int) 10.5 , Here the
value 10.5 is converted to integer type
8. What are
different statements in C++?
Statements are the basic
executable units of a program. There are different types of statements,
a). Declaration
statements :
Used to declare identifiers before their usage.
Eg. int n,x;
Variable initialization
statements are used to assign values at the time of declaration.
float pi = 3.14;
b). Input
statements :
Identifier cin with >> operator( extraction operator or get from
operator) and
variable name
eg. cin>>x;
c). Output
operator:
cout with << operator( insertion operator or put to operator)
eg. cout<< x;
d). Assignment
statement:
Used to store a data in a memory location;
eg. x = 25;
9. What are
arithmetic assignment operator?
The operators like +=, - = ,
*=, /=, %= used for an arithmetic operation and an assignment
operation.
Eg. n+=2; equivalent to n=n+2;
10. What are Control
Statements in C++?
Control statements are used to
alter the normal sequence of execution of a program. They are classified
into two types,
a) Decision
Making or Selection Statements
b). Iteration
Statements
a). Selection
statements
The Selection statements are
based on a condition. They are also known as branching
statements. The flow of control
depends on the result of a particular condition. C++ supports three
types of decision statements,
a) if statement
b) if – else
statement
c) Switch
statement
a). if statement
The if statement is a single path
statement, which executes statement only if the condition is true.
Syntax :
if(condition or
expression)
statement1;
If the condition is true then the
statement1 will be executed.
if (mark >=18)
cout <<”You have Passed “ ;
In the above example the message
You have passed will be displayed only
when the condition is true ie,
mark >=18 is true.
b). if – else
statements
The if... else statement is used
to test a logical condition and choose one of
the alternatives based on the
result of logical condition.
The syntax :
If (condition or
expression)
statement 1;
else
statement 2;
eg.
if (a > b)
cout << “First number is
largest: ” ;
else
cout <<”Second number is
largest: ” ;
c). Nested if
An if – else statement can be
placed inside another if – else statement, is called nesting of if.
Example
if (mark >=80)
{
if (age>18)
cout<<”Eligible for higher
studies” ;
else
cout <<”Not Eligible” ;
}
d). else-if
ladder
The else-if ladder helps
to select one out of many alternative block of statements.
Syntax :
if (test-expression)
{
Statement 1;
}
else if (test-expression)
{
Statement 2;
}
else if (test-expression)
{
Statement 3;
}
else
{
Statements;
}
Eg.
if ( percentage>=90)
cout<<”A+”’
else if ( percentage>=80)
cout<<”A”’
else if ( percentage>=70)
cout<<”B+”’
else if ( percentage>=60)
cout<<”B’
else if ( percentage>=50)
cout<<”C+”’
else
cout<<”Low Score”’
Switch statement
The Switch enables to select one
among several alternatives. It is a multipath statement.
Syntax:
Switch
(expression)
{
case value1 :Statement 1;break;
case value2 :Statement 2;break;
case value3 :Statement 3;break;
default : Statement n;
}
The expression is evaluated and
statements are executed according to the value . If no match is
found, then default statement is
executed.
Eg.
switch(day_no)
{
case 1 : cout<<”Sunday”;
break;
case 2 : cout<<”Monday”;
break;
case 3 : cout<<”Tuesday”;
break;
……………………..
……………………..
default : cout<<”Invalid
entry”;
Looping
Statements
a). for
statement :
The for statement is the most
commonly used statement in C++.It is an
entry controlled loop.
Syntax :
For(initialisation ; test
expression ; updation)
{
Body of loop;
}
Example :Program to Print
first 10 natural numbers
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<10;i++)
{
cout<< i;
cout<<" ";
}
}
b). While
Statement
The while ... loop is an entry
controlled loop. The condition is checked first and if it is true the
loop will be executed.
Syntax:
While (expression)
{
Body of loop;
Update expression;
}
Example: Program to print
first 10 even numbers
#include<iostream>
int main()
{
int i;
i=2;
while(i<=10)
{
cout<<i;
i=i+2;
}
}
c). do ... while
loop
The do while loop executes
statement before the expression is tested.
Syntax:
do
{
Statements ;
}
while
(expression)
Example: Program to print
first 10 even numbers
#include<iostream>
int main()
{
int i;
i=2;
do
{
cout<<i;
i=i+2;
} while(i<=10);
}
11. What is the
Difference between do .... while and while Loop ?
In the while loop the
condition is tested and the body gets executed( Entry controlled
statement). In do..while
loop the condition is checked at the end of the loop(Exit controlled loop).
The do...while loop executes
at least one time even if the condition is false.
12. What is
Conditional Operator (?:):
The conditional operator is a
ternary operator(ie, it takes three operands).The general form is
Condition?
true-part: false-part;
The condition is a boolean
expression. If it is true the result is the true part, else the result is the
false-part .
Eg. final_ce = (ce1>ce2) ? ce1
: ce2;
13. What are Jump statements?
Jump statements are used to jump
unconditionally to a different statement. It is used to alter
the flow of control
unconditionally. There are four types of jump statements in C++ : break,
continue, and go to.
a).break statement is used
to terminate a loop or switch statement.
b). continue statement is
used to continue to the beginning of a loop. When a continue statement is
executed in a loop it skips the
remaining statements in the loop and proceeds with the next iteration of
the loop.
c). goto statement is
used for unconditional jump. It transfers the control from one part of the
program
to another.
Syntax: goto label; (A label is an identifier
followed by a colon)
Eg: int i=1;
start: cout<<i;
++i;
if (i<=50)
goto start;
Previous
Questions:
1.Which
among the following is an insertion operator ?
(a)
<< (b) >> (c) < (d) >
2.What
are the main components of a looping statement ?
3.How
do continue and break statement differ in a loop ?Explain with an example.
4.Write
a C++ program to accept a string and count the number of words and vowels
in
that string.
5.
Write a C++ program to accept N integer numbers and find the sum and average of
even
numbers.Use array to store numbers.
6……………
is an exit control loop.
a)for
loop b)while loop c)do …while loop d)break
7.Explain
switch statement with an example.
8.How does a
‘goto’ statement work ?
No comments:
Post a Comment