C++ Short Questions With Answers
C++ Short Questions With Answers
CE204
1. What is a program?
Answer: A program can be defined as either a set of written instructions created by a programmer or an
executable piece of software.
2. In how many ways source code can be turned into an executable program?
Ans: 2
3. C++ is a ________ language. Case sensitive, compiled (both)
4. What are the four pillars of objected oriented programming? Encapsulation, data hiding,
inheritance, and polymorphism
5. The property of being a self-contained unit is called ______________.
Encapsulation
6. ANSI =? American National Standards Institute
7. What are the steps in the development of a C++ program? Source code => compile => link => run
8. iostream=? Input-Output Stream
9. When a program starts, main() is called ____. Automatically
10. If you want to use cout, then which file should you include?
iostream
11. What are the types of comments in C++?
Double slash and slash star
12. Comments should not say what is happening, they should say ________________.
Why it is happening
13. How will you add any header file in your C++ program?
Using #include<header file name>
14. A C++ program will have maximum and also minimum one function named __________.
main
15. Function consists of a _______ and a _____.
header, body
16. The actual value passed in by the calling function is called the ___________.
Argument
17. The header consists of the________, ________, and _________to that function.
unsigned).
Schaum’s series + Day 3 of C++ in 21 days
22. If the name of a variable is written as bendingMoment, then this form is called __________.
Camel notation
23. Can you use unsigned variables for negative numbers?
No
24. What are the assignment operators? (sign)
#include<iostream.h>
void main ()
{
unsigned short int a;
a=65535;
a=a+3;
cout<<a<<endl;
}
2
27. What will be the output of the following program?
#include<iostream.h>
void main ()
{
short int a;
a=32767;
a=a+3;
cout<<a<<endl;
}
-32766
28. ASCII=?
American Standard Code for Information Interchange
29. Enumerator variables actually are of type ________.
unsigned int
30. Every enumerated constant has _______.
integer value
31. What will be the output of the following program?
Page |3
#include<iostream.h>
void main ()
{
enum x {CE200,CE202,CE204,CE206};
cout<<CE204<<endl;
}
2
32. What will be the output of the following program?
#include<iostream.h>
void main ()
{
enum x {CE200,CE202=90,CE204,CE206};
cout<<CE204<<endl;
}
91
33. What will be the output of the following program?
#include<iostream.h>
void main ()
{
enum x {CE200,CE202=90,CE204,CE206};
cout<<CE200<<endl;
}
0
34. What will be the output of the following program for y=92?
#include<iostream.h>
void main ()
{
enum x {CE200,CE202=90,CE204,CE206};
int y;
cin>>y;
x z;
z=x(y);
if(z==CE204) cout<<"Civil\n";
if(z==CE202) cout<<"Civil Dept.\n";
if(z==CE200) cout<<"CE\n";
if(z==CE206) cout<<"CE DEPT.\n";
}
Page |4
CE DEPT.
35. 500/21%4+870*2%65-80%9=?
45
36. What will be the output of the following program?
#include<iostream.h>
double main ()
{
float a,b; a=10; b=3;
int c; c=(a+b)/2;
cout<<c;
return 0;
6
37. 10.0%3*2+13/2=?
Error: '%' :left operand has type 'const double'
38. What will be the output of the following program?
#include<iostream>
using namespace std;
void main ()
{
int number=100;
{
int number=50;
{
int number=150;
}
cout<<number<<endl;
}
cout<<number<<endl;
}
50
100
39. If the output of the statement named ‘Helpful line’ is y, then what will be the output of the statement
named ‘Exam line’ of the following program?
#include<iostream.h>
double main ()
Page |5
{
{
char variable_1=121;
cout<<variable_1<<endl;//Helpful line
}
int variable_1='y';
int variable_2=10;
variable_1+=variable_2;
cout<<variable_1<<endl;//Exam line
return 0;
}
131
40. What will be the output of the following program?
#include<iostream.h>
void main ()
{
const int number=450;
cin>>number;
cout<<number%4<<endl;
}
#include<iostream.h>
void main ()
{
int a,b,c,d,e;a=b=c=d=e=8;
a*=b+=c-=d/=e%=3;
cout<<a<<"\t"<<b<<"\t"
<<c<<"\t"<<d<<"\t"<<e<<endl;
}
96 12 4 4 2
42. What will be the output of the following program?
#include<iostream.h>
void main ()
{
bool factor=-120;
factor*=10;
cout<<factor<<endl;
Page |6
1
43. What will be the output of the following program?
#include<iostream.h>
void main ()
{
for(int i=0,j=5;i!=j;i++,j--)
{
cout<<"i= "<<i<<endl;
}
}
0
1
2
.
.
.
Infinite
44. What will be the output of the following program?
#include<iostream.h>
void main ()
{
bool x=3;
double a=1;
while(a>=x)
{
cout<<"CE204\n";
}
}
CE204
CE204
CE204
.
.
.
Infinite
Page |7
#include<iostream.h>
void main ()
{
float x=1,y=1;
do
{
cout<<x<<"\t"<<y<<endl;
x++;y++;
}while(x<5,y<4);
}
1 1
2 2
3 3
46. What will be the output of the following program?
#include<iostream.h>
void main ()
{
line1:for(int a=3;;a--)
{
cout<<a<<endl;
if(a%2) goto line1;
else break;
}
}
3
3
3
.
.
.
Infinite
47. What will be the output of the following program?
#include<iostream.h>
void main ()
{
for(int i=0,j=2,k=4;;)
{
Page |8
cout<<(i+j+k)/4<<endl;
i+=2;j++;
if(i==j && j==k)
{
break;
cout<<"Programming\n";
}
}
}
1
2
48. What will be the output of the following program?
#include<iostream.h>
void main ()
{
for(int a=5;a>1;a--)
{
for(int b=a;b>1;b--)
{
cout<<"A\t";
}
cout<<endl;
}
}
A A A A
A A A
A A
A
49. What will be the output of the following program?
#include<iostream.h>
void main ()
{
for(int a=2;a<=6;a=a+2)
{
for(int b=1;b<3;b++)
{
cout<<a<<"\t"<<b<<endl;
}
}
Page |9
2 1
2 2
4 1
4 2
6 1
6 2
50. How many times will CE204 be printed?
#include<iostream.h>
void main ()
{
for(int a=0;a<=4;a=a+2)
{
for(int b=1;b<3;b++)
{
for(int c=3;c<=9;c=c+3)
{
cout<<"CE204\n";
}
}
}
}
18
51. What will be the output of the following program?
#include<iostream.h>
void main ()
{
double b=0;
double a=1;
while(a<=3)
{
double p=1,q=1;
while(q<=a)
{
p*=q;
q++;
}
b+=(a/p);
a++;
P a g e | 10
}
cout<<b<<endl;
}
2.5
52. A __________ is a statement that does nothing.
null statement
53. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int a=8,b=5;
a=a+b;
b=a-b;
a=a-b;
cout<<a<<"\t"<<b<<endl;
}
5 8
#include<iostream.h>
void main ()
{
int a=8,b=3;
if(a%b) cout<<"CE204\n";
else cout<<"CE206\n";
}
CE204
55. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int y=1900;
if(y%4==0)
if(y%100==0)
if(y%400==0) cout<<"LY\n";
else cout<<"NLY\n";
P a g e | 11
else cout<<"LY\n";
else cout<<"NLY\n";
}
NLY
56. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int a=10,b=2;
if(a<9,b<4) cout<<"Civil\n";
else cout<<"BUET\n";
}
Civil
57. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int a=10,b=20;
while(a<b)
{
cout<<"A\n";
break;
}
}
A
58. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int a=10;
while(a!=!a)
{
cout<<"A\n";
a=a+2;
P a g e | 12
}
}
A
A
A
.
.
.
Infinite
59. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int a=10;
while(a=15)
{
cout<<a<<endl;
a++;
}
cout<<"X\n";
}
15
15
15
.
.
.
Infinite
60. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int a=10;
while(a<=15)
{
a+=2;
cout<<a<<endl;
a-=3;
}
cout<<"X\n";
P a g e | 13
12
11
10
9
.
.
.
Infinite
61. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int a=b=10;
cout<<a<<"\t"<<b<<endl;
}
Error: more than one variable cannot be declared and initialized at a time.
62. What will be the output of the following program?
#include<iostream.h>
void main ()
{
bool a=-50;
a*=100;
cout<<a<<endl;
}
1
63. What will be the output of the following program?
#include<iostream.h>
void main ()
{
char a='L';
switch(a){
default:
cout<<"Z\n";
case 'A':
cout<<"X"<<endl;
cout<<"Y"<<endl;
case 'B':
cout<<"P"<<endl;break;
case 'C':
P a g e | 14
cout<<"Q"<<endl;
}
}
Z
X
Y
P
64. What will be the output of the following program?
#include<iostream.h>
void main ()
{
char a='A';
switch(a){
default:
cout<<"Z\n";break;
case 'A':
cout<<"X"<<endl;break;
cout<<"Y"<<endl;
case 'B':
cout<<"P"<<endl;break;
case 'C':
cout<<"Q"<<endl;
}
}
X
65. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int n=-2;
if(n<0) cout<<"Try again.\n";
cin>>n;
else cout<<"ok, n = "<<n<<"\n";
}
Error: illegal else without matching if
66. What will be the output of the following program for n=-8 as input?
#include<iostream.h>
void main ()
{
int n=-2;
if(n<0)
P a g e | 15
{
cout<<"Try again\n";
cin>>n;
}
else cout<<"ok, n = "<<n<<"\n";
}
Try again.
67. What will be the output of the following program for a=0 as input?
#include<iostream.h>
void main ()
{
int a;
cin>>a;
if(a=0) cout<<"Civil\n";
else cout<<"BUET\n";
}
BUET
68. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int x=1,y=3,z=2;
cout<<(x<y<z)<<endl;
}
1
Note: See page 40 of SCHAUM’S Outlines.
69. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int x=1,y=3,z=2;
if(x<y<z)
cout<<x<<"\t"<<y<<"\t"<<z<<endl;
}
1 3 2
70. m – 8 – n =? For m=20, n=8.
4
P a g e | 16
#include<iostream.h>
void main ()
{
int x=25;
if(x>=30) cout<<"A\n";
if(x>=20) cout<<"B\n";
if(x>=15) cout<<"C\n";
}
B
C
75. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int x=25;
if(x>=30) cout<<"A\n";
else if(x>=20) cout<<"B\n";
else cout<<"C\n";
}
B
76. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int x=25;
if(x>20)
{
int x=25;
x=x+10;
}
cout<<x<<endl;
P a g e | 17
25
77. What will be the output of the following program?
#include<iostream.h>
void main ()
{
int x=25;
if(x>20)
{
x=x+10;
}
cout<<x<<endl;
}
35
78. What will be the output of the following program?
#include<iostream.h>
int x(int,int);
double x(int,int);
void main ()
{
cout<<(2,3)<<endl;
}
int x(int a,int b)
{
return a+b;
}
float x(int a,int b)
{
return a-b;
}
#include<iostream.h>
int x(int a,int b)
{
return a+b;
}
P a g e | 18
10
80. What will be the output of the following program?
#include<iostream.h>
int x(int a,int b)
{
return a+b;
}
double x(int a,float b)
{
return a-b;
}
void main ()
{
double a=10;
int b=20;
cout<<x(b,a)<<endl;
}
Error: double cannot be converted to float for variable a which is floating point type in the x function
named as b;
81. What will be the output of the following program?
#include<iostream.h>
double x(int a,int b)
{
return a+b;
}
double x(int a,float b)
{
return a-b;
}
P a g e | 19
200
82. What will be the output of the following program?
#include<iostream.h>
#include<math.h>
double x(double a)
{
if(a<=2) return pow(a,2);
return pow(a,2)+x(a-2);
}
void main ()
{
cout<<x(6)<<endl;
}
56
83. What will be the output of the following program?
#include<iostream.h>
double n=4;
double x(double a)
{
if(a==n) return a;
return a*x(a+1);
}
void main ()
{
cout<<x(1)<<endl;
}
24
84. What will be the output of the following program?
P a g e | 20
#include<iostream.h>
double x(double);
double y(double);
void main ()
{
cout<<x(5)<<endl;
}
double x(double a)
{
if(a<=1) return 1;
return a*a*a+y(a-1);
}
double y(double a)
{
if(a<=1) return 1;
return a*a*a+x(a-1);
}
255
85. What will be the output of the following program?
#include<iostream.h>
double x(double a)
{
if(a<=1) return 1;
return a*a*a+y(a-1);
}
double y(double a)
{
if(a<=1) return 1;
return a*a*a+x(a-1);
}
void main ()
{
cout<<x(5)<<endl;
}
Error: function x does not have access to function y unless y is declared before calling in function x.
(vice-versa also)
86. Lecture notes of Yasin sir