C++ Questions iostream.h

Transcription

C++ Questions iostream.h
C++ Questions
1. What is the purpose of using iostream.h header file?
2. The cin>> input statement is used to receive data from keyboard. a.True b. False
3. The cout<< output statement is used to print text on the screen.
a.True b. False
4. Each C++ program may have many main functions.
a.True b. False
5. A comment in C++ language starts with */ and ends with /*.
a.True b. False
6. Which of the following is not a standard data type?
a.int b.char c.float d.date
7. In C++ language, Which one is not the integer value? a.12 b.'23' c.10 d.100
8. What is the type of each of the following constants?
a.10 b.'2' c.10.7 d."12"
9. What is the type of each of the following constant?
a.) "Dara" b) '2.9' c) "10.7" d)13.9f e)'\a'
10. Find any errors in the following C++ program:
#include< iostream.h.h>
void main(){ /* This is the main function*/
cout<<"Hello"
return 0;
}
11. Find any errors in the following C++ program:
#include< iostream.h>
void main(){ /* This is the main function
cout<<'C++ Programming questions and answers';
}
12. Find any errors in the following C++ program:
#include< iostream.h>
void main(){ // This is the main function
integer a;
float b; }
13. Find any errors in the following C++ program:
#include< iostream.h>
int main(){ // This is another C++ program with some errors
a,b,s int;
d float;
cout<<"The end of the program";
return 0;
}
14. What is the output from the following C++ program?
#include< iostream.h>
int main(){ // This is another C++ program with some errors
int a,b,sum,subtr;
a=100;
b=20;
sum=a+b;
subtr=a-b;
cout<<left;
cout<<setw(5)<<"a+b"<<setw(5)<<"a-b";
cout<<setw(5)<<sum<<setw(5)<<subtr;
}
15. Which of the following are not unary expressions? a).a+b b).++a c). a+=1
16. Which of the following are binary expressions?
A).a+b b).++a c). a+=1 d). a++
17. If originally x=10, what is the value of x after each of the expressions: a) .++x b) .x++
18. If x=10 and sum=0, what is the value of sum after each of the expressions:
a.sum=++x b.sum=x++
19. If originally x=10 what is the value of each of the expressions: a) .--x b) .x-20. If originally x=10 and sub=0 what is the value of sub after each of the expressions:
a.sub=--x b.sub=x-21. If originally x=10 and y=5 what is the value of each of the expressions:
a.++x+y b.--x+y c.--x+(++y) d.--x+y—
22. What is the output of the following C++ program?
#include< iostream.h>
#include<conio.h>
int main(){
int x=10,y=15;
int mul=x*y;
cout<<x<<"*"<<y<<"="<<mul;
getch();
return 0;
}
23. What is the output of the following C++ program?
#include< iostream.h>
#include<conio.h>
#include <iomanip>
int main(){
float in_m,foot,yard,cent,meter;
cout<<"Enter a measurement in inches:";
cin>>in_m;
foot=12*in_m;//1 foot=12 inches
yard=36*in_m;//1 yard=36 inches
cent=in_m/2.54f;//1 inch=2.54 centimeters
meter=39.37*in_m;//1 meter=39.37 inches
cout<<setprecision(2)<<foot<<"feet"<<setw(15)
<<setprecision(2)<<yard<<"yards"<<setw(15)
<<setprecision(2)<<cent<<"centimeters"<<setw(15)
<<setprecision(2)<<meter<<"meters"<<setw(15);
getch();
return 0;
}
24. What is the output of the following C++ program?
#include< iostream.h>
#include<conio.h>
#include <iomanip>
int main(){
int x;
int f,m,l;
cout<<"Enter 3 digit-integer number:";
cin>>x;
f=x/100;
l=x%10;
m=(x/10)%10;
cout<<left; //left alignment
cout<<setw(5)<<f<<setw(5)<<m<<setw(5)<<l<<endl;
cout<<setw(5)<<m<<setw(5)<<l<<endl;
cout<<setw(5)<<l<<endl;
getch();
return 0;
}
25. Which of the following is not a logical operator in C++ language?
a.& b.&& c.|| d.!
26. Which of the following is not a comparison operator in C++ language?
a.> b.<= c.= d.==
27. There are two different ways to implement multi selections in C++ language. They are
a. else-if and switch case b. if...else and switch case
c. else-if and case
28. Evaluate the following expressions to true or false
a.3!=4-1 b. 5>=(1+6)-4 c.5+1==6 || 8-1>4 d.5>=6 || 1<8 && 9>7
29. if originally x=10,y=5, and z=2, evaluate the following expressions to true to false:
a.(x+1)!=y-2 b. !(x+y>20)
c.(x+1==y) || y-1>5 d.z+1>=3 || x+1<z*10 && y+3>7
30 Simplify the following expressions by removing ! operator and parentheses:
a.!(x!=y) b.!(x>z) c.!(x+y>=z) d.!(x>y || !(x>z))
31. If originally x=2,y=3, and z=5, what is the value of x,y, and z after executing the following code?
if(x+1==y) y=y+1;
else x++;
32. If originally x=0,y=0, and z=1, what is the value of x,y, and z after executing the following code?
if(x) {x--;y=x+2;}
else x++;
33. If originally x=2,y=0, and z=1, what is the value of x, y, and z after executing the following code?
if(x>y) y=x;
else z=x;
34. If originally x=1,y=0, and z=1, what is the value of x, y, and z after executing the following code?
if(x>y && x>z) {y=x;z=x+1;}
else if(x+y>=z) {x++;z=x+1;}
else y=z+x;
35. If originally x=1,y=2, and z=3, what is the value of x, y, and z after executing the following code?
switch(x) {
case 0: x++;z=x+1;break;
case 1: y=z+x;break;
default: z=z+x;
}
36 Rewrite the following C++ code using switch statement:
if(ch=='a' || ch=='A') countA++;
else if(ch=='e' || ch=='E') countE++;
else if(ch=='i' || ch=='I') countI++;
else if(ch=='o' || ch=='O') countO++;
else if(ch=='u' || ch=='U') countU++;
else cout<<"No vowel letter";
37. What is the output from the following C++ program?
#include< iostream.h>
#include<conio.h>
int main(){
int m;
cout<<"Enter month from 1 to 12:";
cin>>&m;
switch(m){
case 1: cout<<"January";break;
case 2: cout<<"Frebruary";break;
case 3: cout<<"March";break;
case 4: cout<<"April";break;
case 5: cout<<"May";break;
case 6: cout<<"June";break;
case 7: cout<<"July";break;
case 8: cout<<"August";break;
case 9: cout<<"September";break;
case 10: cout<<"October";break;
case 11: cout<<"November";break;
case 12: cout<<"December";break;
default: cout<<"Invalid month";
}
getch();
return 0;
}
38. What are the input and output from the following C++ program?
#include< iostream.h>
#include<conio.h>
int main(){
int num,rnum,count=1;
ag:
cout<<"Enter your number:";//ask for a guess number
cin>>num;
srand(time(0));//generate seed
rnum=1+rand()%6;//generate random number from 1 to 6
if(num==rnum) cout<<"Correct!"; //correct guess
else {
count++;
cout<<"Incorrect!\n"; //incorrect guess then
if(count<=3) goto ag; //you can try 3 times
}
getch();
return 0;
}
39. Change the following C++ code from using a while loop to for loop:
x=1;
while(x<10){
cout<<x<<"\t";
<x<<"\t";
++x;
}
40.. Change the following C++ code from a while loop to a for loop:
int x;
cin>>x;
while(x!=10)
{
cout<<x<<"\t";
cin>>x;
}
41. What would be the output from the following C++ code segment?
x=10;
while(x>5)
{
cout<<x<<"\n";
x--;
}
42. Change the following C++ code from a for loop to a while loop:
for(i=1;i<50;i++)
cout<<i<<"\t";
43. What would be printed from the following C++ code segment?
for(i=20;i>0;i-=2) cout<<i<<"\n";
44. What would be printed from the following C++ code segment?
for(x=1;x<=5;x++){
for(y=1;y<=x;y++)
cout<<x<<"\t";
cout<<"\n";
}
45. Change the following C++ code from a do while loop to a while loop:
x=0;
do{
cout<<x<<"\t";
x++;
}while(x<10);</x<<"\t";
C++ Programming Questions
1. 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.
2. If the ages of Ram, Sulabh and Ajay are input by the user, write a program to determine the
youngest of the three.
3. 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.
4. Any year is input by the user. Write a program to determine whether the year is a leap year
or not.
5. In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of
basic salary.
If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic
salary.
If the employee's salary is input by the user write a program to find his gross salary.
6. 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.
7. Write a program to find the roots of and quadratic equation of type ax2+bx+c where a is not
equal to zero.
8. 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.
9. Any character is entered by the user; write a program to determine whether the character
entered is a capital letter, a small case letter, a digit or a special symbol. The following table
shows the range of ASCII values for various characters
10. Write a program to print number from 1 to 10.
11. Write a program to calculate the sum of first 10 natural number.
12. Write a program to find the factorial value of any number entered through the keyboard.
13. Two numbers are entered through the keyboard. Write a program to find the value of one
number raised to the power of another. 5 Write a program to reveres any given integer
number.
14. Write a program to sum of digits of given integer number.
15. Write a program to check given number is prime or not.
16. Write a program to calculate HCF of Two given number.
17. 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.
18. Write a program to enter the numbers till the user wants and at the end it should display the
maximum and minimum number entered.
19. 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 )
20. Write a program to print Fibonacci series of n terms where n is input by user : 0 1 1 2 3 5 8
13 24 .....
21. 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
22. Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.