Last Updated :
10 Apr, 2024
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> v;
for(int i = 1; i <= 5; i++)
v.push_back(i);
for(int i = 1; i <= 5; i++)
{
if(v[i] % 2 == 0) // line 1
cout << v[i] << \" \";
}
return 0;
}
Output of the above program will be
(A) 2 4
(B) 0 2 4
(C) 2 4 0
(D) None of the mentioned
Answer: (C)
Explanation:
v[index] returns the reference to the element present at index of vector \’v\’. So there is no error in the line 1.
Elements in the vector are as such:
v[0] = 1
v[1] = 2
v[2] = 3
v[3] = 4
v[4] = 5
So the for traverses and prints the even elements in the vector, element present at v[5] = 0 is also printed.
Share your thoughts in the comments
Please Login to comment...