Advanced OOPs Concepts

Last Updated :
Discuss
Comments

Question 1

Which of the following correctly declares a lambda that adds two integers?

  • [](int a, int b) { return a + b; }

  • [int a, int b] -> int { return a + b; }

  • lambda(int a, int b) { return a + b; }

  • [] { int a, b; return a + b; }

Question 2

Which of the following is true about namespaces in C++?

  • You must put a semicolon after closing the namespace block

  • Namespaces can be extended (i.e. declared in parts) across multiple files or places

  • You can inherit one namespace from another

  • Namespaces allow you to define access specifiers (public/private) inside them

Question 3

Which of these is not a valid use of the const keyword in C++?

  • Marking a member function so it cannot modify the object’s state

  • Declaring a constant local variable whose value cannot be changed after initialization

  • Declaring a pointer such that the pointer’s address cannot be changed

  • Declaring a variable without initializing it and then assigning a value later

Question 4

a

  • static

  • inline

  • volatile

  • friend

Question 5

Consider this lambda in C++:

C++
int a = 5;
auto f = [=]() mutable {
    a = 10;
    return a;
};
int b = f();

What is the value of b, and does a in the outer scope change?

  • b = 10, and the outer a becomes 10

  • b = 10, but outer a remains 5

  • Compiler error (cannot assign to a)

  • b = 5, outer a remains 5

Question 6

Which of the following correctly describes the purpose of RTTI in C++?

  • It enables compile-time template specialization

  • It allows checking and casting types safely at runtime (e.g. using typeid or dynamic_cast)

  • It provides automatic memory management (garbage collection)

  • It ensures that only one object of a given class is created

Question 7

What happens if two namespaces contain variables with the same name and both are used in the same scope using using namespace?

  • The compiler automatically merges them

  • The variable from the first namespace is always chosen

  • Ambiguity error occurs at compile time

  • The variable from the second namespace overrides the first

Question 8

What is the output of the following code?

C++
#include <iostream>
using namespace std;

void print(const int x) {
    // x = x + 1;  // Uncommenting this line will cause an error
    cout << x;
}

int main() {
    print(10);
    return 0;
}


  • 11

  • 10

  • Compiler error

  • Undefined behavior

Question 9

What will be the output of the following code?

C++
#include <iostream>
#include <typeinfo>
using namespace std;

class Base { virtual void f() {} };
class Derived : public Base {};

int main() {
    Base* b = new Derived();
    cout << typeid(*b).name();
    return 0;
}


  • Base

  • Derived

  • *b

  • Runtime error

Tags:

There are 9 questions to complete.

Take a part in the ongoing discussion