- UNIT-I
.
Unit-1 MCQ's
Object Oriented Programming Using C++
1. What is a file in C++?
a) A storage device
b) A named location on disk to store data
c) A memory location in RAM
d) A pointer variable
Answer: b) A named location on disk to store data
2. Which library in C++ is used for file handling?
a) <iostream>
b) <fstream>
c) <file>
d) <iomanip>
Answer: b) <fstream>
3. Which class is used for file input operations in C++?
a) ofstream
b) ifstream
c) fstream
d) iostream
Answer: b) ifstream
4. Which class is used for file output operations in C++?
a) ofstream
b) ifstream
c) fstream
d) iostream
Answer: a) ofstream
5. What is the default mode of ofstream
?
a) ios::app
b) ios::binary
c) ios::out
d) ios::in
Answer: c) ios::out
6. What happens if you open a file in ios::trunc
mode?
a) Data is appended to the file
b) Data is read from the file
c) The existing content is deleted
d) The file remains unchanged
Answer: c) The existing content is deleted
7. How do you check if a file has opened successfully?
a) if (file == true)
b) if (file.is_open())
c) if (file.exists())
d) if (file.check())
Answer: b) if (file.is_open())
8. How do you close a file in C++?
a) file.close();
b) file.stop();
c) file.exit();
d) file.terminate();
Answer: a) file.close();
9. Which file mode is used to append data to an existing file?
a) ios::trunc
b) ios::app
c) ios::binary
d) ios::in
Answer: b) ios::app
10. How do you write data to a file?
a) file << "Hello";
b) file.write("Hello");
c) file.put("Hello");
d) file.add("Hello");
Answer: a) file << "Hello";
11. What happens if an ifstream
tries to open a non-existent file?
a) It creates a new file
b) It throws an error
c) It returns false
on is_open()
d) It automatically redirects to cout
Answer: c) It returns false
on is_open()
12. How do you move the file pointer to the beginning of a file?
a) file.seekp(0, ios::beg);
b) file.seekg(0, ios::beg);
c) file.restart();
d) file.begin();
Answer: b) file.seekg(0, ios::beg);
13. How do you read an entire line from a file?
a) file.readline(str);
b) getline(file, str);
c) file >> str;
d) file.scan(str);
Answer: b) getline(file, str);
14. Which file mode opens a file in binary mode?
a) ios::text
b) ios::bin
c) ios::binary
d) ios::bmode
Answer: c) ios::binary
15. What is exception handling in C++?
a) Mechanism to handle runtime errors
b) Mechanism to handle syntax errors
c) Mechanism to handle file operations
d) Mechanism to prevent logical errors
Answer: a) Mechanism to handle runtime errors
16. Which keyword is used to define an exception?
a) try
b) catch
c) throw
d) exception
Answer: c) throw
17. What block is used to handle an exception?
a) catch
b) throw
c) try
d) finally
Answer: a) catch
18. What happens when an exception is not caught?
a) Program terminates abnormally
b) Exception is ignored
c) Exception is handled automatically
d) Compilation error occurs
Answer: a) Program terminates abnormally
19. Which keyword is used to define a block of code that might throw an exception?
a) try
b) catch
c) throw
d) error
Answer: a) try
20. What is the correct syntax for catching an exception?
a) catch {}
b) catch (exception)
c) catch (...)
d) catch (int e)
Answer: d) catch (int e)
21. What is the purpose of catch (...)
?
a) To catch all exceptions
b) To catch integer exceptions only
c) To ignore exceptions
d) To generate new exceptions
Answer: a) To catch all exceptions
22. Can a constructor throw an exception?
a) Yes
b) No
Answer: a) Yes
23. What happens when an exception is thrown inside a destructor?
a) It is ignored
b) The program crashes
c) It is handled by catch
d) It is passed to the base class
Answer: b) The program crashes
24. Which of the following is true about exception handling?
a) It helps handle runtime errors
b) It reduces code readability
c) It slows down program execution
d) It only works for file handling
Answer: a) It helps handle runtime errors
25. Can a C++ program have multiple catch blocks?
a) Yes
b) No
Answer: a) Yes
26. What will happen if a catch block is not found for an exception?
a) The program will terminate abnormally
b) The exception will be ignored
c) The program will continue execution
d) The exception will be automatically handled
Answer: a) The program will terminate abnormally
27. Can multiple exceptions be thrown in a single try block?
a) Yes
b) No
Answer: a) Yes
28. What is the purpose of std::exception
class?
a) To handle runtime exceptions
b) To handle compile-time errors
c) To catch syntax errors
d) To provide a base class for exception handling
Answer: d) To provide a base class for exception handling
29. What function is used to display the exception message in std::exception
?
a) what()
b) message()
c) error()
d) info()
Answer: a) what()
30. What type of exceptions does std::bad_alloc
handle?
a) Null pointer exceptions
b) Memory allocation failures
c) File handling errors
d) Syntax errors
Answer: b) Memory allocation failures
31. Which keyword is used to rethrow an exception?
a) throw
b) retry
c) again
d) rethrow
Answer: a) throw
32. Can exceptions be thrown from a destructor?
a) Yes
b) No
Answer: a) Yes, but it's not recommended
33. What is the default mode of ifstream
class?
a) ios::in
b) ios::out
c) ios::app
d) ios::trunc
Answer: a) ios::in
34. Which function is used to check the end of a file?
a) file.eof()
b) file.end()
c) file.close()
d) file.stop()
Answer: a) file.eof()
35. Which exception is thrown when an invalid type conversion occurs?
a) std::bad_alloc
b) std::invalid_argument
c) std::bad_cast
d) std::out_of_range
Answer: c) std::bad_cast
36. Can an exception be caught by reference?
a) Yes
b) No
Answer: a) Yes
37. What is the purpose of std::runtime_error
?
a) To handle syntax errors
b) To handle runtime errors
c) To handle logic errors
d) To handle compile-time errors
Answer: b) To handle runtime errors
38. Which exception is thrown when accessing an out-of-range index?
a) std::out_of_range
b) std::overflow_error
c) std::underflow_error
d) std::invalid_argument
Answer: a) std::out_of_range
39. Which of the following is not a valid file mode?
a) ios::nocreate
b) ios::app
c) ios::trunc
d) ios::binary
Answer: a) ios::nocreate
40. Which statement is true about std::ofstream
?
a) It is used to read data from a file
b) It is used to write data to a file
c) It is used to append data to a file
d) It is used to delete a file
Answer: b) It is used to write data to a file
41. What will happen if you try to write to a read-only file using ofstream
?
a) File will be modified
b) File will be deleted
c) Error will occur
d) Nothing will happen
Answer: c) Error will occur
42. What is the purpose of ios::ate
mode?
a) Opens a file and moves the pointer to the end
b) Opens a file and truncates its content
c) Opens a file in append mode
d) Opens a file in text mode
Answer: a) Opens a file and moves the pointer to the end
43. What is the output of file.tellg()
?
a) File pointer's current position in input mode
b) File pointer's current position in output mode
c) File size
d) File contents
Answer: a) File pointer's current position in input mode
44. What will happen if an exception is thrown and not caught?
a) The program will continue execution
b) The program will terminate
c) The exception will be ignored
d) The compiler will fix it automatically
Answer: b) The program will terminate
45. What does ios::nocreate
mode do?
a) Prevents file creation if it doesn’t exist
b) Creates a new file always
c) Deletes an existing file
d) Opens a file in binary mode
Answer: a) Prevents file creation if it doesn’t exist
46. What does ios::noreplace
mode do?
a) Prevents overwriting an existing file
b) Opens a file in binary mode
c) Creates a new file
d) Deletes an existing file
Answer: a) Prevents overwriting an existing file
47. What will happen if you try to open a file using ifstream
that does not exist?
a) A new file is created
b) The file pointer moves to NULL
c) The is_open()
function will return false
d) The program crashes
Answer: c) The is_open()
function will return false
48. What is the purpose of ios::binary
mode?
a) Opens a file in binary mode
b) Opens a file in text mode
c) Prevents file from being written
d) Deletes the file
Answer: a) Opens a file in binary mode
49. What does ios::in | ios::out
do?
a) Opens a file for reading only
b) Opens a file for writing only
c) Opens a file for both reading and writing
d) Appends data to the file
Answer: c) Opens a file for both reading and writing
50. Can an exception be thrown inside a catch block?
a) Yes
b) No
Answer: a) Yes
51. What is exception handling in C++?
a) Handling runtime errors
b) Handling syntax errors
c) Handling logic errors
d) Handling memory allocation
Answer: a) Handling runtime errors
52. 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
53. What is the purpose of the throw
keyword?
a) To define an exception
b) To handle an exception
c) To raise an exception
d) To ignore an exception
Answer: c) To raise an exception
54. Where should the try
block be placed in C++?
a) Before the catch
block
b) After the catch
block
c) Inside the catch
block
d) Inside a function only
Answer: a) Before the catch
block
55. What happens when an exception is thrown and not caught in C++?
a) The program executes normally
b) The program terminates abnormally
c) The exception is ignored
d) The compiler fixes the error
Answer: b) The program terminates abnormally
56. How many catch blocks can be used for a single try block?
a) Only one
b) At most two
c) Any number
d) None
Answer: c) Any number
57. What type of argument does a catch
block take?
a) Any data type
b) Only integer
c) Only string
d) Only user-defined types
Answer: a) Any data type
58. What does catch(...)
do?
a) Catches only integer exceptions
b) Catches only string exceptions
c) Catches all types of exceptions
d) Does nothing
Answer: c) Catches all types of exceptions
59. What is the purpose of the std::exception
class?
a) To create exceptions
b) To provide a base class for exceptions
c) To handle compile-time errors
d) To print error messages
Answer: b) To provide a base class for exceptions
60. What function is used to display the exception message?
a) what()
b) error()
c) message()
d) print()
Answer: a) what()
61. Can an exception be caught by reference?
a) Yes
b) No
Answer: a) Yes
62. What is the output if an exception is caught successfully?
a) The program terminates
b) The program continues execution
c) The program enters an infinite loop
d) The program ignores the exception
Answer: b) The program continues execution
63. Which of the following is an advantage of exception handling?
a) Improved error detection
b) Reduces code readability
c) Increases execution time
d) Makes debugging harder
Answer: a) Improved error detection
64. Can a catch block have multiple parameters?
a) Yes
b) No
Answer: b) No
65. What will happen if a throw
statement is used without a try
block?
a) Compilation error
b) The program runs normally
c) The program terminates abnormally
d) No effect
Answer: a) Compilation error
66. What happens if an exception is thrown inside a destructor?
a) The program terminates
b) The program continues execution
c) It is ignored
d) It is caught automatically
Answer: a) The program terminates
67. What does the noexcept
specifier indicate in C++?
a) The function does not throw exceptions
b) The function can throw only one exception
c) The function throws multiple exceptions
d) The function always throws an exception
Answer: a) The function does not throw exceptions
68. What is std::bad_alloc
used for?
a) Handling memory allocation failure
b) Handling invalid conversions
c) Handling integer overflow
d) Handling division by zero
Answer: a) Handling memory allocation failure
69. Which of the following can be thrown as an exception?
a) Only integers
b) Only objects
c) Only strings
d) Any type
Answer: d) Any type
70. What is the default behavior of an unhandled exception?
a) Program continues execution
b) Program terminates
c) Exception is ignored
d) Compiler fixes the issue
Answer: b) Program terminates
71. What does std::runtime_error
handle?
a) Syntax errors
b) Compile-time errors
c) Runtime errors
d) Memory allocation errors
Answer: c) Runtime errors
72. Can multiple exceptions be thrown in a single try
block?
a) Yes
b) No
Answer: a) Yes
73. Which operator is used to rethrow an exception?
a) throw
b) retry
c) again
d) rethrow
Answer: a) throw
74. Which function handles unexpected exceptions?
a) std::unexpected()
b) std::abort()
c) std::terminate()
d) std::exception()
Answer: c) std::terminate()
75. What does std::logic_error
handle?
a) Syntax errors
b) Logical programming errors
c) Memory errors
d) Compilation errors
Answer: b) Logical programming errors
No comments:
Post a Comment