Introduction to Programming MCQs with Answers
Practice important Introduction to Programming MCQs with answers and explanations.
Chapter: 26
Q441:
What keyword is used to define private data members in a class?
A. private
B. protected
C. public
D. hidden
Correct Answer:
A
Chapter: 26
Q442:
What is the default visibility of data members in a class if no visibility keyword is specified?
A. private
B. public
C. protected
D. hidden
Correct Answer:
A
Chapter: 26
Q443:
Which operator is used to define a function outside the class using the class name?
A. ::
B. .
C. ->
D. :::
Correct Answer:
A
Chapter: 26
Q444:
What does a constructor do in a class?
A. Initializes an object
B. Prints data to the screen
C. Performs arithmetic operations
D. Deletes the object
Correct Answer:
A
Chapter: 26
Q445:
How can you specify default arguments for a constructor in C++?
A. By defining default values in the constructor's parameter list
B. By creating multiple constructors with different parameters
C. By using the default keyword
D. By specifying default values in the class declaration
Correct Answer:
A
Chapter: 26
Q446:
What would be the result of calling myDate.setMonth(13) if month is a private data member?
A. Compilation error because month is private
B. The month will be set to 13
C. The month will be set to 0
D. The program will crash
Correct Answer:
A
Chapter: 26
Q447:
What is the purpose of a 'getter' function in a class?
A. To retrieve the value of private data members
B. To set the value of private data members
C. To display data to the user
D. To delete the data members
Correct Answer:
A
Chapter: 26
Q448:
How do you access a public data member of a class from outside the class?
A. Directly using the object name and dot operator
B. By using a private method
C. By creating a new instance of the class
D. By using the :: operator
Correct Answer:
A
Chapter: 26
Q449:
What is the significance of the scope resolution operator (::) in C++?
A. It specifies the class to which a member function belongs
B. It denotes the end of a function definition
C. It initializes the data members
D. It accesses a data member from outside the class
Correct Answer:
A
Chapter: 27
Q450:
What is the main advantage of using object-oriented programming over function-oriented programming?
A. Data manipulates itself rather than being manipulated by functions
B. Functions are more efficient than methods
C. Programs are easier to write without classes
D. All data types are handled uniformly
Correct Answer:
A
Chapter: 27
Q451:
In object-oriented programming, what are the building blocks of a program?
A. Functions and variables
B. Classes and objects
C. Strings and arrays
D. Data members and methods
Correct Answer:
B
Chapter: 27
Q452:
What concept allows data in a class to be accessed only through defined functions?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Abstraction
Correct Answer:
A
Chapter: 27
Q453:
Which part of the class is visible to functions outside the class?
A. Public interface
B. Private data members
C. Protected members
D. Implementation details
Correct Answer:
A
Chapter: 27
Q454:
What is the role of a constructor in a class?
A. To initialize data members
B. To define member functions
C. To delete objects
D. To access private data members
Correct Answer:
A
Chapter: 27
Q455:
What type of constructor does the compiler provide automatically if no constructor is defined?
A. Default constructor
B. Parameterized constructor
C. Simple constructor
D. Overloaded constructor
Correct Answer:
A
Chapter: 27
Q456:
What is the primary use of parameterized constructors?
A. To initialize data members with specific values
B. To create default objects
C. To delete objects
D. To access private functions
Correct Answer:
A
Chapter: 27
Q457:
Which of the following statements about constructor overloading is true?
A. Multiple constructors can be defined with different parameter lists
B. Constructors cannot be overloaded
C. All constructors must take at least one argument
D. Constructors cannot have default arguments
Correct Answer:
A
Chapter: 27
Q458:
What happens if no constructor is defined in a class?
A. The compiler provides a default constructor
B. The program will not compile
C. The class will not have data members
D. The class will have a runtime error
Correct Answer:
A
Chapter: 27
Q459:
How can you validate data before assigning it to data members in a class?
A. Use set functions in the public part of the class
B. Use private member functions
C. Use destructors
D. Use friend functions
Correct Answer:
A
Chapter: 27
Q460:
What is the purpose of a destructor in a class?
A. To deallocate memory and clean up resources
B. To initialize data members
C. To provide default values
D. To define new data members
Correct Answer:
A