Introduction to C++
Evaluation
Part – II
Answer to all the questions
1. What is meant by a token? Name the token available in C++.
Answer: The smallest individual unit in a program is known as a Token or
a Lexical unit.
C++ has the following tokens:
(i) Keywords
(ii) Identifiers
(iii) Literals
(iv) Operators
(v) Punctuators
2. What are keywords? Can keywords be used as identifiers?
Answer: Keywords are the reserved words which convey specific meaning to
the C++ compiler. They are the essential elements to construct C++ programs.
No, Keywords cannot be used as an identifier.
3. The following constants are of which type?
(i) 39 (ii) 032 (iii) 0XCAFE (iv) 04.14
Answer:
(i) Integer constant
(ii) Octal constant
(iii) Hexadecimal constant
(iv) Floating point constant.
4. Write the following real constants into the exponent form:
(i) 23.197 (ii) 7.214 (iii) 0.00005 (iv) 0.319
Answer:
(i) 0.23197 × 102 ⇒ 0.23197E02
(ii) 0.7214×101 ⇒ 0.7214E01
(iii) 0.5×10-4 ⇒ 0.5E.04
(iv) 0.0319×101 ⇒ 0.0319E01
5. Assume n=10; what will be result of n>>2;?
Answer: n = 10 ⇒ (00001010)2
n >>2 ⇒ (00000010)2⇒ 210
6. Match the following
A : B
(a) Modulus : (1) Tokens
(b) Separators : (2) Remainder of a division
(c) Stream extraction : (3) Punctuators
(d) Lexical Units : (4) get from
[ Answer: (a) -
(2), (b)- (3), (c) - (4), (d) - (1)]
(a) Modulus - (2)
Remainder of a division
(b) Separators - (3)
Punctuators
(c) Stream extraction - (4)
get from
(d) Lexical Units - (1)
Tokens
Part – III
Answer to all the questions
1. Describe the differences between keywords and identifiers?
Answer:
Keywords
Keywords are the reserved words which convey specific meaning to
the C++. Keywords can not be used as an identifier.
Example: Switch, case, for, if etc.,
Identifiers
Identifiers are the user defined names given to different parts
of the C++ program. Identifiers are not reserved.
Example: name, age, class-12B etc.,
2. Is C++ case sensitive? What is meant by the term “case sensitive”?
Answer: Yes, C++ is case sensitive. Case sensitive means C++ treats
upper and lower case characters differently. Capital letters ≠ small letters.
3. Differentiate “=” and “==”.
Answer:
=
(i) It is an assignment operator.
(ii) It is used to assign a value to a variable which is on the
left hand side of an assignment statement.
(iii) Eg: a = b (b
value is assigned to a)
= =
(i) It is a relational operator.
(ii) It is used to compare two values and the result wall be
either true or false.
(iii) Eg: a = = b (a
value will be compared with b value)
4. Assume a=10, b=15; What will be the value of a^b?
Answer: a Ʌ b
10 Ʌ 15
1010 = 10102
1510 = 11112
aɅb = 01012 = 510
5. What is the difference between “Run time error” and “Syntax error”?
Answer:
Syntax error
(i) Syntax is a set of grammatical rules to construct a program.
(ii) Syntax errors occur when grammatical rules of C++ are
violated.
(iii) Example: if a
program tries to open a file which does not exist, it results in a run time
error.
Run-time error
(i) A run time error is that occurs during the execution of a
program.
(ii) It is occurs because of some illegal operation that takes
place.
(iii) Example: if a
program tries to open a file which does not exist, it results in a run time
error.
6. What are the differences between “Logical error” and “Syntax error”?
Answer:
Syntax error
(i) Syntax is a set of grammatical rules to construct a program.
(ii) Every programming language has unique rules for
constructing the source code. (iii) Syntax errors occur when grammatical rules
of C++ are violated.
Logical error
(i) A Program has not produced expected result even though the
program is grammatically correct. It may be happened by wrong use of variable /
operator / order of execution etc.
(ii) This means, program is grammatically correct, but it
contains some logical error.
(iii) Semantic error is also called as "Logic Error".
7. What is the use of a header file?
Answer: Header files contain definitions of functions and variables,
which is used into any C++ program by using the pre-processor #include statement.
It have an extension ".h" which contains C++ function declaration and
macro definitions.
8. Why is main function special?
Answer: C++ program is a collection of functions. Every C++ program must
have a main function. The main( ) function is the starting point where all C++
programs being their execution. Therefore, the executable statements should be
inside the main ( ) function.
9. Write two advantages of using include compiler directive.
Answer: The preprocessor directive # include tells the computer to
insert another file in to the source file.
The two advantages are
(i) The program is broken down into modules thus making it more
simplified
(ii) More library functions can be used at the same time size of
the program is retained.
10. Write the following in real constants.
(i) 15.223 (ii) 211.05 (iii) 0.00025
Answer:
(i) 1.5223 × 101 ⇒ 1.5223E−1
(ii) 2.1105 × l02 ⇒ 2.1105E−2
(iii) 25 × l0−5 ⇒ 25E
−5
Part – IV
Answer to all the questions
1. Write about Binary operators used in C++.
Answer: Binary Operators - Require two operands
(i) Arithmetic Operators
(ii) Relational Operators.
(iii) Logical Operators
(iv) Bitwise Operators
(v) Assignment Operators
(vi) Conditional Operator
(i) Arithmetic
Operators : Arithmetic operators perform simple arithmetic
operations like addition, subtraction, multiplication, division etc.,
(ii) Relational
Operators : Relational operators are used to determine
the relationship between its operands. When the relational operators are
applied on two operands, the result will be a Boolean value i.e 1 or 0 to
represents True or False respectively. C++ provides six relational operators.
(iii) Logical
Operators : A logical operator is used to evaluate
logical and relational expressions. The logical operators act upon the operands
that are themselves called as logical expressions. C++ provides three logical
operators.
(iv) Bitwise
Operators : Bitwise operators work on each bit
of data and perform bit-by-bit operation. In C++, there are three kinds of
bitwise operators, which are:
(1) Logical bitwise operators
(2) Bitwise shift operators
(3) One’s complement operators
(v) Assignment
Operators : Assignment operator is used to
assign a value to a variable which is on the left hand side of an assignment
statement. = (equal to) is commonly used as the assignment operator in all
computer programming languages. This operator copies the value at the right
side of the operator to the left side variable. It is also a binary operator.
(vi) Conditional
Operators : In C++, there is only one
conditional operator is used. ?: is a conditional Operator. This is a Ternary
Operator. This operator is used as an alternate to if... else control
statement.
2. What are the types of Errors?
Answer:
Syntax Error:
Syntax is a set of grammatical rules to construct a program.
Every programming language has unique rules for constructing the source code.
Syntax errors occur when grammatical rules of C++ are violated.
Example: If you type as follows, C++ will throw an error, cout <<”Welcome to Programming in
C++” As per grammatical rules of C++, every executable statement should
terminate with a semicolon. But, this statement does not end with a semicolon.
Semantic Error:
A Program has not produced expected result even though the program
is grammatically correct. It may be happened by wrong use of variable/ operator
/order of execution etc. This means, program is grammatically correct, but it
contains some logical error. So, Semantic error is also called as ’’Logic Error”.
Run-time error:
A run time error is that occurs during the execution of a
program. It is occurs because of some illegal operation that takes place.
For example, if a program tries to open a file which does not
exist, it results in a run-time error.
3. Assume a=15, b=20; What will be the result of the following operations?
(a) a&b (b) a|b (c) a^b (d) a>>3 (d) (~b)
Answer:
(a) a&b = (00000100)2 = (4)10
(b) a|b = (00011111)2 = (31)10
(c) aɅb = (00011011)2 = (27)10
(d) a>>3 = (00000001)2 = (1)10
(e) (~b) = (00001011)2 = (11)10
EVALUATE YOURSELF
1. What is meant by literals? How
many types of integer literals available in C++?
Answer:
(i) Literals are data items whose values do not change during
the execution of a program. Therefore Literals are called as Constants.
(ii) Three types of integer 1. Decimal 2. Octal 3. Hexadecimal
2. What kind of constants are
following?
(i) 26 (ii) 015 (iii) 0×F (iv) 014.9
Answer:
(i) Integer
(ii) Octal
(iii) Hexadecimal
(iv) Floating
3. What is character constant in
C++?
Answer: A character constant in C++ must contain one character and must
be enclosed in a single quote. Valid character constants : 'A', '2', ‘$’
4. How are non graphic characters
represented in C++?
Answer: Non- printable characters can be represented by using escape
sequences. An escape sequence is represented by a backslash followed by one or
two characters.
5. Write the following real
constants into exponent form:
(i) 32.179 (ii) 8.124 (iii)
0.00007
Answer:
(i) 32179E-3
(ii) 8124E-3
(iii) 7E - 5
6. Write the following real
constants into fractional form:
(i) 0.23E4 (ii) 0.517E-3 (iii)
0.5E-5
Answer:
(i) 0.000023
(ii) 0.000517
(iii) 0.000005
7. What is the significance of
null (\0) character in a string?
Answer: Sequence of characters enclosed within double quotes are called
as String literals. By default, string literals are automatically added with a
special character ‘\0’ (Null) at the end.
Therefore, the string “welcome” will actually be represented as
“welcome\0” in memory and the size of this string is not 7 but 8 characters
i.e., inclusive of the last character \0.
8. What is the use of operators?
Answer: Operators are used to perform calculations on an operands that
yield a new value.
9. What are binary operators?
Give examples of arithmetic binary operators.
Answer: Binary operators are the operators that required two operands.
Eg : x = a + b − c.
10. What does the modulus
operator % do?
Answer: Modulus operator (%) is used to get the remainder of two integer
division.
Eg: 10% 3 = 1
11. What will be the result of
8.5 % 2?
Answer: 0.5 (Reminder is the answer).
12. Assume that R starts with value
35. What will be the value of S from the following expression? S=(R −)+(++R).
Answer: S = 70.
13. What will be the value of j =
- - k + 2k. if k is 20 initially ?
Answer: j = 59.
14. What will be the value of p =
p * ++j where j is 22 and p = 3 initially?
Answer: 69.
15. Give that i = 8, j = 10, k =
8, What will be result of the following expressions?
(i) i < k (ii) i < j (iii)
i > = k (iv) i = = j (v) j ! = k
Answer:
(i) False
(ii) True
(iii) True
(iv) False
(v) True
16. What will be the order of evaluation
for the following expressions?
(i) i + 3 >= j – 9 (ii) a+10 <
p −3 + 2q
Answer:
17. Write an expression involving
a logical operator to test, if marks are 75 and grade is ’A’.
Answer: If (marks > = 75)
cant << "grade is 'A' ;
HANDS ON PRACTICE
Type the following C++ programs
in Dev C++ IDE and execute. If compiler shows any errors, try to rectify it and
execute again and again till you get the expected result.
1. C++ Program to find the total
marks of three subjects.
#include <iostream>
using namespace std;
int main()
{
int ml, m2, m3, sum;
cout << “\n Enter Mark 1: ”;
cin >> m1;
cout<< “\n Enter Mark 2: ”;
cin >> m2;
cout<<”\n Enter Mark 3: ”;
cin >> m3;
sum = ml + m2 + m3;
cout<< ”\n The sum = ” <<
sum;
}
• Make changes in the above code
to get the average of all the given marks.
Answer:
#include <iostream>
using namespace std;
int main()
{
int ml, m2, m3, avg;
cout << "\n Enter the mark In";
cin >> m1>>m2>>m3;
avg = (ml+m2+m3)/3;
cout <<"The average = "<<avg;
2. C++ program to find the area
of a circle.
#include <iostream>
using namespace std;
int main()
{
int radius;
float area;
cout<< ”\n Enter Radius: ”;
cin >> radius;
area = 3.14 * radius * radius;
cout<< "\n The area of
circle =” << area;
}
Answer:
#include <iostream>
using namespace std;
int main()
{
float r, area;
Cout<< "\n Enter Radius: ";
cin >> r;
area = 3.14 * r * r;
cout<< "\n The Area of the circle is " <<
area;
}
Output:
Enter Radius: 6.5
The Area of the circle is 132.665
3. Point out the errors in the
following program:
Using namespace std;
int main()
{
Cout<< “Enter a value ”;
cin << numl >> num2
num+num2=sum;
cout >>“\n The Sum= ”
>> sum;
Answer:
1. #include<iostream.h>(pre-processor statement missing)
2. int numl, num2; (variable declaration statement missing)
3. cin >> numl >> num2; (instead of extraction operator,
insertion operator in the statement)
4. sum = numl + num 2; (always the expression come in RHS)
5. cout << "\n The sum = " << sum;
(insertion operator is required)
6. } (to End the program using curly braces)
Corrected program :
#include<iostream.h>
int main ()
{
Cout<< "Enter a value";
cin >> numl >>num2'
sum = num 1 + num2;
cout<< "\n The sum = "<< sum;
}
4. Point out the type of error in
the following program
#include <iostream>
using namespace std;
int main()
{
int h=l0; w=12;
cout<< ’’Area of rectangle”
<< h+w;
}
Answer: int h = 10, w = 12;
Cout<< "Area of rectangle"<<h + w;
Data Types, Variables and Expressions
Evaluation
Part – II
Answers to all the questions (2 Marks):
1. Write a short note const keyword with an example.
Answer: Const is the keyword used to declare a constant. It
modifies/restricts the accessibilty of a variable
Eg: const int n = 200;
2. What is the use of setw() format manipulator?
Answer: Setw() manipulator sets the width of the field assigned for the
output. The field width determines the minimum number of characters to be written
in output. Syntax : setw(number of
characters)
3. Why is char often treated as integer data type?
Answer: Character data type is often said to be an integer type, since
all the characters are represented in memory by their associated ASCII Codes. If
a variable is declared as char, C++ allows storing either a character or an
integer value.
4. What is a reference variable? What is its use?
Answer: Reference is an alternative name for a variable. A reference
provides an alias for a previously defined variable. Declaration of a reference
consists of base type and an & (ampersand) symbol; reference variable name
is assigned the value of a previously declared variable.
5. Consider the following C++ statement. Are they equivalent?
char ch = 67;
char ch = ‘C’;
Answer: yes, they are equal.
6. What is the difference between 56L and 56?
Answer: 56 is an integer number.
56L is a long integer number.
7. Determine which of the following are valid constant? And specify their type.
(i) 0.5 (ii) ‘Name’ (iii) ‘\t’ (iv) 27,822
Answer:
(i) 0.5 - Valid Floating Constant
(ii) 'Name' - Invalid String Constant (Enclosed within Double
quotes)
(iii) '\t' - Valid - Non graphic character data type
(iv) 27,822 - Invalid Decimal Constant (Commas is not allowed).
8. Suppose x and y are two double type variable that you want add as integer and assign to an integer variable. Construct a C++ statement for the doing so.
Answer:
doubles x;
double y;
int z = (int) x +
(int) y; or int z = (int)(x + y);
9. What will be the result of following if num=6 initially.
(a) cout << num;
(b) cout << (num==5);
Answer:
(a) 6
(b) 0
10. Which of the following two statements are valid? Why? Also write their result.
int a;
(i) a = 3,014; (ii) a=(3,014);
Answer: (i) Above the two statements is Invalid.
(ii) Special Symbols are not allowed in the integer values (
Commas, Open and Close Brackets).
Part – III
Answers to all the questions (3 Marks):
1. What are arithmetic operators in C++? Differentiate unary and binary arithmetic operators. Give example for each of them.
Answer: (i) The operators, which are applied to perform arithmetical
calculations in a program are known as arithmetical operators.
(ii) A unary operator requires a single (one) operand. Unary +,
Unary −, ++, − − are the
examples of many operators.
(iii) A binary operator requires two operands +, −,*,/, % are the examples of binary
operators.
2. Evaluate x+= x + ++x; Let x=5;
Answer: x + = x + + + x
x = x + x+++ x;
= 5 + 5 + 6= 16.
3. How relational operators and logical operators related to one another?
Answer: Logical operators uses AND (&&), OR (||) and NOT(!).
These operators yield true or false depending upon the out come of different
relational expressions. Relational expressions are formed by Relational
operators.
4. Evaluate the following C++ expressions where x, y, z are integers and m, n are floating point numbers. The value of x = 5, y = 4 and m=2.5;
(i) n = x + y / x;
(ii) z = m * x + y;
(iii) z = (x++) * m + x;
Answer:
(i) n=x+y/x;
n=5+(4/5)
n = 5 + 0.8
n = 5.8
(ii) z = m*x + y;
z = (2.5*5) + 4
z=12.5 + 4
z= 16.5
z = 16 (z is an integer)
(iii) z = (x++) * m + x;
z = ((5++) * 2.5) + 5
z = (5*2.5) + 5
z= 12.5 + 5
z= 17.5
z =17 (z is an integer).
EVALUATE YOURSELF
1. What do you mean by fundamental
data types?
Answer:
(i) Fundamental data types are predefined data types available
in C++.
(ii) These are five fundamental data types in C++.
2. The data type char is used to
represent characters, then why is it often termed as an integer type?
Answer: Since all the characters are represented in memory by their
associated ASCII codes.
3. What is the advantage of
floating point numbers over integers?
Answer:
(i) Floating point number can represent values between the
integers.
(ii) Floating point number can represent a much greater range of
values.
4. The data type double is
another floating point type. Why is it treated as a distinct data type?
Answer: In double data type, more fractions can be accommodated in
double than in float data type.
5. What is the use of void data
type?
Answer: Void data type is used as a return type for functions that do
not return any value.
6. What is modifiers? What is the
use of modifiers?
Answer: (i) Modifiers can be used to modify (expand or reduce) the
memory allocation of any fundamental data type.
(ii) Modifier is a qualifiers used along with data type.
7. What is wrong with the
following C++ statement: long float x;
Answer: Instead of long float x use double x;
8. What is a variable ? Why a
variable called symbolic variable?
Answer: (i) Variables are user-defined names assigned to specific memory
locations in which the values are stored.
(ii) These are called as symbolic variables because these are
named locations.
9. What do you mean by dynamic
initialization of a variable? Give an example.
Answer:
(i) A variable can be initialized during the execution of a
program. It is known as 'Dynamic initialization.'
(ii) For example : int n1, n2,s; s = n1 +n2. This two statements combined into a
single statement using dynamic initialization int s = nl+n2;
10. What is wrong with the
following statement? const int x;
Answer: No value specified to the variable x correct statement;
const int x = 100;
11. What is meant by type
conversion?
Answer: The process of converting one fundamental type into another is
called as "Type conversion".
12. How implicit conversion
different from explicit conversion?
Answer: (i) Implicit type conversion is automatically done by compiler.
(ii) Explicit type conversion is done by the programmer.
13. What is difference between
endl and \n?
Answer: endl - Inserts a new line and flushes the buffer (Flush means -
clean) ‘\n’ - Inserts only a new line.
14. What is the use of
references?
Answer: References means addresses. Address are used to locate the value
in the memory.
15. What is the use of setprecision ( ) ?
Answer: It is used to display numbers with fractions in specific number of digits.
Hands on practice:
1. Write C++ programs to interchange the values of two variables.
a. Using with third variable
b. Without using third variable
Answer:
a. Using with third
variable
#include<iostream.h>
using namespace.std;
int main ()
{
int a, b, t;
cout<<"\n Enter First number";
cin>>a;
cout<<"\n Enter Second number";
cin>>b;
t=a;
a-b;
b=t;
cout<<a<<'\t'<<b;
return 0;
}
b. Without using
third variable :
#include<iostream.h>
using namespace.std;
int main ()
{
int a, b;
cout<<"\n Enter First number";
cin>>a;
cout<<"\n Enter Second number";
cin>>b;
a = a+b;
b = a−b;
a = a−b;
cout<<a<<'\t'<<b;
return 0;
}
2. Write C++ programs to do the following:
a. To find the perimeter and area of a quadrant.
b. To find the area of triangle.
c. To convert the temperature from Celsius to Fahrenheit.
Answer:
a. To find the
perimeter and area of a quadrant
#include<iostream.h>
using namespace.std;
int main ()
{
float area, peri, r;
cout<<"\n Enter radius";
cin>>r;
area=0.25* 3.14*r*r;
//Area= 1/4πr2
peri=0.5*3.14*r;
//perimeter = l/2πr
cout <<"Area
of a quadrant = "<<area<endl;
cout<<"Perimeter of a quadant = "<< peri
<<endl;
return 0;
}
b. To find the area
of triangle :
#include<iostream.h>
using namespace.std;
int main ()
{
float area, b, h;
cout<<"\n
Enter base";
cin>>b;
cout<<"\n Enter height";
cin>>h;
area = 0.5*b*h //Area=l/2
bh
cout <<"Area of a triangle =
"<<area<endl;
return 0;
}
c. To convert the
temperature from Celsius to Fahrenheit
#include<iostream.h>
using namespace.std;
int main ()
{
float tempC, tempF;
cout<<"\n Enter celcius";
cin>>tempC;
tempF =(tempC * 1.8F)+32;
cout<<"Equivalent temperature of <<tempC
<<"in Fahrenheit is"<< tempF<<endl;
return 0;
}
3. Write a C++ to find the total and percentage of marks you secured from 10th Standard Public Exam. Display all the marks one-by-one along with total and percentage. Apply formatting functions.
Answer:
#include<iostream.h>
#include<iomanip.h>
using namespace std;
int main ()
{
char name[30];
float E,L,M,S,SS,tot, per;
cout<<"\n Name
= ";
cin>> name;
cout <<"\ Enter English, Language, Maths, Science,
Social Marks";
cin >> E >> L >> M >> S >>SS;
tot = E+L+M+S+SS;
per = tot/500 * 100;
cout<<setw(20)<<"Name:"<<setw(
10)<< name;
cout <<setw (20) <<"English:"
<<setw(10)<< E<<endl;
cout <<setw (20) <<
"Tamil:"<<setw(10)<< T<<endl;
cout <<setw
(20)<<"Maths:"<<setw(10)<< M<<endl;
cout<<setw(20)<<"Science:"<<setw(10)<<S<<endl;
cout<<setw(20)<<"SocialScience:"<<
setw(10)<< SS<<endl;
cout <<setw (20) <<"Total:"<< setw(
10)<<tot<<endl;
cout<< setw (20)<< "Percentage:" <<
setw( 10) << per<<endl;
return 0;
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.