Site icon T4Tutorials.com

What is the output of the following code in C++?

1. What is the output of the following code in C++? (Variables & Arithmetic)
int a=4,b=6; cout<<a*b;

(A) 24


(B) 10


(C) 46


(D) Error



2. What is the output of the following code in C++? (Increment Operator)
int a=5; cout<<a++<<” “<<a;

(A) 5 5


(B) 6 6


(C) 5 6


(D) 6 5



3. What is the output of the following code in C++? (Pre-increment)
int a=5; cout<<++a;

(A) 6


(B) 5


(C) 7


(D) Error



4. What is the output of the following code in C++? (If-Else)
int a=10; if(a>5) cout<<“Yes”; else cout<<“No”;

(A) No


(B) Yes


(C) 1


(D) Error



5. What is the output of the following code in C++? (For Loop)
for(int i=1;i<=3;i++) cout<<i;

(A) 123


(B) 012


(C) 321


(D) 111



6. What is the output of the following code in C++? (While Loop)
int i=1; while(i<=3){cout<<i;i++;}

(A) 012


(B) 321


(C) 123


(D) Infinite loop



7. What is the output of the following code in C++? (Do-While Loop)
int i=3; do{cout<<i;i–;}while(i>0);

(A) 123


(B) 210


(C) 321


(D) 012



8. What is the output of the following code in C++? (Break Statement)
for(int i=1;i<=5;i++){ if(i==3) break; cout<<i; }

(A) 12345


(B) 123


(C) 1234


(D) 12



9. What is the output of the following code in C++? (Continue Statement)
for(int i=1;i<=5;i++){ if(i==3) continue; cout<<i; }

(A) 1245


(B) 12345


(C) 135


(D) 12



10. What is the output of the following code in C++? (Ternary Operator)
int a=4; cout<<(a%2==0?”Even”:”Odd”);

(A) Odd


(B) Error


(C) 0


(D) Even



11. What is the output of the following code in C++? (Logical Operators)
int a=5; cout<<(a>3 && a<10);

(A) 0


(B) Error


(C) True


(D) 1



12. What is the output of the following code in C++? (Relational Operators)
cout<<(10!=5);

(A) 0


(B) False


(C) True


(D) 1



13. What is the output of the following code in C++? (Switch Statement)
int x=2; switch(x){case 1:cout<<“One”;break; case 2:cout<<“Two”;break; default:cout<<“Other”;}

(A) One


(B) Error


(C) Other


(D) Two



14. What is the output of the following code in C++? (Array)
int arr[3]={1,2,3}; cout<<arr[1];

(A) 1


(B) 3


(C) 2


(D) Error



15. What is the output of the following code in C++? (String Length)
string s=”C++”; cout<<s.length();

(A) 3


(B) 2


(C) 4


(D) Error



16. What is the output of the following code in C++? (Function Call)
void show(){cout<<“Hello”;} int main(){show();}

(A) Error


(B) show


(C) Hello


(D) No output



17. What is the output of the following code in C++? (Recursion)
int fun(int n){ if(n==0) return 0; return n+fun(n-1);} cout<<fun(3);

(A) 3


(B) Error


(C) 9


(D) 6



18. What is the output of the following code in C++? (Pointer)
int a=10; int *p=&a; cout<<*p;

(A) 10


(B) Address of a


(C) 0


(D) Error



19. What is the output of the following code in C++? (Reference)
int a=5; int &r=a; r=10; cout<<a;

(A) 5


(B) 0


(C) 10


(D) Error



20. What is the output of the following code in C++? (Dynamic Memory)
int *p=new int(5); cout<<*p;

(A) 0


(B) Error


(C) Garbage


(D) 5



21. What is the output of the following code in C++? (Class & Object)
class Test{public:int x=10;}; Test t; cout<<t.x;

(A) 10


(B) 0


(C) Garbage


(D) Error



22. What is the output of the following code in C++? (Constructor)
class A{public:A(){cout<<“A”;}}; A obj;

(A) No output


(B) Constructor


(C) Error


(D) A



23. What is the output of the following code in C++? (Destructor)
class A{public:~A(){cout<<“X”;}}; int main(){A obj;}

(A) No output


(B) X


(C) Error


(D) XX



24. What is the output of the following code in C++? (Inheritance)
class A{public:void show(){cout<<“A”;}}; class B:public A{}; int main(){B obj; obj.show();}

(A) A


(B) B


(C) AB


(D) Error



25. What is the output of the following code in C++? (Function Overloading)
void fun(int x){cout<<“Int”;} void fun(double x){cout<<“Double”;} int main(){fun(5);}

(A) Error


(B) Double


(C) Int


(D) No output



26. What is the output of the following code in C++? (Virtual Function)
class A{public:virtual void show(){cout<<“A”;}}; class B:public A{public:void show(){cout<<“B”;}}; int main(){A* ptr; B obj; ptr=&obj; ptr->show();}

(A) A


(B) AB


(C) B


(D) Error



1000+ Programming C Plus Plus MCQs

Highly Recommended C++  Important MCQs with Explanation

  1. Which symbol is used to create multiple inheritances?
  2. If a derived class object is created, which constructor is called first?
  3. Which of the following is not a type of constructor?
  4. a ____ is a member function that is automatically called when a class object is __.
  5. What is the output of the following code in C++?
  6. How many times will the following code print?
  7. Which of the following is a procedural language?
  8. Which of the following is not a programming language?
  9. Which of the following is not an example of high-level language?
  10. While declaring pointer variables which operator do we use?
  11. To which does the function pointer point to?
  12. Which of these best describes an array?
  13. Which of the following is a Python Tuple?

Other important MCQs

Data Structure

Which of the following is not the type of queue?
Database

If one attribute is a determinant of the second, which in turn is a determinant of the third, then the relation cannot be
Python
Which of the following is a Python Tuple?

Exit mobile version