Pageviews

Friday 12 July 2019

DEL +2 ] 1.Review of C++ programming - Solved questions from text book



                                        Chapter 1.Review of C++ programming

(+2 Computer Application , Text book Questions and Answers)


1.What is meant by token in C++?

          Tokens are the basic building blocks of a C++ program. There are five types of tokens in C++.

The are Keywords,literals,punctuators,operators&identifiers.

2.  Read the following tokens and identify the type of token to which each of these belongs:


a. number

b. 23.98

c. "\0"

d. cin

e. ++

f. void

g. '\\'

h. ;

i. =

j. a
Ans. 
     
a. identifer

b. floating point literal

c. string literal
   
d.  keywod

e.  increment operator

f. keyword

g. character literal

h. punctuators

i.  assignment operator

j.identifier


3.  What is the role of #include statement in C++ program?

  # is a pound sign which helps to pre-process the program before the compilation &include is a simple directive that tells pre-processor to include the library's data(i.e.function declarations). You might have seen, the number of line compiled are more than that of actual written code.

4.  What is wrong in the statement: cin>>25; ?

         The first operand is the pre-defined identifier cin that identifies keyboard as input object. The second operand is strictly a variable. We can use more than one variable in the same statement to receive more than one input. The following are valid examples:

cin>>r;
cin>>a>>b>>c;

5.  List the type modifiers of C++.

          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 the selection statements of C++?

     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”’     

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

7.  What are the four components of a loop?

     Loop statements usually have four components: initialization (usually of a loopcontrol variable), continuation test on whether to do another iteration, an update step, and a loop body.

8.  Give examples for entry-controlled loop.

    for loop,while loop

9.   Which control transfer statement is equivalent to the conditional operator (?:)?
   
       if-else statement.

10.   All switch statements can be replaced by any form of if statement. State whether it is true or false.

     Ans: False. Reason: In some cases switch statement can't replaced by if...else.

11.  What is wrong with the following code segment?

 for (short i=1; i<5; ++i)
 for (i=5; i>0; --i) 
cout<<i<<"\t"; 

12. Distinguish between break and continue statements

   Break statement is used to terminate a loop or switch statement. 
 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.

13. The default case in switch is equivalent to the else block of else if ladder. Justify this statement with the help of example. 

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

Corresponding switch statement

Switch(percentage)
 case

14.  What will be the output of the following code fragment? 

for (outer=10; outer>5; --outer)
 for (inner=1; inner<4; ++inner) 
cout<<outer<<"\t"<<inner<<endl; 

Ans. 10 9 8 7 6
          1 2 3

15.  Write a program to produce the following output using nested loop:

 A
 A B
 A B C
A B C D
A B C D E

#include<iostream.h>
#include<conio.h>

int main()
{
char ch='A';
int n;
cout<< "\n Enter the height of the triangle :";
cin>> n;
for(int i=1; i< = n ; i++)
{
ch = 'A';
for(int j = 1; j< = i; j++)
{
cout<< ch<< " ";
ch++;
}
cout<< "\n";
return 0;
}


16. Read the following C++ code snippet:

for (n=1; n<5; ++n)

cout<<i;
if (i==2)
continue;
if (i%3==0)
break;
cout<<"Hello";
}

Choose the correct output of this code from the following:

a. 1Hello2Hello3Hello4Hello

b. 1Hello2Hello3

c. 1Hello23

d. 1Hello23Hello 

Ans d


17.  Read the following C++ code segment and replace it with a looping statement:

cin>>n; 
loop: r=n%10; 
s=s*10+r; 
n=n/10; 
if (n!=0) goto loop;
 cout<<s;

Ans
cin>>n;
for(int n=1;n!=0;n=n/10)
r=n%10;
s=s*10+r;
cout<<s;

18. Which of the following is not a character constant in C++? 

a. '\t' 
b. 'a'
 c. '9'
 d. '9a' 
Ans. d

19. Some of the following identifiers are invalid. Identify them and give reason for the invalidity.

 a. unsigned
 b. cpp
 c. 2num
 d. cout 

unsigned is not an identifier.This is a keyword.
2num is not an identifier. Starting with digit is not allowed in program

20.  What happens if break is not used in switch statement?

Switch case statements are used to execute only specific case statements based on the switch expression. If we do not use break statement at the end of eachcase, program will execute all consecutive case statements until it finds next break statement or till the end of switch case block.