ProgrammingVery hardProduct-company placement · C++
Which line fails to compile?
Which line fails to compile?
string a = "a" + "b"; // L1
string b = string("a") + "b"; // L2
string c = "a" + string("b"); // L3
string d = "a" "b"; // L4- A. L1 and L3
- B. L1 only
- C. L1 and L4
- D. L4 only
Correct answer: B. L1 only
"a" + "b" tries to add two const char* pointers, which is not allowed. As soon as one operand is a std::string, its operator+ is used (L2, L3). Adjacent string literals (L4) are joined by the compiler.
More OOP with C++ and Java questions
- What is printed? cpp int main() { string s = "hi"; try { cout << s.at(1); s.at(5); cout << "X"; } catch (const outofrange&) { cout << "R"…
- What is printed? cpp int main() { map<string, int> m; if (m["x"] == 0) cout << m.size(); cout << m.count("y") << m.size(); }
- What is printed? cpp int main() { set<int, greater<int>> s{3, 1, 3, 2}; for (int x : s) cout << x; cout << s.size(); }
- What is printed? cpp int main() { vector<int> v{1, 2, 3, 2, 4}; v.erase(remove(v.begin(), v.end(), 2), v.end()); cout << v.size() << ":"…
- What is wrong with this loop? cpp vector<int> v{1, 2, 3}; for (int x : v) if (x == 1) v.pushback(1);
- What is printed? cpp void log(int) { cout << "G"; } struct S { void log() { cout << "M"; } void run() { log(5); } }; int main() { S s…