C++ Knowledge Challenge - CSU1287 - Shoolini U

FAQ's / Test Your Knowledge in C++

1. What is the primary purpose of the keyword "virtual" in C++?

A) To provide polymorphism

B) To provide dynamic binding

C) To allow overriding of base class functions in derived classes

D) All of the above

The key to this answer lies in your understanding of OOP concepts.

2. What is the output of the following program snippet?


class Base {
public:
    virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base {
public:
    void show() { cout<<"In Derived \n"; }
};
int main(void) {
    Base *bp = new Derived;
    bp->show();
}

A) In Base

B) In Derived

C) Compilation Error

D) None of the above

Here's a clue: "virtual" and "override" can often lead to surprising results...

3. What does the following C++ statement mean?

int *ptr = new int[10];

A) ptr is a pointer to an array of integers of size 10

B) ptr is an array of pointers to integers

C) ptr is a pointer to the first element of an array of 10 integers

D) ptr is a pointer to an integer array of 10 bytes

The answer reveals the power of dynamic memory allocation.

4. If class B is publicly derived from class A, which of the following is not true?

A) Each object of class B includes an object of class A

B) Class B can access protected members of class A, but not its private members

C) Class B is a subtype of class A

D) Class B can change the value of private data members of class A

Answer: D

5. What does the keyword "friend" do in a class definition?

A) It gives another class or function access to the private and protected members of the class

B) It makes a class or function a member of the class

C) It provides a way for a class to hide data from other classes

D) It makes a class or function a subclass of the class

Here's a hint: Think about the intimacy of the term "friend."

6. Which one is NOT a characteristic of Object Oriented Programming?

A) Inheritance

B) Data hiding

C) Polymorphism

D) Direct access to private data of classes

Answer: D

7. When does the constructor for a derived class call the constructor for the base class?

A) Whenever an object of the derived class is created

B) Only when the programmer specifically writes code to call the base class constructor

C) Whenever an object of the base class is created

D) Only when the derived class constructor is not defined

Recall the order of construction in inheritance...

8. Which one is not an access specifier in C++?

A) Public

B) Private

C) Protected

D) Confidential

Answer: D

9. What is the output of the following code snippet?


#include <iostream>
using namespace std;
class A {
public:
A() {cout << "Constructor A " << endl;}
~A() {cout << "Destructor A " << endl;}
};

class B : public A {
public:
B() {cout << "Constructor B " << endl;}
~B() {cout << "Destructor B " << endl;}
};

int main() {
B obj;
return 0;
}

A) Constructor A Constructor B Destructor B Destructor A

B) Constructor B Constructor A Destructor A Destructor B

C) Constructor A Constructor B Destructor A Destructor B

D) Constructor B Constructor A Destructor B Destructor A

Hint: Destruction is the reverse of construction...

10. Which statement about copy constructors is correct?

A) Copy constructor is called when an object is returned by value

B) Copy constructor is called when an object is passed by value as an argument

C) Copy constructor is called when compiler generates a temporary object

D) All of the above

Ponder this, the secret lies within the name itself...

11. What is the output of the following code snippet?


class A
{
public:
    A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};

class C: public B, public A
{
public:
C() { cout << "C's constructor called" << endl; }
};

int main()
{
C c;
return 0;
}

A) C's constructor called, B's constructor called, A's constructor called

B) A's constructor called, B's constructor called, C's constructor called

C) B's constructor called, A's constructor called, C's constructor called

D) None of the above

This question tests your understanding of constructor order in multiple inheritance...

12. What is a pure virtual function in C++?

A) A virtual function that must be overridden in every derived class

B) A virtual function that has no definition in the base class

C) A virtual function that can be overridden in the derived class

D) Both A and B

Answer: D

13. If you do not provide a constructor for a class, the compiler provides one for you. What does this constructor do?

A) Initializes all data members to zero

B) Allocates memory for the object

C) Does nothing

D) Both A and B

What secrets are hidden in the unseen code?

14. What is meant by function overriding in C++?

A) It refers to a derived class defining a function that is already defined in its base class

B) It refers to a class defining a function that is already defined in its base class

C) It refers to a derived class defining a function that is already defined in another derived class

D) It refers to a base class defining a function that is already defined in its derived class

Answer: A

15. What is the purpose of the "this" pointer?

A) To allow functions to reference the object that invoked them

B) To allow functions to reference any object of the class

C) To allow functions to modify the data members of the class

D) To allow functions to access private members of the class

A small keyword, yet its significance is deep...

16. What is the "diamond problem" in C++?

A) It is a problem caused by multiple inheritance

B) It is a problem caused by private inheritance

C) It is a problem caused by public inheritance

D) It is a problem caused by the "virtual" keyword

Answer: A

17. When does a compiler create a default copy constructor?

A) When we define a parameterized constructor

B) When we don’t define any constructor in the class

C) When we define an overloaded constructor

D) Both A and B

The compiler's duty is more than just error checking...

18. What is a namespace in C++?

A) It is a container for identifiers. It puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace

B) It is a class of objects

C) It is a special function used in classes

D) None of the above

Answer: A

19. What is the output of the following code snippet?


#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
int get() {return x;}
};

int main() {
MyClass obj(100);
cout << obj.get();
return 0;
}

A) 0

B) 1

C) 100

D) Compilation error

Remember, initializations occur before the body of constructors...

20. Can we have a class in C++ without any data member?

A) Yes

B) No

C) Only if it is a derived class

D) Only if it is a base class

Answer: A

21. When are static member functions invoked?

A) When an object of the class is created

B) When only static data members are accessed

C) Without the creation of an object

D) Both A and C

A hint: The keyword "static" comes with special privileges...

22. What is a conversion constructor in C++?

A) It is a constructor that can be used to convert a type to a class type

B) It is a constructor that converts the values of data members of a class

C) It is a constructor that is used for type conversion

D) It is a constructor that can be called with a single argument

Consider the essence of conversion...

23. What is the output of the following code snippet?


#include <iostream>
using namespace std;
class A {
public:
void print() { cout << "Class A" << endl; }
};

class B : public A {
public:
void print() { cout << "Class B" << endl; }
};

int main() {
A* a = new B();
a->print();
return 0;
}

A) Class A

B) Class B

C) Compilation error

D) None of the above

Answer: A

24. Which statement about virtual functions in C++ is NOT true?

A) They support dynamic binding

B) They are defined in the base class and overridden in the derived class

C) They cannot be static

D) They are invoked based on the type of the object, not the type of the pointer/reference

Answer: D

25. What is the output of the following code snippet?


#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A's constructor called" << endl; }
};

class B {
public:
B() { cout << "B's constructor called" << endl; }
};

class C: public B, public A {
public:
C() { cout << "C's constructor called" << endl; }
};

int main() {
C c;
return 0;
}

A) C's constructor called, B's constructor called, A's constructor called

B) A's constructor called, B's constructor called, C's constructor called

C) B's constructor called, A's constructor called, C's constructor called

D) None of the above

Revisit your knowledge on the sequence of constructor calls...

26. Can a friend function be used to override a member function?

A) Yes

B) No

C) Only in derived classes

D) Only in base classes

Answer: B

27. What is a virtual destructor?

A) A destructor that is called virtually

B) A destructor that is used to destroy virtual objects

C) A destructor that can be overridden in derived classes

D) A destructor that can be used to delete a polymorphic object

Contemplate the purpose of destructors in a polymorphic scenario...

28. What is the use of the "explicit" keyword in C++?

A) To convert implicit constructors to explicit

B) To prevent the compiler from using a constructor for implicit conversion

C) To specify explicit values for data members

D) To specify explicit functions in a class

Answer: B

29. What is the output of the following code snippet?


#include <iostream>
using namespace std;
class A {
public:
A(int x) { cout << "A's constructor called" << endl; }
};

class B: public A {
public:
B(): A(10) { cout << "B's constructor called" << endl; }
};

int main() {
B b;
return 0;
}

A) B's constructor called, A's constructor called

B) A's constructor called, B's constructor called

C) Compilation error

D) None of the above

Reflect upon the sequence of constructor calls in inheritance...

30. How does protected access specifier differ from private in classes?

A) Members declared as private are accessible from within the same class only

B) Members declared as protected are accessible from the class they are declared in and from any class derived from it

C) Members declared as protected are accessible from any class

D) Both A and B

Answer: D

31. What is the purpose of a destructor?

A) To initialize objects

B) To destroy objects

C) To allocate memory to objects

D) To copy objects

A little pondering and the name shall reveal itself...

32. What is the order of destruction for objects created from derived and base classes?

A) The derived class is destroyed first, followed by the base class

B) The base class is destroyed first, followed by the derived class

C) The order of destruction is random

D) None of the above

Answer: A

33. Which of the following is not a type of inheritance in C++?

A) Single inheritance

B) Multiple inheritance

C) Multilevel inheritance

D) Bilateral inheritance

Answer: D

34. What is the output of the following code snippet?


#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A's constructor called" << endl; }
};

class B {
public:
B() { cout << "B's constructor called" << endl; }
};

class C: public A, public B {
public:
C() { cout << "C's constructor called" << endl; }
};

int main() {
C c;
return 0;
}

A) C's constructor called, B's constructor called, A's constructor called

B) A's constructor called, B's constructor called, C's constructor called

C) B's constructor called, A's constructor called, C's constructor called

D) None of the above

Answer: B

35. Can pure virtual functions be private in C++?

A) Yes

B) No

C) Only in base classes

D) Only in derived classes

Dive deep into the realms of access specifiers and virtual functions...

36. What is the purpose of a pure virtual function?

A) To provide a base for other classes to inherit from

B) To create abstract base classes

C) To ensure that derived classes provide their own implementation of the function

D) Both B and C

Answer: D

37. What is the output of the following code snippet?


#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A's constructor called" << endl; }
~A() { cout << "A's destructor called" << endl; }
};

class B: public A {
public:
B() { cout << "B's constructor called" << endl; }
~B() { cout << "B's destructor called" << endl; }
};

int main() {
B b;
return 0;
}

A) A's constructor called, B's constructor called, B's destructor called, A's destructor called

B) A's constructor called, B's constructor called, A's destructor called, B's destructor called

C) B's constructor called, A's constructor called, A's destructor called, B's destructor called

D) B's constructor called, A's constructor called, B's destructor called, A's destructor called

Answer: A

38. What is the use of a copy constructor in C++?

A) To initialize an object from another of the same type

B) To make a copy of an existing object

C) To perform deep copy

D) All of the above

Think about copying...

39. Which of the following concepts makes C++ a multi-paradigm language?

A) Procedural programming

B) Object-oriented programming

C) Generic programming

D) All of the above

Answer: D

40. What is the diamond problem in multiple inheritance?

A) It is a problem when a class inherits from two classes that have a common base class

B) It is a problem when a class inherits from two classes that have the same method

C) It is a problem when a class inherits from two classes that have a common derived class

D) None of the above

Ponder upon the complexities of multiple inheritance...

41. In what scenario is a virtual destructor required?

A) When the base class has a virtual function

B) When the derived class has a virtual function

C) When we deal with a polymorphic object

D) All of the above

Answer: C

42. What happens when we attempt to delete a NULL pointer in C++?

A) Nothing

B) It results in a compile-time error

C) It results in a runtime error

D) It results in a segmentation fault

Think about the nature of NULL...

43. What is the default access specifier in a C++ class?

A) Public

B) Private

C) Protected

D) None

Answer: B

44. Which of the following is NOT a feature of Object-Oriented Programming?

A) Encapsulation

B) Polymorphism

C) Inheritance

D) Late Binding

E) Recursion

Try to picture the key pillars of OOP...

45. What is RTTI in C++?

A) Real Time Type Information

B) Run Time Type Information

C) Real Type Time Information

D) Run Time Type Interface

Answer: B

46. Can we use the "this" pointer in a static member function?

A) Yes

B) No

C) Only in derived classes

D) Only in base classes

Reflect on the nature of static functions...

47. What is a conversion constructor in C++?

A) A constructor that converts an object of one class to another class

B) A constructor that converts basic data types to objects

C) A constructor that takes one argument of a different type

D) None of the above

Answer: C

48. Which of the following correctly describes Object-Oriented Programming?

A) OOP is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

B) OOP is a programming style that is associated with the concept of Class, Objects and various other concepts revolving around these two, like Inheritance, Polymorphism, Abstraction, Encapsulation etc.

C) OOP allows for simplified programming. Its benefits include reusability, refactoring, extensibility, maintenance and efficiency.

D) All of the above

Answer: D

49. When does the compiler provide a default constructor?

A) If no constructor is defined

B) If a parameterized constructor is defined

C) If a destructor is not defined

D) None of the above

Think about the compiler's role in providing constructors...

50. Can we have a function overloading in different scopes?

A) Yes

B) No

C) Only in the global scope

D) Only in the local scope

Answer: A