Pageviews

Wednesday 3 July 2019

+2] 1. Review of C++ Programming - Previous Questions Chapter wise

PLUS TWO COMPUTER APPLICATION

Second  Year Computer Application(Commerce) Previous Questions Chapter wise ..

                                         Chapter 1: Review of C++ Programming


1.Which among the following is an insertion operator ?
  • (a) << 
  • (b) >> 
  • (c) < 
  • (d) >
Ans. a

2. …………… is an exit control loop.
  • a)for loop 
  • b)while loop 
  • c)do …while loop 
  • d)break
Ans. C.

3. ………………. Statement in a loop forces the termination of that loop
Ans. Continue

4. ….. …… … operator is the arithmetic assignment operator ?
  • (a) >> 
  • (b) ++ 
  • (c) += 
  • (d) = 14
Ans. C

5. Which one of the following is equivalent to the statement series b=a,a=a+1
  • a). b + = a 
  • b) b= a ++ 
  • c). b = ++ a 
  • d) b + = a + b;
Ans. b

6. Identity the following C++ tokens
  • (a) "welcome" 
  • (b) int 
  •  (c) >= 
  •  (d) ++
Ans : “Welcome” is a string literal
           Int is a keyword
           >= is a relational operator
          ++ is a increment operator

7. What are the main components of a looping statement ?
             
Ans : 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. .Explain switch statement with an example?
Ans : 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”;

}


9. How does a ‘goto’ statement work ?

Ans : 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;


10. How do continue and break statement differ in a loop ?Explain with an example.

Ans : 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.

Break statement is used to terminate a loop or switch statement.

Example: continue statement inside for loop
#include <stdio.h>
int main()
{
   for (int j=0; j<=8; j++)
   {
      if (j==4)
      {
            /* The continue statement is encountered when
             * the value of j is equal to 4.
             */
            continue;
       }

       /* This print statement would not execute for the
        * loop iteration where j ==4  because in that case
        * this statement would be skipped.
        */
       printf("%d ", j);
   }
   return 0;
}

Output

0 1 2 3 5 6 7 8

Value 4 is missing in the output, why? When the value of variable j is 4, the program encountered a continue statement, which makes the control to jump at the beginning of the for loop for next iteration, skipping the statements for current iteration.

Example – Use of break in a while loop
#include <stdio.h>
int main()
{
     int num =0;
     while(num<=100)
     {
        printf("value of variable num is: %d\n", num);
        if (num==2)
        {
            break;
        }
        num++;
     }
     printf("Out of while-loop");
     return 0;
}

Output:

value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop
 
11. Write a C++ program to accept a string and count the number of words And Vowels in that string.

 
1.     #include <iostream>
2.  using namespace std;
3.   
4.  int main()
5.  {
6.      char line[150];
7.      int vowels, consonants, digits, spaces;
8.   
9.      vowels =  consonants = digits = spaces = 0;
10. 
11.    cout << "Enter a line of string: ";
12.    cin.getline(line, 150);
13.    for(int i = 0; line[i]!='\0'; ++i)
14.    {
15.        if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
16.           line[i]=='o' || line[i]=='u' || line[i]=='A' ||
17.           line[i]=='E' || line[i]=='I' || line[i]=='O' ||
18.           line[i]=='U')
19.        {
20.            ++vowels;
21.        }
22.        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
23.        {
24.            ++consonants;
25.        }
26.        else if(line[i]>='0' && line[i]<='9')
27.        {
28.            ++digits;
29.        }
30.        else if (line[i]==' ')
31.        {
32.            ++spaces;
33.        }
34.    }
35. 
36.    cout << "Vowels: " << vowels << endl;
37.    cout << "Consonants: " << consonants << endl;
38.    cout << "Digits: " << digits << endl;
39.    cout << "White spaces: " << spaces << endl;
40. 
41.    return 0;
42.}

Output


 
Enter a line of string: This is 1 hell of a book.
Vowels: 7
Consonants: 10
Digits: 1
White spaces: 6
 12. define the term keyword in C++, also give an example?
      
     Keywords are tokens that carry a specific meaning to the language compiler.  
 Eg. int, switch etc.. 

13. Define jump statement?

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. 

14.  Write the output for the code given below

    for(n=1;n<10;++n)
{
   cout<<"HAI";
   if(n==4)
    break;
   cout<<n;
}

output

15. Explain about nested loops

Placing a loop inside the body of another loop is called nesting of a loop. In a nested loop, the inner loop statement will be executed repeatedly as long as the condition of the outer loop is true.
Here the loop control variables for the two loops should be different.

16. Define jump statement.explain give any two.
     
          Anwsers are QS in 13 and 10.

17.   Re- write the following C++ code using  “ switch ‘’ statement.                                         
 cin >> pcode;                                               
  if(pcode == ‘C’ )
 cout<< “Computer”;                                                 
 else  if(pcode == ‘M’ )                                                             
 cout<< “Mobile Phone”;                                                         
else  if(pcode == ‘L’ )                                                                     
 cout<< “Laptop”;                                                                 
else                                                                     
 cout<<  “ Invalid Code”;

correct code

switch(pcode)
{
case C:
            cout<<"Computer";
 break;
case M:
             cout<<"Mobilephone";
break;
case L:
             cout<<"Laptop";
break;
default: cout<<"Invalid Code";