Vous êtes sur la page 1sur 12

In-service Course Jaipur

Question Bank
Pointers

Late Bloomers
Question 1. Describe C++ memory map when a program is executing. Highlight the role and
use of Heap area.

Ans 1. When a program is executed, C++ creates four logically distinct regions of memory:
(i) area to hold the compiled program code
(ii) area to hold global variable
(iii) the stack are to hold the return addresses of function calls, arguments passes to the
functions, local variables for functions, and the current state of the CPU.
the heap area from which the memory is dynamically allocated to the program.

Question 2 What is pointer arithmetic? How is it performed? Support your answer


with example.
Only two arithmetic operations, addition ad subtraction, may be performed on
pointers. In pointer arithmetic, all pointers increase and decrease by the length of the
data type they point to.For example,
int *p; p++;
If address of p is 1000, then p++ statement will increase p to 1002, not 1001.

Find the output of the following code.

#include<iostream.h>
#include<conio.h>
int *iptr;
char *cptr;
float *fptr;
void main()
{
clrscr();
cout<<"size of integer pointer is :"<<sizeof(iptr)<<endl;
cout<<"size of character pointer is :"<<sizeof(cptr)<<endl;
cout<<"size of float pointer is :"<<sizeof(fptr);
}

output
size of integer pointer is : 4
size of character pointer is : 4
size of float pointer is : 4
Question 3. Given the following definitions:
int ival=2048; int *iptr; double *dptr;
which of the following assignment, if any, are illegal? Explain why.
(a) ival=*iptr (b) *iptr=ival;
(c) *iptr=&ival (d) dptr=iptr;
(e) ival=iptr; (f) iptr=ival;'
(g) iptr=&ival; (h) dptr=*iptr;

Answer 3
a)legal assignment.
(b) legal assignment.
(c)illegal assignment, cannot assign a address of normal variable to pointer variable.
(d) illegal assignment, cannot assign a integer pointer to a double pointer.
(e) illegal assignment, cannot assign pointer variable to a normal variable.
(f) illegal assignment, cannot assign normal variable to a pointer variable.
(g) legal assignment.
illegal assignment, cannot assign a integer pointer to a double pointer

Question 4 Given the following set of variable definitions:


int
*ip1,ip
2; char
ch,*cp;
which of the following assignment are type violations? Explain why.
(a) ip1="Smile Always"; (b) cp=0;
(c) ip1=0; (d) cp=&'a';
(e) ip1=ip2; (f) cp='\0';
(g) ip1='\0'; (h) cp=&ch;
(i) *ip=ip2;

Answer 4 (a)Type violation, cannot assign string to integer pointer.


(a) Correct
(b) Correct
(c) Type violation, cannot assign string with ‘&’ operator.
(d) Type violation, cannot assign normal variable to a pointer variable.
(e) Correct
(f) Correct
(g) Correct
Type violation, variable ‘*ip’ is undefined.
Questions for Average Learners

1. Showing the use of pointers.

#include <iostream.h>

int main ()

int first, second;


int * mypointer;
mypointer = &first;
*mypointer = 1000;
mypointer = &second;
*mypointer = 200;
cout << "first value is " << first << endl;
cout << "second value is " << second << endl;
return 0;
}

output

first value is 100


second value is 200

2. Find the output of the following if the address of n is 200.

float l=7;
int *ptr=&l;
cout<<ptr << endl;
cout<<ptr+1 << endl;
cout<<ptr +2<< endl;

output:

200
204
208

3. Find the output


#include<iostream.h>
int one[]={1,2,3};
void main()
{
int *ptr;
ptr=one;
cout<<*ptr<,endl;
ptr++;
cout<<*ptr
}
output
1
2

4. Find the output

#include <iostream.h>
void main()
{
int a[10];
cout <<”Enter the elements of the array :”;
int sum = 0;
for (int *p=a ; p<a+10; p++)
{
cout<<”Enter the element : “;
cin>>a[i];
sum += *p;
}
cout<< “ Sum of all the elements of the array is :”<< sum
}

5. Write the output of the following :


(Assume that the base address of the array number is 5020)

#include <iostream.h>
int main()
{
int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
cout << "An integer occupies " << sizeof(int) << " bytes\n";
cout << "\n Number: " << Number;
cout << "\n&number[0]: " << &number[0] << endl;
cout << "\n Number+1: " << Number+1;
cout << "\n&Number:[1] " << &number[1] << endl;
cout << "\n Number+2: " << Number+2;
cout << "\n&Number:[2] " << &number[2] << endl;
return 0;
}
This would produce:
Output :
An integer occupies 4 bytes
Number: 5020
&number[0]: 5020
Number+1: 5024
&Number:[1] 5024
Number+2: 5028
&Number:[2] 5028

6. To count length of the string

#include<iostream.h>
#include<stdio.h>
void main()
{
int count=0;
char *p;
cout<<”Enter the string “;
gets(p);
for ( ; *p!='\0'; *p++)
{ count++; }
cout<<”The length of the string is :”<< count;
getch();
}

Output
Enter the string : kvs
Length of the string is : 3
7. To count the number of vowels in a string

#include<iostream.h>
#include<conio.h>
const int size = 7;
char str[size] = "Computer Science";
int vowels = 0;
for (char *Ptr = str; Ptr < str + size; Ptr++)
{
switch (*Ptr)
{ case 'A':
case 'a':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': vowels++; break;
}
}
cout << str << " has " << vowels << " vowels" << endl;
}
output
Computer Science has 6 vowels

8. Write a program to read the integer in dynamic variable ,calculates and display
its square .

# include<iostream.h>
main()
{
int *p;
p = new int;
cout<<”\n Enter an integer : “;
cin >> *p;
cout<< “\nSquare of the number is : “<<*p * * p;
}

Output
Enter an integer : 5
Square of the number is : 25
Questions for Brilliant learners

1. #include<string.h>
#include<ctype.h>
void main()
{ int i;
char *p=”Student”;
char c;
for (i=1;i<6;i++)
{ if (i%2==0)
++p;
}
c=*p;
cout<<c; }
}
(a) d (b) S (c) u (d) None of these
Ans (c)

2. . Find the output of the following program :


#include<iostream.h>
void main()
{
int Numbers[] = {12,4,10,8};
int *ptr = Numbers;
for (int C = 0; C<3; C++)
{
cout<< *ptr << “@”;
ptr++;
}
cout<<endl;
for(C = 0; C<4; C++)
{
(*ptr)*=2;
--ptr;
}
for(C = 0; C<4; C++)
cout<< Numbers [C]<< “#”;
cout<<endl;
}
Ans 2@4@8@

4 # 8 # 16 # 20 #

3. Find the output of the following program code :

(i) char *s1 , *s2;


s1 = “BUTTER”;

strcpy(s1 , strncpy( s1 , s2 , 3));

strnset( s1 , 77 , 3);

cout<<s1;

Ans MMMTER

4. Write the output of the follwoing program segment


#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
char *NAME="ComPUteR";
clrscr();
for(int x=0;x<strlen(NAME);x++)
{
if(islower(NAME[x]))
NAME[x]=toupper(NAME[x]);
else
if(isupper(NAME[x]))
if(x%2==0)
NAME[x]=tolower(NAME[x]);
else
NAME[x]=NAME[x-1];
}
puts(NAME);

Ans cOMMuTEE

5. What will be the output of the following program :


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

void changestring(char text[], int &counter)


{
char *ptr = text;
int length=strlen(text);
for(;counter<length-2;counter+=2,ptr++)
{
*(ptr+counter) = toupper(*(ptr+counter));
}
}
void main()
{
clrscr();
int position = 0;
char message[]= "Mouse Fun";
changestring(message,position);
cout<<message<< "@" <<position;
}

Ans MouSe Fun@8

6. Find the output of the following program:


#include<iostream.h>
#include<string.h>
class student
{ char *name;
int I ;
public:
student( ) {I =0; name=new char [ I +1]; }
student (char *s)
{ I =strlen(s); name=new char[I+1];
strcpy (name,s);
}
void display( ) {cout<<name<<endl;}
void manipulate(student & a, student & b)
{ I = a.I + b.I;
delete name;
name=new char[I+1];
strcpy(name, a.name);
strcat(name, b.name);
}
};
void main( )
{ char * temp = “Jack”;
student name1 (temp), name2(” Jill”), name3(”John”),S1,S2;
S1 .manipulate (name1, name2);
S2.manipulate (S1, name3);
S1.display ( );
S2.display ( );
}

Ans
JackJill
JackJillJohn

7. Give the output of the following program


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
main()
{
clrscr();
char *name,*name1;
int l=0;
name=”Windows98";
l = strlen(name);
cout<<endl;
for (intasc=90;asc>=65;asc—)
{
for(inti=0;i<l;i++)
{
if (name[i]==char(asc) || (name[i]==char(asc+32)))
cout<<name[i];
}
}
cout<<endl;
getch();
return 0;
}
(8)Give the output of the following program :
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
main()
{
clrscr();
char *name,ans;
int l=0,count=0,max=0;
name=”Multimedia”;
l = strlen(name);
cout<<endl;
for(int a=0;a<l;a++)
{
for (int b=0;b<l;b++)
{
if (name[a]==name[b] && name[a]!=’ ‘)
count = count+1;
}
if (max<count)
{
max=count;
ans=name[a];
}
count=0;}
cout<<ans<<“ “<<max<<endl;
getch();
return 0;
}
(9)Give the output of the following program :
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
main()
{
clrscr();
char *name;
int l=0,c=0;
name=”the computers in the city of the nawabs”;
l = strlen(name);
cout<<endl;
for(inti=0;i<=l;i++)
{
if ((name[i]==’t’) && (name[i+1]==’h’) && ((name[i+2])==’e’))
c=c+1;}
cout<<endl;
cout<<c;
getch();
return 0;
}

Vous aimerez peut-être aussi