Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Pointers
Exam Practice
Short Answer Type Questions [2/3 Mark]
Question 1:
Obtain the output from the following C++ program as expected to appear on the screen after its execution. All India 2014,2013
Important Note
* All the desired header files are already included in the code, which are required to run the code.
void main() { char *Text = "AJANTA"; int *P, Num[] = {11,5,7,9}; P = Num; cout<<*P<<Text<<endl; Text++; P++; cout<<*P<<Text<<endl; }
Аnswer:
Output
1 AJANTA
5JANTA
Question 2:
Give the output of the following program segment: (Assuming all desired header file(s) are already included) Delhi 2013C
void main() { float *Ptr,Points[]={20,50,30,40,10}; Ptr=Points; cout<<*Ptr<<endl; Ptr+=2; Points[2]+=2.5; cout<<*Ptr<<endl; Ptr++; (*Ptr)+=2.5; cout<<Points[3]<<endl; }
Аnswer:
Output
20
32.5
42.5
Question 3:
Find the output of the following program: Delhi 2012
#include<iostream.h> #include<ctype.h> typedef char Txt80[80]; void main() { char *PText; Txt80 Txt = "Ur2GReAt"; int N = 6; PText = Txt; whi1e(N >= 3) { Txt[N] = isupper(Txt[N])? tolower(Txt[N]):toupper(Txt[N]); cout<<PText<<endl; N--; PText++; } }
Аnswer:
Output
Ur2GReat
r2GREat
2GrEat
grEat
Question 4:
Find the output of the following program: Delhi 2009C
#include<iostream.h> struct Score { int Year; float topper; }; void change(Score *s,int x=20) { s->topper = (s->topper+25)-x; s->Year++; } void main() { Score Arr[] = {{2007, 100},{2008,95}}; Score *Point = Arr; change(Point,50); cout<<Arr[0].Year<<"#"<<Arr[0].topper<<endl; change(++Point); cout<<Point->Year<<"#"<<Point->topper<<endl; }
Аnswer:
Output
2008#75
2009# 100
Question 5:
Find the output of the following program: All India 2011
#include<iostream.h> void main() { int Moves[] = {11, 22, 33, 44},*Queen; Queen = Moves; Moves[2] += 22; cout<<"Queen @"<<*Queen<<endl; *Queen -= 11; Queen += 2; cout<<"Now@"<<*Queen<<endl; Queen++; cout<<"Finally@"<<*Queen<<endl; cout<<"New Origin@"<<*Moves[0]<<endl; }
Аnswer:
There is an error in last line, i.e. *Moves[0], so the program will not run. In case, if there is no subscript in Moves pointer, then the output would be:
Queen @ 11
Now@55
Finally@44
New Origin@0
Question 6:
Find the output of the following program: Delhi 2009
#include<iostream.h> void main() { int X[] = {10, 25, 30, 55, 110}; int *p = X; while(*p<110) } if(*p%3 != 0) *p = *p+l; else *p = *p+2; P++; } for(int I=4;I>=1;I--) { cout<<X[I]<<"*"; if(I%3 == 0) cout<<endl; } cout<<X[0]*3<<endl; }
Аnswer:
Output
110*56*
32*26*33
Question 7:
What will be the output? All India 2008
#include<iostream.h> #include<conio.h> void main() { static int a[]={0,1,2,3,4}; int *p[]={a,a+1,a+2,a+3,a+4}; int **ptr=p; ptr++; cout<<ptr-p<<" "<<*ptr-a<<" "<<**ptr<<endl; *ptr++; cout<<ptr-p<<" "<<*ptr-a<<" "<<**ptr<<endl; *++ptr; cout<<ptr-p<<" "<<*ptr-a<<" "<<**ptr<<endl; ++*ptr; cout<<ptr-p<<" "<<*ptr-a<<" "<<**ptr<<endl; getch(); }
Аnswer:
Output
1 1 1
2 2 2
3 3 3
4 4 4
Question 8:
Find the output of the following program:
#include<iostream.h> void main() { int Numbers[] = {2,4,8,10}; 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=1;c<4;c++) cout<<Numbers[c]<<"#"; cout<<endl; }
Аnswer:
Output
2@4@8@
8#16#20#
Question 9:
What will be the output of the following program?
#include<iostream.h> #include<string.h> class state { char *state_name; int size; public: state() { size = 0; state_name = new char[size+1]; } state(char *s) { size = strlen(s); state_name = new char[size+1]; strcpy(state_name,s); } void display() { cout<<state_name<<endl; } void Replace(state &a,state &b) { size = a.size + b.size; delete state_name; state_name = new char[size+1]; strcpy(state_name,a.state_name); streat(state_name,b.state_name); } }; void main() { char *temp = "Delhi"; state statel(temp),state2("Mumbai"),state3("Nagpur"),S1,S2; S1.Replace(state1, state2); S2.Replace(S1,state3); S1.display(); S2.display(); }
Аnswer:
There is semicolon(;) after state (char *s ); which will result into syntax error.In case, there is no semicolon(;) after state(char *s),then the output will be:
DelhiMumbai
DelhiMumbaiNagpur
Question 10:
What will be the output of the following program?
#include<iostrearn.h> #includeCctype.h> #include<conio.h> #include<string.h> void ChangeString(char Text[],int SCounter) { char *Ptr = Text; int Length = strlen(Text); for(;Counter<Length-2;Counter+=2;Ptr++) { *(Ptr+Counter)=toupper(*Ptr+Counter); } } void main() { int Position = 0; char Messaged = "Pointers Fun"; ChangeString(Message,Position); cout<<Message<<"@"<<Position; }
Аnswer:
Output
PoiQteMs Wun |@10
Question 11:
Give the output of the following program segment. (Assume all required header files are included in the program).
#include<iostream.h> void main() { int a = 32,*X = &a; char ch = 65,&eco = ch; eco += a; *X += ch; cout<<a<<','<<ch<<endl; }
Аnswer:
Output
129, a
Question 12:
Identify the syntax error(s), if any, in the following program. Also, give reason for errors.
void main() { const int i = 20; const int *const ptr = &i; (*ptr)++; int j=15; ptr = &j; }
Аnswer:
In the given program, there are two errors:
(*ptr) + + cannot modify const object
ptr = &j cannot modify const object
Question 13:
Give the output of the following program (assume all required header files are include in the program).
void main() { int array[] = {2,3,4,5}; int *arptr = array; int value = *arptr; cout<<value<<"\n"; value = *arptr++; cout<<value<<"\n"; value = *arptr; cout<<value<<"\n"; value = *++arptr; cout<<value<<"\n"; }
Аnswer:
Output
2
2
3
4
Question 14:
Give the output of the following program segment (assume all required header files are included in the program).
void main() { char *s = "GOODLUCK"; for(int x=strlen(s)-1;x>=0;x--) { for(int y=0;y<=x;y++) cout<<s[y]; cout<<endl; } }
Аnswer:
Output
GOODLUCK
GOODLUC
GOODLU
GOODL
GOOD
GOO
GO
G
Question 15:
Give the output of the following program segment (assume all required header files are include in the program)
void main() { char *NAME = "a ProFile"; for(int x=0;x<strlen(NAME);x++) if(islower(NAME[x])); else if(isupper(NAME[x])) if(x%2 != 0) NAME[x] = tolower(NAME[x-1]); else NAME[x]--; cout<<NAME<<endl; }
Аnswer:
a Orooile
Question 16:
Find the output of the following program:
#include<iostream.h> #include<conio.h> void main() { int *PointerArray[10]; int marks[]={75,68,90,34,0,10,90,65}; for(int I=0;marks[I]!=0;I++) { PointerArray[I]=&marks[I]; *(PointerArray[I])+=5; } int index=0; while(index<I) { int p=*(PointerArray[index]); if(p>60) cout<<p<<","; index++; } getch(); }
Аnswer:
Output
80, 73, 95,
Question 17:
A C++ program contains following declaration: static int x[5] = {5, 20, 30, 50, 70};
- What is the meaning of x?
- What is the meaning of (x + 2)?
- What is the value of *x?
- What is the value of (*x + 2)?
- What is the value of *(x + 2)?
Аnswer:
Let us assume the base address or the starting address of the array as 1000, i.e
Here, each box is taking 2 bytes because array contains integer type variables and int takes 2 bytes.
- x represents an array of size 5 and also the base address of array, i.e. 1000.
- It will increase the value of x by 4 bytes, because x + 2 = x + (size of int) *2 = 1000+2*2 =1004
- *x means value at the address contained in x[0], i.e. 5.
- (*x+ 2)— *(1000)+ 2 [because *(dercfcrcnce operator) has higher priority than binary operators] =5+2=7
- *(x+ 2)= *(1000+ 2x 2)= *(1004)= 30
Question 18:
Give the output of the following program:
void main() { char *NAME="CoMPutER"; 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] = to!ower(NAME[x]); else NAME[x] = NAME[x-1]; puts(NAME); }
Аnswer:
Output
cOmmUTee
Question 19:
Write the output of the following program:
#include<iostream.h> static int i = 100; void Print(char *p) { p = "Pass"; cout<<"Value is "<<p<<endl; } void main() { char *q = "Best of Luck"; Print(q); cout<<"New value is"<<q; }
Аnswer:
Output
Value is Pass
New value is Best of Luck
Question 20:
Give the output of the following program:
void main() { char *Text[2] = {"Two", "six"}; for(int C=0;C<2;C++) { for(int D=0;D<strlen(Text[0]);D++) if(islower(Text[C][D])) cout<<(char)toupper(Text[C][D]); else if(isupper(Text[C][D])&&(D%2=0)) cout<<(char)tolower(Text[C][D]); else cout<<Text[C][D]; cout<<D<<endl; }
Аnswer:
Output
tW03
SIX3
Question 21:
Find syntax error(s), if any, in the following program:
#include<iostream.h> main() { int x[5],*y,z[5] for(i-0;i<5;i++) { x[i] = i; z[i] = i+3; y = z; x = y; } }
Аnswer:
#incl udeCiostream.h> main() { int x[5], *y, z[5] //Errorl for(i=0;i<5;i++) //Error2 { x[i]=i; z[i]=i+3; y=z; x=y; } } Error 1 Semicolon missing (;) Error 2 Declaration syntax error, i is not declared
Computer ScienceChapterwise Question Bank for Computer ScienceNCERT Solutions