0% found this document useful (0 votes)
10 views18 pages

C++ Theory

The document provides a series of C++ programming exercises, including code snippets and corrections for various concepts such as outputting text, using variables, control structures, loops, arrays, and object-oriented programming principles. It also highlights common errors and the correct syntax for different programming constructs. Additionally, it covers fundamental topics like data types, functions, and memory management.

Uploaded by

21 Ayush Devkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

C++ Theory

The document provides a series of C++ programming exercises, including code snippets and corrections for various concepts such as outputting text, using variables, control structures, loops, arrays, and object-oriented programming principles. It also highlights common errors and the correct syntax for different programming constructs. Additionally, it covers fundamental topics like data types, functions, and memory management.

Uploaded by

21 Ayush Devkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C++ THEORY

1. insert the missing part of the code below the output ‘hello
world!’.
Int main()
{
____print___<<”Hello World!”;
Return 0;
}
 #include<iostream.h>
int main()
{
Cout<<”Hello World!”;
Return 0;
}

2. Insert the new line after “Hello World”, by using a special


character:
Int main()
{
Cout<<” Hello World!___\n__”;
Cout<<”Im learning c++”;
Return 0;
}
 #include<iostream.h>
Using namespace std;
Int main()
{
Cout<<”Hello World!\n”;
Cout<<”I’m learning c++;
Return 0;
}
3. This is a single line comment __/**/ __
 No ,it is multi line comment.
4. _// __ this is a multi-line comment __//__.
 No , it is a single line comment.
5. Create a variable named myNum and assign the value of 50 to
it.
 #include<stdio.h>
#include<conio.h>
Void main()
Int myNum=50;
Cout<<”int myNum=”,%d;
Return 0;
6. Display the sum of 5+10,using two variables: x and y.
 #include<stdio.h>

#include<conio.h>

Int main()

Int x=5;

Int y=10;

Int sum=x+y;

Cout<<”The sum of”<<x<<”+”<<y<<”is:”<<sum;

Return 0;

7. Create a variable called z, assign x+y to it, and display the result.
 #include<stdio.h>
#include<conio.>
Void main()
Int x=5;
Int y=10;
Int z=x+y;
Cout<<”x+y =”,z;
Return 0;
8. Fill the missing parts to create three variables of the same
type , using a comma separated list:
 #include<iostream>
Using namespace std;
Int main()
{
Int x=5, y=6 , z=50;
Cout<<” the sum of x+y+z is:”<<x+y+z;
Return 0;
}
9. Use the missing parts to print the sum of two numbers.
Int x,y;
Int sum;
Cout<<”type a number:”;
__cin__>>__x_;
 #include<iostream>
Using namespace std;
Int main()
{
Int x=5,y=6;
Int sum;
Cout<<”Type a number:”;
Cin>>x;
Cout<<”Type another number:”;
Cin>>y;
Sum=x+y;
Cout<<”The sum is:”<<sum;
Return 0;
}
10. Fill the missing parts to print the sum of two numbers.
Int x,y;
Int sum;
Cout<<”type a number:”;
Cin>>__x_;
Cout<<”type another number:”;
Cin>>_y_;
Sum=x+y;
Cout<<”sum is:”<<x+y;
 #include<iostream>
Using namespace std;
Int main()
{
Int x=5,y=6;
Int sum;
Cout<<”Type a number:”;
Cin>>x;
Cout<<”Type another number:”;
Cin>>y;
Sum=x+y;
Cout<<”The sum is:”<<sum;
Return 0;
}
11. Add the correct data type for the following variables:
myNum =9;int
myDoubleNum =8.99;float
myLetter =’A’;char
myBool =false;Boolean
myText=”Hello World”;double
 #include <iostream>
Using namespace std;
Int main()
{
Int myNum=9;
Double myDoubleNum=8.99;
Char myLetter=‘A’;
Bool myBool=false;
String myText = “Hello World”;
Cout<< “myNum:”<<myNum<<endl;
Cout<< “myDoubleNum:”<< myDoubleNum<<endl;
Cout<< “myLetter:”<< myLetter<<endl;
Cout<< “myBool:”<< myBool<<endl;
Cout<< “myText:”<< myText<<endl;
Return 0;
}
12. create two Boolean variables,named yay and nay and add
appropriate values to them.
 #include<stdio.h>
#include<conio.h>
Void main()
{
Int a=10;
Int b=20;
If:
Int a+b=30;
Cout<<”yay”;
Else:
Cout<<”nay”;
Return 0;
}
13. Create a greeting variable and display the value of it in c+
+.
 #include<stdio.h>
#include<conio.h>
Int main()
{
Cout<<”Hello World!”;
Return 0;
14. Multiply 10 with 5 and print the result.
 #include<stdio.h>
#include<conio.h>
Void main()
Int a=10;
Int b=5;
Int c=a*b;
Cout<<”10*5”;
Cin>>c;
Return 0;
15. Divide 10 by 5 and print the result.
 #include<stdio.h>
#include<conio.h>
Void main()
Int a=10;
Int b=5;
Int c=ab;
Cout<<”10/5”;
Cin>>c;
Return 0;
16. Use the operator to add the value 5 to the variable x.
 {int x=10;
X+=5;
Cout<<”The new value of x is:<<x;
Return 0;
}

17. Use the correct function to print the length of thr txt
string.
String txt=”Hello”;
Cout<<__len str__;
 Int main(){
String txt:”Hello”;
Cout<<txt.length()<< txt.size()
Return 0;}
18. Access the first character (H) in myString and print the
result:
 {String myString=”Hello”;
cout<< mystrig.at(0);
endl;
return 0;}
19. Write function to print the highest value of x and y in c++.
 #include<iostream.h>
#include<stdio.h>
Void main(){
Int x=5;
Int y=10;
Cout<<max(x,y)
Return 0;
}
20. Use the correct function to print the square root of x.
#include <iostream.h>
#include<_stdio.h__>
Void main() {
Int x =64;
Cout<<__sqrt_(x);
}
 #include<iostream>
#include<cmath>
Using namespace std;
Int main()
{
Int x=64;
Cout<< “The square root of”<<x<< “is:”<<sqrt(x)<<endl;
Return 0;
}
21. Use the correct function to round the number 2,6 to its
nearest integer.
Floating point number.
#include<iostream.h>
#include<___>
Void main(){
Cout<<___(2,6);
}
 #include<iostream>
#include<cmath>
Using namespace std;
Int main()
{
Float number = 2.6;
Cout<< “The rounded value of”<<number<<
“is:”<<round(number)<<endl;
Return 0;
}
22. Fill in the missing parts to print the values 1(for true) and
0 (for false);
…
23. Print “Hello World” if x is greater than y:
Int x=50;
Int y=10;
__if x>y;_
Cout<<”Hello world”;
 #include <iostream>
Using namespace std;
Int main()
{
Int x = 50;
Int y = 10;
If(x>y) {
Cout<< “Hello World”;
}
Return 0;
}
24. Print “Hello World” if x is equal to y.
 #include <iostream>
Using namespace std;
Int main() {
Int x=10;
Int y=10;
If ( x==y){
Cout<< “Hello Word”;
}
Return 0;
}
25. Print “yes” if x is equal to y, otherwise print “No”, print
both values alternately.
 #include <iostream>
Using namespace std;
Int main(){
Int x=50;
Int y=50;
If (x==y){
Cout<< “yes\n”;
}
Else{
Cout<< “No\n”;
}
Cout<< “x;”<<x<< “\n”;
Cout<< “y;”<<y<< “\n”;
Return 0;
}
26. Print “1” if x is equal to y, print “2” if x is greater than y,
otherwise print “3”.
 #include<iostream>
Using namespace std;
Int main()
{
Int x =50;
Int y=50;
If (x==y){
Cout<< “1”;
} else if(x>y) {
Cout<< “2”;
} else {
Cout<< “3”;
}
Return 0;
}
27. Write down the syntax of switch case in c++
 Switch (variable)
{
Case1:
Break;
Case2:
Break;
Default;
}
28. Write a part of the program to print I as long I is less than
6.(While loop)
 #include<iostream.h>
Using namespce std;
Int main(){
Int i=0;
While (i<6){
Cout<< “I ”;
I++;
}
Return 0;
}
29. Use a for loop to print “Yes” 5 times.
 #include<iostream>
Using namespace std;
Int main(){
For (int i=0; i<5; i++){
Cout<< “Yes” << endl;
}
Return 0;
}
30. Write a program to stop the loop if I is 5.
 #include <iostream>
Using namespace std;
Int main(){
For (int i=0; i<10 ;i++)
If( i==5){
Break;
}
}
Cout<< “Loop stopped at i<5.” << endl;
Return 0;
}
31. Create an array of type string called cars.
 #include<iostream>
#include<string>
Using namespace std;
Int main(){
String cars[] = {“Toyota” , “BMW” , “Audi”};
Cout<< “The first car is:”<< cars[0]<<endl;
Cout<< “The second car is:”<< cars[1]<<endl;
Cout<< “The third car is:”<< cars[2]<<endl;
Return 0;
}
32. Print the value of the second element in the cars array.
 #include <iostream>
#include <string>
Using namespace std;
Int main(){
String cars []= {“Toyota” , “BMW” , “Audi”};
Cout<< “The second car is:”<< cars[1]<<endl;
Return 0;
}
33. Create a reference variable named meal,which should be
a reference to food variable.
 #include <iostream>
#include <string>
Using namespace std;
Int main() {
String food= “Pizza”;
String meal= food;
Cout<< “The meal is:” <<meal <<endl;
Return 0;
}
34. Get the memory address of the variable of the food
variable:
 #include <iostream>
#include <string>
Using namespace std;
Int main() {
String food = “Pizza”;
Cout<< “The memory address of ‘food’ is:” <<&food<< endl;
Return 0;
}
35. Use an access specifier to make members of MyClass
accessible from outside the class.
 #include<iostream>
Using namespace std;
Class MyClass {
Public:
Int myNum;
}
Int main() {
MyClass obj;
Obj.myNum =5;

Cout<< “The value of myNum is:” <<obj.myNum<< endl;

Return 0;

36. Create an object of MyClass called myObj and use it to set


the value of myNum to fifteen.
 #include <iostream>
Using namespace std;
Class MyClass {
Int myNum;
};
Int main(){
MyClass myobj;
myObj.myNum = 15;
cout<< “The value of myNum is:” << myObj.myNum <<endl;
return 0;
}
37. Create a constructor of myClass and call it:
 #include<iostream>
Using namespace std;
Class MyClass {
Public;
MyClass() {
Cout<< “Hello World!” <<endl;
}
};
Int main() {
MyClass myobj;
Return 0;
}
38. What are the four pillars of OOPs.
 Polymorphism
Inheritance
Encapsulation
abstraction

You might also like