Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Getting Started With C++ - 104832

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

Chapter 06 – Getting Started With C++

Conceptual Question/Answers

Introduction

1. Who developed C++?


» Bjarne Stroustrup in 1983 at the AT & T Bell Laboratories, USA.
2. Who two languages involved in C++?
» Simula 67 and C
3. What was C++ initially called?
» C with Classes
4. Who coined the name C++?
» Rick Mascitti
5. Why C++ is termed as superset of C language?
» The ++ operator is an increment operator in C. Since C++ is an increment of C language with Simula
67, therefore, it is termed as an extension of C i.e., superset of C with additional features of Simula67.

Tokens

1. What do you mean by Tokens/Lexical unit? Enlist the tokens in C++?


» The smallest individual unit in a program is known as Token, Lexical unit or Lexical element.
Tokens in C++ are keywords, identifiers, literals or constants, punctuators or separators and
operators.
2. Define the following tokens
a) Keywords : These are predefined words that convey some special or predefined meaning
reserved by the programming language. E.g. if, for, while, else, int etc.
b) identifier : These are the general terminology for the names given to different parts of
the program viz. variables, functions etc.
c) Literals : Literals or constants are data items that never change their value during a
program. E.g. -12.5, "C++" etc.
d) Punctuators : These are symbols used in programming languages to indicate the rhythm
and emphasis of expressions, statements, and program structure. E.g., # ;
~ etc.
e) Operators : These are the tokens that trigger some computation when applied to
variables and other objects in an expression. E.g., + % etc.
3. Why identifiers are needed in a programming language?
» Identifiers are the fundamental building blocks of a program. They are needed in a programming
language to specify names for items like variables, functions, arrays etc. in a program.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

4. State the naming convention of an identifier?


» i) It must start with an alphabet or an underscore, and thereafter can have sequence of alphabets,
digits or underscore
ii) No keywords or standard identifiers should be used.
iii) No special characters other than underscore, should be included in an identifier.
iv) C++ is case sensitive and hence differs in upper case and lower case letters.
5. Differentiate between identifier and keywords?
» Identifier Keyword
i) User-defined name given to a variable, class, i) Predefined reserved words that have special
objects, functions etc. purpose for compiler.
ii) Consists of any combination of letters, digits ii) Consists only letters.
and underscore, starting with a letter or
underscore.
iii) Allows both uppercase and lowercase. iii) Allows only lowercase.
iv) E.g. num1, _num2, etc. iv) E.g. if, while, for etc.

Literals (Constants)

1. What is use of literals (Constants) in C++? Name the literals (Constants) allowed by C++.
» They are used to express specific values within the program.
C++ allows integer-constant, floating-constant, character-constant, string-literal and Bool literal.
2. What do you mean by integer constants?
» Integer constants are the whole numbers without any fractional part i.e., a constant having at least
one digit, +ve or -ve, without any decimal point.
3. Exemplify the different types of C++ integer constants.
» i) Decimal (base 10) : These are the integer constants that begin with a non-zero digit. e.g. 12 etc
ii) Octal (base 8) : These are the integer constants that begins with a zero. e.g. the decimal 12 will
be written as 010 in octal
iii) Hexadecimal (base 16) : These are the integer constants that begins with 0x or 0X. e.g. the
decimal 12 will be written as 0XC in hexadecimal.
4. What is a floating constant?
» A floating constants or a real constant is a number having fractional parts. e.g. -2.02, 0.5E01 etc.
5. Exemplify the different form of expressing a floating constant in C++.
» i) In fractional form, a real constant must have at least one digit before and after a decimal point.
It may be signed or unsigned. E.g. 17.215 etc.
ii) In exponential form, a real constant consists of two parts: mantissa, must be either an integer
or a proper real constant, followed by a letter 'E' or 'e' and, the exponent. e.g. 0.17215E02.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

6. What is a character constant in C++?


» A character constant in C++, is a single character enclosed in single quotation marks. Beside a
character, C++ compiler interprets the escape sequence also as a character constant. e.g. 'a', '\n',
etc.
7. How C++ encounters/interprets a character constant?
» C++ compiler encounters/interprets a character constant by translating it into a corresponding ASCII
code and stores the number in the program.
8. What do you mean by escape sequence?
» These are one byte character preceded by a backslash, used in C++ program to represent non
printable characters or white space characters like line break, horizontal tab etc. E.g. \n for line
break,\t for horizontal etc.
9. Why a character constant is termed as an integer constant in C++?
» Because, C++ interprets a character constant by translating it into a corresponding ASCII code, which
is an integer value.
10. What is a string constant in C++?
» In C++, the constant which is stored in a variable within a pair of double quotation is termed as a
string constant. These represents a sequence of zero or more characters including spaces, enclosed
by double quotes. C++ compiler terminates a string constant by adding a null characters '\0' by
default. e.g. "a", "C++" etc.
11. Difference between 'a' and "a" in C++?
» 'a' is a character constant whereas "a" is a string constant.
The size of 'a' is 1 byte, i.e., the character itself whereas size of "a" is 2 bytes i.e., the characters 'a'
and '\0'.
12. What are the Boolean literals?
» Literals that represent one of the two Boolean values i.e., Boolean true or Boolean false with internal
value as 1 or 0 are Boolean literals.
Punctuators

1. What is the significance of punctuators in C++?


» Punctuators or separators are symbols that are used in programming languages to organize sentence
structures. In C++, though punctuators do not yield and values yet they have syntactic and semantic
meaning to the compiler.
Operators

1. What are operands?


» Variables and objects to which the computation is applied by operators, are called operands.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

2. Why operators are needed in C++?


» Operator are required to perform some computation when applied to variables and other objects in
an expression.
3. What are unary operators? Name the unary operators in C++.
» These are the operators that require one operand to perform various type of operations such as
increasing/decreasing a value by 1, negating a value or expression, inverting a Boolean value,
referencing the address etc.
These are + (unary plus), -(unary minus), ++(increment operator), --(decrement operator), !(unary
not), &(address operator), *(indirection operator) and ~(bitwise complement operator).
4. What are binary operators? Name the category of binary operators in C++.
» These are the operators that require more than one operand to perform some kind of operations.
These are categorised as arithmetic operators, logical operators, assignment operators, relational
operators, conditional operators etc.
5. What do you mean by operator precedence? Illustrate with an example?
» The predefined order, in which the evaluation of the operators take place in an expression having
more than one arithmetic operator, is called operator precedence.
e.g. In an expression,
int c = 4 + 2 * 3 % 4;
Output: 6
According to operator precedence, * will evaluate first, then % and lastly +, hence c evaluates to 6.
6. State the significance of
a) Arithmetical Operator : To performs addition, subtraction, multiplication, and division on
two or more operands.
b) Relational Operator : To test for some kind of relation between two entities and returns
a Boolean value i.e., either Boolean true or false as a result.
c) Logical Operator : To make one or more relational expressions, resulting to a Boolean
value i.e., either Boolean true or false.
d) Assignment Operator : To perform an operation with the value currently stored in a
variable and store the resulting value in the same variable.
7. Differentiate between '/' and '%' operator in C++.
» '/' operator '%' operator
i) Returns the result of division of two operands. i) Returns the remainder of division of two
operands.
ii) May returns an integer or a floating value. ii) Always returns an integer value.
8. Write the similarity between '/' and '%' operator in C++.
» Both are binary operators and performs division of two operands.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

9. What value is always returned by a relational operator?


» A relational operator always returns a Boolean value i.e., either Boolean true or Boolean false.
10. Difference between the following C++ statements:
i) a = 5;
ii) if (a = = 5)
» Statement i) uses assignment operator, where variable 'a' is being assigned the value 5, while,
Statement ii) uses relational operator, where variable 'a' is being checked for equality with 5.
11. Exemplify the two forms of the increment/decrement operator.
» i) Prefix form, where operator comes beforehand. In this form, the operand value is evaluated
after the increment/decrement operation. E.g.
int a = 10, c, d;
c = (a++) + 5; will evaluate c to 15 and a to 11
ii) Postfix form, where operator comes beforehand. In this form, the operand value is evaluated
before the increment/decrement operation. E.g.
int a = 10, c, d;
c = (++a) + 5; will evaluate c to 16 and a to 11
12. What do you mean by conditional operator? Explain it with an example?
» The conditional operator (? :) is a ternary operator i.e., it takes three operands. It first evaluates an
expression for a true or false value and then executes one of the two given statements depending
upon the result of the evaluation. The general format of it is :
data_type result = condition/expression ? true_value statement : false_value statement;
E.g.
int x=100;
int a=(x%2==0)? x/2 : x*2;
In the above statement, a will evaluates to 50 since, the condition specified, returns true and as such
the second operand i.e., the true_value is assigned to the variable a.
Backbones of C++

1. What do you mean by comments? What are its type supported by C++?
» Comments are the short explanations, notes or descriptions used by the programmers within a
program to enhance readability or understand ability of the program. C++ compiler ignores such
comments.
C++ supports two types of comments:
i) Single line comment: It starts with a pair of slash sign (//). The C++ compiler ignores everything
following the sign // in that line. E.g. //This is a single line comment

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

ii) Multi-line comment: This is also known as block comment. It includes more than one line
between /* and */ characters. The compiler ignores the statements between these characters.
E.g.,
/* This is a multi-line comment,
where line can flow below */
2. Which essential portion or condition, every C++ program must fulfil?
» Every C++ program must contain one and only one main () function, termed as driver function.
3. What is the significance of the main () function in C++?
» Every C++ program must contain the main() function, since
In C++, the execution of the program starts and ends at main() i.e. only the main() function is
executed.
It is the driver function of the program i.e., if not present, no execution takes place.
4. What do you mean by pre-processor directive in C++? What is its significance?
» The statement starting with an '#'(hash/pound) symbol is a pre-processor directive in C++.
It give instructions to the compiler to process the information defined before the start of actual
compilation.
5. What do you mean by include directive in C++? What is its significance?
» The include directive is the include statement, followed after the pre-processor directive #, i.e.
#include.
It causes the pre-processor to add the contents of the corresponding header file followed by the
include directive to the program where it is written.
6. Write two advantage of #include (include compiler) directive?
» It lets us to include all the desired header files in C++ program that enables us to work with all
declarations or definitions or macros inside the included header file(s).
It supports modularity i.e., decomposing a big program in terms of header files which can be later
included through this directive.
7. What is a header file? In C++, how many types of header files are there?
» Header files are C++ standard library files having an extension of .h to its name, containing the C++
functions declaration and macros definitions that are to be shared between several source files.
There are two types of header file, the files that comes with C++ compiler and the files that the
programmer writes.
8. Difference between #include<filename.h> and #include"filename.h".
» The statement #include<filename.h> describes that the specific header file is in the C++ standard
library directory that comes along with the C++ package, while, the statement #include "filename.h"
describes that the header file is user-defined present in the same directory where the source code is
but not in the standard library directory.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

9. What is the purpose of a header file in a program?


» The language C++, contains only statements. But to perform I/O and other operations, various
functions are needed. These functions are stored together in form a library, the standard library of
the language, having an extension .h, termed as header file, which are then included into the program
to perform the desired operations.
10. What does the file iostream.h consist of?
» The file iostream.h consists of declaration of standard stream input and output facilities. It also
consists of procedures that predefine a set of operations for handling reading and writing of built-in
data types.
11. What is the purpose of iostream.h? Why is the need of including the header file iostream.h at the top
of C++ program code?
» The file iostream.h contains the declaration of predefined stream objects for input, output and error
and also contains a set of operations for handling reading and writing of built-in data types.
For I/O operations in a C++ program, the header file iostream.h is included at the top of each
program.
12. What are the predefined stream objects in I/O library?
» The I/O library defines the objects for input, output and error as:
i) cin - It is an istream (input stream) class object tied to standard input.
ii) cout - It is an ostream (output stream) class object tied to standard output.
iii) cerr - It is an ostream (output stream) class object tied to standard err.
Note: cin, cout or cerr are stream objects not a keyword
13. Define the stream extraction and stream insertion operator?
» Stream extraction operator looks like >>. It defines that extract the next value from the stream
named cin and assign to the next-named variable.
Stream insertion operator looks like <<. It defines that insert the value that follows into the stream
named cout.
14. If a reference to cerr is producing a type error in a program, what could be the possible reason? How
can this error be removed?
» The possible reason for this error could be that the header file iostream.h that declares the predefined
stream objects, has not been included in the program.
To remove this error, # include<iostream.h> must be included at the top of the program code.
15. In C++, how devices are treated?
» In C++, devices are treated as files and at the lowest level, the files are interpreted as streams of
bytes.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

C++ Compilation

1. What is an error? How errors are classified in C++?


» An error is a flaw, fault or failure in a computer program that cause it to produce an incorrect or
unexpected result, or to behave in unintended ways.
In C++, errors are broadly classified as Compile-time error includes syntax and semantic errors, Type
Error, Run-time error and Logical error.
2. What type of errors generally occur while programming?
» i) Syntax errors, when rules of the programming language is violated.
ii) Semantics errors, when program statements are not meaningful.
iii) Type errors, when data or values of unexpected type is passed or input.
3. What do you mean by compile-time error? Explain the categories of compile-time error with example?
» All the errors that are detected and displayed by the language compiler are known as compile-time
error.
There are two categories of compile-time error:
i) Syntax error: These are the errors generated by the compiler when the formal set of rules for
writing a program in a particular language is not followed.
E.g. in C++, missing of semicolon after a statement, mismatched pair of braces etc.
ii) Semantic error: These are the errors generated by the compiler when program statement are
not meaningful for the compiler.
E.g. use of an undeclared variable, assigning an expression with a constant i.e. a + b = c etc.
4. What do you mean by run-time error? State a run-time error?
» If a program is syntactically correct but results in a abnormal termination during its execution, is
termed as run-time error. e.g. a number division by zero, file not found etc.
5. What is a logical error?
» When a program does not get terminated abnormally i.e., gets executed successfully but produces
an incorrect or undesired output, is termed as logical error. These errors occur due to mistakes of
the programmer.
6. Why are logical errors harder to locate?
» In spite of the presence of logical error, a program executes successfully producing an incorrect
output. Therefore, each and every statement of the program need to be scanned. Thus, the logical
errors are harder to locate.
7. Differentiate between semantics error and syntax error with an example.
» Syntax errors are those that occurs when the rules of a programming language is misused, while, a
semantic error occurs when the when statements are not meaningful for the compiler. E.g.
int a, b, ans // statement ends without a semicolon, hence it is a syntax error.
a + b = ans; // expression cannot come on left, hence it is a semantic error.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

8. Differentiate between compile-time error and run-time error.


» Compile-time error Run-time error
i) Occurs due to violation of rules i) Occurs due to unavailability of resource
ii) Errors are caught during compilation ii) Error are caught during execution
iii) Indicates the line of error iii) The line of error is not indicated
E.g. 7 + 5 = ans; E.g. ans = 15/0;
9. Differentiate between Logical and Run-time Error.
» Logical Error occurs due to programmer mistake, while, run-time error occurs due to illegal
operations.
In spite of the presence of logical error a program gets executed, while, in case of run-time error the
program gets terminated abnormally.
It is hard to trace a logical error since the program gets executed, but run time error cause the
program termination end hence can be traced.
10. What do you mean by code generation?
» After removal of all errors in a program, compiler compiles the program to translate the program
code in object code or assembly instruction set, understandable by the computer. This process of
translation is called code generation.
*****************

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

You might also like