Exercises II (Unit 4 to 6)


Q.) Explain the following code snippet: 

switch(choice)
{
            case 'y':
            case 'Y':
                        cout<<"\nYou entered yes";
                        break;

            case 'n':
            case 'N':
                        cout<<"\nYou entered no";
                        break;
            default:
                        cout<<"\nWrong choice";
                        break;
}

A.) This is an illustration of a fall-through switch case. If the value of choice is ‘y’ or ‘Y’, the same set of statements will be executed:

            cout<<"\nYou entered yes";
            break;

The programmer needn’t code separately for case ‘y’ and case ‘Y’ since in either case the same set of statements have to be executed.


Q.)       If an array is initialized as shown below:
            int ar[5]={1,2,3};
            What will be the value for ar[3]?

A.) Array elements will be initialized to 0. Hence ar[3] and ar[4] will be 0.


Q.)                   int ar[5];
            What will be the value for ar[3]?

A.) All the array elements will have unpredictable values (commonly called garbage values).


Q.) Will the following code work:

            int ar[5]={1,2,3};
            for (int i=0;i<5;i++)
            {
                        cout<<i[ar];
            }

 A.)   i[ar] is like writing 1[ar], 2[ar] etc. This is legal and i[ar] is equivalent to ar[i]. Thus the output will be: 12300

Generally this is not used in programs because it can lead to confusion and this is not a good practice. Why this works is explained in Pointers.


Q.) In the below structure how can we access ‘x’?

             struct st1 {struct{struct{int x;} st2;} st3;}y;

 A.)   This might seem really confusing. First of all just rewrite the coding as shown below:

 struct st1
{
      struct
      {
                 struct
                  {
                             int x;
                  } st2;
      } st3;
}y;

 Now it might not seem all that complicated. To access ‘x’ you should go from the outermost structure to the innermost one (i.e. from ‘y’ to ‘st2’). So we can access ‘x’ by using:

            y.st3.st2.x


Q.) Can you predict what will happen?

#include <iostream.h>
int main( )
{
unsigned int j=2;
cout<<j--;
while(j>=0)
{
    cout<<j--;
}
return 0;
}

 A.) The program will run forever because ‘j’ is an unsigned integer and thus it will never become negative.


Q.) Why can’t we make main ( ) as a recursive function?

A.)  C++ doesn’t permit the program to call the main ( ) function.


Q.) Is the following piece of code valid? 

int main( )
{
            int array1[]={1,2,3};
            int array2[]={10,20,30};
            array2=array1;
            return 0;
}

 A.)  You will get a compilation error because you cannot assign arrays using the = operator.


Q.) What is the result of the code given below: 

static int x=2; 

int main( )
{
int sum=0;
do
{
    sum+=(1/x);
}while(0<x--);
return 0;
}

 A.)  A run-time error (divide by zero error).


Q.) What would be the output for the following code: 

int main( )
{
            int i=5; int j=6; int k=7;
            int result;
            result = j>k ? 2 : i>j ? 3 : 4;
            cout<<result;
            return 0;
}

 A.)  Answer is 4. Conditional operator has associativity from right to left. Thus start simplifying from the rightmost side of the expression. The conversion would be as below:

j>k ? 2 : i>j ? 3 : 4; 

For the part i>j ? 3 : 4; the result will be 4. Now plug this into the original problem and you will get: 

j>k ? 2 : 4; 

The result of this is 4 (because j is not greater than k).


Q.) What does the code fragment yield:

             const int SIZE = 5;
            int array1[SIZE]={1,2,3,4,5};
            cout<<array1[2,3];

A.) In some other programming languages, this notation is used to access elements of a multi dimensional array. But in C++ the expression within the square bracket is evaluated as:

            2,3

the result of which is 3 (the comma operator principle is applied here). Thus the equivalent statement is:

            cout<<array[3];


Q.) Will the following code compile? 

int main( )
{
            int rad;
            cout<<"\nEnter the radius:";
            cin>>rad;
            if(rad>0)
            {
                        double area=3.14*3.14*rad;
                        cout<<area;
            }
return 0;

A.) Yes. In C++ we are allowed to declare variables anywhere in the code (it is not mandatory that all variables have to be declared in the beginning).


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved. Sign my guestbook.