ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
void f(int a[10]) { cout << (sizeof(a) == sizeof(int*)); }
int main() {
int arr[10];
cout << sizeof(arr) / sizeof(arr[0]);
f(arr);
}- A. 1010
- B. 100
- C. 101
- D. 11
Correct answer: C. 101
In main, sizeof(arr) is the size of the whole array (10 elements). A parameter declared as int a[10] is adjusted to int*, so inside f sizeof(a) is just the size of a pointer.
More OOP with C++ and Java questions
- What is printed? cpp struct S { int next() { static int c = 0; return ++c; } }; int main() { S a, b; a.next(); b.next(); cout << a.next(); }
- What is printed? cpp int main() { try { throw "oops"; } catch (string& s) { cout << "S"; } catch (const char s) { cout << s; } }
- Which line fails to compile? cpp struct A { A(int) { } }; struct B { explicit B(int) { } }; A f() { return 5; } // L1 B g() { return 5; }…
- Which line fails to compile? cpp int main() { int a[3] = {1, 2, 3}, b[3]; b = a; // L1 array<int, 3> c{1, 2, 3}, d; d = c; // L2 }
- What is printed? cpp template <sizet N> sizet len(int (&)[N]) { return N; } int main() { int a[7]; cout << len(a); // L1 int p = a; cout…
- What is printed? cpp struct S { void f() & { cout << "L"; } void f() && { cout << "R"; } }; int main() { S s; s.f(); S().f()…