3.3 C++ language constructs with objects and classes

3.3. C++ LANGUAGE CONSTRUCT WITH OBJECT AND CLASS

 

NAMESPACE 

 

  1. What is a namespace in C++?

a) A way to define functions

b) A way to group related identifiers

c) A way to declare variables

d) A way to control access to class members
 

Answer: b) A way to group related identifiers
 

Explanation: Namespaces in C++ are used to group related identifiers (such as functions, classes, and variables) together to avoid naming conflicts.

 

  1. Which keyword is used to define a namespace in C++?

a) class

b) namespace

c) struct

d) using
 

Answer: b) namespace
 

Explanation: The keyword "namespace" is used to define a namespace in C++.

 

  1. What is the purpose of using namespaces?

a) To increase code readability

b) To reduce compilation time

c) To avoid naming conflicts

d) To improve runtime performance
 

Answer: c) To avoid naming conflicts
 

Explanation: One of the main purposes of namespaces is to avoid naming conflicts by encapsulating identifiers within a named scope.

 

  1. Which of the following is a valid way to access a function defined in a namespace?

a) function()

b) namespace::function()

c) using namespace function;

d) function::namespace();
 

Answer: b) namespace::function()
 

Explanation: To access a function defined in a namespace, you use the scope resolution operator "::" followed by the namespace name and then the function name.

 

  1. Can namespaces be nested in C++?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Namespaces can be nested within other namespaces in C++.

 

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

a) They are accessible from other translation units

b) They have external linkage

c) They are used for forward declarations only

d) They are limited to the current translation unit
 

Answer: d) They are limited to the current translation unit
 

Explanation: Unnamed namespaces in C++ are limited to the current translation unit and provide internal linkage.

 

  1. What happens if two namespaces have the same name?

a) The compiler throws an error

b) Both namespaces are merged

c) Only the first namespace is considered

d) The program becomes ambiguous
 

Answer: a) The compiler throws an error
 

Explanation: If two namespaces have the same name, the compiler throws an error due to conflicting declarations.

 

  1. Which directive is used to bring an entire namespace into the current scope in C++?

a) using

b) import

c) include

d) namespace
 

Answer: a) using
 

Explanation: The "using" directive is used to bring an entire namespace into the current scope in C++.

  1. What is the output of the following code?

#include <iostream>

namespace A {

    int x = 10;

}

namespace B {

    int x = 20;

}

int main() {

    using namespace A;

    std::cout << x;

    return 0;

}

}

a) 10

b) 20

c) Compilation error

d) Runtime error
 

Answer: a) 10
 

Explanation: The code brings the entire namespace A into the current scope using "using namespace A;" and then prints the value of variable x which is 10.

 

  1. Which of the following is not a valid namespace declaration?

a) namespace { }

b) namespace name { }

c) namespace name = value;

d) namespace name = identifier;
 

Answer: c) namespace name = value;
 

Explanation: This syntax is not valid for namespace declaration. Namespaces can be declared using the first three options.

 

  1. In which C++ standard were namespaces introduced?

a) C++98

b) C++03

c) C++11

d) C++17
 

Answer: a) C++98
 

Explanation: Namespaces were introduced in the original C++ standard, C++98.

 

  1. Which of the following is not a valid namespace rule in C++?

a) You can nest a namespace within another namespace.

b) Namespaces can be overloaded across different translation units.

c) Namespaces can be reopened and extended with additional declarations.

d) Namespaces provide a mechanism for avoiding name clashes in large projects.
 

Answer: b) Namespaces can be overloaded across different translation units.
 

Explanation: Namespaces cannot be overloaded across different translation units. They must be unique within each translation unit.

 

  1. What is the scope of a namespace in C++?

a) Global

b) Local

c) File

d) Program
 

Answer: c) File
 

Explanation: The scope of a namespace in C++ is limited to the file in which it is declared.

 

  1. Which of the following is the correct syntax to access a nested namespace in C++?

a) namespace::nested_namespace::identifier

b) namespace::identifier::nested_namespace

c) nested_namespace::namespace::identifier

d) identifier::namespace::nested_namespace
 

Answer: a) namespace::nested_namespace::identifier
 

Explanation: To access a nested namespace, you use the scope resolution operator "::" in the correct order.

 

  1. What happens if you declare two variables with the same name in different namespaces?

a) It causes a compilation error.

b) The variable declared in the innermost namespace takes precedence.

c) Both variables coexist without any conflict.

d) It causes a runtime error.
 

Answer: c) Both variables coexist without any conflict.
 

Explanation: Each variable resides within its respective namespace, so there's no conflict even if they have the same name.

FUNCTION OVERLOADING 

  1. What is function overloading in C++?

a) Redefining a function with a different return type

b) Defining multiple functions with the same name but different parameters

c) Defining multiple functions with the same name and same parameters

d) Redefining a function with different access specifiers
 

Answer: b) Defining multiple functions with the same name but different parameters
Explanation: Function overloading allows defining multiple functions in the same scope with the same name but different parameter lists.

  1. Which of the following is true regarding function overloading?

a) Overloaded functions must have the same return type.

b) Overloaded functions must have different return types.

c) Overloaded functions must have the same number of parameters.

d) Overloaded functions must have different parameter types or a different number of parameters.
 

Answer: d) Overloaded functions must have different parameter types or a different number of parameters.
 

Explanation: In function overloading, functions must differ in their parameter types or number of parameters to be distinguished.

  1. Can function overloading be based solely on the return type of the function?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Function overloading is based on parameters, not the return type. Functions with the same name and parameter list but different return types are not considered overloaded.

  1. Which of the following is not considered when determining if two functions are overloaded?

a) Function name

b) Return type

c) Parameter type

d) Number of parameters
 

Answer: b) Return type
 

Explanation: Function overloading is based on the function name and the parameters it takes, not on the return type.

  1. Can functions be overloaded based on whether their parameters are passed by value or by reference?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Functions can be overloaded based on whether their parameters are passed by value, by reference, or by pointer.

  1. Which of the following is not a valid way to overload a function in C++?

a) Changing the order of parameters

b) Changing the const-qualification of parameters

c) Changing the name of the function

d) Changing the default arguments of parameters
 

Answer: c) Changing the name of the function
 

Explanation: Function overloading requires the same function name with different parameters; changing the function name would create a different function.

  1. What is the significance of function overloading in C++?

a) It allows defining functions with multiple return types.

b) It simplifies code by providing multiple ways to call a function.

c) It improves runtime performance.

d) It allows defining functions with different access specifiers.
 

Answer: b) It simplifies code by providing multiple ways to call a function.
 

Explanation: Function overloading provides a way to create functions that perform similar tasks but can accept different types of parameters.

  1. Can the return type alone differentiate between two overloaded functions?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Overloaded functions must have different parameter lists; the return type alone cannot differentiate between them.

  1. What happens if a function is overloaded with another function but differs only in the default arguments?

a) It causes a compilation error.

b) The function with default arguments is called if no arguments are provided.

c) The function without default arguments is called if no arguments are provided.

d) It causes a runtime error.
 

Answer: c) The function without default arguments is called if no arguments are provided.
 

Explanation: In function overloading, default arguments do not participate in overload resolution unless specified explicitly.

  1. Which of the following is true about the scope of overloaded functions?

a) Overloaded functions must have the same scope.

b) Overloaded functions can have different scopes.

c) Overloaded functions must have global scope.

d) Overloaded functions must have local scope.
 

Answer: a) Overloaded functions must have the same scope.
 

Explanation: Overloaded functions must have the same scope; they cannot be overloaded across different scopes.

  1. Can member functions of a class be overloaded?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Member functions of a class can be overloaded just like standalone functions.

  1. What is the role of the compiler in function overloading?

a) The compiler selects the most appropriate overloaded function at compile time based on the arguments provided.

b) The compiler creates multiple versions of the same function with different parameter lists.

c) The compiler checks for syntax errors in overloaded functions.

d) The compiler defers function resolution until runtime.
 

Answer: a) The compiler selects the most appropriate overloaded function at compile time based on the arguments provided.
 

Explanation: The compiler determines the best match for an overloaded function based on the arguments provided and binds the call to that function at compile time.

  1. Can functions be overloaded based on the constantness of parameters?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Functions can be overloaded based on whether parameters are declared as const or non-const.

  1. Which of the following is true about the conversion sequences in function overloading?

a) The compiler always prefers an exact match for the parameters.

b) The compiler prefers promotions over standard conversions.

c) The compiler prefers standard conversions over promotions.

d) The compiler ignores conversion sequences in function overloading.
 

Answer: a) The compiler always prefers an exact match for the parameters.
 

Explanation: The compiler prefers an exact match for the parameters over any conversions or promotions in function overloading.

  1. What happens if no suitable overloaded function is found during compilation?

a) It causes a compilation error.

b) It selects the function with the closest match.

c) It selects the function with the least number of parameters.

d) It defers function resolution until runtime.
 

Answer: a) It causes a compilation error.
 

Explanation: If no suitable overloaded function is found during compilation, it results in a compilation error, indicating an ambiguity or lack of matching function.

INLINE FUNCTION

  1. What is an inline function in C++?

a) A function defined within a class

b) A function that is expanded in place at each call site

c) A function declared with the keyword "inline"

d) A function that performs input/output operations
 

Answer: c) A function declared with the keyword "inline"
 

Explanation: An inline function is a function defined with the "inline" keyword, suggesting to the compiler that it should be expanded in place at each call site rather than being invoked through a function call.

  1. What is the main advantage of using inline functions in C++?

a) Improved code readability

b) Reduced code duplication

c) Increased runtime performance

d) Enhanced error handling
 

Answer: c) Increased runtime performance
 

Explanation: Inline functions can lead to improved runtime performance by avoiding the overhead of function call mechanisms.

  1. When should you use inline functions?

a) For large, complex functions

b) For frequently called, small functions

c) For functions with recursion

d) For functions with switch-case statements
 

 

Answer: b) For frequently called, small functions
 

Explanation: Inline functions are most effective for small, frequently called functions where the overhead of a function call may be significant.

  1. Can an inline function contain a loop?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Inline functions can contain loops, conditional statements, and other typical constructs found in regular functions.

  1. What happens if an inline function becomes too large?

a) It causes a compilation error

b) It is automatically converted to a regular function

c) It may degrade runtime performance

d) It increases code readability
 

Answer: c) It may degrade runtime performance
 

Explanation: If an inline function becomes too large, it may lead to increased code size and potentially degrade runtime performance due to increased memory usage and instruction cache misses.

  1. Which of the following is a limitation of inline functions?

a) They cannot be recursive

b) They cannot contain loops

c) They cannot have a return type

d) They cannot be declared within a class
 

Answer: a) They cannot be recursive
 

Explanation: Inline functions cannot be recursive because the compiler must know the exact number of function calls to inline the function at compile time.

  1. What happens if a function declared as "inline" is defined outside the class or header file?

a) It causes a compilation error

b) It is automatically inlined

c) The "inline" keyword is ignored

d) It leads to linker errors
 

Answer: c) The "inline" keyword is ignored
 

Explanation: If a function declared as "inline" is defined outside the class or header file, the "inline" keyword is typically ignored, and the function may not be inlined by the compiler.

  1. Can a function defined in a header file automatically be inlined?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Functions defined in header files can be automatically inlined by the compiler if the function is small and suitable for inlining.

  1. Which of the following is a scenario where using inline functions is not recommended?

a) Mathematical computations

b) Getter and setter functions

c) Complex algorithms

d) Simple utility functions
 

Answer: c) Complex algorithms
 

Explanation: Inline functions are not recommended for complex algorithms as they may lead to code bloat and decreased performance.

  1. What is the syntax for declaring an inline function in C++?

a) inline returnType functionName(parameters)

b) returnType functionName(parameters) inline

c) returnType inline functionName(parameters)

d) functionName(parameters) inline returnType
 

Answer: c) returnType inline functionName(parameters)
 

Explanation: The "inline" keyword is placed before the function declaration, followed by the return type and function name.

  1. Which of the following statements is true about inline functions and templates?

a) Inline functions cannot be used with templates.

b) Templates must always be inline.

c) Inline functions can be used with templates.

d) Templates negate the need for inline functions.
 

Answer: c) Inline functions can be used with templates.
 

Explanation: Inline functions can be used with templates to improve performance and reduce code bloat.

  1. What is one potential drawback of using inline functions?

a) Increased code size

b) Reduced runtime performance

c) Limited code readability

d) Dependency on external libraries
 

Answer: a) Increased code size
 

Explanation: Inlining functions may lead to increased code size due to duplicating the function's code at each call site.

  1. How does the "inline" keyword affect the function's linkage?

a) It makes the function internal linkage

b) It makes the function external linkage

c) It has no effect on the function's linkage

d) It depends on the compiler implementation
 

Answer: c) It has no effect on the function's linkage
 

Explanation: The "inline" keyword does not affect the function's linkage; it only suggests to the compiler to inline the function if possible.

  1. Can a function declared as "inline" be defined in a separate source file?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: A function declared as "inline" can be defined in a separate source file, but the "inline" keyword might be ignored by the compiler.

  1. In which section of the C++ standard library are most inline functions defined?

a) <iostream>

b) <cstdlib>

c) <cmath>

d) <algorithm>
 

Answer: c) <cmath>
 

Explanation: Many mathematical functions in the C++ standard library, such as trigonometric functions, are often defined as inline functions in the <cmath> header.

DEFAULT ARGUMENT

  1. What is a default argument in C++?

a) An argument provided by the caller of a function

b) An argument specified in the function declaration, which is used if the caller does not provide a value

c) An argument that has a default value set by the compiler

d) An argument passed by reference to a function
 

Answer: b) An argument specified in the function declaration, which is used if the caller does not provide a value
 

Explanation: A default argument is a value specified in the function declaration that is used when the caller does not provide a corresponding argument.

  1. Which of the following statements about default arguments is true?

a) Default arguments must be specified in the function definition.

b) Default arguments must be specified for all parameters of a function.

c) Default arguments are evaluated at runtime.

d) Default arguments must appear at the beginning of the parameter list.
 

Answer: d) Default arguments must appear at the beginning of the parameter list.
 

Explanation: Default arguments must appear at the end of the parameter list after any non-default arguments.

  1. Can default arguments be used in member functions of a class?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Default arguments can be used in member functions of a class, similar to non-member functions.

  1. What is the purpose of default arguments?

a) To specify the return type of a function

b) To specify the parameters of a function

c) To provide flexibility by allowing callers to omit certain arguments

d) To enforce strict parameter passing rules
 

Answer: c) To provide flexibility by allowing callers to omit certain arguments
 

Explanation: Default arguments allow callers of a function to omit certain arguments, providing flexibility in function calls.

  1. Can default arguments be redefined in function declarations and definitions?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Default arguments can be specified in both the function declaration and definition, but once a default argument is provided in the declaration, subsequent declarations and definitions must also specify the default argument for that parameter.

  1. Which of the following is true about default arguments in C++?

a) Default arguments can only be constants.

b) Default arguments can be expressions evaluated at runtime.

c) Default arguments can only be applied to built-in types.

d) Default arguments cannot be provided for reference parameters.
 

Answer: b) Default arguments can be expressions evaluated at runtime.
 

Explanation: Default arguments can be expressions evaluated at runtime, not just constants.

  1. What happens if a default argument is provided in the function declaration but not in the definition?

a) It causes a compilation error.

b) The default argument is taken from the declaration.

c) The function becomes overloaded.

d) The default argument is set to 0.
 

Answer: b) The default argument is taken from the declaration.
 

Explanation: If a default argument is provided in the function declaration but not in the definition, the default argument specified in the declaration is used.

  1. Can default arguments be specified for some parameters but not others in the same function?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Default arguments can be specified for some parameters while leaving others without default arguments in the same function.

  1. Which of the following is a valid declaration of a function with default arguments?

a) void func(int a = 5, int b);

b) void func(int a, int b = 5);

c) void func(int a = 5, int b = 10);

d) void func(int a = 5; int b = 10);
 

Answer: c) void func(int a = 5, int b = 10);
 

Explanation: The correct syntax for declaring a function with default arguments is to specify the default values in the function declaration.

  1. Can default arguments be overridden by providing explicit arguments during function invocation?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Default arguments can be overridden by providing explicit arguments during function invocation, in which case the explicit argument values are used.

  1. Which of the following is not a valid way to provide default arguments for a function?

a) Using literals

b) Using global variables

c) Using expressions

d) Using function calls
 

Answer: b) Using global variables
 

Explanation: Default arguments must be constant expressions or literals; they cannot rely on variables, including global variables.

  1. What happens if a default argument is specified in the function declaration but omitted in the function definition?

a) It causes a compilation error.

b) The default argument is set to 0.

c) The default argument is taken from the definition.

d) The default argument is taken from the declaration.
 

Answer: d) The default argument is taken from the declaration.
 

Explanation: If a default argument is specified in the function declaration but omitted in the definition, the default argument from the declaration is used.

  1. Can default arguments be provided for array parameters?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Default arguments cannot be provided for array parameters; however, they can be provided for pointers or references.

  1. Can default arguments be specified for variadic functions (functions taking a variable number of arguments)?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Default arguments cannot be specified for variadic functions because the number of arguments passed to such functions can vary.

  1. What is the scope of default arguments in C++?

a) Local to the function in which they are defined

b) Global to the entire program

c) Limited to the translation unit

d) Limited to the class in which they are defined
 

Answer: a) Local to the function in which they are defined
 

Explanation: Default arguments are local to the function in which they are defined and do not affect other functions or parts of the program.

 

PASS/RETURN BY REFERENCE

  1. What is passing by reference in C++?

a) Passing the address of a variable to a function

b) Passing a copy of a variable to a function

c) Passing the value of a variable to a function

d) Passing a reference to a variable to a function
 

Answer: d) Passing a reference to a variable to a function
 

Explanation: Passing by reference involves passing a reference (or alias) to a variable, allowing the function to directly manipulate the original variable.

  1. Which symbol is used to denote a reference variable in C++?

a) *

b) &

c) %

d) $
 

Answer: b) &
 

Explanation: In C++, the "&" symbol is used to denote a reference variable.

  1. What is the advantage of passing by reference in C++?

a) It reduces memory usage

b) It avoids unnecessary copies of large data structures

c) It simplifies function declarations

d) It prevents the function from modifying the original variable
 

Answer: b) It avoids unnecessary copies of large data structures
 

Explanation: Passing by reference avoids the overhead of copying large data structures, improving performance and memory usage.

  1. When a parameter is passed by reference to a function, can the function modify the original value of the parameter?

a) Yes, always

b) Yes, but only if the parameter is declared as const

c) No, never

d) No, only the copy of the parameter can be modified
 

Answer: a) Yes, always
 

Explanation: When a parameter is passed by reference, the function can modify the original value of the parameter.

  1. In which situation would you typically use pass by reference instead of pass by value?

a) When modifying the original value of a parameter within a function

b) When the function requires a constant value

c) When the function requires a copy of the parameter

d) When the function does not need access to the parameter
 

Answer: a) When modifying the original value of a parameter within a function
 

Explanation: Passing by reference is typically used when the function needs to modify the original value of a parameter.

  1. What is the syntax for passing a variable by reference to a function in C++?

a) functionName(variable)

b) functionName(&variable)

c) functionName(*variable)

d) functionName(%variable)
 

Answer: b) functionName(&variable)
 

Explanation: To pass a variable by reference to a function, you use the "&" symbol before the variable name in the function call.

  1. Can a function return a reference in C++?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Functions in C++ can return references to variables, allowing the caller to directly access the original variable.

  1. What is the main advantage of returning by reference in C++?

a) It reduces memory usage

b) It simplifies function declarations

c) It avoids returning copies of large data structures

d) It allows the function to be declared as const
 

Answer: c) It avoids returning copies of large data structures
 

Explanation: Returning by reference avoids the overhead of copying large data structures, improving performance and memory usage.

  1. Which of the following statements about returning by reference is true?

a) Returning by reference always returns a copy of the original variable

b) Returning by reference prevents the original variable from being modified

c) Returning by reference allows the caller to modify the original variable

d) Returning by reference is not allowed in C++
 

Answer: c) Returning by reference allows the caller to modify the original variable
 

Explanation: Returning by reference allows the caller to directly access and modify the original variable returned by the function.

  1. What is the syntax for returning a reference from a function in C++?

a) returnType functionName()

b) returnType functionName(variable)

c) returnType functionName(&variable)

d) returnType& functionName()
 

Answer: d) returnType& functionName()
 

Explanation: To return a reference from a function, you specify the return type followed by "&" before the function name.

  1. Which of the following is a potential drawback of passing by reference in C++?

a) It increases memory usage

b) It can lead to aliasing issues

c) It requires additional syntax for function calls

d) It prevents functions from modifying the original variable
 

Answer: b) It can lead to aliasing issues
 

Explanation: Passing by reference can lead to aliasing issues where multiple references refer to the same memory location, potentially causing unintended side effects.

  1. In C++, can you return a reference to a local variable from a function?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Returning a reference to a local variable from a function results in undefined behavior because the local variable goes out of scope when the function returns.

  1. Can a function return a reference to a dynamically allocated object?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Functions can return references to dynamically allocated objects, but proper memory management is required to prevent memory leaks.

  1. Which of the following statements about passing by reference is false?

a) Passing by reference allows modifications to the original variable

b) Passing by reference can improve performance by avoiding unnecessary copies

c) Passing by reference requires the use of pointers

d) Passing by reference is indicated by the "&" symbol in the function declaration
 

Answer: c) Passing by reference requires the use of pointers
 

Explanation: Passing by reference does not require the use of pointers; references provide a safer and more intuitive way to pass variables.

  1. What happens if a function expects a reference parameter but is called with a non-reference argument?

a) It causes a compilation error.

b) It automatically converts the argument to a reference.

c) It creates a temporary reference to the argument.

d) It creates a copy of the argument.
 

Answer: d) It creates a copy of the argument.
 

Explanation: If a function expects a reference parameter but is called with a non-reference argument, a copy of the argument is made to match the function's parameter type.

INTRODUCTION TO CLASS AND OBJECTS

  1. What is a class in C++?

a) A blueprint for creating objects

b) A built-in data type

c) A function that returns multiple values

d) A collection of unrelated functions
 

Answer: a) A blueprint for creating objects
 

Explanation: A class in C++ serves as a blueprint for creating objects, defining their structure and behavior.

  1. What is an object in C++?

a) An instance of a class

b) A reserved keyword

c) A function that takes multiple arguments

d) A data structure
 

Answer: a) An instance of a class
 

Explanation: An object is an instance of a class, created based on the blueprint provided by the class.

  1. Which keyword is used to declare a class in C++?

a) class

b) object

c) struct

d) instance
 

Answer: a) class
 

Explanation: The keyword "class" is used to declare a class in C++.

  1. What does the term "encapsulation" refer to in object-oriented programming?

a) Binding data and functions that manipulate the data together

b) Hiding data within a class

c) Restricting access to class members

d) Exposing internal implementation details
 

Answer: a) Binding data and functions that manipulate the data together
 

Explanation: Encapsulation refers to the practice of binding data and functions that operate on the data together within a class.

  1. Which access specifier allows class members to be accessed from outside the class?

a) public

b) private

c) protected

d) friend
 

Answer: a) public
 

Explanation: The "public" access specifier allows class members to be accessed from outside the class.

  1. Which access specifier restricts access to class members to only within the class itself?

a) public

b) private

c) protected

d) friend
 

Answer: b) private
 

Explanation: The "private" access specifier restricts access to class members to only within the class itself.

  1. What is the default access specifier for members of a class in C++?

a) public

b) private

c) protected

d) friend
 

Answer: b) private
 

Explanation: By default, members of a class are private unless specified otherwise.

  1. Which member function is automatically called when an object is created?

a) initialize()

b) main()

c) constructor()

d) destructor()
 

Answer: c) constructor()
 

Explanation: The constructor is automatically called when an object is created, allowing initialization of object properties.

  1. What is the purpose of a destructor in C++?

a) To create objects

b) To destroy objects

c) To copy objects

d) To initialize objects
 

Answer: b) To destroy objects
 

Explanation: The destructor is called when an object is destroyed, allowing cleanup of resources held by the object.

  1. Which keyword is used to create an instance of a class in C++?

a) class

b) object

c) instance

d) new
 

Answer: b) object
 

Explanation: An instance of a class is created using the class name followed by the object name.

  1. Can a class have multiple constructors in C++?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: A class can have multiple constructors in C++, allowing for different ways to initialize objects.

  1. Which of the following best describes the relationship between a class and an object?

a) A class is a blueprint, and an object is an instance created from the blueprint.

b) A class is an instance, and an object is a blueprint created from the instance.

c) A class and an object are the same thing.

d) A class and an object are unrelated concepts.
 

Answer: a) A class is a blueprint, and an object is an instance created from the blueprint.
 

Explanation: A class defines the structure and behavior of objects, while objects are instances created based on the class blueprint.

  1. Which of the following is true about static members in a class?

a) Static members belong to each individual object of the class.

b) Static members are accessible only within the class.

c) Static members are shared among all objects of the class.

d) Static members are created and destroyed with each object.
 

Answer: c) Static members are shared among all objects of the class.
 

Explanation: Static members are shared among all objects of the class, rather than belonging to individual objects.

  1. Can a class contain other classes as members?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: A class can contain other classes as members, known as composition or containment.

  1. Which of the following is not a characteristic of object-oriented programming?

a) Inheritance

b) Encapsulation

c) Polymorphism

d) Procedural programming
 

Answer: d) Procedural programming
 

Explanation: Procedural programming is not a characteristic of object-oriented programming; it is a separate programming paradigm.

ACCESS SPECIFIERS

  1. What is the purpose of access specifiers in C++?

a) To specify the size of data members

b) To determine the order of execution of member functions

c) To control the visibility of class members

d) To define the lifetime of objects
 

Answer: c) To control the visibility of class members
 

Explanation: Access specifiers in C++ determine the visibility and accessibility of class members from outside the class.

  1. How many access specifiers are available in C++?

a) One

b) Two

c) Three

d) Four
 

Answer: b) Two
 

Explanation: C++ provides two access specifiers: "public" and "private".

  1. Which access specifier allows members to be accessible from outside the class?

a) public

b) private

c) protected

d) friend
 

Answer: a) public
 

Explanation: The "public" access specifier allows members to be accessible from outside the class.

  1. Which access specifier restricts access to class members to only within the class itself?

a) public

b) private

c) protected

d) friend
 

Answer: b) private
 

Explanation: The "private" access specifier restricts access to class members to only within the class itself.

6. Which of the following statements about the "protected" access specifier is true?

a) It allows access to class members from any function.

b) It restricts access to class members to only within the class itself.

c) It allows access to class members from derived classes.

d) It is the default access specifier in C++.
 

Answer: c) It allows access to class members from derived classes.
 

Explanation: The "protected" access specifier allows access to class members from derived classes as well as from within the class itself.

7. What happens if no access specifier is provided for members of a class in C++?

a) They default to "public".

b) They default to "private".

c) They default to "protected".

d) The compiler throws an error.
 

Answer: b) They default to "private".
 

Explanation: If no access specifier is provided, members of a class in C++ default to "private".

8. Which access specifier is used to indicate that certain non-member functions can access private members of a class?

a) public

b) private

c) protected

d) friend
 

Answer: d) friend
 

Explanation: The "friend" keyword is used to declare non-member functions or other classes as friends, allowing them to access private members of a class.

9. Can access specifiers be used within a function?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Access specifiers control the visibility of class members and are specified at the class level, not within individual functions.

10.  Which of the following statements about access specifiers is true?

a) Members with a lower access specifier can access members with a higher access specifier.

b) Members with a higher access specifier can access members with a lower access specifier.

c) Members with the same access specifier can access each other.

d) Access specifiers do not affect member access within the same class.
 

Answer: b) Members with a higher access specifier can access members with a lower access specifier.
 

Explanation: Members with a higher access specifier (e.g., public) can access members with a lower access specifier (e.g., private) within the same class.

11. Which access specifier should be used for class members that need to be accessible from outside the class?

a) public

b) private

c) protected

d) friend
 

Answer: a) public
 

Explanation: The "public" access specifier should be used for class members that need to be accessible from outside the class.

12. Can access specifiers be changed within the same class?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Access specifiers can be changed within the same class, allowing for different levels of visibility for different sections of the class.

13. Which access specifier allows derived classes to access members inherited from a base class?

a) public

b) private

c) protected

d) friend
 

Answer: c) protected
 

Explanation: The "protected" access specifier allows derived classes to access members inherited from a base class.

13. Can access specifiers be used in a struct in C++?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Access specifiers can be used in a struct in C++, just like in a class, to control the visibility of its members.

14. Which access specifier should be used for data members that should not be modified directly by external code?

a) public

b) private

c) protected

d) friend
 

Answer: b) private
 

Explanation: The "private" access specifier should be used for data members that should not be modified directly by external code, encapsulating their access within member functions.

15. Can a class have multiple levels of access specifiers (e.g., public within private)?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: A class can have multiple levels of access specifiers, allowing for fine-grained control over the visibility of its members.

 

OBJECT AND MEMBER ACCESS

  1. What is an object in C++?

a) A variable used to store data

b) An instance of a class

c) A function used to manipulate data

d) A data type
 

Answer: b) An instance of a class
 

Explanation: An object is an instance of a class, created based on the blueprint provided by the class.

  1. How are objects created in C++?

a) Using the "new" keyword

b) Using the "malloc" function

c) Using the class name followed by parentheses

d) Using the "object" keyword
 

Answer: c) Using the class name followed by parentheses
 

Explanation: Objects are created by using the class name followed by parentheses, similar to calling a function.

  1. What is member access in C++?

a) Accessing class members from outside the class

b) Accessing built-in functions

c) Accessing data stored in memory

d) Accessing global variables
 

Answer: a) Accessing class members from outside the class
 

Explanation: Member access refers to the ability to access class members (variables and functions) from outside the class, typically using the dot operator (.) or arrow operator (->).

  1. Which operator is used to access members of an object in C++?

a) .

b) ::

c) ->

d) :
 

Answer: a) .
 

Explanation: The dot operator (.) is used to access members of an object in C++, typically for objects and references.

  1. What does the arrow operator (->) do in C++?

a) Compares two values

b) Accesses members of a pointer to an object

c) Performs logical negation

d) Performs bitwise XOR operation
 

Answer: b) Accesses members of a pointer to an object
 

Explanation: The arrow operator (->) is used to access members of an object through a pointer to that object.

  1. Which of the following is true about member access in C++?

a) All class members are public by default.

b) Private members can be accessed from outside the class.

c) Protected members can be accessed from any function.

d) Access specifiers control member access.
 

Answer: d) Access specifiers control member access.
 

Explanation: Access specifiers such as public, private, and protected control the visibility and accessibility of class members.

  1. Can private members of a class be accessed from outside the class?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Private members of a class cannot be accessed from outside the class; they are accessible only within the class itself.

  1. Which access specifier allows class members to be accessed from outside the class?

a) public

b) private

c) protected

d) friend
 

Answer: a) public
 

Explanation: The "public" access specifier allows class members to be accessed from outside the class.

  1. Which of the following statements about member access is true?

a) Private members can be accessed by derived classes.

b) Public members cannot be accessed from outside the class.

c) Protected members can be accessed only by friend functions.

d) Member access does not affect the behavior of member functions.
 

Answer: a) Private members can be accessed by derived classes.
 

Explanation: Private members can be accessed by member functions of the same class and by friend functions and classes, including derived classes.

  1. Can member functions of a class access private members of other objects of the same class?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Member functions of a class can access private members of other objects of the same class, as they have access to the private members of any object of their class.

  1. Which of the following best describes the purpose of encapsulation?

a) To restrict access to class members

b) To allow access to private members from outside the class

c) To combine data and functions into a single unit

d) To create multiple instances of a class
 

Answer: c) To combine data and functions into a single unit
 

Explanation: Encapsulation combines data and functions into a single unit (a class), allowing for better organization and abstraction of data and behavior.

  1. Can the visibility of class members be changed dynamically during program execution?

a) Yes

b) No
 

Answer: b) No
 

Explanation: The visibility of class members, determined by access specifiers, is fixed at compile time and cannot be changed dynamically during program execution.

  1. Which access specifier restricts access to class members to only within the class itself?

a) public

b) private

c) protected

d) friend
 

Answer: b) private
 

Explanation: The "private" access specifier restricts access to class members to only within the class itself.

  1. Can objects of a class access each other's private members?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Objects of the same class can access each other's private members, as they belong to the same class.

  1. Which of the following statements about member access is false?

a) Protected members can be accessed by derived classes.

b) Private members can be accessed by friend functions.

c) Public members can be accessed from outside the class.

d) Member access specifiers do not affect inheritance.
 

Answer: d) Member access specifiers do not affect inheritance.
 

Explanation: Member access specifiers do affect inheritance; derived classes inherit the access level of base class members.

DEFINING MEMBER FUNCTION

  1. What is a member function in C++?

a) A function that is a member of a class

b) A function that takes a member of a class as an argument

c) A function declared within another function

d) A function that operates on non-class objects
 

Answer: a) A function that is a member of a class
 

Explanation: A member function is a function that is declared and defined within the scope of a class.

  1. How are member functions declared in C++?

a) Inside the main() function

b) Using the "function" keyword

c) As part of the class definition

d) As global functions
 

Answer: c) As part of the class definition
 

Explanation: Member functions are declared within the class definition using the normal function declaration syntax.

  1. Which of the following statements about member functions is true?

a) They can only be called from outside the class.

b) They cannot access private members of the class.

c) They can be overloaded.

d) They are declared using the "private" keyword.
 

Answer: c) They can be overloaded.
 

Explanation: Member functions can be overloaded, meaning multiple functions with the same name but different parameter lists can exist in the same class.

  1. What is the purpose of defining member functions in C++?

a) To perform operations on objects of the class

b) To declare variables inside a class

c) To access global variables

d) To define functions that are not related to the class
 

Answer: a) To perform operations on objects of the class
 

Explanation: Member functions are used to define operations that can be performed on objects of the class.

  1. How can member functions access data members of a class?

a) By using the "this" pointer

b) By passing parameters

c) By using the "new" keyword

d) By declaring them as static
 

Answer: a) By using the "this" pointer
 

Explanation: Member functions have access to the data members of the class through the implicit "this" pointer.

  1. Which keyword is used to define member functions outside the class definition?

a) member

b) function

c) class

d) inline
 

Answer: c) class
 

Explanation: Member functions can be defined outside the class definition using the class name followed by the scope resolution operator (::).

  1. Can member functions be defined inside the class definition?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Member functions can be defined inside the class definition, providing the function body directly within the class.

  1. What is the syntax for defining a member function outside the class definition?

a) void MyClass::myFunction() {}

b) void myFunction(MyClass) {}

c) MyClass::myFunction() {}

d) void myFunction() {}
 

Answer: a) void MyClass::myFunction() {}
 

Explanation: Member functions defined outside the class definition use the syntax: return-type ClassName::FunctionName() {}.

  1. Which of the following is not a valid way to define a member function?

a) void MyClass::myFunction() {}

b) void MyClass::myFunction() const {}

c) void myFunction() {}

d) void MyClass::myFunction(int x) {}
 

Answer: c) void myFunction() {}
 

Explanation: This syntax defines a global function, not a member function of MyClass.

  1. Can member functions be overloaded?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Member functions in C++ can be overloaded, allowing multiple functions with the same name but different parameter lists to exist in the same class.

  1. Which of the following statements about member functions is false?

a) They are invoked using the dot operator (.)

b) They can be declared static.

c) They cannot be called from outside the class.

d) They have access to private members of the class.
 

Answer: c) They cannot be called from outside the class.
 

Explanation: Member functions can be called from outside the class using object instances.

  1. Can member functions have a return type other than void?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Member functions can have any return type, including void, int, double, etc.

  1. What is the default access specifier for member functions in a class in C++?

a) public

b) private

c) protected

d) friend
 

Answer: a) public
 

Explanation: Member functions in a class default to the "public" access specifier if not explicitly specified.

  1. Which keyword is used to specify that a member function does not modify the object's state?

a) const

b) static

c) mutable

d) volatile
 

Answer: a) const
 

Explanation: The "const" keyword is used to specify that a member function does not modify the object's state and can be safely called on const objects.

  1. Which of the following is true about member functions?

a) They are always called by reference.

b) They cannot be called from constructors.

c) They have access to the address of the calling object through the "this" pointer.

d) They are automatically called when an object is destroyed.
 

Answer: c) They have access to the address of the calling object through the "this" pointer.
 

Explanation: Member functions have access to the address of the calling object through the implicit "this" pointer, allowing them to access data members and call other member functions.

CONSTRUCTOR AND ITS TYPE AND DESTRUCTOR 

  1. What is a constructor in C++?

a) A function used to destruct objects

b) A function used to initialize objects

c) A function used to allocate memory

d) A function used to copy objects
 

Answer: b) A function used to initialize objects
 

Explanation: Constructors are special member functions used to initialize objects of a class.

  1. When is a constructor called in C++?

a) When an object is declared

b) When an object is created

c) When an object is destroyed

d) When an object is assigned a value
 

Answer: b) When an object is created

Explanation: Constructors are automatically called when an object of a class is created.

  1. Which of the following is true about constructors?

a) They have a return type

b) They can be overloaded

c) They can be inherited

d) They are called explicitly by the user
 

Answer: b) They can be overloaded
 

Explanation: Constructors can be overloaded, meaning multiple constructors with different parameter lists can exist in the same class.

  1. What is the default constructor?

a) A constructor with default arguments

b) A constructor provided by the compiler when no constructor is explicitly defined

c) A constructor with no parameters

d) A constructor that initializes all members to zero
 

Answer: b) A constructor provided by the compiler when no constructor is explicitly defined
 

Explanation: The default constructor is provided by the compiler when no constructor is explicitly defined in the class.

  1. Which of the following is not a valid constructor type in C++?

a) Default constructor

b) Parameterized constructor

c) Copy constructor

d) Destructor constructor
 

Answer: d) Destructor constructor
 

Explanation: There is no such thing as a "destructor constructor." Destructors are separate special member functions.

  1. What is the purpose of a copy constructor in C++?

a) To create a copy of an object

b) To destruct an object

c) To assign a value to an object

d) To allocate memory for an object
 

Answer: a) To create a copy of an object
 

Explanation: The copy constructor is used to create a copy of an existing object.

  1. When is the copy constructor called in C++?

a) When an object is declared

b) When an object is created

c) When an object is assigned to another object

d) When an object is destroyed
 

Answer: c) When an object is assigned to another object
 

Explanation: The copy constructor is called when an object is assigned to another object, either explicitly or implicitly.

  1. What is the syntax for a copy constructor in C++?

a) ClassName() {}

b) ClassName(ClassName obj) {}

c) ClassName(const ClassName &obj) {}

d) ClassName(int x) {}
 

Answer: c) ClassName(const ClassName &obj) {}
 

Explanation: The copy constructor takes a reference to another object of the same class as a parameter.

  1. Can a class have multiple copy constructors?

a) Yes

b) No
 

Answer: b) No
 

Explanation: A class can have only one copy constructor, but it can be overloaded to handle different parameter types.

  1. What is the purpose of a destructor in C++?

a) To create an object

b) To initialize an object

c) To release resources held by an object

d) To copy an object
 

Answer: c) To release resources held by an object
 

Explanation: Destructors are special member functions used to release resources held by an object before it is destroyed.

  1. When is the destructor called in C++?

a) When an object is declared

b) When an object is created

c) When an object goes out of scope

d) When an object is assigned to another object
 

Answer: c) When an object goes out of scope
 

Explanation: Destructors are automatically called when an object goes out of scope or is explicitly deleted.

What is the syntax for a destructor in C++?

a) ~ClassName() {}

b) ClassName() {}

c) ClassName(const ClassName &obj) {}

  • d) ClassName(int x) {}
    Answer: a) ~ClassName() {}
    Explanation: The destructor is named with the class name preceded by a tilde (~) and has no parameters or return type.

Can a class have multiple destructors?

a) Yes

  • b) No
    Answer: b) No
    Explanation: A class can have only one destructor, which is automatically called when the object is destroyed.

Which of the following is true about constructors and destructors?

a) Constructors can return values, but destructors cannot.

b) Constructors cannot be overloaded, but destructors can.

c) Constructors are called before an object is destroyed, and destructors are called after.

  • d) Constructors are automatically called when an object is destroyed, and destructors are automatically called when an object is created.
    Answer: c) Constructors are called before an object is destroyed, and destructors are called after.
    Explanation: Constructors are called when an object is created to initialize it, while destructors are called when an object is destroyed to release resources.

What happens if a user-defined destructor is not provided for a class?

a) The compiler provides a default destructor.

b) The class cannot have objects created from it.

c) The class cannot be instantiated.

  • d) The program throws a runtime error.
    Answer: a) The compiler provides a default destructor.
    Explanation: If a user-defined destructor is not provided, the compiler provides a default destructor, which releases resources held by the object.

DYNAMIC MEMORY ALLOCATION FOR OBJECTS AND OBJECT ARRAY 

  1. What is dynamic memory allocation in C++?

a) Allocation of memory at compile-time

b) Allocation of memory at runtime

c) Allocation of memory on the stack

d) Allocation of memory for global variables
 

Answer: b) Allocation of memory at runtime
 

Explanation: Dynamic memory allocation refers to the allocation of memory during program execution, typically using operators like new and delete.

  1. Which operator is used for dynamic memory allocation in C++?

a) malloc and free

b) new and delete

c) alloc and dealloc

d) heap and stack
 

Answer: b) new and delete
 

Explanation: The new operator is used for dynamic memory allocation, and the delete operator is used to deallocate memory allocated with new.

  1. How is memory deallocated for dynamically allocated objects in C++?

a) Automatically by the compiler

b) Using the free() function

c) Using the delete operator

d) Manually by the programmer
 

Answer: c) Using the delete operator
 

Explanation: Memory allocated with new must be deallocated using the delete operator to avoid memory leaks.

3. Which of the following statements is true about dynamic memory allocation for objects?

a) Objects allocated dynamically are stored on the stack.

b) Dynamic allocation is always faster than static allocation.

c) Dynamically allocated objects have a fixed size.

d) Dynamic allocation allows for flexible memory management.
 

Answer: d) Dynamic allocation allows for flexible memory management.
 

Explanation: Dynamic memory allocation allows you to allocate memory based on runtime conditions, offering flexibility in memory management.

  1. What does the new operator return if memory allocation fails?

a) NULL

b) 0

c) A pointer to the allocated memory

d) An exception
 

Answer: d) An exception
 

Explanation: If memory allocation fails, the new operator throws a std::bad_alloc exception unless a no-throw version of new is used.

  1. Which of the following is true about dynamic memory allocation for object arrays?

a) Object arrays cannot be allocated dynamically.

b) Object arrays are allocated using the new[] operator.

c) Object arrays are deallocated using the delete operator.

d) Object arrays cannot be resized after allocation.
 

Answer: b) Object arrays are allocated using the new[] operator.
 

Explanation: Object arrays are allocated dynamically using the new[] operator, and they must be deallocated using the delete[] operator.

  1. What is the correct syntax for dynamically allocating a single object in C++?

a) object_ptr = new Object();

b) object_ptr = new Object;

c) object_ptr = new Object[];

d) object_ptr = new Object();
 

Answer: b) object_ptr = new Object;
 

Explanation: When allocating a single object dynamically, the parentheses after the class name are optional.

  1. How is memory deallocated for a dynamically allocated object array?

a) Using the free() function

b) Using the delete operator

c) Using the delete[] operator

d) Automatically by the compiler
 

Answer: c) Using the delete[] operator
 

Explanation: Dynamically allocated object arrays must be deallocated using the delete[] operator to properly release the memory.

  1. What happens if delete[] is used to deallocate memory allocated with new?

a) Memory leak occurs

b) Program crashes

c) Undefined behavior

d) No effect
 

Answer: c) Undefined behavior
 

Explanation: Using the delete[] operator on memory allocated with new results in undefined behavior, potentially leading to memory corruption or program crashes.

  1. Which of the following is true about dynamic memory allocation for objects compared to static allocation?

a) Dynamic allocation is generally faster.

b) Dynamic allocation allows for greater flexibility.

c) Static allocation always consumes less memory.

d) Static allocation is more error-prone.
 

Answer: b) Dynamic allocation allows for greater flexibility.
 

Explanation: Dynamic memory allocation allows for flexible memory management, enabling allocation and deallocation of memory at runtime.

  1. Can dynamic memory allocation be used to create objects of abstract classes in C++?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Dynamic memory allocation cannot be used to create objects of abstract classes, as abstract classes cannot be instantiated.

  1. Which operator is used to deallocate memory allocated for a single object in C++?

a) delete[]

b) delete

c) free()

d) deallocate()
 

Answer: b) delete
 

Explanation: The delete operator is used to deallocate memory allocated for a single object using the new operator.

  1. What happens if memory allocation fails when using the new operator?

a) The program crashes.

b) The program continues execution with uninitialized memory.

c) An exception is thrown.

d) The program returns a NULL pointer.
 

Answer: c) An exception is thrown.
 

Explanation: If memory allocation fails, the new operator throws a std::bad_alloc exception.

  1. Which of the following is a disadvantage of dynamic memory allocation?

a) It allows for flexible memory management.

b) It is more error-prone than static allocation.

c) It consumes less memory.

d) It is faster than static allocation.
 

Answer: b) It is more error-prone than static allocation.
 

Explanation: Dynamic memory allocation requires proper management of memory allocation and deallocation, which can be more error-prone than static allocation.

  1. What is a potential risk associated with dynamic memory allocation?

a) Memory leaks

b) Buffer overflow

c) Stack corruption

d) Segmentation fault
 

Answer: a) Memory leaks
 

Explanation: If memory allocated dynamically is not properly deallocated, it can lead to memory leaks, where memory is no longer accessible but still allocated, leading to wasted resources.

THIS POINTER 

  1. What is the this pointer in C++?

a) A pointer to the current object

b) A pointer to the previous object

c) A pointer to the next object

d) A pointer to a static member
 

Answer: a) A pointer to the current object
 

Explanation: The this pointer is a pointer available to C++ classes that points to the object for which the member function is called.

  1. Which of the following statements about the this pointer is true?

a) It is automatically created in every C++ program.

b) It points to the last object created in memory.

c) It can be explicitly modified by the programmer.

d) It is implicitly available within non-static member functions.
 

Answer: d) It is implicitly available within non-static member functions.
 

Explanation: The this pointer is automatically available within non-static member functions of a class and points to the object on which the member function is called.

  1. When is the this pointer used in C++?

a) To access static member variables

b) To access non-static member variables

c) To access global variables

d) To access local variables
 

Answer: b) To access non-static member variables
 

Explanation: The this pointer is used to access non-static member variables within member functions of a class.

  1. What is the type of the this pointer in C++?

a) int*

b) float*

c) char*

d) ClassName*
 

Answer: d) ClassName*
 

Explanation: The this pointer is of type ClassName*, where ClassName is the name of the class to which the member function belongs.

  1. Which of the following statements about the this pointer is false?

a) It cannot be dereferenced.

b) It can be used to access member functions of the class.

c) It can be used to return the address of the current object.

d) It is passed as an argument to the member functions.
 

Answer: d) It is passed as an argument to the member functions.
 

Explanation: The this pointer is not passed as an argument to member functions; it is implicitly available within them.

  1. Can the this pointer be used in static member functions?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Static member functions do not have a this pointer because they are not associated with any specific object instance.

  1. How is the this pointer used to differentiate between local and member variables with the same name?

a) It implicitly points to the member variables.

b) It implicitly points to the local variables.

c) It can be explicitly dereferenced to access member variables.

d) It can be explicitly dereferenced to access local variables.
 

Answer: a) It implicitly points to the member variables.
 

Explanation: Within member functions, when a variable name matches both a local variable and a member variable, the this pointer is used to implicitly access the member variable.

  1. What is the value of the this pointer within the constructor of a class?

a) NULL

b) 0

c) The address of the current object being constructed

d) The address of the previous object
 

Answer: c) The address of the current object being constructed
 

Explanation: Within the constructor of a class, the this pointer holds the address of the current object being constructed.

  1. Can the this pointer be explicitly modified within a member function?

a) Yes

b) No
 

Answer: b) No
 

Explanation: The this pointer is implicitly determined by the object on which the member function is called and cannot be explicitly modified.

  1. Which of the following statements about the this pointer is true?

a) It can be used in global functions.

b) It can be used in friend functions.

c) It can be used in lambda functions.

d) It can be used in static member functions.
 

Answer: b) It can be used in friend functions.
 

Explanation: The this pointer can be used within friend functions to access members of the class.

  1. Can the this pointer be used outside of member functions?

a) Yes

b) No
 

Answer: b) No
 

Explanation: The this pointer is only available within non-static member functions of a class and cannot be used outside of them.

  1. Which of the following is an appropriate use of the this pointer?

a) Accessing global variables

b) Accessing local variables

c) Accessing member variables

d) Accessing static variables
 

Answer: c) Accessing member variables
 

Explanation: The this pointer is used to access member variables within member functions of a class.

  1. What happens if the this pointer is dereferenced in a null object?

a) The program crashes.

b) NULL is returned.

c) Undefined behavior occurs.

d) A compilation error occurs.
 

Answer: c) Undefined behavior occurs.
 

Explanation: Dereferencing a null this pointer leads to undefined behavior, which can result in a crash or unexpected program behavior.

  1. Can the this pointer be used to access static member variables?

a) Yes

b) No
 

Answer: b) No
 

Explanation: The this pointer is used to access non-static member variables; it cannot be used to access static member variables.

  1. Which of the following statements about the this pointer is true?

a) It is available within the destructor of a class.

b) It can be passed as an argument to other functions.

c) It is a keyword in C++.

d) It is always equal to nullptr.
 

Answer: a) It is available within the destructor of a class.
 

Explanation: The this pointer is available within the destructor of a class and can be used to access member variables and call member functions.

STATIC DATA MEMBER AND STATIC FUNCTION

  1. What is a static data member in C++?

a) A member variable that is dynamically allocated

b) A member variable shared by all objects of the class

c) A member variable that is automatically initialized to zero

d) A member variable declared with the const keyword
 

Answer: b) A member variable shared by all objects of the class
 

Explanation: Static data members are shared among all objects of the class rather than being specific to each object instance.

  1. How is a static data member declared in a C++ class?

a) using the static keyword

b) using the const keyword

c) using the volatile keyword

d) without any keyword
 

Answer: a) using the static keyword

Explanation: Static data members are declared using the static keyword in the class declaration.

  1. What is the storage duration of a static data member in C++?

a) Automatic

b) Dynamic

c) Static

d) Register
 

Answer: c) Static
 

Explanation: Static data members have static storage duration, meaning they exist throughout the program's execution.

  1. Which of the following statements about static data members is true?

a) They can be accessed using the arrow operator (->).

b) They are initialized using the constructor of the class.

c) They are accessible only within member functions.

d) They can be accessed without creating an object of the class.
 

Answer: d) They can be accessed without creating an object of the class.
 

Explanation: Static data members can be accessed using the class name and the scope resolution operator (::) without the need for an object instance.

  1. What is a static member function in C++?

a) A member function that cannot modify the state of the object

b) A member function that can be called without creating an object of the class

c) A member function declared with the const keyword

d) A member function declared with the static keyword
 

Answer: b) A member function that can be called without creating an object of the class
 

Explanation: Static member functions belong to the class rather than individual objects and can be called using the class name without the need for an object instance.

  1. How is a static member function declared in a C++ class?

a) using the const keyword

b) using the volatile keyword

c) using the static keyword

d) without any keyword
 

Answer: c) using the static keyword
 

Explanation: Static member functions are declared using the static keyword in the class declaration.

  1. Which of the following statements about static member functions is true?

a) They can access non-static data members directly.

b) They are automatically called when an object is created.

c) They can be virtual functions.

d) They cannot access other member functions of the class.
 

Answer: a) They can access non-static data members directly.
 

Explanation: Static member functions can access static data members and other static member functions directly, but not non-static data members or member functions.

  1. What is the primary purpose of using static member functions in C++?

a) To modify the state of individual objects

b) To access non-static member functions

c) To perform operations that do not depend on any specific object

d) To create multiple instances of a class
 

Answer: c) To perform operations that do not depend on any specific object
 

Explanation: Static member functions are often used for operations that do not require access to individual object states and can be called without creating an object instance.

  1. Which of the following statements about static member functions is false?

a) They cannot be declared as const functions.

b) They cannot be declared as virtual functions.

c) They have access to the this pointer.

d) They can be overloaded.
 

Answer: c) They have access to the this pointer.
 

Explanation: Static member functions do not have access to the this pointer, as they do not operate on individual object instances.

  1. Can a static member function access non-static data members of a class?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Static member functions cannot access non-static data members directly, as they are not associated with any specific object instance.

  1. Which keyword is used to define the static data member outside the class in C++?

a) static

b) extern

c) const

d) volatile
 

Answer: b) extern
 

Explanation: When defining a static data member outside the class declaration, the extern keyword is used to specify its linkage.

  1. Can a static member function be called using an object of the class?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: While static member functions can be called using the class name, they can also be called using an object instance, but it is not a recommended practice.

  1. Which of the following statements about static member functions is true?

a) They can access non-static member functions directly.

b) They are automatically called when an object is created.

c) They have access to the this pointer.

d) They cannot be declared as const functions.
 

Answer: d) They cannot be declared as const functions.
 

Explanation: Static member functions cannot be declared as const functions, as they do not operate on individual object instances.

  1. Can a static member function be declared as virtual?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Static member functions can be declared as virtual, but they do not participate in dynamic dispatch as virtual non-static member functions do.

 

CONSTANT MEMBER FUNCTION AND CONSTANT OBJECT 

  1. What is a constant member function in C++?

a) A function that cannot modify the object's data members

b) A function declared with the const qualifier

c) A function that takes constant arguments

d) A function declared with the static keyword
 

Answer: b) A function declared with the const qualifier
 

Explanation: A constant member function is declared with the const qualifier, indicating that it does not modify the object's data members.

  1. Which of the following statements about constant member functions is true?

a) They can modify the object's data members.

b) They cannot be called on constant objects.

c) They can be overloaded with non-constant member functions.

d) They must return a constant value.
 

Answer: c) They can be overloaded with non-constant member functions.
 

Explanation: Constant member functions can be overloaded with non-constant member functions based on their parameter lists.

  1. How is a constant member function declared in a C++ class?

a) by using the const keyword after the return type

b) by using the const keyword before the function name

c) by using the const keyword after the function name

d) by using the const keyword after the function parameters
 

Answer: b) by using the const keyword before the function name
 

Explanation: A constant member function is declared by placing the const keyword after the function's parameter list but before the function name.

  1. What is the purpose of using constant member functions?

a) To prevent the object's data from being modified

b) To prevent the function from returning a non-constant value

c) To make the function callable from non-constant objects only

d) To allow the function to modify the object's data safely
 

Answer: a) To prevent the object's data from being modified
 

Explanation: Constant member functions ensure that the object's data members are not modified when the function is called.

  1. Which of the following statements about constant member functions is false?

a) They can be called on constant objects.

b) They can be called on non-constant objects.

c) They can access non-constant member functions.

d) They cannot modify the object's data members.
 

Answer: c) They can access non-constant member functions.
 

Explanation: Constant member functions can only access other constant member functions or non-static data members.

  1. Can constant member functions modify mutable data members of a class?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Constant member functions can modify mutable data members of a class, as these members are explicitly declared as modifiable.

  1. What is the result of calling a non-constant member function on a constant object?

a) Compilation error

b) Runtime error

c) Undefined behavior

d) The function is called without error
 

Answer: a) Compilation error
 

Explanation: Calling a non-constant member function on a constant object results in a compilation error because it violates the const-correctness principle.

  1. Which of the following is true about constant objects in C++?

a) Constant objects cannot be modified after creation.

b) Constant objects cannot have constant member functions.

c) Constant objects can only call non-constant member functions.

d) Constant objects are initialized with the volatile keyword.
 

Answer: a) Constant objects cannot be modified after creation.
 

Explanation: Constant objects are objects whose state cannot be modified after creation.

  1. How is a constant object declared in C++?

a) by using the const keyword after the class name

b) by using the const keyword before the object name

c) by using the const keyword after the object name

d) by using the const keyword before the class name
 

Answer: b) by using the const keyword before the object name
 

Explanation: A constant object is declared by placing the const keyword before the object name.

  1. Which of the following statements about constant objects is true?

a) They cannot call constant member functions.

b) They can only be initialized using the default constructor.

c) They can only access constant data members.

d) They cannot be passed as arguments to non-constant functions.
 

Answer: a) They cannot call constant member functions.
 

Explanation: Constant objects cannot call non-constant member functions to maintain const-correctness.

  1. Can a constant object call non-constant member functions?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Constant objects cannot call non-constant member functions to prevent modifications to their state.

  1. What is the primary advantage of using constant member functions and constant objects?

a) Improved performance

b) Enhanced security

c) Better code readability

d) Safer code maintenance
 

Answer: d) Safer code maintenance
 

Explanation: Constant member functions and constant objects help in maintaining const-correctness, leading to safer code maintenance and fewer bugs.

  1. Which keyword is used to declare mutable data members in C++?

a) const

b) volatile

c) mutable

d) static
 

Answer: c) mutable
 

Explanation: The mutable keyword is used to declare data members that can be modified even within constant member functions.

FRIEND FUNCTION AND FRIEND CLASS

  1. What is a friend function in C++?

a) A function declared within a class

b) A function declared outside a class that has access to the class's private and protected members

c) A function declared with the friend keyword

d) A function that can only access public members of a class


Answer: b) A function declared outside a class that has access to the class's private and protected members
 

Explanation: A friend function is a function declared outside a class that is granted access to the private and protected members of the class.

  1. How is a function declared as a friend of a class in C++?

a) by using the private keyword

b) by using the protected keyword

c) by using the friend keyword

d) by using the static keyword
 

Answer: c) by using the friend keyword
 

Explanation: A function is declared as a friend of a class by using the friend keyword in the class declaration.

  1. Which of the following statements about friend functions is true?

a) They have access to the class's public members only.

b) They are inherited by derived classes.

c) They are automatically called when an object is created.

d) They are declared within the class definition.
 

Answer: b) They are inherited by derived classes.
 

Explanation: Friend functions are not inherited by derived classes; however, they have access to the private and protected members of the base class.

  1. Can a friend function be declared as const?

a) Yes

b) No
Answer: a) Yes
 

Explanation: Friend functions can be declared as const, allowing them to be called on const objects of the class.

  1. Which of the following statements about friend functions is false?

a) They can be declared within the class definition.

b) They can be overloaded.

c) They can be called without any object.

d) They can access private members of the class.
 

Answer: a) They can be declared within the class definition.
 

Explanation: Friend functions are declared outside the class definition, although their prototypes can be included within the class definition.

  1. What is a friend class in C++?

a) A class declared with the friend keyword

b) A class that inherits from another class

c) A class that has access to the private and protected members of another class

d) A class declared within another class
 

Answer: c) A class that has access to the private and protected members of another class
 

Explanation: A friend class is a class declared with the friend keyword that has access to the private and protected members of another class.

  1. How is a class declared as a friend of another class in C++?

a) by using the private keyword

b) by using the protected keyword

c) by using the friend keyword

d) by using the static keyword
 

Answer: c) by using the friend keyword
 

Explanation: A class is declared as a friend of another class by using the friend keyword in the class declaration.

  1. Which of the following statements about friend classes is true?

a) They inherit the members of the class they are friends with.

b) They have access to the public members of the class they are friends with.

c) They can be derived from the class they are friends with.

d) They can be instantiated without any objects.
 

Answer: b) They have access to the public members of the class they are friends with.
 

Explanation: Friend classes have access to the public members of the class they are friends with, but not necessarily to its private members.

  1. Can a friend class be inherited by another class?

a) Yes

b) No
 

Answer: a) Yes
 

Explanation: Friend classes can be inherited by another class, but the friendship relationship is not inherited.

  1. Which of the following statements about friend functions and friend classes is true?

a) Friend functions and classes violate encapsulation principles.

b) Friend functions and classes are automatically called when an object is created.

c) Friend functions and classes cannot be declared outside the class definition.

d) Friend functions and classes can only access public members of the class.
 

Answer: a) Friend functions and classes violate encapsulation principles.
 

Explanation: Friend functions and classes provide access to the private and protected members of a class, potentially violating encapsulation principles.

  1. Can a friend function access the private members of a class directly?

a) Yes

b) No
 

Answer: a) Yes


Explanation: Friend functions are granted access to the private members of a class, allowing them to access and modify them directly.

  1. Which of the following is a disadvantage of using friend functions and friend classes?

a) Improved encapsulation

b) Reduced coupling

c) Limited access control

d) Enhanced modularity
 

Answer: c) Limited access control
 

Explanation: Friend functions and classes provide access to the private and protected members of a class, which can lead to limited access control and potentially increased complexity.

  1. Can a friend function or class access the private members of a derived class?

a) Yes

b) No
 

Answer: b) No
 

Explanation: Friend functions and classes can access the private members of the class they are friends with, but not necessarily the private members of derived classes.