C++ Theory questions


1A Questions ( explanation with example compulsory for all theory questions )

a)    State the difference between runtime error and syntax error
b)    State the difference between logical error and syntax error
c)    State the difference between syntax error and semantic error
d)    State the difference between local prototype and global prototype
e)    State the difference between pre tested loop and post tested loop
f)     State the difference between while and do while / entry controlled and exit controlled loop
g)    State the difference between switch and if else
h)   What happens when the break is not given in switch case
i)     State the difference between call by value and call by reference
j)      State the difference between actual parameter and formal parameter
k)    State the difference between function returning reference and function returning pointer
l)     State the difference between default argument and constant argument
m)  State the difference between implicit and explicit type casting
n)   State the difference between array and structure
o)    State the difference between structure and class
p)    State the difference between local and global scope  /  local variable and global variable
q)    State the difference between pointer and reference
r)     State the difference between post increment and pre increment operators
s)    State the difference between unary and binary operators
t)     State the difference between binary and ternary operators
u)   State the difference between ternary operator and if statement
v)    State the difference between break and continue
w)   State the difference between break and exit
x)    State the difference between break and goto
y)    State the difference between fundamental data type and derived data type
z)    State the difference between datatype and modifier


2. STRUCTURES

1. What is the difference between #define and const? Explain with suitable example.
2 What is the purpose of using a typedef command in C++? Explain with suitable example.
3 Illustrate the use of #define in C++ to define a macro.
4 Rewrite the following program after removing the syntactical error(s), if any.Underline each correction.
#include<iostream.h>
void main( )
{
struct STUDENT
{
char stu_name[20];
char stu_sex;
int stu_age=17;
}student;
gets(stu_name);
gets(stu_sex);
}
5) What are Nested Structures? Give an example.
6) Rewrite the following program after removing the syntactical error(s), if any. Underline each correction.
#include<iostream.h>
void main( )
{
struct movie
{
char movie_name[20];
char movie_type;
int ticket_cost=100;
}MOVIE;
gets(movie_name);
gets(movie_type);
}

7) Find the output of the following program:
#include<iostream.h>
struct MyBox
{
int Length,Breadth,Height;
};
void Dimension(MyBox M)
{
cout<<M.Length<<.x.<<M.Breadth<<.x.;
cout<<M.Height<<endl;
}
void main( )
{
MyBox B1={100,150,50},B2,B3;
++B1.Height;
Dimension(B1);
B3=B1;
++B3.Length;
B3.Breadth++;
Dimension(B3);
B2=B3;
B2.Height+=5;
B2.Length--;
Dimension(B2);
}

8) Find the output of the following program:
#include<iostream.h>
struct Package
{ int Length,Breadth,Height; };
void Occupies(Package M)
{
cout<<M.Length<<’x’<<M.Breadth<<’x’;
cout<<M.Height<<endl;
}
void main( )
{
Package P1={100,150,50},P2,P3;
++P1.Height;
Occupies(P1);
P3=P1;
++P3.Lengh;
P3.Breadth++;
Occupies(P3);
P2=P3;
P2.Height+=50;
P2.Length--;
Occupies(P2);
}

9) Rewrite the corrected code for the following program. Underline each correction if any.
#include<iostream.h>
structure Supergym
{
int member number;
char membername[20];
char membertype[]=.HIG.;
};
void main( )
{
Supergym person1,person2;
cin>>”Member Number:”;
cin>>person1.membhernumber;
cout<<”Member Name:”;
cin>>person1.membername;
person1.member type = .MIG.;
person2=person1;
cin>>”Member Number”<<person2.membernumber;
cin<<”Member Name”<<person2.membername;
cin<<”Member Number”<<person2.membertype;
}

10) Give the output of the following program:
#include<iostream.h>
struct Pixel
{
int C,R;
};
void Display(Pixel P)
{
cout<<.col.<<P.C<<.Row.<<P.R<<endl;
}
void main( )
{
Pixel X={450,530},Y,Z;
Z=X;
X.C+=10;
Y=X;
Y.R+=20;
Z.C-=15;
Display(X);
Display(Y);
Display(Z);
}

11) What are self refrential structures? Explain with an example.
12)Give the output of the following program.
#include<iostream.h>
#include<conio.h>
int g=20;
void func(int &x,int y)
{
x=x-y;
y=x*10;
cout<<x<<.,.<<y<<.\n.;
}
void main( )
{
int g=7;
func(g,::g);
cout<<g<<.,.<<::g<<.\n.;
func(::g,g);
cout<<g<<.,.<<::g<<.\n.;
}

3.OBJECT ORIENTED PROGRAMMING

1.    Define the term Data Hiding in the context of Object Oriented Programming. Give a suitable example using a C++ code to illustrate the same. 2
2.     Define the term Data Encapsulation in the context of Object Oriented Programming. Give a suitable example using a C++ code to illustrate the same. 2
3.     What is polymorphism? Give an example in C ++ to show its implementation in C++.
4.    What do you understand by function overloading? Give an example illustrating its use in a c++ program.
5.     Illustrate the concept of function overloading with the help of an example. 1
6.     Define the following terms: (i) Inheritance (ii)Encapsulation.
7.    State the difference between OOP and POP
8.    State the advantages and disadvantages of OOP
9.    How is the oop principles implemented in C++
10. Name some OOP languages and POP languages.

8.POINTERS

  1. What is a pointer ? How it is different from an ordinary variable
  2. State the operations performed on pointers
  3. State the use of & and *
  4. State the use of new and delete
  5. What are memory leaks?
  6. How dynamic memory allocation is done in C++?
  7. What is this pointer? Explain with example.

8) Find the output of the following program:
#include<iostream.h>
void main( )
{ int Array[]={4,6,10,12};
int *pointer=Array;
for(int I=1;I<=3;I++)
{ cout<<*pointer<<’#’;
pointer++;
}
cout<<endl;
for(I=1;I<=4;I++)
{ (*pointer)*=3;
--pointer;
}
for(I=1;I<5;I++)
cout<<Array[I-1]<<’@’;
cout<<endl;
}

9) State the difference between a function returning reference and function returning a pointer.                                                                                                                                               
10)  Rewrite the program removing the errors if any. Underline the correction.              
  #include<iostream.h>
Main()
{
            int ch=9,sch=90;
            char S[2,2];
            if ch<=9
                        cout<<ch;
            for(int x=0;x<2;x++)
                        for(int y=0;y<2;y++)
                        {
                        if(y==0) S[x][y]=’A’;
                        else
                                    cout>>S[x][y];
                        }
            getch(); }

11)Find the output of the following program:                                                                            
          #include<iostream.h>
#include<conio.h>
int main()
{
            char string[]=”Pointers and strings”;
            cout<<*(&string[2])<<endl;
            cout.write(string+5,15).put(‘\n’);
            cout<<*(string+3)<<”\n”;
            return 0;
       }
12) Find the output of the following program:                                                                           
#include <iostream.h>
struct GAME
{ int Score, Bonus;};
void Play(GAME &g, int N=10)
{
g.Score++;g.Bonus+=N;
}
void main()
{
GAME G={110,50};
Play(G,10);
cout<<G.Score<<":"<<G.Bonus<<endl;
Play(G);
cout<<G.Score<<":"<<G.Bonus<<endl;
Play(G,15);
cout<<G.Score<<":"<<G.Bonus<<endl;
}

13) Explain function overloading with a example.                                                                               
14) What is the difference between the constructor and normal function?                                     
15) Name the header file(s) that shall be needed for successful compilation of the following C++ code
void main ( )
{
clrscr( );
char String [20];
gets (String);
strcat (String, “CBSE”);
puts (String);
}

16) In the following C++ program what is the expected value of MyMarks from Options (i) to iv) given below. Justify answer.
                                                                       
#include<stdlib.h >
# include<iostream.h>
void main ()
{
randomize ();
int Marks [ ]= {99, 92, 94, 96, 93, 95}, MyMarks;
MyMarks = Marks [1 + random (2) ];
cout<<MyMarks<<endl;
}
 (I) 99        (ii) 96       (iii) 94           (iv) None of the above

17) Find the output of the following program: 2
#include<iostream.h>
void main( )
{ int Numbers[]={2,4,8,10};
int *ptr=Numbers;
for(int C=1;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; }

18) Find 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);
strcat(state_name,b.state_name);
}
};
void main( )
{ char *temp=.Delhi.;
state state1(temp),state2(.Mumbai.),state3(.Nagpur.),S1,S2;
S1.Replace(state1,state2);
S2.Replace(S1,state3);
S1.display( );
S2.display( );
}

19) 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);
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( );
}

20) What is .this. pointer? Give an example to illustrate the use of it in C++.

21) What will be the output of the following program:
#include<iostream.h>
#include<conio.h>
#include<ctype.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[]=”Pointers Fun”;
ChangeString(Message,Position);
cout<<Message<<’@’<<Position;
}

22) 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;
}

23) Give the output of the following program segment.
(Assuming all required header files are included in the program)
void main( )
{ int a=32,*x=&a;
char ch=65,&cho=ch;
cho+=a;
*x+=ch;
cout<<a<<’,’<<ch<<endl; }

24) Distinguish between int *ptr=new int(5); int *ptr=new int[5];

25) Give the output of the following program:

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

class per
{ char name[20];
float salary;

public:

per(char *s, float a)
{ strcpy(name,s);
salary=a;
}

per *GR(per &x)
{ if(x.salary>=salary)
return &x;
else
return this;
}

void display( )
{ cout<<”Name:”<<name<<”\n”;
cout<<”Salary:”<<salary<<”\n”;
}
};

void main( )
{
Per P1(“REEMA”,10000),
P2(“KRISHNAN”,20000),
P3(“GEORGE”,5000);
per *P;
P=P1.GR(P3);P->display( );
P=P2.GR(P3);P->display( ); }

26) Give the output of the following program.
#include<stdio.h>
void main( )
{ char *p= “Difficult” ;
char c; c=*p++; cout << c ; }


Comments