Last Updated : 10 Apr, 2024

What will be the output for the below code snippet:

vector <int> vec = {1, 2, 3, 4, 5, 6};  // line 1
cout << vec.front() + 1 << \" \" << vec.back() + 1; 

(A) 1 6
(B) 2 7
(C) 2 6
(D) error in vector declaration in line 1


Answer: (B)

Explanation: line 1 is also one of the way of declaring vectors so there is no error in that. front() and back() are the reference to the first and last element of the vector.


Share your thoughts in the comments