C++ MCQs 31–50: OOP, Pointers, File Handling
-
Which of the following is not an access specifier in C++?
a)public
b)private
c)protected
d)internal
Answer: d -
How do you declare a class named
Car
in C++?
a)class Car { };
b)Car class { };
c)struct Car { };
d) Both a and c
Answer: d -
Which keyword is used to inherit a class in C++?
a)implements
b)extends
c)inherits
d):
Answer: d -
What is the default access specifier for members of a class?
a)public
b)private
c)protected
d)internal
Answer: b -
Which of the following correctly declares a pointer to an integer?
a)int ptr;
b)int *ptr;
c)pointer int ptr;
d)int &ptr;
Answer: b -
How do you allocate memory dynamically for an integer in C++?
a)int *p = malloc(sizeof(int));
b)int *p = new int;
c)int p = new int;
d)int p = malloc(sizeof(int));
Answer: b -
Which operator is used to access a member function or variable through a pointer to an object?
a).
b)->
c)*
d)&
Answer: b -
What is polymorphism?
a) Ability of a function to perform different tasks
b) Overloading of functions
c) Runtime or compile-time binding
d) All of the above
Answer: d -
Which keyword is used to declare a virtual function?
a)virtual
b)override
c)final
d)inline
Answer: a -
What is the output of this code snippet?
a) Base
b) Derived
c) Compiler error
d) Runtime error
Answer: b
-
What is encapsulation?
a) Wrapping data and functions into a single unit
b) Inheriting properties from another class
c) Overloading operators
d) None of the above
Answer: a -
Which of the following is used to open a file for reading in C++?
a)ifstream file("test.txt");
b)ofstream file("test.txt");
c)fstream file("test.txt");
d)openfile file("test.txt");
Answer: a -
Which header file is required for file handling in C++?
a)<iostream>
b)<fstream>
c)<stdio.h>
d)<file.h>
Answer: b -
What happens if a file fails to open using an
ifstream
object?
a) Program crashes
b) Returns false on checking with.is_open()
or.fail()
c) Throws an exception automatically
d) None of the above
Answer: b -
Which operator is overloaded for output streaming in C++?
a)>>
b)<<
c)+
d)->
Answer: b -
What does the
delete
operator do?
a) Deletes a file
b) Frees memory allocated bynew
c) Deletes a variable
d) Deletes a class
Answer: b -
Which of the following is a correct syntax to declare a pure virtual function?
a)virtual void display() = 0;
b)void display() = 0;
c)virtual void display();
d)abstract void display() = 0;
Answer: a -
What is the base class in inheritance called?
a) Derived class
b) Parent class
c) Child class
d) Virtual class
Answer: b -
Which of the following statements about constructors is true?
a) Constructors have return types
b) Constructors can be overloaded
c) Constructors are inherited
d) Constructors can be virtual
Answer: b -
Which of the following is true about destructors?
a) Destructors have parameters
b) Destructors can be overloaded
c) Destructors are called automatically when an object goes out of scope
d) Destructors are inherited
Answer: c
0 Comments