Cute FTP Pro

Chapters 7 and 8 (Classes and Pointers)

Workshop Quiz


This is an evaluation test. Choose the answers and click on the "Submit" button to view your score. The correct answers with explanations will be shown after you take the test.

Q.) Predict the output:

void change( int *b, int n)
{
   int i;
   for( i = 0;  i < n; i++)
            {
         *(b+i) = *(b+i) + 5;
            }
}

int main( )
{
    int a[]={ 2,4,6,8,10 };
    int i;
    change(a,5);
    for( i = 0; i <= 4; i++)
    {
           cout<<“  ”<<a[i];
    }
return 0;
}

Answer.)

3  5  7  9  11
7  9  11  13  15
2  4  6  8  10
15  13  11  9  7

 

2. Q.)    What will the program below do?         

void func(int *x, int *y)
{
int *t;
t = x;
x = y;
y = t;
}

int main( )
{
int a=2;
int b=3;
func(&a,&b);
cout<<endl<<a<<" , "<<b;
return 0;
}

Answer.)

swap values of a and b.
will produce an error
Nothing
swap the addresses of a and b

 

3. Q.) What is the value of ‘sum’ in the program below:

int getNewValue(int x)
{
            static int div=1;
            return(x/++div);
}

int main( )
{
            int a[ ]={12,24,45,0,6};
            int i;
            int sum=0;
            for(i = 0; a[i]; i++)
            {
            sum+=getNewValue(a[i]);
            }
            cout<<sum;
            return 0;
}

Answer.)

program won't compile
24
25
26

 

4. Q.) What is the use of this function?

int len(char *str)
{
            int length=0;
            while(*str++!='\0')
            {
                        length++;
            }
            return length;
}

Answer.)

error
returns the number of characters in the string
returns one more than the length
returns one less than the length

 

5. Q.) Can you predict what will happen?

int main( )
{
int const max=5;
int ar[max]={1,2,3,4,5};
int *ptr=&ar[0];
for (int i=0;i<max;i++)
{
            *ptr=ar[i] + 5;
            ptr++;
}

for (int i=0;i<max;i++)
{
            cout<<endl<<ptr[i];
}
}

Answer.)

1 2 3 4 5
6 7 8 9 10
Garbage values
Compile-error

 

6. Q.) Will this compile?

void print(int &x)
{
            cout<<endl<<"Reference";
}

void print(int x)
{
            cout<<endl<<"By value";
}

 int main( )
{
            int y=5;
            print(y);
            return 0;
}

Answer.)

Yes
No
Depends on compiler

7. Q.) Is there an error? What would be the output?

class dummy
{
private:
            int data;
public:
            int get_data( )
            {
                        return data;
            }
};

int main( )
{
dummy d1;
cout<<d1.get_data( );
return 0;
}

Answer.)

Compile-time error
Output is 0.
Run-time error
Output will be a garbage value.

 

8. Q.) Will the following code compile? If it will, what is the output? 

int increment(int &x)
{
            x++;
            return x;
}

 int main ( )
{
int y=5;
increment(y)=2;
cout<<y;
return 0;
}

Answer.)

Runtime error
Compile error
Output is 2.
Output is 3.

 

9. Q.) By changing the increment function will it work?

int& increment(int &x)
{
            x++;
            return x;
}

 int main ( )
{
int y=5;
increment(y)=2;
cout<<y;
return 0;
}

Answer.)

Runtime error
Compile error
Output is 2.
Output is 3.

 

10. Q.) What is the output? 

class bacteria
{
public:
            static int count;

public:               //multiple use of public
            void modify( )
            {
              count=count+1;
              cout<<endl<<"The new count is : "<<count;
            }
};

int bacteria::count=5;                

 int main( )
{
            bacteria b;
            bacteria::count=1;
            b.modify( );
            return 0;
}

Answer.)

Compile error.
5
1
2

If you didn't get the solutions pop-up window then click here for the solutions page.


Go back to the Contents Page


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