3.5 Pure virtual function and file handling
VIRTUAL FUNCTION
- What is a virtual function in C++?
a) A function that can only be accessed within the class where it is declared
b) A function declared with the 'virtual' keyword that can be overridden in derived classes
c) A function that is automatically called when an object of a class is created
d) A function that is explicitly called by the programmer
Answer: b) A function declared with the 'virtual' keyword that can be overridden in derived classes
Explanation: A virtual function in C++ is declared with the 'virtual' keyword in the base class, allowing it to be overridden in derived classes, enabling runtime polymorphism.
2. Which keyword is used to declare a function as virtual in C++?
a) override
b) final
c) virtual
d) extend
Answer: c) virtual
Explanation: In C++, the 'virtual' keyword is used to declare a function as virtual in the base class, indicating that it can be overridden in derived
classes.
3. What is the purpose of a virtual destructor in C++?
a) To prevent memory leaks
b) To ensure proper cleanup of derived class objects
c) To improve performance
d) To restrict access to derived class objects
Answer: b) To ensure proper cleanup of derived class objects
Explanation: A virtual destructor in C++ ensures that the destructor of the most derived class is called when deleting an object through a base class pointer, allowing proper cleanup of resources.
4. In C++, can a virtual function have a definition in the base class?
a) Yes, it is mandatory
b) No, it is not allowed
c) Yes, but it is optional
d) Yes, but it must be private
Answer: c) Yes, but it is optional
Explanation: In C++, a virtual function in the base class can have a definition, but it is optional. If not provided, derived classes must override the virtual function.
5. Which type of polymorphism is achieved through virtual functions in C++?
a) Compile-time polymorphism
b) Run-time polymorphism
c) Static polymorphism
d) Dynamic polymorphism
Answer: d) Dynamic polymorphism
Explanation: Virtual functions in C++ enable dynamic polymorphism, where the function call is resolved at runtime based on the actual type of the object rather than the type of the pointer or reference.
6. What happens if a derived class overrides a virtual function without using the 'override' keyword in C++?
a) It results in a compilation error
b) It leads to undefined behavior
c) It does not affect the behavior of the program
d) It improves the performance of the program
Answer: c) It does not affect the behavior of the program
Explanation: Not using the 'override' keyword when overriding a virtual function in C++ does not affect the behavior of the program, but it is a good practice to use it for clarity and to catch errors during compilation.
7. In C++, can a constructor be declared as virtual?
a) Yes, but it is not recommended
b) Yes, it is mandatory
c) No, it is not allowed
d) Yes, it improves performance
Answer: c) No, it is not allowed
Explanation: In C++, constructors cannot be declared as virtual. Virtual functions are not allowed to be constructors or destructors.
8. Which function is called when a virtual function is invoked through a base class pointer pointing to a derived class object in C++?
a) Base class function
b) Derived class function
c) Both base and derived class functions
d) No function is called
Answer: b) Derived class function
Explanation: When a virtual function is invoked through a base class pointer pointing to a derived class object in C++, the derived class function is called due to dynamic binding.
9. What is the purpose of the 'override' keyword in C++?
a) To prevent inheritance
b) To explicitly call the base class function
c) To mark a virtual function override for better code readability and error checking
d) To restrict access to derived class functions
Answer: c) To mark a virtual function override for better code readability and error checking
Explanation: The 'override' keyword in C++ is used to explicitly mark a virtual function override in derived classes, improving code readability and enabling the compiler to perform error checking.
10. In C++, can a virtual function have a different return type in the base class and its derived classes?
a) Yes, it is allowed
b) No, it is not allowed
c) Yes, but only if the return types are compatible
d) Yes, but only if the return type in the derived class is a subclass of the return type in the base class
Answer: b) No, it is not allowed
Explanation: In C++, a virtual function must have the same signature (including return type) in both the base class and its derived classes. Otherwise, it leads to compilation errors.
DYNAMIC BINDING
- What is dynamic binding in C++?
a) A technique for static memory allocation
b) A mechanism for resolving function calls at compile time
c) The process of associating function calls with the actual function implementation at runtime
d) A method for optimizing code execution
Answer: c) The process of associating function calls with the actual function implementation at runtime
Explanation: Dynamic binding, also known as late binding or runtime polymorphism, is the process in C++ where the appropriate function implementation is determined at runtime based on the actual object type.
2. Which feature of C++ enables dynamic binding?
a) Virtual functions
b) Friend functions
c) Static functions
d) Inline functions
Answer: a) Virtual functions
Explanation: Virtual functions in C++ enable dynamic binding by allowing the function call to be resolved at runtime based on the actual object type rather than the type of the pointer or reference.
3. What is the primary advantage of dynamic binding in C++?
a) Increased compilation speed
b) Enhanced code readability
c) Improved runtime flexibility and extensibility
d) Reduced memory usage
Answer: c) Improved runtime flexibility and extensibility
Explanation: Dynamic binding in C++ provides improved runtime flexibility and extensibility by allowing the behavior of function calls to be determined at runtime, enabling polymorphic behavior.
4. Which type of polymorphism is achieved through dynamic binding in C++?
a) Compile-time polymorphism
b) Run-time polymorphism
c) Static polymorphism
d) Dynamic polymorphism
Answer: d) Dynamic polymorphism
Explanation: Dynamic binding in C++ enables dynamic polymorphism, where the function call is resolved at runtime based on the actual object type rather than the type of the pointer or reference.
5. What happens if a function is declared as virtual in the base class but not overridden in the derived class in C++?
a) It results in a compilation error
b) The function call is resolved at compile time
c) The base class function implementation is called
d) The derived class function implementation is called
Answer: c) The base class function implementation is called
Explanation: If a virtual function is not overridden in the derived class in C++, the base class function implementation is called when the function is invoked through a base class pointer or reference.
6. In C++, when is the determination of which function to call made in dynamic binding?
a) At compile time
b) At link time
c) At runtime
d) At preprocessing time
Answer: c) At runtime
Explanation: In dynamic binding, the determination of which function to call is made at runtime based on the actual type of the object being pointed to or referenced.
7. Which keyword is used to declare a function as virtual in C++?
a) override
b) final
c) virtual
d) extend
Answer: c) virtual
Explanation: The 'virtual' keyword is used to declare a function as virtual in C++, indicating that it can be overridden in derived classes and enabling dynamic binding.
8. What is the significance of the 'override' keyword in C++?
a) It prevents inheritance
b) It explicitly calls the base class function
c) It marks a virtual function override for better code readability and error checking
d) It restricts access to derived class functions
Answer: c) It marks a virtual function override for better code readability and error checking
Explanation: The 'override' keyword in C++ is used to explicitly mark a virtual function override in derived classes, improving code readability and enabling the compiler to perform error checking.
9. In C++, can dynamic binding be achieved without virtual functions?
a) Yes, through function overloading
b) No, virtual functions are necessary for dynamic binding
c) Yes, through operator overloading
d) Yes, through friend functions
Answer: b) No, virtual functions are necessary for dynamic binding
Explanation: In C++, virtual functions are necessary for dynamic binding. Other techniques like function overloading, operator overloading, or friend functions do not enable dynamic binding.
10. Which type of binding is used when a function call is resolved based on the type of the pointer or reference in C++?
a) Static binding
b) Dynamic binding
c) Early binding
d) Late binding
Answer: a) Static binding
Explanation: Static binding, also known as early binding, occurs when the function call is resolved based on the type of the pointer or reference at compile time, rather than at runtime as in dynamic binding.
DEFINING, OPENING AND CLOSING A FILE
- Which header file is required to work with file operations in C++?
a) <iostream>
b) <fstream>
c) <stdio.h>
d) <string>
Answer: b) <fstream>
Explanation: The <fstream> header file in C++ provides classes and functions for working with file input and output operations.
- What is the function used to open a file for writing in C++?
a) open_file()
b) write_file()
c) fopen()
d) ofstream::open()
Answer: d) ofstream::open()
Explanation: In C++, the ofstream::open() function is used to open a file for writing.
- Which mode is used to open a file for both reading and writing in C++?
a) ios::in
b) ios::out
c) ios::binary
d) ios::in | ios::out
Answer: d) ios::in | ios::out
Explanation: The ios::in | ios::out mode is used to open a file for both reading and writing in C++.
- What is the syntax for opening a file named "data.txt" for reading in C++?
a) ifstream file("data.txt", ios::out);
b) ofstream file("data.txt", ios::in);
c) fstream file("data.txt", ios::out);
d) ifstream file("data.txt", ios::in);
Answer: d) ifstream file("data.txt", ios::in);
Explanation: The syntax for opening a file named "data.txt" for reading in C++ is to use ifstream with the ios::in mode.
- How do you check if a file is successfully opened in C++?
a) Using the if statement with the is_open() function
b) Using the if statement with the open() function
c) Using the while loop with the fail() function
d) Using the while loop with the eof() function
Answer: a) Using the if statement with the is_open() function
Explanation: In C++, you can check if a file is successfully opened by using the if statement with the is_open() function of the file stream object.
- What is the function used to close a file in C++?
a) close()
b) fclose()
c) end_file()
d) shutdown()
Answer: a) close()
Explanation: The close() function is used to close a file in C++.
- Which of the following statements is true about file closing in C++?
a) Closing a file is optional
b) Closing a file is done automatically by the compiler
c) Failure to close a file may lead to loss of data or resources
d) Closing a file requires explicit deletion of the file object
Answer: c) Failure to close a file may lead to loss of data or resources
Explanation: In C++, it is important to explicitly close files after use to ensure that all data is written to the file and to release system resources associated with the file.
- What is the effect of trying to open a non-existent file in C++?
a) It results in a compilation error
b) It creates a new file with the specified name
c) It opens the file in read-only mode
d) It results in a failure to open the file
Answer: d) It results in a failure to open the file
Explanation: If you try to open a non-existent file in C++, it will result in a failure to open the file, and subsequent operations on the file stream will fail.
- Which of the following modes is used to append data to an existing file in C++?
a) ios::ate
b) ios::app
c) ios::binary
d) ios::trunc
Answer: b) ios::app
Explanation: The ios::app mode is used to open a file for appending data to the end of an existing file in C++.
- What is the default mode used when opening a file in C++?
a) ios::in
b) ios::out
c) ios::binary
d) ios::in | ios::out
Answer: a) ios::in
Explanation: When opening a file in C++ without specifying a mode, the default mode used is ios::in, which opens the file for input operations.
INPUT/OUTPUT OPERATIONS ON FILE
- Which header file is required to perform input/output operations on files in C++?
a) <iostream>
b) <fstream>
c) <stdio.h>
d) <string>
Answer: b) <fstream>
Explanation: The <fstream> header file in C++ provides classes and functions for performing input/output operations on files.
- Which class is used to read data from a file in C++?
a) ifstream
b) ofstream
c) fstream
d) istream
Answer: a) ifstream
Explanation: The ifstream class in C++ is used for reading data from files.
- Which function is used to open a file for reading in C++?
a) open()
b) read()
c) fopen()
d) ifstream::open()
Answer: d) ifstream::open()
Explanation: The ifstream::open() function is used to open a file for reading in C++.
- What is the purpose of the get() function in C++ file input operations?
a) To read a single character from a file
b) To read a line of text from a file
c) To write data to a file
d) To close a file
Answer: a) To read a single character from a file
Explanation: The get() function in C++ file input operations is used to read a single character from a file.
- Which function is used to write data to a file in C++?
a) put()
b) write()
c) read()
d) getline()
Answer: b) write()
Explanation: The write() function in C++ is used to write data to a file.
- What is the purpose of the getline() function in C++ file input operations?
a) To read a single character from a file
b) To read a line of text from a file
c) To write data to a file
d) To close a file
Answer: b) To read a line of text from a file
Explanation: The getline() function in C++ file input operations is used to read a line of text from a file.
- Which mode is used to open a file for both reading and writing in C++?
a) ios::in
b) ios::out
c) ios::binary
d) ios::in | ios::out
Answer: d) ios::in | ios::out
Explanation: The ios::in | ios::out mode is used to open a file for both reading and writing in C++.
- What is the effect of trying to read from a file that is not open in C++?
a) It results in a compilation error
b) It creates a new file with the specified name
c) It reads data from the standard input (keyboard)
d) It fails to read any data
Answer: d) It fails to read any data
Explanation: If you try to read from a file that is not open in C++, the read operation will fail and no data will be read.
- Which function is used to check if the end of a file has been reached in C++?
a) eof()
b) end_of_file()
c) file_end()
d) eofbit()
Answer: a) eof()
Explanation: The eof() function in C++ is used to check if the end of a file has been reached during file input operations.
- What is the effect of trying to write to a file that is not open in C++?
a) It results in a compilation error
b) It creates a new file with the specified name
c) It writes data to the standard output (console)
d) It fails to write any data
Answer: d) It fails to write any data
Explanation: If you try to write to a file that is not open in C++, the write operation will fail and no data will be written.
ERROR HANDLING DURING INPUT/OUTPUT OPERATION
- Which C++ library function is used to retrieve the last error occurred during file operations?
a) perror()
b) strerror()
c) errno()
d) error()
Answer: b) strerror()
Explanation: The strerror() function is used to retrieve the textual representation of the last error occurred during file operations, based on the value stored in the global variable 'errno'.
- What does the perror() function do in C++?
a) Prints the value of errno and its corresponding error message to the standard error stream
b) Prints the current working directory path
c) Prints the contents of a file to the standard output stream
d) Prints the time when the last error occurred
Answer: a) Prints the value of errno and its corresponding error message to the standard error stream
Explanation: The perror() function in C++ prints the value of errno and its corresponding
error message to the standard error stream, which is typically the console.
- Which header file is required to use the perror() function in C++?
a) <iostream>
b) <cstdlib>
c) <cstring>
d) <cerrno>
Answer: d) <cerrno>
Explanation: The perror() function is declared in the <cerrno> header file in C++, along with the 'errno' global variable.
- What is the purpose of the clearerr() function in C++?
a) To clear the contents of a file
b) To reset the error indicators for a file stream
c) To close a file stream
d) To check if the end-of-file indicator is set
Answer: b) To reset the error indicators for a file stream
Explanation: The clearerr() function in C++ is used to reset the error indicators for a file stream, allowing subsequent input/output operations to be performed without interference from previous errors.
- Which macro is used to indicate an end-of-file condition during file operations in C++?
a) EOF
b) EOL
c) EOT
d) EOB
Answer: a) EOF
Explanation: The EOF macro in C++ is used to indicate an end-of-file condition during file operations, typically returned by input/output functions when the end of a file is reached.
- In C++, which function is used to handle exceptions related to file operations?
a) throw()
b) try()
c) catch()
d) throw_exception()
Answer: c) catch()
Explanation: In C++, exceptions related to file operations can be handled using the catch() block, where specific error conditions can be caught and appropriate actions can be taken.
- What does the feof() function do in C++?
a) Returns true if the end-of-file indicator is set for a file stream
b) Clears the end-of-file indicator for a file stream
c) Checks if an error has occurred during file operations
d) Closes a file stream
Answer: a) Returns true if the end-of-file indicator is set for a file stream
Explanation: The feof() function in C++ returns true if the end-of-file indicator is set for a file stream, indicating that the end of the file has been reached.
- Which exception is thrown when a file cannot be opened in C++?
a) file_open_error
b) file_read_error
c) file_write_error
d) std::ios_base::failure
Answer: d) std::ios_base::failure
Explanation: In C++, when a file cannot be opened, a std::ios_base::failure exception is thrown, indicating a failure during input/output operations.
- How can you handle errors related to file operations in C++ without using exceptions?
a) By returning error codes from functions
b) By using conditional statements to check for error conditions
c) By ignoring errors and continuing execution
d) By terminating the program
Answer: b) By using conditional statements to check for error conditions
Explanation: Without using exceptions, errors related to file operations in C++ can be handled by using conditional statements to check for error conditions returned by file
input/output functions.
- Which function is used to retrieve the error message associated with the last error occurred during file operations?
a) perror()
b) strerror()
c) errno()
d) perror_message()
Answer: b) strerror()
Explanation: The strerror() function is used to retrieve the textual representation of the last error occurred during file operations, based on the value stored in the global variable 'errno'.
STREAM CLASS HIERARCHY FOR CONSOLE INPUT/OUTPUT
- In C++, which stream class is used for console input operations?
a) istream
b) ostream
c) ifstream
d) ofstream
Answer: a) istream
Explanation: The istream class in C++ is used for console input operations, allowing data to be read from the standard input stream (usually the keyboard).
- Which stream class is used for console output operations in C++?
a) istream
b) ostream
c) ifstream
d) ofstream
Answer: b) ostream
Explanation: The ostream class in C++ is used for console output operations, allowing data to be written to the standard output stream (usually the console).
- Which operator is used for formatted console output in C++?
a) <<
b) >>
c) &
d) |
Answer: a) <<
Explanation: The << operator in C++ is used for formatted console output. It is known as the insertion operator and is used to insert data into the output stream.
- What is the base class of both the istream and ostream classes in C++?
a) ios
b) streambuf
c) fstream
d) iostream
Answer: d) iostream
Explanation: The iostream class in C++ serves as the base class for both istream and ostream classes, providing common functionality for input and output streams.
- Which function is used to read a single character from the console in C++?
a) get()
b) getline()
c) read()
d) getchar()
Answer: a) get()
Explanation: The get() function in C++ is used to read a single character from the console.
- Which function is used to write a string to the console in C++?
a) put()
b) write()
c) print()
d) cout()
Answer: b) write()
Explanation: The write() function in C++ is used to write a string to the console.
- Which stream class is used for unformatted console input/output operations in C++?
a) stringstream
b) streambuf
c) fstream
d) iostream
Answer: b) streambuf
Explanation: The streambuf class in C++ is used for unformatted console input/output operations, providing a buffer for data transfer between the program and the console.
- What is the purpose of the endl manipulator in C++?
a) To insert a newline character into the output stream
b) To clear the contents of the output buffer
c) To close the output stream
d) To reset the error flags for the output stream
Answer: a) To insert a newline character into the output stream
Explanation: The endl manipulator in C++ is used to insert a newline character into the output stream, equivalent to the '\n' character.
- Which function is used to perform unformatted console input operations in C++?
a) get()
b) getline()
c) read()
d) cin.get()
Answer: d) cin.get()
Explanation: The cin.get() function in C++ is used to perform unformatted console input operations, reading a single character from the console.
- What is the default stream for input operations in C++?
a) cin
b) cout
c) cerr
d) clog
Answer: a) cin
Explanation: The cin stream in C++ is the default stream for input operations, allowing data to be read from the standard input (usually the keyboard).
UNFORMATTED INPUT/OUTPUT AND FORMATTED INPUT/OUTPUT WITH IOS MEMBER FUNCTION AND FLAG
- Which of the following functions in C++ is used to format output to a stream?
A) cin
B) cout
C) fprintf
D) scanf
Answer: B) cout
Explanation: The cout object in C++ is used for formatted output to the standard output stream.
- What is the purpose of the setw manipulator in C++?
A) It sets the width of the output field.
B) It sets the precision of floating-point output.
C) It sets the fill character for output.
D) It sets the format flags for the stream.
Answer: A) It sets the width of the output field.
Explanation: setw is used to set the width of the output field in formatted output.
- Which flag in the ios class is used to enable left justification of output?
A) left
B) right
C) setw
D) setf
Answer: A) left
Explanation: The left flag enables left justification of output in C++.
- What is the default fill character in C++ output streams?
A) Space
B) Tab
C) Newline
D) Asterisk
Answer: A) Space
Explanation: Space is the default fill character in C++ output streams.
- Which of the following is not a member function of the ios class in C++?
A) width()
B) fill()
C) precision()
D) setf()
Answer: B) fill()
Explanation: fill() is not a member function of the ios class. It is a manipulator.
- The ios::fixed flag is used to specify:
A) Output in scientific notation.
B) Fixed-point output format.
C) Floating-point output format.
D) Output with a fixed width.
Answer: B) Fixed-point output format.
Explanation: ios::fixed is used to specify fixed-point output format in C++.
- What does the ios::showpos flag do in C++ output streams?
A) Shows the position of the cursor in the output.
B) Shows the positive sign for positive numeric values.
C) Shows the position of the end-of-file marker.
D) Shows the position of the beginning of the file.
Answer: B) Shows the positive sign for positive numeric values.
Explanation: ios::showpos flag in C++ output streams displays the positive sign for positive numeric values.
- Which manipulator in C++ is used to set the precision of floating-point output?
A) setw
B) setprecision
C) precision
D) setf
Answer: C) precision
Explanation: precision manipulator is used to set the precision of floating-point output in C++.
- In C++, which flag is used to enable scientific notation for floating-point output?
A) scientific
B) fixed
C) showpoint
D) noshowpos
Answer: A) scientific
Explanation: scientific flag enables scientific notation for floating-point output in C++.
- Which function in C++ is used to flush the output buffer?
A) flush()
B) clear()
C) sync()
D) rewind()
Answer: C) sync()
Explanation: sync() function in C++ is used to flush the output buffer.
FORMATTING WITH MANIPULATORS
- Which manipulator in C++ is used to set the width of the output field?
A) setw
B) setwidth
C) width
D) fieldwidth
Answer: A) setw
Explanation: The setw manipulator in C++ is used to set the width of the output field.
- What is the purpose of the setprecision manipulator in C++?
A) It sets the width of the output field.
B) It sets the precision of floating-point output.
C) It sets the fill character for output.
D) It sets the format flags for the stream.
Answer: B) It sets the precision of floating-point output.
Explanation: The setprecision manipulator in C++ is used to set the precision of floating-point output.
- Which manipulator in C++ is used to set the fill character for output?
A) fillchar
B) setfill
C) fill
D) setw
Answer: B) setfill
Explanation: The setfill manipulator in C++ is used to set the fill character for output.
- What does the endl manipulator do in C++ output streams?
A) It sets the precision of floating-point output.
B) It inserts a newline character and flushes the output buffer.
C) It sets the width of the output field.
D) It sets the fill character for output.
Answer: B) It inserts a newline character and flushes the output buffer.
Explanation: The endl manipulator in C++ output streams inserts a newline character and flushes the output buffer.
- Which manipulator in C++ is used to control whether the positive sign is shown for positive numeric values?
A) showpos
B) noshowpos
C) setpos
D) unsetpos
Answer: A) showpos
Explanation: The showpos manipulator in C++ is used to control whether the positive sign is shown for positive numeric values.
- What does the noshowpoint manipulator do in C++ output streams?
A) It disables the showing of the decimal point.
B) It disables the showing of the positive sign.
C) It disables the showing of trailing zeros for floating-point values.
D) It disables the showing of the decimal point and trailing zeros for floating-point values.
Answer: D) It disables the showing of the decimal point and trailing zeros for floating-point values.
Explanation: The noshowpoint manipulator in C++ output streams disables the showing of the decimal point and trailing zeros for floating-point values.
- Which manipulator in C++ is used to set the format flags for the stream?
A) setflags
B) flags
C) setformat
D) setf
Answer: D) setf
Explanation: The setf manipulator in C++ is used to set the format flags for the stream.
- What is the purpose of the hex manipulator in C++ output streams?
A) It sets the output format to hexadecimal.
B) It sets the output format to octal.
C) It sets the output format to binary.
D) It sets the output format to decimal.
Answer: A) It sets the output format to hexadecimal.
Explanation: The hex manipulator in C++ output streams sets the output format to hexadecimal.
- Which manipulator in C++ is used to reset the format flags for the stream?
A) resetflags
B) clearflags
C) unsetf
D) resetf
Answer: C) unsetf
Explanation: The unsetf manipulator in C++ is used to reset the format flags for the stream.
- What does the showpoint manipulator do in C++ output streams?
A) It shows the decimal point for floating-point values.
B) It shows the positive sign for positive numeric values.
C) It shows trailing zeros for floating-point values.
D) It shows the position of the end-of-file marker.
Answer: C) It shows trailing zeros for floating-point values.
Explanation: The showpoint manipulator in C++ output streams shows trailing zeros for floating-point values.