Question 1
EasyWhich operator prints output to the console with cout in C++?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Programming Languages
Which operator prints output to the console with cout in C++?
->
::
<<
Answer: D
cout << value inserts into the output stream; cin >> x extracts from input.
What does the scope resolution operator :: do?
Accesses a name in a particular scope, such as a namespace or class
Compares two values
Allocates memory
Ends a statement
Answer: A
std::cout names cout in the std namespace; Box::count names a static member.
What is the output of this program?
#include <iostream>
using namespace std;
int main() {
int x = 1;
int &r = x;
r = 5;
cout << x;
return 0;
}
Error
0
Answer: C
A reference is another name for x; changing r changes x.
Which C++ keyword releases memory allocated with new?
release
delete
free
remove
Answer: B
Use delete for single objects and delete[] for arrays allocated with new[].
Which C++ standard container is a dynamic array that grows automatically?
std::map
std::list
std::vector
std::set
Answer: C
vector stores elements contiguously and supports push_back in amortised O(1).
What is the output of this program?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
cout << v.size();
return 0;
}
0
4
Answer: D
push_back adds three elements, so size() is 3.
What does the 'const' keyword on a variable mean?
It is stored on disk
Its value can't be changed after initialisation
It can be changed only once
It is global
Answer: B
The compiler rejects attempts to modify a const variable.
What is the output of this program?
#include <iostream>
using namespace std;
int main() {
cout << 5 / 2 << " " << 5.0 / 2;
return 0;
}
2.5 2.5
3 2.5
Answer: D
5 / 2 is integer division (2); 5.0 / 2 is floating-point division (2.5).
What is the output of this program?
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abc";
cout << s.size();
return 0;
}
4
2
Answer: C
size() returns the number of characters in the string.
What is the output of this program?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {3, 1, 2};
v.push_back(5);
cout << v.size();
return 0;
}
4
11
Answer: A
push_back adds one element to the three already there.
5
Garbage
2
3
2 2
2 2.5
3
abc
3
5