0% found this document useful (0 votes)
19 views

Inheritance_4,5_units_1marks

Inheritance_4,5_units_1marks

Uploaded by

keerthana.m2021
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Inheritance_4,5_units_1marks

Inheritance_4,5_units_1marks

Uploaded by

keerthana.m2021
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Inheritance

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


a) Single
b) Multiple
c) Multilevel
d) All of the above
Answer: d) All of the above
2. Which inheritance allows a class to inherit properties from multiple base classes?
a) Single
b) Multilevel
c) Multiple
d) Hybrid
Answer: c) Multiple
3. What is the base class in a single inheritance?
a) A derived class
b) A parent class
c) A child class
d) A virtual class
Answer: b) A parent class
4. Which inheritance involves a virtual base class to avoid ambiguity?
a) Single
b) Hierarchical
c) Multipath
d) Hybrid
Answer: c) Multipath
5. In hybrid inheritance, a combination of which types of inheritance is used?
a) Single and Multiple
b) Multilevel and Hierarchical
c) Multiple and Multilevel
d) Single and Multilevel
Answer: c) Multiple and Multilevel
6. Which type of class cannot be instantiated?
a) Base class
b) Abstract class
c) Derived class
d) Virtual class
Answer: b) Abstract class
7. A class that contains at least one pure virtual function is called:
a) Static class
b) Abstract class
c) Derived class
d) Concrete class
Answer: b) Abstract class
8. Virtual base classes help in:
a) Speed optimization
b) Memory management
c) Avoiding ambiguity in multiple inheritances
d) Simplifying single inheritance
Answer: c) Avoiding ambiguity in multiple inheritances
Pointers

9. What is a pointer in C++?


a) A reference to an object
b) A variable that stores the memory address of another variable
c) A data type
d) A constant variable
Answer: b) A variable that stores the memory address of another variable
10. Which operator is used to get the value at the address stored in a pointer?
a) &
b) *
c) ->
d) ::
Answer: b) *
11. Which pointer is automatically passed to all non-static member functions of a
class?
a) This pointer
b) Null pointer
c) Smart pointer
d) Base pointer
Answer: a) This pointer
12. How do you declare a pointer to an object?
a) ClassName objPtr;
b) ClassName* objPtr;
c) ClassName &objPtr;
d) ClassName objPtr*;
Answer: b) ClassName* objPtr;
13. What is a null pointer?
a) A pointer that has no reference
b) A pointer that points to memory
c) A pointer that is zero-initialized
d) A pointer to a base class
Answer: c) A pointer that is zero-initialized
14. Which of the following can be used to point to a derived class object?
a) Base class pointer
b) Abstract class pointer
c) Friend pointer
d) Null pointer
Answer: a) Base class pointer
15. In pointer arithmetic, which operation is allowed?
a) Division
b) Addition
c) Multiplication
d) Bitwise XOR
Answer: b) Addition

Arrays and Files

16. An array of objects in C++ is:


a) A collection of different types of objects
b) A collection of similar types of objects
c) A list of pointers
d) A combination of functions and data
Answer: b) A collection of similar types of objects
17. How do you access an element in an array of objects?
a) Using ->
b) Using []
c) Using .
d) Using ::
Answer: b) Using []
18. Which file mode is used to write to a binary file?
a) ios::binary
b) ios::out
c) ios::app
d) ios::trunc
Answer: a) ios::binary
19. Which of the following is used to read a file sequentially?
a) File stream
b) Random access
c) Get function
d) Pointers
Answer: a) File stream
20. What type of file supports random access?
a) Binary file
b) ASCII file
c) Text file
d) Any file
Answer: a) Binary file
21. Which class is used for file input operations in C++?
a) ifstream
b) ofstream
c) fstream
d) istream
Answer: a) ifstream
22. What is the default mode for opening a file in C++?
a) Binary
b) Read
c) Write
d) Append
Answer: b) Read

Function Overloading

23. What is function overloading?


a) Using the same function name with different parameters
b) Using multiple functions with different return types
c) Defining multiple return types for the same function
d) Redefining a function from a base class
Answer: a) Using the same function name with different parameters
24. Which is not a valid function overloading parameter change?
a) Changing the number of parameters
b) Changing the data type of parameters
c) Changing the return type
d) Changing the order of parameters
Answer: c) Changing the return type

Exception Handling

25. Which keyword is used to handle exceptions in C++?


a) catch
b) throw
c) try
d) All of the above
Answer: d) All of the above
26. What happens if an exception is thrown but not caught?
a) The program continues execution
b) The program stops abruptly
c) The operating system handles the exception
d) The exception is ignored
Answer: b) The program stops abruptly
27. Which of the following is used to throw an exception?
a) try
b) catch
c) throw
d) throws
Answer: c) throw

Templates

28. What is a template in C++?


a) A generic blueprint for functions and classes
b) A predefined function
c) A function that supports overloading
d) A macro used to define variables
Answer: a) A generic blueprint for functions and classes
29. Which keyword is used to define a function template?
a) class
b) template
c) typename
d) friend
Answer: b) template

Constructors and Destructors

30. Which of the following is true for a constructor?


a) It is called manually
b) It has no return type
c) It is called automatically when an object is destroyed
d) It must always be public
Answer: b) It has no return type
31. What is a destructor in C++?
a) A function that initializes objects
b) A function that deletes objects
c) A function that deallocates resources when an object goes out of scope
d) A function that reinitializes memory
Answer: c) A function that deallocates resources when an object goes out of scope
32. Which of the following defines the correct destructor syntax for a class named
MyClass?
a) ~MyClass();
b) delete MyClass();
c) destruct MyClass();
d) !MyClass();
Answer: a) ~MyClass();
33. How many times is a destructor called for a single object?
a) Once
b) Twice
c) Depends on the object type
d) None of the above
Answer: a) Once

Operator Overloading

34. Which operator cannot be overloaded in C++?


a) +
b) =
c) ::
d) *
Answer: c) ::
35. Which of the following operators can be overloaded as a friend function?
a) ==
b) ++
c) ()
d) +
Answer: d) +
36. Overloading a unary operator takes how many arguments?
a) 0
b) 1
c) 2
d) 3
Answer: b) 1

Static Members

37. A static member function:


a) Can access only static member variables
b) Can access both static and non-static variables
c) Cannot be called outside the class
d) Must always be public
Answer: a) Can access only static member variables
38. Static member variables:
a) Are shared between all objects of the class
b) Are unique to each object
c) Can only be declared inside member functions
d) Cannot be initialized inside the class
Answer: a) Are shared between all objects of the class

Miscellaneous

39. Which of the following is used for file handling in C++?


a) ifstream
b) fstream
c) ofstream
d) All of the above
Answer: d) All of the above
40. How do you initialize a file stream object for input?
a) ofstream in;
b) ifstream in;
c) iostream in;
d) finput in;
Answer: b) ifstream in;
41. Which stream is used for writing to files?
a) ifstream
b) ofstream
c) cin
d) infile
Answer: b) ofstream
42. Which keyword is used to handle errors in C++?
a) try
b) catch
c) throw
d) All of the above
Answer: d) All of the above
43. Which is not a type of inheritance in C++?
a) Single
b) Multiple
c) Tree
d) Hierarchical
Answer: c) Tree
44. In C++, virtual base classes are used to:
a) Improve performance
b) Resolve ambiguity in multiple inheritance
c) Make the program more readable
d) Handle exceptions
Answer: b) Resolve ambiguity in multiple inheritance
45. Which of the following is not a valid access specifier in C++?
a) Private
b) Public
c) Protected
d) Friend
Answer: d) Friend
46. What does a pure virtual function mean?
a) A function with no return type
b) A function without a body
c) A function that can be overloaded
d) A function that cannot be inherited
Answer: b) A function without a body
47. Which class cannot create an object?
a) Derived class
b) Abstract class
c) Static class
d) None of the above
Answer: b) Abstract class
48. Which of the following types is used to handle exceptions?
a) int
b) char
c) exception
d) error
Answer: c) exception
49. In C++, arrays are:
a) Dynamic
b) Static
c) Fixed-length
d) All of the above
Answer: d) All of the above
50. In a class, what is the correct way to access a static variable?
a) Using an object of the class
b) Using the class name directly
c) Using a derived class
d) Using a constructor
Answer: b) Using the class name directly

You might also like