Professional Documents
Culture Documents
[SET – 1]
1Write a program to print HELLO WORLD on screen. solution
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"Hello world";
getch();
return 0;
}
2Write a program to display the following output using a single cout statement.
Subject Marks
Mathematics 90
Computer 77
Chemistry 69 solution
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"subject " <<"\tmarks"<<"\nmathematic\t"
<<90<<"\ncomputer\t"<<77<<"\nchemistry\t"<<69;
getch();
return 0;
}
3Write a program which accept two numbers and print their sum. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int a,b,c;
cout<< "\nEnter first number : ";
cin>>a;
cout<<"\nEnter second number : ";
cin>>b;
c=a+b;
cout<<"\nThe Sum is : "<<c;
getch();
return 0;
}
4Write a program which accept temperature in Farenheit and print it in centigrade. solution
#include<iostream.h>
#include<conio.h>
int main()
{
float F,C;
cout<< "\nEnter temperature in Farenheit : ";
cin>>F;
C=5*(F-32)/9;
cout<<"Temperature in celcius is : "<<C;
getch();
return 0;
}
5Write a program which accept principle, rate and time from user and print the simple interest.
solution
#include<iostream.h>
#include<conio.h>
int main()
{
int p,r,t,i;
cout<<"Enter Principle : ";
cin>>p;
cout<<"Enter Rate : ";
cin>>r;
cout<<"Enter Time : ";
cin>>t;
i=(p*r*t)/100;
cout<<"Simple interest is : "<<i;
getch();
return 0;
}
6Write a program which accepts a character and display its ASCII value. solution
#include<iostream.h>
#include<conio.h>
int main()
{
char ch;
cout<< "\nEnter any character : ";
cin>>ch;
cout<<"ASCII equivalent is : "<<(int)ch;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
int a,b,temp;
cout<<"\nEnter two numbers : ";
cin>>a>>b;
temp=a;
a=b;
b=temp;
cout<<"\nAfter swapping numbers are : ";
cout<<a<<" "<<b;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
float r,area;
cout<< "\nEnter radius of circle : ";
cin>>r;
area = 3.14*r*r;
cout<<"Area of circle : "<<area;
getch();
return 0;
}
9Write a program to check whether the given number is positive or negative (using ? : ternary
operator ) solution
#include<iostream.h>
#include<conio.h>
int main()
{
int a;
cout<<"Enter any non-zero Number : ";
cin>>a;
(a>0)?cout<<"Number is positive":cout<<"Number is negative";
getch();
return 0;
}
Variable, Operator and Expression
[SET – 2]
1 Write a program to swap value of two variables without using third variable.
solution
#include<iostream.h>
#include<conio.h>
int main()
{
int a,b;
cout<<"\nEnter two numbers : ";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\nAfter swapping numbers are : ";
cout<<a<<" "<<b;
getch();
return 0;
}
2 Write a program which input three numbers and display the largest number using
ternary operator. solution
#include <iostream.h>
#include <conio.h>
int main()
{
int a,b,c,greatest;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
greatest=(a>b&&a>c)?a:(b>c)?b : c;
cout<<"Greatest number is "<<greatest;
getch();
return 0;
}
3 Write a program which accepts amount as integer and display total number of Notes
of Rs. 500, 100, 50, 20, 10, 5 and 1.
For example, when user e
#include<iostream.h>
#include<conio.h>
int main()
{
int amt,R500,R100,R50,R20,R10,R5,R1;
cout<<"Enter amount : ";
cin>>amt;
R500=amt/500;
amt=amt%500;
R100=amt/100;
amt=amt%100;
R50=amt/50;
amt=amt%50;
R20=amt/20;
amt=amt%20;
R10=amt/10;
amt=amt%10;
R5=amt/5;
amt=amt%5;
R1=amt;
cout<<"Rs.500 : "<<R500<<"\nRs.100 : "<<R100<<"\nRs. 50 : "<<R50<<
"\nRs. 20 : "<<R20<<"\nRs. 10 : "<<R10<<"\nRs. 5 :
"<<R5<<"\nRe. 1 : "<<R1;
getch();
return 0;
}
4 Write a program which accepts a character and display its next character. solution
#include<iostream.h>
#include<conio.h>
int main()
{
char ch;
cout<< "\nEnter any character : ";
cin>>ch;
ch++;
cout<<"Next character is : "<<ch;
getch();
return 0;
}
5 Write a program which accepts days as integer and display total number of years, months and
days in it.
for example : If user input as 856 days the output should be 2 years 4 months 6 days. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int days,y,m,d;
cout<<"Enter no. of days : ";
cin>>days;
y=days/365;
days=days%365;
m=days/30;
d=days%30;
cout<<"Years : "<<y<<"\nMonths : "<<m<<"\nDays : "<<d;
getch();
return 0;
}
int result = 4 + 5 * 6 + 2;
cout<<result;
int a = 5 + 7 % 2;
cout<<a;
7 What is the output of following program?
To calculate the equivalent length in centimeters, you need to multiply the total
inches by 2.54. Instead of using the value 2.54 directly in the program, you will
declare this value as a named constant. Similarly, to find the total inches, you need
to multiply the feet by 12 and add the inches. Instead of using 12 directly in the
program, you will also declare this value as a named constant. Using a named
constant makes it easier to modify the program later.
Write a program that takes length as input in feet and inches. The program should then convert
the lengths in centimeters and display it on screen. Assume that the given lengths in feet and
inches are integers.
/********************************************************
Purpose :This program converts measurements
in feet and inches into centimeters.
@Author :cppforschool.com
********************************************************/
int main ()
{
int feet, inches;
int totalInches;
double centimeter;
cout << "Enter two integers, one for feet and one for inches: ";
cin >> feet >> inches;
cout << endl;
cout << "The numbers you entered are " << feet << " for feet and " <<
inches << " for inches. " << endl;
totalInches = INCHES_PER_FOOT * feet + inches;
cout << "The total number of inches = " << totalInches << endl;
centimeter = CENTIMETERS_PER_INCH * totalInches;
cout << "The number of centimeters = " << centimeter << endl;
return 0;
}
10Write a program to check whether the given number is even or odd (using ? : ternary operator
) solution
#include<iostream.h>
#include<conio.h>
int main()
{
int a;
cout<<"Enter the Number : ";
cin>>a;
(a%2==0)?cout<<"Number is even":cout<<"Number is odd";
getch();
return 0;
}
flow of control
[SET – 1]
1 Any integer is input by the user. Write a program to find out whether it is an odd
number or even number. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int a;
cout<<"Enter any number : ";
cin>>a;
if(a%2==0)
cout<<"The number is even";
else
cout<<"The number is odd";
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
#include<iostream.h>
#include<conio.h>
int main()
{
int a;
cout<<"Enter any number:";
cin>>a;
if(a>0)
cout<<"The absolute value of number is:"<<a;
else
cout<<"The absolute value of number is:"<<-(a);
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
3 Write a program to calculate the total expenses. Quantity and price per item are
input by the user and discount of 10% is offered if the expense is more than 5000.
solution
#include<iostream.h>
#include<conio.h>
int main()
{
int totalexp, qty, price, discount;
cout<<"Enter quantity:";
cin>>qty;
cout<<"Enter price:";
cin>>price;
totalexp=qty*price;
if(totalexp>5000)
{
discount=(totalexp*0.1);
totalexp=totalexp-discount;
}
getch();
return 0;
}
SAMPLE RUN # 1
Enter quantity:14
Enter price:300
Total Expense is Rs.4200
SAMPLE RUN # 2
Enter quantity:50
Enter price:300
Total Expense is Rs. 13500
4 Write a program to determine whether the seller has made profit or incurred loss.
Also determine how much profit he made or loss he incurred. Cost price and selling
price of an item is input by the user. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int cp,sp,result;
result=sp-cp;
if(result>0)
cout<<"Profit : "<<result;
else
if(result<0)
cout<<"Loss : "<<-(result);
else
cout<<"No profit no loss";
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
SAMPLE RUN # 3
5 If the ages of Ram, Sulabh and Ajay are input by the user, write a program to
determine the youngest of the three. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int ram_age,sulabh_age,ajay_age;
cout<<"Enter Ram age:";
cin>>ram_age;
cout<<"Enter Sulabh age:";
cin>>sulabh_age;
cout<<"Enter Ajay age:";
cin>>ajay_age;
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
6 Write a program to check whether a triangle is valid or not, when the three angles of
the triangle are entered by the user. A triangle is valid if the sum of all the three
angles is equal to 180 degrees. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int angle1,angle2,angle3;
cout<<"Enter the three angles of triangle:";
cin>>angle1>>angle2>>angle3;
if (angle1+angle2+angle3==180)
cout<<"Triangle is valid";
else
cout<<"Triangle is not valid";
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
7 Any year is input by the user. Write a program to determine whether the year is a
leap year or not. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int year;
cout<<"Enter the year : ";
cin>>year;
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
SAMPLE RUN # 3
SAMPLE RUN # 4
#include<iostream.h>
#include<conio.h>
int main()
{
float basic_salary, gross_salary, HRA, DA;
cout<<"Enter basic salary of Employee : ";
cin>>basic_salary;
if (basic_salary<1500)
{
HRA=0.1*basic_salary;
DA=0.9*basic_salary;
}
else
{
HRA=500;
DA=0.98*basic_salary;
}
gross_salary=basic_salary+HRA+DA;
cout<<"Gross salary is : "<<gross_salary;
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
9 Write a program to calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for upto 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int calls;
float bill;
cout<<"Enter number of calls : ";
cin>>calls;
if(calls<=100)
bill=200;
else if (calls>100 && calls<=150)
{
calls=calls-100;
bill=200+(0.60*calls);
}
else if (calls>150 && calls<=200)
{
calls=calls-150;
bill=200+(0.60*50)+(0.50*calls);
}
else
{
calls=calls-200;
bill=200+(0.60*50)+(0.50*50)+(0.40*calls);
}
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
SAMPLE RUN # 3
SAMPLE RUN # 4
10 Write a program to find the roots of and quadratic equation of type ax2+bx+c where
a is not equal to zero. solution
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,d,root1,root2;
cout<<"Enter value of a, b and c : ";
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Roots are real & equal";
}
else if(d>0)
{
root1=-(b+sqrt(d))/(2*a);
root2=-(b-sqrt(d))/(2*a);
cout<<"Roots are real & distinct";
}
else
{
root1=(-b)/(2*a);
root2=sqrt(-d)/(2*a);
cout<<"Roots are imaginary";
}
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
SAMPLE RUN # 3
11 The marks obtained by a student in 5 different subjects are input by the user. The
student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int sub1,sub2,sub3,sub4,sub5,percentage;
cout<<"Enter marks of five subjects : ";
cin>>sub1>>sub2>>sub3>>sub4>>sub5;
percentage=(sub1+sub2+sub3+sub4+sub5)/5;
if(percentage>=60)
cout<<"Ist division";
else if(percentage>=50)
cout<<"IInd division";
else if(percentage>=40)
cout<<"IIIrd division";
else
cout<<"Fail" ;
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
SAMPLE RUN # 3
SAMPLE RUN # 4
A–Z 65 – 90
a–z 97 – 122
0–9 48 – 57
solution
#include<iostream.h>
#include<conio.h>
int main ()
{
char ch;
cout<<"Enter any character:";
cin>>ch;
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
SAMPLE RUN # 4
#include<iostream.h>
#include<conio.h>
int main()
{
int i=1;
while(i<=10)
{
cout<<i<<"\n";
i++;
}
getch();
return 0;
}
1
2
3
4
5
6
7
8
9
10
2 Write a program to calculate the sum of first 10 natural number. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int i=1,sum=0;
while(i<=10)
{
sum+=i;
i++;
}
cout<<"Sum :"<<sum;
getch();
return 0;
}
Sum : 55
3 Write a program to find the factorial value of any number entered through the
keyboard. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int n,fact=1;
cout<<"Enter any number : ";
cin>>n;
while(n>=1)
{
fact*=n;
n--;
}
cout<<"Factorial :"<<fact;
getch();
return 0;
}
4 Two numbers are entered through the keyboard. Write a program to find the value
of one number raised to the power of another. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;
cout<<"Result :"<<r;
getch();
return 0;
}
Enter the base number and exponent : 5 3
Result :125
#include<iostream.h>
#include<conio.h>
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}
#include<iostream.h>
#include<conio.h>
int main()
{
int n,flag=0;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
#include<iostream.h>
#include<conio.h>
int main()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor;
if(rem==0)
hcf=divisor;
else
{
dividend=divisor;
divisor=rem;
}
}
cout<<"HCF is : "<<hcf;
getch();
return 0;
}
9 Write a program to enter the numbers till the user wants and at the end it should
display the count of positive, negative and zeros entered. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int n, sum_p=0, sum_n=0, sum_z=0;
char choice;
do
{
cout<<"Enter number ";
cin>>n;
if(n>0)
sum_p++;
else if(n<0)
sum_n++;
else
sum_z++;
getch();
return 0;
}
Enter number : 56
Do you want to Continue(y/n)? y
Enter number : -9
Do you want to Continue(y/n)? y
Enter number : 67
Do you want to Continue(y/n)? y
Enter number : 54
Do you want to Continue(y/n)? y
Enter number : -98
Do you want to Continue(y/n)? y
Enter number : -13
Do you want to Continue(y/n)? y
Enter number : 0
Do you want to Continue(y/n)? y
Enter number : -98
Do you want to Continue(y/n)? y
Enter number : 0
Do you want to Continue(y/n)? n
Positive Number :3
Negative Number :4
Zero Number :2
10 Write a program to enter the numbers till the user wants and at the end it should
display the maximum and minimum number entered. solution
#include<iostream.h>
#include<conio.h>
int main()
{
int n, max=0, min=32767;
char choice;
do
{
cout<<"Enter number : ";
cin>>n;
if(n>max)
max=n;
if(n<min)
min=n;
}while(choice=='y' || choice=='Y');
getch();
return 0;
}
Enter number : 34
Do you want to Continue(y/n)? y
Enter number : 88
Do you want to Continue(y/n)? y
Enter number : 3
Do you want to Continue(y/n)? y
Enter number : 54
Do you want to Continue(y/n)? y
Enter number : 41
Do you want to Continue(y/n)? y
Enter number : 20
Do you want to Continue(y/n)? n
Maximum Number :88
Minimum Number :3
11 Write a program to print out all Armstrong numbers between 1 and 500. If sum of
cubes of each digit of the number is equal to the number itself, then the number is
called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ) solution
#include<iostream.h>
#include<conio.h>
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}
getch();
return 0;
}
1
153
370
371
407
#include<iostream.h>
#include<conio.h>
int main()
{
int f=0,s=1,t,n;
for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}
getch();
return 0;
}
13 Write a program to calculate the sum of following series where n is input by user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n solution
#include<iostream.h>
#include<conio.h>
int main()
{
int i,n;
float sum=0;
for(i=1;i<=n;i++)
sum += 1.0/i;
cout<<"Sum : "<<sum;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
int i,n,sign=-1;
float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;
getch();
return 0;
}
SAMPLE RUN # 1
SAMPLE RUN # 2
//Solution of (i)
#include<iostream.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}
getch();
return 0;
}
//Solution of (ii)
#include<iostream.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}
getch();
return 0;
}
//Solution of (iii)
#include<iostream.h>
#include<conio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}
getch();
return 0;
}
2 Write a program to compute sinx for given x. The user should supply x and a positive
integer n. We compute the sine of x using the series and the computation should use
all terms in the series up through the term involving xn
#include<iostream.h>
#include<conio.h>
int main()
{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1;
for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;
getch();
return 0;
}
3 Write a program to compute the cosine of x. The user should supply x and a positive
integer n. We compute the cosine of x using the series and the computation should use
all terms in the series up through the term involving xn
#include<iostream.h>
#include<conio.h>
int main()
{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=2;i<=n;i+=2)
{
fact=1;
for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sum+=sign*p/fact;
sign=-1*sign;
}
cout<<"cos "<<x<<"="<<1+sum;
getch();
return 0;
}
Answer: (a)
Answer: (d)
3. Manipulators without parameters such as endl, hex, dec etc are defined in __________ file.
a) iomanip.h
b) conio.h
c) io.h
d) fstream.h
e) none of the above
Answer: (e)
Answer: (b)
Answer: (d)
6. Statement cin >> ch ; will start executing when we press _______ key.
a) any
b) any character
c) only character
d) escape
e) enter
Answer: (e)
Other C++ Interview Questions with answers
Answer : Whenever a C++ program is executed, execution of the program starts and ends at
main(). The main is the driver function of the program. If it is not present in a program, no
execution can take place.
Answer : Syntax error - The errors which are traced by the compiler during compilation, due to
wrong grammar for the language used in the program, are called syntax errors.
For example, cin<<a; // instead of extraction operator insertion operator is used.
Run time Error - The errors encountered during execution of the program, due to unexpected
input or output are called run-time error.
For example - a=n/0; // division by zero
Logical Error - These errors are encountered when the program does not give the desired
output, due to wrong logic of the program.
For example : remainder = a+b // instead of using % operator + operator is used.
Answer : The preprocessor directive #include tells the complier to insert another file into your
source file. In effect, #include directive is replaced by the contents of the file indicated.
Answer : The memory implementation of char data type is in terms of the number code.
Therefore, it is said to be another integer data type.
Answer : When two operands of different data types are encountered in the same expression, the
variable of lower data type is automatically converted to the data tpes of variable with higher
data type, and then the expression is calculated.
For example: int a=98; float b=5; cout<<a/3.0; //converts to float type, since 3.0 is of float type.
cout<<a/b; // converts a temporarily to float type, since b is of float type, and gives the result
19.6.
Question : What is type casting in C++ ?
Answer : Type casting refers to the data type conversions specified by the programmer, as
opposed to the automatic type conversions. This can be done when the compiler does not do the
conversions automatically. Type casting can be done to higher or lower data type.
For example : cout<<(float)12/5; //displays 2.4, since 12 is converted to float type.
Answer : The break keyword causes the entire switch statement to exit, and the control is passed
to statement following the switch.. case construct. Without break, the control passes to the
statements for the next case. The break statement is optional in switch..case construct.
Answer : This keyword gives the switch..case construct a way to take an action if the value of
the switch variable does not match with any of the case constants. No break statement is
necessary after default case, since the control is already at the end of switch..case construct. The
default is optional in case of switch..case construct.
Answer : While is an Entry Controlled Loop, the body of the loop may not execute even once if
the test expression evaluates to be false the first time, whereas in do..while, the loop is executed
at least once whether the condition holds true the first time or not. more details
Question : What is the difference between call by value and call by reference in a
user defined function in C++?
Answer : The value of the actual parameters in the calling function do not get affected when the
arguments are passed using call by value method, since actual and formal parameters have
different memory locations.
The values of the formal parameters affect the values of actual parameters in the calling function,
when the arguments are passed using call by reference method. This happens since the formal
parameters are not allocated any memory, but they refer to the memory locations of their
corresponding actual parameters. more details
Question : What is the difference between local variable and global variable?
Answer : Local variables are those variables which are declared within a function or a
compound statement and these variables can only be used within that function/scope. They
cannot be accessed from outside the function or a scope of it's declaration. This means that we
can have variables with the same names in different functions/scope. Local variables are local to
the function/scope in which they are declared.
Global variables are those variables which are declared in the beginning of the program. They
are not declared within a function. So, these variables can be accessed by any function of the
program. So, global variables are global to all the functions of the program.
Answer :Data Abstraction, Data Hiding, Data Encapsulation, Inheritance and Polymorphism are
the basic concepts of OOP. more details
Answer : A class binds together data and its associated function under one unit thereby
enforcing encapsulation.
The private and protected member remain hidden from outside world. Thus a class enforces data
hiding
The outside world is given only the essential information through public members, thereby
enforcing abstraction.
Answer : Inheritance is a process of creating new classes (derived classes) from existing classes
(base classes). The derived classes not only inherit capabilities of the base class but also can add
new features of own. The most important aspect of inheritance is that it allows reusability of
code. more details
Question : How are binary files different from text files in C++?
Answer : A text file store information in ASCII characters. In text files, each line of text is
terminated, with a special character known as EOL character.
A binary file store information in the same format in which the information is held in memory.
In binary file, there is no delimeter for a line.
Question : What is a stream? Name the streams generally used for file I/O.
Answer : get() does not extract the delimeter newline character from input stream. On the other
hand getline() does extract the delimeter newline character from the input stream so that the
stream is empty after getline() is over.
Answer : The ios::out is the default mode of ofstream. With the mode of the file does not exist, it
gets created but if the file exists then its existing contents get deleted.
The ios::app is output mode of ofstream. With the mode of the file does not exist, it gets created
but if the file exists then its existing contents are retained and new information is appended to it.
more details
Answer : In the static memory allocation, the amount of memory to be allocated is predicted and
preknown. This memory is allocated during the compilation itself.
In the dynamic memory allocation, the amount of memory allocated is not known beforehead.
This memory is allocated during run time as and when required.
Question : What do you understand by memory leaks? How can memory leaks
be avoided?
Answer : Memory leak is a situation in which there lie so many orphaned memory blocks that
are still allocated but no pointers are referencing to them.
Answer : The this pointer is an object pointer which points to the currently calling object, The
this pointer is, by default, available to each called member function.
Question : What is the full form of LIFO? Give an example of LIFO list?
Answer : Full form of LIFO is LAST IN FIRST OUT. An example of LIFO list is stack. Stack is
a linear data structure in which insertion and deletion of elements takes place only one end
known as TOP.
Question : What is the full form of FIFO? What is FIFO list technically called?
Answer : Full form of FIFO is FIRST IN FIRST OUT. An example of FIFO list is Queue.
Queue is a linear data structure in which insertion and deletion of elements takes place from two
opposite ends rear and front respectively.
Answer : Precondition for Binary search to be performed on a single dimensional array is array
should be sorted.
1. What is C++
C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-oriented
computer language used in the development of enterprise and commercial applications. Microsoft’s
Visual C++ became the premier language of choice among developers and programmers.
Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message passing
3. Define inheritance?
The mechanism of deriving a new class (derived) from an old class (base class) is called inheritance. It
allows the extension and reuse of existing code without having to rewrite the code from scratch.
Inheritance is the process by which objects of one class acquire properties of objects of another
class.
4. Define polymorphism?
Polymorphism means one name, multiple forms. It allows us to have more than one function with
the same name in a program.It allows us to have overloading of operators so that an operation can
exhibit different behaviours in different instances.
All the features of C are similiar to C++ except some features, such as polymorphism,
operator overloading which are supported in C++ but not in C language.
Both C and C++ language is similiar in their functionality but C++ provides with more tools
and options.
6. What is encapsulation?
The wrapping up of data and functions into a single unit (called class) is known as
encapsulation. Encapsulation containing and hiding information about an object, such as
internal data structures and code.
Keywords
Identifiers
Constants
Strings
Operators
Variable declaration
Constant declaration
Function declaration
Object declaration
Set
Multiset
Map
Multimap
Encapsulation
Inheritance
Polymorphism
Similar to the static variables and are associated with the class.
Can only access static variables of a class.
Can also be called using the scope resolution operator.
Data members
Methods
69. Can a function overloading depend only on passing by value and passing by reference?
No, the reason is that whether a function is called the passing a parameter as a value or by
reference, it appears similar to the caller of the function.
Const : A const variable is one that the program may not modify except through
initialiazation when the variable is declared.
Volatile: A volatile type qualifier tells the compiler that the program could change
the variable.
Mutable: A const member function may modify a data member only if the data
member is declared with the mutable qualifier.
Expression statement
Assignment statement
Selection statement
Iteration statement
Jump statement
What are the advantages of using cin and cout compared to scanf(...) and printf(...),
77.
respectively?
Compared to the standard C functions printf() and scanf(), the usage of the cin and
cout is more type safe.
The format strings, which are used with printf() and scanf() can define wrong format
specifies for their arguments, for which the compiler does not warn.
In contrast, argument checking with c in and cout is performed by the compiler.
C in and Cout are stream classes that could be used to receive and print objects
respectively.
82. How can you return the current involving object from its member function?
return(*this);
83. What is the difference between prefix and postfix versions of operator++()?
The prefix and postfix versions of operator ++() can be differentiated on the basis of
arguments defined.
The postfix operator ++() consists of a dummy parameter of int datatype; whereas, a
dummy parameter is not found in the prefix operator ++().
In C and C++, the variables of the structures are public; however, in C, the variable
cannot be declared as private or protected. On the contrary, in C++, the variables can
be declared as private or protected.
In C, the concept of inheritance is not supported. In C++, the concept of inheritance
is fully supported.
On declaring a struct in C, the addition of the struct keyword is must. On the
contrary, there is no need of the struct keyword on declaring struct in C++.
In C, the initialization cannot be done outside the scope of a structure. However, in
C++, the initialization can be done outside the scope of a structure.
In C, structures do not have direct functions or methods.
92. How the keyword struct is different from the keyword class in C++?
In C++, a class is similar to a struct with the exception that, by default, all the members of a
class are private; while the members of a struct are public. Encapsulation is not supported by
structures but supported by classes.
Public
protected
private
auto
register
static
extern
107. What is the difference between method overloading and method overriding?
Overloading a method (or function) in C++ is the ability for functions of the same name to
be defined as long as these methods have different signatures (different set of parameters).
Method overriding is the ability of the inherited class rewriting the virtual method of the
base class.
containers
Algorithm
Iterators