Pageviews

Wednesday 28 August 2019

+1] 4. Getting Started with C++ / Introduction in C++Programming - Solved Questions from text book.


                     PLUS ONE 
COMPUTER APPLICATION / COMPUTER SCIENCE 


                                     Chapter 4 Getting Started with C++ (for CA)

                                    Chapter 5.   Introduction in C++Programming (for CS)

                  (+1. Computer Application  Questions and answers from text book)

1. What are the different types of characters in C++ character set?

   (i)  Letters : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o                               p q  r s t u v w x y z

(ii) Digits : 0 1 2 3 4 5 6 7 8 9

(iii) Special characters : + - * / ^ \ ( ) [ ] { } = < > . ’ “ $ , ; : % ! & ? _ (underscore) # @

(iv) White spaces : Space bar (Blank space), Horizontal Tab (à), Carriage Return (↵ ) , Newline,                   Form  feed

(v) Other characters : C++ can process any of the 256 ASCII characters as data or as literals

 2. What is meant by escape sequences?

          C++ language has certain non-graphic character constants, which cannot be typed directly from the keyboard. For example, there is no way to express the Carriage Return or Enter key, Tab key and Backspace key. These non-graphic symbols can be represented by using escape sequences, which consists of a backslash (\) followed by one or more characters.

 3. Who developed C++?

         Dr. Bjarne Stroustrup developed C++

 4. What is meant by tokens? Name the tokens available in C++.

          The term ‘token’ in the C++ language is similar to the term ‘word’ in natural languages. Tokens are the fundamental building blocks of the program. They are also known as lexical units. C++ has five types of tokens as listed below:

 1. Keywords

 2. Identifiers

3. Literals

4. Punctuators

 5. Operators

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

        When we refer a single character enclosed in single quotes that never changes its value during the program run, we call it a character literal or character constant. Note that x without single quote is an identifier whereas ‘x’ is a character constant. The value of a single character constant is the ASCII value of the character.

 6. How are non-graphic characters represented in C++? Give an example.

         C++ language has certain non-graphic character constants, which cannot be typed directly from the keyboard. For example, there is no way to express the Carriage Return or Enter key, Tab key and Backspace key. These non-graphic symbols can be represented by using escape sequences, which consists of a backslash (\) followed by one or more characters. It should be noted that even though escape sequences contain more than one character enclosed in single quotes, it uses only one corresponding ASCII code to represent it. That is why they are treated as character constants.

Example  \n - New line,\b - Back space

7. Why are the characters \ (slash), ' (single quote), " (double quote) and ? (question mark) typed using escape sequences?

           These characters can be typed from the keyboard but when used without escape sequence, they carry a special meaning and have a special purpose. However, if these are to be displayed or printed as it is, then escape sequences should be used. Examples of some valid character constants are: 's', 'S', '$', '\n', '+', '9'.

8. Which escape sequences represent newline character and null character?

      Ans. \n and \0

9. An escape sequence represents ______ characters.

10. Which of the following are valid character/string constants in C++?

 'c'

 'anu'

"anu"

 mine

'main's'

" "

 'char

 '\ \'

Ans. Character constant: 'c'  'anu'  '\ \'  'main's'

         String constant   :  "anu"


   11. What is a floating point constant? What are the different ways to represent a floating point constant?

          Floating point literals, also known as real constants are numbers having fractional parts. These can be written in one of the two forms called fractional form or exponential form. A real constant in fractional form consists of signed or unsigned digits including a decimal point between digits. The rules for writing a real constant in fractional form are given below:

• A real constant in fractional form must have at least one digit and a decimal point.

 • It may also have either + (plus) or – (minus) sign preceding it.

• A real constant with no sign is assumed to be positive.

   A real constant in exponential form consists of two parts: mantissa and exponent. For instance, 5.8 can be written as 0.58×101 = 0.58E1 where mantissa part is 0.58 (the part appearing before E) and exponential part is 1 (the part appearing after E).

 12. What are string-literals in C++? What is the difference between character constants and string literals?

        String constant is a set of characters enclosed inside double quotes. ... The basic difference between string and character is that character constant can only be represented as a single entity whereas string constant is an array of characters with a null character at the end of the string.

      Examples of  character constant 'a'  '\ \'  'b' 'C'

       Examples of string constant  "'anu" " "

13. What is the extension of C++ program file used for running?

        Ans  .cpp

14. Find out the invalid identifiers among the following. Give reason for their invalidity

a) Principal amount

b) Continue

 c) Area

d) Date-of-join

e) 9B

 Principal amount : Blank space are not allowed

Date-of-join : Identifier is an arbitrary long sequence of letters, digits and underscores( _ ).

9B :  First character must be a letter or under_score.

 15. A label in C++ is ________.

 a) Keyword b) Identifier c) Operator d) Function

   Ans. b

 16. The following tokens are taken from a C++ program. Fill up the given table by placing them at the proper places

 ( int, cin, %, do, =, "break", 25.7, digit)

 ans.   Keywords       Identifiers         Literals            Operators

            int                  digit                     "Break"                   %

            cin                                                25.7                       =

              do

17. Write down the rules governing identifiers.

      Identifier is an arbitrary long sequence of letters, digits and underscores( _ ).

      • The first character must be a letter or underscore ( _ ).

      • White space and special characters are not allowed.

     • Keywords cannot be used as identifiers.

       • Upper and lower case letters are treated differently,

  i.e. C++ is case sensitive. Examples for some valid identifiers are Count, Sumof2numbers, _
_1stRank, Main, Average_Height, FOR

 18. Distinguish between identifiers and keywords.

         We usually assign names to places, people, objects, etc. in our day to day life, to identify them from one another. In C++ we use identifiers for this purpose. Identifiers are the user-defined words that are used to name different program elements such as memory locations, statements, functions, objects, classes etc. The identifiers of memory locations are called variables. The identifiers assigned to statements are called labels. The identifiers used to refer a set of statements are called function names

      The words (tokens) that convey a specific meaning to the language compiler are called keywords. These are also known as reserved words as they are reserved by the language for special purposes and cannot be redefined for any other purposes.

19. How are integer constants represented in C++? Explain with examples.

          Consider the numbers 1776, 707, -273. They are integer constants that identify integer decimal values. The tokens constituted only by digits are called integer literals and they are whole numbers without fractional part. The following are the characteristics of integer literals:

• An integer constant must have at least one digit and must not contain any decimal point.

 • It may contain either + or – sign as the first character, which indicates whether the number is positive or negative.


  •  number with no sign is treated as positive. 


    • No other characters are allowed.

   Example 70,714,-314 etc

20. Briefly describe different types of tokens.

       Tokens are the fundamental building blocks of the program. They are also known as lexical units. C++ has five types of tokens as listed below:

1. Keywords 2. Identifiers 3. Literals 4. Punctuators 5. Operators

         Keywords

            The words (tokens) that convey a specific meaning to the language compiler are called keywords. These are also known as reserved words as they are reserved by the language for special purposes and cannot be redefined for any other purposes.

 Examples do,for,while,int,float,break etc

          Identifiers

                Identifiers are the user-defined words that are used to name different program elements such as memory locations, statements, functions, objects, classes etc. The identifiers of memory locations are called variables. The identifiers assigned to statements are called labels. The identifiers used to refer a set of statements are called function names. While constructing identifiers certain rules are to be strictly followed for their validity in the program.

The rules are as follows:

 • Identifier is an arbitrary long sequence of letters, digits and underscores( _ ).

 • The first character must be a letter or underscore ( _ ).

 • White space and special characters are not allowed.

 • Keywords cannot be used as identifiers.

• Upper and lower case letters are treated differently, i.e. C++ is case sensitive.

 Examples for some valid identifiers are Count, Sumof2numbers, _1stRank, Main, Average_Height, FOR

          Literals

             Literals can be divided into four types as follows:

1. Integer literals

2. Floating point literals

3. Character literals

4. String literals

     Integer literals: Consider the numbers 1776, 707, -273. They are integer constants that identify integer decimal values. The tokens constituted only by digits are called integer literals and they are whole numbers without fractional part

      Floating point literals, also known as real constants are numbers having fractional parts. These can be written in one of the two forms called fractional form or exponential form.

 Example 314.42,52.0 etc

        When we want to store the letter code for gender usually we use ’f’ or ‘F’ for Female and ‘m’ or ‘M’ for Male. Similarly, we may use the letter ‘y’ or ‘Y’ to indicate Yes and the letter ‘n’ or ‘N’ to indicate No. These are single characters. When we refer a single character enclosed in single quotes that never changes its value during the program run, we call it a character literal or character constant.

examples '\n' 'a' 'Z' etc

     String constant is a set of characters enclosed inside double quotes.

     Examples "anu"    " "  etc
             
               Punctuators

       In languages like English, Malayalam, etc. punctuation marks are used for grammatical perfection of sentences. Consider the statement: Who developed C++? Here ‘?’ is the punctuation mark that tells that the statement is a question. Similarly at the end of each sentence we put a full stop (.). In the same way C++ also has some special symbols that have syntactic or semantic meaning to the compiler. These are called punctuators. Examples are: # ; ‘ “ ( ) [ ] { }

               Operators

      When we have to add 5 and 3, we express it as 5 + 3. Here + is an operator that represents the addition operation. Similarly, C++ has a rich collection of operators. An operator is a symbol that tells the compiler about a specific operation. They are the tokens that trigger some kind of operation. The operator is applied on a set of data called operands. C++ provides different types of operators like arithmetic, relational, logical, assignment, conditional, etc.

 21. Explain different types of literals with examples.

             Literals can be divided into four types as follows:

1. Integer literals

2. Floating point literals

3. Character literals

4. String literals

     Integer literals: Consider the numbers 1776, 707, -273. They are integer constants that identify integer decimal values. The tokens constituted only by digits are called integer literals and they are whole numbers without fractional part

      Floating point literals, also known as real constants are numbers having fractional parts. These can be written in one of the two forms called fractional form or exponential form.

 Example 314.42,52.0 etc

        When we want to store the letter code for gender usually we use ’f’ or ‘F’ for Female and ‘m’ or ‘M’ for Male. Similarly, we may use the letter ‘y’ or ‘Y’ to indicate Yes and the letter ‘n’ or ‘N’ to indicate No. These are single characters. When we refer a single character enclosed in single quotes that never changes its value during the program run, we call it a character literal or character constant.

examples '\n' 'a' 'Z' etc

     String constant is a set of characters enclosed inside double quotes.

        Examples "anu " "  "amount" "break"

 22. Briefly describe the Geany IDE and its important features.

          There are many compilers available like GCC, Borland C++, Turbo C++, Microsoft C++ (Visual C++), Unix AT & T C++, etc. Out of these, GCC is a free software available with Linux operating system. GCC stands for GNU Compiler Collection and is one of the popular C++ compilers which works with ISO C++ standard. GCC versions are available for Windows platforms also. Geany is a cross-platform IDE that works with GCC for writing, compiling and executing C++ programs.

Opening the edit window

 The edit window of Geany IDE can be opend from the Applications menu of Ubuntu Linux by proceeding as follows:

 Applications----> Programming-------> Geany

 To open a new file, choose File menu, then select New option or click New button on the toolbar.

    Saving the program

 Once a file is opened, enter the C++ program and save it with a suitable file name with extension .cpp. GCC being a collection of compilers, the extension decides which compiler is to be selected for the compilation of the code. Therefore we have to specify the extension without fail. If we give the file name before typing the program, GCC provides different colours automatically to distinguish the types of tokens used in the program. It also uses indentation to identify the level of statements in the source code.To save the program, choose File menu and select Save option or use the keyboard shortcut Ctrl+S. Alternatively the file can be saved by clicking the Save button in the toolbar.

Compiling and linking the program

The next step is to compile the program and modify it, if errors are detected. For this, choose Build menu and select Compile option.Alternatively, we can use the Compile button . If there are some errors, those errors will be displayed in the compiler status window at the bottom, otherwise the message Compilation finished successfully will be displayed.

Running/Executing the program

 Running the program is the process by which a computer carries out the instructions of a computer program. To run the program, choose Build menu and select Execute option. The program can also be executed by pressing the function key F5. The output will be displayed in a new window

Closing the IDE

 Once we have executed the program and desired output is obtained, it can be closed by selecting Close option from File menu or by clicking the X symbol in the active tab or in the title bar of the IDE. For writing another program, a new file can be opened by the New option from the File menu or by clicking the New button in the tool bar. The key combination Ctrl+N can also be used for the same. After developing program, we can come out of Geany IDE by choosing File menu and selecting Quit option. The same can be achieved by clicking the Close button of the IDE window or by using the key combination Ctrl+Q