4.11.26

CS201 Final Term Past Papers: Solved MCQs, Subjective Questions and Short Notes (Introduction to Programming)

Getting ready for the final term of CS201 (Introduction to Programming)? Here is everything in one place: solved MCQs, subjective questions with answers and short notes and key terms, gathered from past papers and files shared by students. No duplicates and all the answers are checked and verified. Use the tabs above to switch between Objective, Subjective and Short Notes & Key Terms, and tap Show answer only after you have tried the question yourself. Best of luck with your exam!

Part 1: Objective (MCQs)

Q1. A matrix is commonly represented in C++ as which of the following?

  1. Single-dimensional array
  2. Multidimensional array
  3. Vector product
  4. Scalar product
Show answer

Answer: (b) Multidimensional array
Handout reference: Lecture 12, p. 120

Q2. Which of the following correctly declares main() with conventional command-line parameters?

  1. float argc, char **argv
  2. int argc, char **argv
  3. int *argc, char *argv
  4. int argc, float **argv
Show answer

Answer: (b) int argc, char **argv
Handout reference: Lecture 16, p. 177

Q3. In pass-by-reference, what is passed so the function can modify the original variable?

  1. Only a copy of its value
  2. Access to the original variable through a reference
  3. A new independent local copy
  4. A textual representation of the variable
Show answer

Answer: (b) Access to the original variable through a reference
Handout reference: Lecture 10, p. 94

Q4. Both endl and flush are examples of which of the following?

  1. Functions unrelated to streams
  2. Arithmetic operators
  3. Stream manipulators
  4. Data objects
Show answer

Answer: (c) Stream manipulators
Handout reference: Lecture 36, p. 432

Q5. What are the first and second parameters of an overloaded stream-insertion operator?

  1. Input stream, class object
  2. Class object, output stream
  3. Output stream, class object
  4. Class object, input stream
Show answer

Answer: (c) Output stream, class object
Handout reference: Lecture 37, p. 445

Q6. How must the first parameter of an overloaded stream-insertion (<<) operator be passed?

  1. By value
  2. By reference
  3. By either value or reference
  4. As an object of the user-defined class
Show answer

Answer: (b) By reference
Handout reference: Lecture 37, p. 445

Q7. What is an appropriate data type for storing the number of rows and columns in a matrix?

  1. float
  2. string
  3. int
  4. char
Show answer

Answer: (c) int
Handout reference: Lecture 43, p. 521

Q8. What does ANSI stand for?

  1. American Neutral Standards Institute
  2. Australian National Standards Institute
  3. Asian National Standards Institute
  4. American National Standards Institute
Show answer

Answer: (d) American National Standards Institute
Handout reference: Lecture 2, p. 11

Q9. The member-initializer list initializes contained class objects at which stage?

  1. Run time after all constructors finish
  2. Debugging time
  3. Construction time
  4. Compilation time
Show answer

Answer: (c) Construction time
Handout reference: Lecture 40, p. 482

Q10. In a C++ member-initializer list, data members are initialized in which order?

  1. Left to right as written in the initializer list
  2. Right to left in the initializer list
  3. The order of their declarations in the class
  4. There is no defined order
Show answer

Answer: (c) The order of their declarations in the class
Handout reference: Lecture 40, p. 482

Q11. Which function is NOT an input-stream member function?

  1. getline()
  2. peek()
  3. unget()
  4. write()
Show answer

Answer: (d) write()
Handout reference: Lecture 35, p. 428

Q12. When an object is a data member of another class, whose constructor is called first?

  1. The enclosing class constructor
  2. The contained object constructor
  3. Both constructors simultaneously
  4. Only the destructor
Show answer

Answer: (b) The contained object constructor
Handout reference: Lecture 40, p. 481

Q13. In what order are destructors called relative to constructor calls?

  1. The same construction order
  2. Reverse construction order
  3. An unspecified independent order
  4. The same order as variable declarations
Show answer

Answer: (b) Reverse construction order
Handout reference: Lecture 40, p. 480

Q14. The void* parameter of an overloaded operator delete function identifies what?

  1. The memory block to be deallocated
  2. All objects of the class
  3. An unused argument
  4. Nothing
Show answer

Answer: (a) The memory block to be deallocated
Handout reference: Lecture 34, p. 412

Q15. What pointer type is returned by C allocation functions such as malloc()?

  1. int*
  2. double*
  3. void*
  4. char*
Show answer

Answer: (c) void*
Handout reference: Lecture 28, p. 329

Q16. C++ new and delete can be used with which categories of data types?

  1. Loops only
  2. Structures and pointers only
  3. Class and primitive data types
  4. Constants only
Show answer

Answer: (c) Class and primitive data types
Handout reference: Lecture 28, p. 331

Q17. A friend function of a C++ class is which of the following?

  1. A private member function
  2. A non-member function
  3. A public member function
  4. An ordinary member function
Show answer

Answer: (b) A non-member function
Handout reference: Lecture 31, p. 373

Q18. What does the declaration Date operator++(int); inside class Date represent?

  1. A member postfix-increment operator
  2. A member prefix-increment operator
  3. A non-member postfix-increment operator
  4. A non-member prefix-increment operator
Show answer

Answer: (a) A member postfix-increment operator
Handout reference: Lecture 32, p. 388

Q19. Which kind of statement repeats an action while a condition remains true?

  1. Repetition statement
  2. Control declaration
  3. Conditional expression
  4. Assignment statement
Show answer

Answer: (a) Repetition statement
Handout reference: Lecture 6, p. 45

Q20. The ++ and -- operators increase or decrease an integer variable by how much?

  1. 3
  2. 2
  3. 1
  4. 4
Show answer

Answer: (c) 1
Handout reference: Lecture 7, p. 62

Q21. Which memory region is generally used by dynamic memory allocation?

  1. Stack
  2. Heap
  3. System cache
  4. Secondary storage
Show answer

Answer: (b) Heap
Handout reference: Lecture 24, p. 278

Q22. Which execution model processes statements until encountering an error in the statement being executed?

  1. Interpreter
  2. Compiler
  3. Linker
  4. Debugger
Show answer

Answer: (a) Interpreter
Handout reference: Lecture 2, p. 11

Q23. What is the return type of a function that returns no value?

  1. float
  2. int
  3. void
  4. double
Show answer

Answer: (c) void
Handout reference: Lecture 9, p. 78

Q24. Which is NOT a standard base-format flag or manipulator used to select decimal, octal, or hexadecimal output?

  1. oct
  2. hex
  3. dec
  4. bin
Show answer

Answer: (d) bin
Handout reference: Lecture 36, p. 439

Q25. What is the normal return type of an overloaded operator<< for output streams?

  1. The user-defined class
  2. std::ostream&
  3. std::istream&
  4. void
Show answer

Answer: (b) std::ostream&
Handout reference: Lecture 37, p. 445

Q26. An overloaded assignment operator (=) of a class must be which type of function?

  1. Friend function
  2. Non-member friend
  3. Non-member function
  4. Member function
Show answer

Answer: (d) Member function
Handout reference: Lecture 31, p. 373

Q27. What does cin.get() normally do when used with standard input?

  1. Read a string from the keyboard
  2. Read one character from the keyboard
  3. Read a string from a file
  4. Read a character from an arbitrary file
Show answer

Answer: (b) Read one character from the keyboard
Handout reference: Lecture 35, p. 428

Q28. At link time, library functions are ordinarily available in which form?

  1. Compiled object code
  2. Plain source text only
  3. Uncompiled source comments
  4. User interface code
Show answer

Answer: (a) Compiled object code
Handout reference: Lecture 23, p. 268

Q29. Which is the general declaration syntax for an overloaded operator function?

  1. return-type operator-symbol(parameter-list);
  2. operator return-type operator-symbol(parameter-list);
  3. return-type operator operator-symbol(parameter-list);
  4. return-type operator-symbol operator(parameter-list);
Show answer

Answer: (c) return-type operator operator-symbol(parameter-list);
Handout reference: Lecture 31, p. 369

Q30. Given class M { public: M operator+(const M&); };, the + operator is implemented as what?

  1. Member function
  2. Non-member function
  3. Friend function
  4. Virtual function
Show answer

Answer: (a) Member function
Handout reference: Lecture 31, p. 373

Q31. When is the expression exp1 && exp2 true?

  1. Only when both expressions are true
  2. Only when both are false
  3. When only exp1 is true
  4. When only exp2 is true
Show answer

Answer: (a) Only when both expressions are true
Handout reference: Lecture 5, p. 39

Q32. In top-down programming, decomposition into functions is associated with what approach?

  1. Object-oriented only
  2. Structured programming
  3. Assembly programming
  4. High-level language syntax only
Show answer

Answer: (b) Structured programming
Handout reference: Lecture 25, p. 291

Q33. A loop is also called what kind of control structure?

  1. Decision
  2. Repetition
  3. Sequential
  4. Hierarchical
Show answer

Answer: (b) Repetition
Handout reference: Lecture 6, p. 44

Q34. Which punctuation marks enclose a C++ character literal?

  1. Parentheses
  2. Single quotation marks
  3. Double quotation marks
  4. Curly braces
Show answer

Answer: (b) Single quotation marks
Handout reference: Lecture 3, p. 20

Q35. Which type stores a character such as a, b, or c?

  1. int
  2. double
  3. char
  4. float
Show answer

Answer: (c) char
Handout reference: Lecture 3, p. 20

Q36. What is the meaning of x += y;?

  1. Add y to x and store the result in x
  2. Add x to y and store in y
  3. Add x to itself
  4. Add y to itself and store in x
Show answer

Answer: (a) Add y to x and store the result in x
Handout reference: Lecture 7, p. 63

Q37. A variable declared inside a code block is normally a ______ variable for that block.

  1. Global
  2. Static
  3. Local
  4. Inner
Show answer

Answer: (c) Local
Handout reference: Lecture 10, p. 89

Q38. Allowing an outside function to declare itself a friend of a class would violate which principle?

  1. Data accessing
  2. Encapsulation and data hiding
  3. Public interfaces
  4. Function overloading
Show answer

Answer: (b) Encapsulation and data hiding
Handout reference: Lecture 29, p. 344

Q39. In older C++ stream classes, width(), fill(), and precision() are member functions provided through which stream header?

  1. conio.h
  2. iomanip.h
  3. iostream.h
  4. stdlib.h
Show answer

Answer: (c) iostream.h
Handout reference: Lecture 36, p. 438

Q40. The width() formatting member function can be called on which stream objects?

  1. cin only
  2. cout only
  3. Both cin and cout
  4. User-defined classes only
Show answer

Answer: (c) Both cin and cout
Handout reference: Lecture 36, p. 438

Q41. Which statement about static data members is true?

  1. They can never be public or private
  2. They must be private
  3. They must be public
  4. They may be declared public or private
Show answer

Answer: (d) They may be declared public or private
Handout reference: Lecture 38, p. 465

Q42. Which is a valid declaration of an overloaded operator new?

  1. void* new operator(size_t size);
  2. void* new(size_t size);
  3. operator new/(size_t size);
  4. void* operator new(size_t size);
Show answer

Answer: (d) void* operator new(size_t size);
Handout reference: Lecture 34, p. 414

Q43. Which is the correct preprocessor macro definition for PI?

  1. #undef PI 3.1415926
  2. #define PI 3.1415926
  3. #define PI = 3.1415926
  4. #ifdef PI = 3.1415926
Show answer

Answer: (b) #define PI 3.1415926
Handout reference: Lecture 23, p. 269

Q44. Which value is returned by the destructor of a class?

  1. A pointer to the class
  2. An object of the class
  3. A status code determining whether the class was destructed correctly
  4. Destructors do not return a value
Show answer

Answer: (d) Destructors do not return a value
Handout reference: Lecture 27, p. 321

Q45. Automatic variable are created on ________

  1. Heap
  2. Free store
  3. static storage
  4. stack
Show answer

Answer: (d) stack
Handout reference: Lecture 38, p. 459

Q46. In a flowchart, which symbol represents the flow of control?

  1. Rectangle
  2. Circle
  3. Diamond
  4. Arrow
Show answer

Answer: (d) Arrow
Handout reference: Lecture 5, p. 34

Q47. What is the first step in developing a computer program?

  1. Linking
  2. Debugging
  3. Analysis
  4. Loading
Show answer

Answer: (c) Analysis
Handout reference: Lecture 1, p. 6

Q48. Dereferencing operator is represented by

  1. *
  2. +
  3. -
  4. None of above
Show answer

Answer: (a) *
Handout reference: Lecture 14, p. 146

Q49. There are mainly -------------------- types of software

  1. Two
  2. Three
  3. Four
  4. Five
Show answer

Answer: (a) Two
Handout reference: Lecture 2, p. 8

Q50. When a pointer is incremented, it actually jumps the number of memory addresses

  1. According to data type
  2. 1 byte exactly
  3. 1 bit exactly
  4. A pointer variable cannot be incremented
Show answer

Answer: (a) According to data type
Handout reference: Lecture 15, p. 159

Q51. When an array is passed to a function then default way of passing this array is,

  1. By data
  2. By reference
  3. By value
  4. By data type
Show answer

Answer: (b) By reference
Handout reference: Lecture 12, p. 119

Q52. Skill(s) that is/are needed by programmers _______________________

  1. Paying attention to detail
  2. Think about the reusability
  3. Think about user interface
  4. All of the given options
Show answer

Answer: (d) All of the given options
Handout reference: Lecture 1, p. 3

Q53. UNIX has been developed in ________ language

  1. JAVA
  2. B
  3. C
  4. FORTRAN
Show answer

Answer: (c) C
Handout reference: Lecture 2, p. 11

Q54. Pointers store the ____________________

  1. value of a variable
  2. memory address
  3. characters
  4. None of the given
Show answer

Answer: (b) memory address
Handout reference: Lecture 14, p. 145

Q55. In C/C++ the string constant is enclosed ……………

  1. In curly braces
  2. In small braces
  3. In single quotes
  4. In double quotes
Show answer

Answer: (d) In double quotes
Handout reference: Lecture 12, p. 113

Q56. In Flow chart process is represented by

  1. Rectangle
  2. Arrow symbol
  3. Oval
  4. Circle
Show answer

Answer: (a) Rectangle
Handout reference: Lecture 5, p. 34

Q57. If the break statement is missed in switch statement then

  1. The compiler will give error
  2. This may cause a logical error
  3. No effect on program
  4. Program stops its execution
Show answer

Answer: (b) This may cause a logical error
Handout reference: Lecture 8, p. 68

Q58. How many total elements must be in two-dimensional array of 3 rows and 2 columns?

  1. 4
  2. 5
  3. 6
  4. 7
Show answer

Answer: (c) 6
Handout reference: Lecture 16, p. 172

Q59. Which of the following is the correct statement for the following declaration?

const int *ptr;

  1. ptr is a constant pointer
  2. ptr is constant integer pointer
  3. ptr is a constant pointer to int
  4. ptr is a pointer to const int
Show answer

Answer: (d) ptr is a pointer to const int
Why: const int *ptr declares a pointer to a constant int.
Handout reference: Lecture 14, p. 144

Q60. Syntax of union is identical to ______

  1. Structure
  2. Class
  3. Function
  4. None of the given options
Show answer

Answer: (a) Structure
Handout reference: Lecture 20, p. 241

Q61. A character is stored in memory in

  1. byte
  2. integer
  3. string
  4. None of above
Show answer

Answer: (a) byte
Handout reference: Lecture 17, p. 186

Q62. _________ contain functions for manipulation of character data

  1. Ctype.h
  2. Iostream.h
  3. String.h
  4. None of the above
Show answer

Answer: (a) Ctype.h
Handout reference: Lecture 17, p. 187

Q63. In functions that return reference, use __________variables

  1. Local
  2. Global
  3. Global or static
  4. None of the given option
Show answer

Answer: (c) Global or static
Handout reference: Lecture 30, p. 364

Q64. What is the sequence of event(s) when allocating memory using new operator?

  1. Only block of memory is allocated for objects
  2. Only constructor is called for objects
  3. Memory is allocated first before calling constructor
  4. Constructor is called first before allocating memory
Show answer

Answer: (c) Memory is allocated first before calling constructor
Handout reference: Lecture 34, p. 412

Q65. new and delete are _____ whereas malloc and free are _____

  1. Functions, operators
  2. Classes, operators
  3. Operators, functions
  4. Operators, classes
Show answer

Answer: (c) Operators, functions
Handout reference: Lecture 28, p. 340

Q66. Like member functions, ______ can also access the private data members of a class

  1. Non-member functions
  2. Friend functions
  3. Any function outside class
  4. None of the given options
Show answer

Answer: (b) Friend functions
Handout reference: Lecture 29, p. 344

Q67. Which of the following statement is best regarding declaration of friend function?

  1. Friend function must be declared after public keyword
  2. Friend function must be declared after private keyword
  3. Friend function must be declared at the top within class definition
  4. It can be declared anywhere in class as these are not affected by the public and private
Show answer

Answer: (d) It can be declared anywhere in class as these are not affected by the public and private
Handout reference: Lecture 29, p. 344

Q68. The static data members of a class are initialized _______

  1. at file scope
  2. within class definition
  3. within member function
  4. within main function
Show answer

Answer: (a) at file scope
Handout reference: Lecture 38, p. 465

Q69. Which kind of functions can access private member variables of a class?

  1. Friend functions of the class
  2. Private member functions of the class
  3. Public member functions of the class
  4. Friend, private and public functions
Show answer

Answer: (d) Friend, private and public functions
Handout reference: Lecture 29, p. 344

Q70. When an array of objects is defined, how often is the constructor called?

  1. Constructor will be called once for whole array
  2. Constructor will be called for each object of the array
  3. Constructor will never call
  4. Depends on the size of array
Show answer

Answer: (b) Constructor will be called for each object of the array
Handout reference: Lecture 34, p. 407

Q71. The stream objects cin and cout are included in which header file?

  1. iostream.h
  2. fstream.h
  3. istream.h
  4. ostream.h
Show answer

Answer: (a) iostream.h
Handout reference: Lecture 36, p. 431

Q72. Missing semicolon ‘;’ at the end of C++ statement is

  1. Logical error
  2. Syntax error
  3. Runtime error
  4. None of the given options
Show answer

Answer: (b) Syntax error
Handout reference: Lecture 3, p. 15

Q73. Which of the following function calling mechanism is true for the function prototype given below? float func(float &)

  1. Call by value
  2. Call by reference using pointer
  3. Call by reference using reference variable
  4. None of the given options
Show answer

Answer: (c) Call by reference using reference variable
Handout reference: Lecture 30, p. 361

Q74. The region of memory, available for allocation at run time in C language is called ________ memory whereas in C++ language is called as ________________

  1. Heap, Stack
  2. Stack, Free Store
  3. Heap, Free Store
  4. None of above
Show answer

Answer: (c) Heap, Free Store
Handout reference: Lecture 28, p. 342

Q75. A variable which is defined inside a function is called

  1. Automatic variable
  2. Global variable
  3. Functional variable
  4. None of the given option
Show answer

Answer: (a) Automatic variable
Handout reference: Lecture 38, p. 458

Q76. Default mechanism of function calling in case of array is _____ and in case of variable is ___

  1. Call by value, call by reference
  2. Call by referene, call by reference
  3. Call by reference, call by value
  4. Call by value, call by value
Show answer

Answer: (c) Call by reference, call by value
Handout reference: Lecture 14, p. 153

Q77. What does STL stand for?

  1. Source template library
  2. Standard template library
  3. Stream template library
  4. Standard temporary library
Show answer

Answer: (b) Standard template library
Handout reference: Lecture 42, p. 517

Q78. For which array, the size of the array should be one more than the number of elements in an array?

  1. int
  2. double
  3. float
  4. char
Show answer

Answer: (d) char
Handout reference: Lecture 12, p. 112

Q79. While calling function, the arguments are assigned to the parameters from _____________

  1. left to right
  2. right to left
  3. no specific order is followed
  4. none of the given options
Show answer

Answer: (a) left to right
Handout reference: Lecture 25, p. 293

Q80. new operator allocates memory from free store and return _____________

  1. A pointer
  2. A reference
  3. An integer
  4. A float
Show answer

Answer: (a) A pointer
Handout reference: Lecture 28, p. 330

Q81. In if structure the block of statements is executed only,

  1. When the condition is false
  2. When it contain arithmetic operators
  3. When it contain logical operators
  4. When the condition is true
Show answer

Answer: (d) When the condition is true
Handout reference: Lecture 5, p. 32

Q82. Header file: fstream.h includes the definition of the stream classes __________

  1. ifstream, fstream, cout
  2. ifstream, fstream, ofstream
  3. fstream, cin, cout
  4. None of the above
Show answer

Answer: (b) ifstream, fstream, ofstream
Handout reference: Lecture 18, p. 198

Q83. To access the data members of structure _______ is used

  1. dot operator (.)
  2. * operator
  3. arrow operator (->)
  4. None of given
Show answer

Answer: (a) dot operator (.)
Handout reference: Lecture 20, p. 230

Q84. Classes defined inside other classes are called ________ classes

  1. looped
  2. nested
  3. overloaded
  4. none of the given options
Show answer

Answer: (b) nested
Handout reference: Lecture 40, p. 494

Q85. If constructor contains a return statement in its body then compiler will give __________

  1. No error
  2. Syntax error
  3. Logical error
  4. Run time error
Show answer

Answer: (a) No error
Why: A constructor may contain a plain return; (no value), so there is no error.
Handout reference: Lecture 27, p. 320

Q86. The keyword_______ is used to return some value from a function

  1. return
  2. break
  3. continue
  4. goto
Show answer

Answer: (a) return
Handout reference: Lecture 9, p. 78

Q87. The input/output streams cin and cout are ________ therefore have _______

  1. Structures, function
  2. Objects, member functions
  3. Functions, objects
  4. None of the given options
Show answer

Answer: (b) Objects, member functions
Handout reference: Lecture 35, p. 425

Q88. Assignment operator is -------------------------associative

  1. right
  2. left
  3. binary
  4. unary
Show answer

Answer: (a) right
Handout reference: Lecture 31, p. 370

Q89. Static variable which is defined in a function is initialized __________

  1. Only once during its life time
  2. Every time the function call
  3. Compile time of the program
  4. None of the above
Show answer

Answer: (a) Only once during its life time
Handout reference: Lecture 38, p. 460

Q90. A constructor that will create a new object with a full copy of the other object, is copy is known as ___________

  1. deep copy
  2. shallow copy
  3. constructor copy
  4. none of the options
Show answer

Answer: (a) deep copy
Handout reference: Lecture 39, p. 472

Q91. Overloaded new operator function takes parameter of type size_t and returns

  1. void (nothing)
  2. void pointer
  3. object pointer
  4. int pointer
Show answer

Answer: (b) void pointer
Handout reference: Lecture 34, p. 413

Q92. In C++, what does & indicate in a reference declaration and in an address-of expression, respectively?

  1. Address of variable, reference variable
  2. Reference variable, value of variable
  3. Reference variable, address of variable
  4. Address of variable, value of variable
Show answer

Answer: (c) Reference variable, address of variable
Handout reference: Lecture 30, p. 365

Q93. All preprocessor directives are started with the symbol______

  1. *
  2. ++
  3. @
  4. #
Show answer

Answer: (d) #
Handout reference: Lecture 23, p. 270

Q94. When we use manipulators in our program then which header file should be included?

  1. iostream.h
  2. stdlib.h
  3. stdio.h
  4. iomanip.h
Show answer

Answer: (d) iomanip.h
Handout reference: Lecture 36, p. 431

Q95. Which character is inserted at the end of string to indicate the end of string?

  1. new line
  2. tab
  3. null
  4. carriage return
Show answer

Answer: (c) null
Handout reference: Lecture 12, p. 113

Q96. Assignment operator is used for ___________

  1. calculation
  2. reading
  3. assigning value to variables
  4. None of the given options
Show answer

Answer: (c) assigning value to variables
Handout reference: Lecture 3, p. 16

Q97. Overloaded delete operator function takes parameter of void pointer and returns ________

  1. void
  2. void pointer
  3. pointer to an object
  4. pointer of type int
Show answer

Answer: (a) void
Handout reference: Lecture 34, p. 413

Q98. What should be the return type of the constructor?

  1. void pointer
  2. int
  3. same as object type
  4. constructors do not return any thing
Show answer

Answer: (d) constructors do not return any thing
Handout reference: Lecture 27, p. 320

Q99. "setprecision" manipulator will set

  1. The number of digits after the decimal point
  2. The number of digits before the decimal point
  3. The number of digits in a number
  4. None of the given options
Show answer

Answer: (c) The number of digits in a number
Why: Judgment call: in the default mode setprecision(n) sets the number of significant digits; with fixed it sets the digits after the decimal point.
Handout reference: Lecture 36, p. 436

Q100. Which of the following option will be true, if we overload "-=" operator?

  1. only - operator needs to be overloaded
  2. Minus (-) and = operators need to be overloaded
  3. the -= operator need to be overloaded explicitly
  4. the - and = operators need to be overloaded implicitly
Show answer

Answer: (c) the -= operator need to be overloaded explicitly
Handout reference: Lecture 31, p. 369

Q101. Operator overloading can be performed through__________________

  1. Classes
  2. Functions
  3. Operators
  4. Reference
Show answer

Answer: (b) Functions
Handout reference: Lecture 31, p. 370

Q102. When a value is referred by a normal variable then it is known as,

  1. Direct Reference
  2. Indirect Reference
  3. Partial Reference
  4. Proper Reference
Show answer

Answer: (a) Direct Reference
Handout reference: Lecture 14, p. 142

Q103. _______ is used to trace the logic of the program and correct the logical errors

  1. Compiler
  2. Editor
  3. Linker
  4. Debugger
Show answer

Answer: (d) Debugger
Handout reference: Lecture 2, p. 12

Q104. Which of the following function do NOT initialize the chunk of memory to all zero?

  1. calloc() function
  2. Both malloc() and calloc()
  3. None of the above
  4. malloc() function
Show answer

Answer: (d) malloc() function
Handout reference: Lecture 24, p. 280

Q105. The function free() returns back the allocated memory got thorough calloc and malloc to _____

  1. stack
  2. heap
  3. stack and heap
  4. None of the given options
Show answer

Answer: (b) heap
Handout reference: Lecture 24, p. 282

Q106. If we want to use stream insertion and extraction operators with _______ then we have to overload these operators

  1. int, float, double
  2. objects of class
  3. int, float, object
  4. int, char, float
Show answer

Answer: (b) objects of class
Handout reference: Lecture 37, p. 449

Q107. If text is a pointer of class String then what is meant by the following statement? text = new String [5]

  1. Creates an array of 5 string objects statically
  2. Creates an array of 5 string objects dynamically
  3. Creates an array of pointers to string
  4. Creates a string Object
Show answer

Answer: (b) Creates an array of 5 string objects dynamically
Handout reference: Lecture 34, p. 409

Q108. The compiler does not provide a copy constructor if we do not provide it………

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 39, p. 474

Q109. new and delete operators cannot be overloaded as member functions

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 34, p. 412

Q110. To avoid dangling reference, always return the reference of a local variable from a function

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 30, p. 364

Q111. We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 34, p. 410

Q112. The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 37, p. 451

Q113. We can also do conditional compilation with preprocessor directives

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 23, p. 270

Q114. The precedence of an operator can be changed through operator overloading

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 31, p. 370

Q115. Deleting an array of objects without specifying [] brackets may lead to memory leak

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 34, p. 410

Q116. break statement can be used outside a loop or switch statement

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 22, p. 264

Q117. When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 39, p. 477

Q118. With New keyword, data types and class members are initialized with meaningful values instead of garbage

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 28, p. 332

Q119. A function can declare itself a friend of a class

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 29, p. 344

Q120. When new operator is overloaded at global level then corresponding built-in new operator will not be visible to whole of the program

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 34, p. 411

Q121. Operating System is

  1. An application software
  2. System Software
  3. Computer Language
  4. Interpreter
Show answer

Answer: (b) System Software
Handout reference: Lecture 2, p. 8

Q122. ofstream is used for________

  1. Input file stream
  2. Output file stream
  3. Input and output file stream
  4. All of the given
Show answer

Answer: (b) Output file stream
Handout reference: Lecture 18, p. 198

Q123. Transpose of a matrix means that when we interchange rows and columns_____________

  1. the first row becomes the Last column
  2. the first row becomes the first column
  3. the Last row becomes the first column
  4. the first column becomes the first row
Show answer

Answer: (b) the first row becomes the first column
Handout reference: Lecture 13, p. 130

Q124. In_________, we try to have a precise problem statement

  1. Analysis
  2. Design
  3. Coding
  4. None of the given
Show answer

Answer: (a) Analysis
Handout reference: Lecture 13, p. 135

Q125. _______________are conventional names of the command line parameters of the ‘main()’ function

  1. ‘argb’ and ‘argv’
  2. ‘argc’ and ‘argv’
  3. ‘argc’ and ‘argu’
  4. None of the given
Show answer

Answer: (b) ‘argc’ and ‘argv’
Handout reference: Lecture 16, p. 177

Q126. A function that calls itself is known as

  1. Iterative function
  2. Inline function
  3. Recursive function
  4. main ()
Show answer

Answer: (c) Recursive function
Handout reference: Lecture 10, p. 96

Q127. Loader loads the executable code from hard disk to main memory

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 2, p. 12

Q128. The number 544.53 must be stored in _____ data type

  1. int
  2. short
  3. float
  4. char
Show answer

Answer: (c) float
Handout reference: Lecture 3, p. 19

Q129. Which of the following is used with bit manipulation?

  1. Signed integer
  2. Un-signed integer
  3. Signed double
  4. Un-signed double
Show answer

Answer: (b) Un-signed integer
Handout reference: Lecture 21, p. 244

Q130. Memory allocated at run time is a system resource and it is the responsibility of _____ to de-allocate the

  1. System
  2. Programmer
  3. User of program
  4. None of given options
Show answer

Answer: (b) Programmer
Handout reference: Lecture 39, p. 469

Q131. Which looping process checks the test condition at the end of the loop?

  1. for
  2. while
  3. do while
  4. no looping process checks the test condition at the end
Show answer

Answer: (c) do while
Handout reference: Lecture 7, p. 55

Q132. There is a pointer variable named ptr of type int then address of which type of variable the ptr will store in it?

  1. variable of type char
  2. variable of type short
  3. variable of type int
  4. variable of type double
Show answer

Answer: (c) variable of type int
Handout reference: Lecture 14, p. 145

Q133. The input/output streams; cin and cout are ____

  1. Operators
  2. Functions
  3. Objects
  4. Structures
Show answer

Answer: (c) Objects
Handout reference: Lecture 35, p. 421

Q134. dec, hex, oct are all __________

  1. Member functions
  2. Objects of input/output streams
  3. Parameterized manipulators
  4. Non-parameterized manipulators
Show answer

Answer: (d) Non-parameterized manipulators
Handout reference: Lecture 36, p. 439

Q135. A stream is an ordered sequence of bytes

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 35, p. 421

Q136. Which of the following array is the most suitable for storing a matrix structure?

  1. Single-dimensional array
  2. Two-dimensional array
  3. Three-dimensional array
  4. Four-dimensional array
Show answer

Answer: (b) Two-dimensional array
Handout reference: Lecture 12, p. 120

Q137. Copy constructor becomes necessary while dealing with _______allocation in the class

  1. Dynamic memory
  2. Static memory
  3. Both Dynamic and Static memory
  4. None of the given options
Show answer

Answer: (a) Dynamic memory
Handout reference: Lecture 39, p. 474

Q138. Default constructor takes _________

  1. one parameter
  2. two parameters
  3. no parameters
  4. character type parameter
Show answer

Answer: (c) no parameters
Handout reference: Lecture 27, p. 320

Q139. All the preprocessors directives start with ________

  1. $
  2. &
  3. #
  4. _
Show answer

Answer: (c) #
Handout reference: Lecture 23, p. 270

Q140. The members of a class declared with the keyword struct are _____________by default

  1. static
  2. private
  3. protected
  4. public
Show answer

Answer: (d) public
Handout reference: Lecture 28, p. 335

Q141. In statement Matrix m2 = m1

  1. Assignment operator is being used
  2. Copy constructor is being used
  3. Statement has syntax error
  4. None of the given options
Show answer

Answer: (b) Copy constructor is being used
Handout reference: Lecture 39, p. 478

Q142. To get the value stored at a memory address, we use the________________

  1. referencing operator
  2. dereferencing operator
  3. simple operator
  4. None of the given
Show answer

Answer: (b) dereferencing operator
Handout reference: Lecture 14, p. 146

Q143. In C/C++; by default arguments are passed by _______ to a function

  1. Reference
  2. Value
  3. Type
  4. Data
Show answer

Answer: (b) Value
Handout reference: Lecture 25, p. 293

Q144. The string in the array is terminated by a _______

  1. zero
  2. nil
  3. null
  4. one
Show answer

Answer: (c) null
Handout reference: Lecture 12, p. 112

Q145. At the___________, we try to break up the problem into functional units

  1. analysis phase
  2. design phase
  3. Implementation phase
  4. None of the given
Show answer

Answer: (b) design phase
Handout reference: Lecture 13, p. 135

Q146. _________operator uses the sign ^

  1. Bit-wise OR
  2. Exclusive OR
  3. AND Operator
  4. NOT operator
Show answer

Answer: (b) Exclusive OR
Handout reference: Lecture 21, p. 247

Q147. Suppose int multi[5][10]; when we are using **multi , it means,

  1. Single dereferencing
  2. Single referencing
  3. Double referencing
  4. Double dereferencing
Show answer

Answer: (d) Double dereferencing
Handout reference: Lecture 16, p. 172

Q148. What will be the output of the following statement?

cout << setfill('0') << setw(7) << 128;

  1. 0000128
  2. 0128128
  3. 1280000
  4. 0012800
Show answer

Answer: (a) 0000128
Handout reference: Lecture 36, p. 435

Q149. The operator function for << (stream insertion) >> and stream extraction must be

  1. Member function of class
  2. Non-member function of class
  3. Both member and non-member function
  4. None of the given options
Show answer

Answer: (b) Non-member function of class
Handout reference: Lecture 37, p. 443

Q150. Which of the following operators can not be overloaded?

  1. new
  2. delete
  3. +=
  4. sizeof
Show answer

Answer: (d) sizeof
Handout reference: Lecture 31, p. 369

Q151. The second parameter of operator function for >> operator must always be passed

  1. By value
  2. By reference
  3. None of the given option
  4. Function takes no argument
Show answer

Answer: (b) By reference
Handout reference: Lecture 37, p. 451

Q152. In overloading the assignment (=) operator, which object(s) will call the operator function?

  1. Right object of the assignment operator
  2. No object will call the operator function
  3. Left object of the assignment operator
  4. Both objects will call the operator function
Show answer

Answer: (c) Left object of the assignment operator
Handout reference: Lecture 31, p. 370

Q153. The only operator that the compiler overloads for user define data type by default is

  1. Plus (+) operator
  2. Minus (-) operator
  3. Equal (==) operator
  4. Assignment (=) operator
Show answer

Answer: (d) Assignment (=) operator
Handout reference: Lecture 31, p. 370

Q154. What does the this pointer always point to?

  1. A value of data member
  2. Current object of the class
  3. Current function of the class
  4. Current pointer of the class
Show answer

Answer: (b) Current object of the class
Handout reference: Lecture 32, p. 388

Q155. What is the purpose of endl stream manipulator?

  1. Insert a new line
  2. Flush the stream
  3. Terminate a string with null
  4. Insert a new line and flush the stream
Show answer

Answer: (d) Insert a new line and flush the stream
Handout reference: Lecture 36, p. 434

Q156. When we used eof (end of file) with the file reading than which of the following is a true statement?

  1. This is a way to check type of the stream
  2. This is a way to check source of the stream
  3. This is a way to check state of the stream
  4. This is a way to check destination of the stream
Show answer

Answer: (c) This is a way to check state of the stream
Handout reference: Lecture 35, p. 425

Q157. Ostream class is ____ and not under our control

  1. User-defined
  2. Built-in
  3. Both
  4. None
Show answer

Answer: (b) Built-in
Handout reference: Lecture 38, p. 453

Q158. _____ must be included to use stream manipulator in your code

  1. Conio.h
  2. Stdlib.h
  3. Iomanip
  4. Iostream
Show answer

Answer: (c) Iomanip
Handout reference: Lecture 36, p. 431

Q159. Which of the following is a destination of cout stream?

  1. Printer
  2. Constant
  3. Variable
  4. Data type
Show answer

Answer: (a) Printer
Why: Judgment call: cout is connected to the standard output (normally the screen); of the options given, Printer is the closest.
Handout reference: Lecture 35, p. 422

Q160. Static data members of a class can be ____

  1. Public
  2. Private
  3. Both
  4. None
Show answer

Answer: (c) Both
Handout reference: Lecture 38, p. 466

Q161. Once the _____ are created, they exist for the life time of the program

  1. Local variables
  2. Static variables
  3. Automatic variables
  4. non static variables
Show answer

Answer: (b) Static variables
Handout reference: Lecture 38, p. 459

Q162. If the memory in the free store is not sufficient ____________________

  1. Malloc function returns 0
  2. Malloc function returns 1
  3. Malloc function returns free space
  4. Malloc functions returns NULL pointer
Show answer

Answer: (d) Malloc functions returns NULL pointer
Handout reference: Lecture 28, p. 342

Q163. Reference is not really an address it is ______________

  1. A value
  2. A number
  3. A synonym
  4. An antonym
Show answer

Answer: (c) A synonym
Handout reference: Lecture 30, p. 363

Q164. Which situation would require the use of non-member overloaded operator?

  1. The overloaded operator is an Assignment operator
  2. The left most operand is an object of a class
  3. The left operand is built-in data type
  4. The operator returns a reference
Show answer

Answer: (c) The left operand is built-in data type
Handout reference: Lecture 31, p. 373

Q165. When break statement is encountered in switch statement,it

  1. Exits from switch statement
  2. enter from switches statement
  3. both a&b
  4. none of the given option
Show answer

Answer: (a) Exits from switch statement
Handout reference: Lecture 8, p. 68

Q166. The remainder(%) operator is a _ operator

  1. logical
  2. Arithmetic
  3. Relational
  4. Conditional
Show answer

Answer: (b) Arithmetic
Handout reference: Lecture 22, p. 257

Q167. Which loop is particularly suitable when the number of iterations is known in advance?

  1. for
  2. while
  3. do-while
  4. all looping processes require that the iterations be known
Show answer

Answer: (a) for
Handout reference: Lecture 7, p. 58

Q168. In order to get the right most digit of a number, we divide this number by 10 and take _

  1. Its remainder
  2. Its quotient
  3. Its divisor
  4. The number
Show answer

Answer: (a) Its remainder
Handout reference: Lecture 4, p. 28

Q169. How many bytes of memory are occupied by array str? char str[] = "programming";

  1. 10
  2. 11
  3. 12
  4. 13
Show answer

Answer: (c) 12
Handout reference: Lecture 15, p. 169

Q170. Switch statement deals with _ _type of data

  1. Integer
  2. Float
  3. Character
  4. Both Integer and Character
Show answer

Answer: (d) Both Integer and Character
Handout reference: Lecture 8, p. 67

Q171. A constructor has the same name as that of_

  1. Variable
  2. Class
  3. Structure
  4. Function
Show answer

Answer: (b) Class
Handout reference: Lecture 26, p. 316

Q172. Whenever New operator is called to create an object, the _

  1. Constructor
  2. Destructor
  3. Pointer
  4. Array
Show answer

Answer: (a) Constructor
Handout reference: Lecture 28, p. 336

Q173. Sequence of event(s) when deallocating memory using delete operator

  1. Only block of memory is deallocated for object
  2. Only destructor is called for object
  3. Memory is deallocated first before calling destructor
  4. Destructor is called first before deallocating memory
Show answer

Answer: (d) Destructor is called first before deallocating memory
Handout reference: Lecture 34, p. 410

Q174. if there is a symbol(& sign) used with the variable name following by data type then it refers to

  1. Logic expression
  2. Value of variable
  3. Reference variable
  4. Address of variable
Show answer

Answer: (c) Reference variable
Handout reference: Lecture 30, p. 365

Q175. A macro when takes arguments is called

  1. Function
  2. Procedure
  3. Parameterized macro
  4. Simple macro
Show answer

Answer: (c) Parameterized macro
Handout reference: Lecture 23, p. 273

Q176. We can use cin.peek() function to -

  1. See the next character that we are going to get
  2. Get to the end of the object buffer
  3. Get to the end of input buffer
  4. See the first character of input buffer
Show answer

Answer: (a) See the next character that we are going to get
Handout reference: Lecture 35, p. 428

Q177. data members of a contained object can be accessed from inside of the containing class

  1. Private
  2. Public
  3. Protected
  4. All
Show answer

Answer: (b) Public
Handout reference: Lecture 40, p. 487

Q178. The C language was developed in late 60’s and 70’s in

  1. Richards laboratories
  2. Microsoft laboratories
  3. Bell laboratories
  4. IBM laboratries
Show answer

Answer: (c) Bell laboratories
Handout reference: Lecture 2, p. 10

Q179. What happens to an argument that is passed by reference to a function?

  1. The function cannot access the argument’s value
  2. A separate local variable is created to hold a copy of the argument
  3. A temporary independent value is created in the caller
  4. The function accesses the original argument through a reference
Show answer

Answer: (d) The function accesses the original argument through a reference
Handout reference: Lecture 13, p. 134

Q180. In year C was approved as a standard language worldwide

  1. 1988
  2. 1967
  3. 1989
  4. 1986
Show answer

Answer: (c) 1989
Why: C was standardized by ANSI in 1989 (ISO adopted it in 1990).
Handout reference: Lecture 2, p. 11

Q181. Which statement immediately exits the innermost enclosing loop or switch?

  1. Continue
  2. Goto
  3. Pause
  4. Break
Show answer

Answer: (d) Break
Handout reference: Lecture 8, p. 68

Q182. If we write a statement like s2 = s1; ___ will be the calling object and ____ will be passed to the = operator as an argument

  1. s1, s1
  2. s1, s2
  3. s2, s1
  4. s2, s2
Show answer

Answer: (c) s2, s1
Handout reference: Lecture 33, p. 395

Q183. In the S2=S1, ___ is the object that is calling or driving the = operator. ___ object is being passed as an argument to the = operator

  1. S1, S2
  2. S2,S1
  3. =, +
  4. None of the Above
Show answer

Answer: (b) S2,S1
Handout reference: Lecture 31, p. 371

Q184. Stream insertion ( << ) and extraction operators ( >> ) are always implemented as -- _______functions

  1. Member
  2. Non Member
  3. Objects
  4. Classes
Show answer

Answer: (b) Non Member
Handout reference: Lecture 37, p. 451

Q185. We can change the arity (number of operands) through operator overloading

  1. False
  2. True
Show answer

Answer: (a) False
Handout reference: Lecture 31, p. 379

Q186. Self-assignment is dangerous especially at a time when we have memory manipulation in a class

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 33, p. 398

Q187. Which declaration represents an overloaded pre-increment operator implemented as a class member?

  1. ClassName operator+();
  2. ClassName operator+(int);
  3. ClassName operator++();
  4. ClassName operator++(int);
Show answer

Answer: (c) ClassName operator++();
Handout reference: Lecture 32, p. 387

Q188. Assume sizeof(int) is 4 bytes. By how many bytes does an int pointer advance after executing intPtr += 3;?

  1. 3 bytes
  2. 6 bytes
  3. 12 bytes
  4. 24 bytes
Show answer

Answer: (c) 12 bytes
Handout reference: Lecture 15, p. 159

Q189. Which expression dynamically allocates an array of ten int elements in C++?

  1. new int(10);
  2. new int[10];
  3. int new(10);
  4. int new[10];
Show answer

Answer: (b) new int[10];
Handout reference: Lecture 28, p. 333

Q190. Which C memory-management function can resize an already allocated block of memory?

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()
Show answer

Answer: (c) realloc()
Handout reference: Lecture 24, p. 283

Q191. For which types are the standard stream insertion (<<) and extraction (>>) operators already overloaded?

  1. User-defined classes only
  2. Built-in data types
  3. All user-defined classes and built-in data types automatically
  4. None of the given options
Show answer

Answer: (b) Built-in data types
Handout reference: Lecture 37, p. 443

Q192. What is the normal input source for the cin stream object?

  1. File
  2. Disk
  3. Keyboard
  4. RAM
Show answer

Answer: (c) Keyboard
Handout reference: Lecture 35, p. 422

Q193. Which statement initializes an int variable x to 10 at the time of its declaration?

  1. int x; x = 10;
  2. int x = 10;
  3. int x, x = 10;
  4. x = 10;
Show answer

Answer: (b) int x = 10;
Handout reference: Lecture 5, p. 35

Q194. Which is the correct declaration of a two-dimensional 2-by-2 array of float values?

  1. float arr{2}{2};
  2. float arr[2][2];
  3. float arr[2,2];
  4. float[2][2] arr;
Show answer

Answer: (b) float arr[2][2];
Handout reference: Lecture 12, p. 120

Q195. Does an ordinary non-static local variable continue to exist after its function returns?

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 38, p. 458

Q196. The C++ delete operator returns memory previously allocated with new to free store.

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 28, p. 330

Q197. A function template may contain only generic data types, not ordinary data types.

  1. True
  2. False
Show answer

Answer: (b) False
Handout reference: Lecture 41, p. 499

Q198. Assume arr is an array and ptr is a compatible pointer. Which statement makes ptr point to the first element of arr?

  1. *ptr = arr;
  2. ptr = arr;
  3. *ptr = arr[5];
  4. ptr = arr[5];
Show answer

Answer: (b) ptr = arr;
Handout reference: Lecture 15, p. 158

Q199. Which C header files define the NULL macro, as described in the handouts?

  1. strings.h and iostream.h
  2. ctype.h and conio.c
  3. conio.c and conio.h
  4. stdlib.h and stddef.h
Show answer

Answer: (d) stdlib.h and stddef.h
Handout reference: Lecture 24, p. 279

Q200. For a class that manages dynamically allocated memory, an incorrectly implemented self-assignment can produce which kind of error?

  1. Syntax error
  2. Logical error
  3. Link error
  4. None of the given options
Show answer

Answer: (b) Logical error
Handout reference: Lecture 39, p. 476

Q201. Including objects of existing classes as data members of a new class is a way to reuse code.

  1. True
  2. False
Show answer

Answer: (a) True
Handout reference: Lecture 40, p. 488

Q202. Which is a valid generic declaration for a binary member operator+ that returns an object of the class?

  1. ClassName operator+(ClassName rhs);
  2. operator ClassName+();
  3. operator ClassName+(rhs);
  4. ClassName operator+();
Show answer

Answer: (a) ClassName operator+(ClassName rhs);
Handout reference: Lecture 31, p. 371

Q203. What is the conventional return type of an overloaded stream extraction (>>) operator?

  1. The class being read
  2. ostream&
  3. istream&
  4. void
Show answer

Answer: (c) istream&
Handout reference: Lecture 37, p. 445

Q204. What is the range of values returned by rand() on the implementation used in the course (RAND_MAX = 32767)?

  1. 0–32768
  2. 1–32768
  3. 0–32767
  4. 1–32767
Show answer

Answer: (c) 0–32767
Why: RAND_MAX is 32767 on the compilers used in the course; it can differ on other compilers.

Q205. When using a library function, predefined object, or macro, which file is normally included?

  1. Source file
  2. Object file
  3. Header file
  4. Executable file
Show answer

Answer: (c) Header file

Q206. In the statement int &x = y;, what happens when x is initialized?

  1. A new integer variable x is created with the value of y
  2. x is a pointer holding the address of y
  3. x becomes another name for y without creating a separate integer object
  4. A new variable y is created with the value of x
Show answer

Answer: (c) x becomes another name for y without creating a separate integer object

Q207. Which operator is used in a C-style type cast?

  1. Binary
  2. Cast
  3. Cost
  4. Const
Show answer

Answer: (b) Cast

Q208. When a function template is instantiated, the compiler identifies the type parameters and generates what?

  1. A function specialization for the required types
  2. An old function copy
  3. A separate unrelated template declaration
  4. A runtime-only function
Show answer

Answer: (a) A function specialization for the required types

Q209. Templates provide a way of abstracting which kind of information?

  1. Type
  2. Data value
  3. Method name only
  4. Access level
Show answer

Answer: (a) Type

Q210. What is a name defined by the preprocessor that is substituted with its replacement text?

  1. Include
  2. Directive
  3. Macro
  4. Define statement
Show answer

Answer: (c) Macro

Q211. Where may a header file be included in a C++ source file?

  1. Anywhere syntactically valid before its contents are needed
  2. Only at the beginning of the file
  3. Only at the end of the file
  4. Only inside main()
Show answer

Answer: (a) Anywhere syntactically valid before its contents are needed

Q212. Which is NOT, by itself, a memory-management error?

  1. Memory leak
  2. Lost or unreferenced allocated memory
  3. Using a void pointer
  4. Dangling pointer
Show answer

Answer: (c) Using a void pointer

Q213. In obj3 = obj1 - obj2;, if obj1 invokes the overloaded operator and obj2 is its parameter, what kind of operator function is used?

  1. Friend function
  2. Virtual function
  3. Member function
  4. Non-member function
Show answer

Answer: (c) Member function

Q214. The base address of an array is the address of which element?

  1. First
  2. Second
  3. Third
  4. Fourth
Show answer

Answer: (a) First

Q215. What type of expression normally controls a loop?

  1. Arithmetic expression only
  2. Constant expression only
  3. Boolean expression
  4. Primary expression only
Show answer

Answer: (c) Boolean expression

Q216. What does a break statement do when executed inside a loop?

  1. Exits the current loop
  2. Exits the entire program
  3. Immediately starts the next loop iteration
  4. Causes a compilation error
Show answer

Answer: (a) Exits the current loop

Q217. Which of the following is not an integer literal?

  1. 0
  2. -32
  3. 65531
  4. 4.0
Show answer

Answer: (d) 4.0

Q218. The ws manipulator is used with which kind of stream?

  1. Output
  2. Input
  3. Both input and output
  4. Buffered only
Show answer

Answer: (b) Input

Q219. Using objects as data members of other classes promotes which software quality?

  1. Accessibility
  2. Reliability
  3. Reusability
  4. Usability
Show answer

Answer: (c) Reusability

Q220. When an array of class objects is destroyed, how are destructors invoked?

  1. Only once for the whole array
  2. Once for each constructed array element
  3. Never
  4. Only for the first element
Show answer

Answer: (b) Once for each constructed array element

Q221. When #define PI 3.1415926 is used, what happens to PI during preprocessing?

  1. It cannot be replaced with another value automatically during execution
  2. A function modifies its value
  3. Its occurrences are replaced by the fixed macro text
  4. A runtime operation updates the macro
Show answer

Answer: (c) Its occurrences are replaced by the fixed macro text

Q222. Which is a valid C++ class declaration?

  1. class B {}
  2. public class A {}
  3. object A { int x; };
  4. class A { int x; };
Show answer

Answer: (d) class A { int x; };

Q223. When accessing an element in a C++ multidimensional array, how are subscripts written?

  1. Separated only by commas
  2. All inside one set of brackets separated by commas
  3. Separated by commas and braces
  4. Each subscript enclosed in its own brackets
Show answer

Answer: (d) Each subscript enclosed in its own brackets

Q224. What is the default type of an unsuffixed decimal integer literal such as 10 when its value fits?

  1. short
  2. float
  3. int
  4. double
Show answer

Answer: (c) int

Q225. Which header traditionally declares malloc() and calloc() in C/C++?

  1. memory.h
  2. iostream.h
  3. stdlib.h
  4. conio.h
Show answer

Answer: (c) stdlib.h

Q226. An allocation expression new T normally returns a pointer to which part of the allocated object?

  1. Second address
  2. Start of the allocated object
  3. Last address
  4. Previous memory block
Show answer

Answer: (b) Start of the allocated object

Q227. For obj3 = obj1 + obj2;, when + is overloaded as a member of obj1’s class, which operand calls the operator?

  1. obj2 calls + and obj1 is passed
  2. obj1 calls + and obj2 is passed
  3. Both are passed as explicit arguments
  4. Either may invoke it interchangeably
Show answer

Answer: (b) obj1 calls + and obj2 is passed

Q228. A function template must have at least how many template parameters in this course context?

  1. Zero
  2. One
  3. Two
  4. Three
Show answer

Answer: (b) One

Q229. What does the compiler-generated copy-assignment operator normally perform?

  1. It prohibits assignment
  2. It assigns no data members
  3. It assigns only selected members
  4. It performs memberwise assignment
Show answer

Answer: (d) It performs memberwise assignment

Q230. A pointer declared float* ptr; may directly hold the address of which type of object?

  1. int
  2. float
  3. char
  4. double
Show answer

Answer: (b) float

Q231. Which of the following is NOT a preprocessor directive?

  1. #error
  2. #define
  3. #line
  4. #ndefine
Show answer

Answer: (d) #ndefine

Q232. When x = 7; then the expression x%= 2; will calculate the value of x as,

  1. 1
  2. 3
  3. 7
  4. 2
Show answer

Answer: (a) 1

Q233. When we are using const keyword with a variable x then initializing it at the time of declaration is,

  1. Must
  2. Optional
  3. Not necessary
  4. A syntax error
Show answer

Answer: (a) Must

Q234. What is the output of the following C++ code?

int arr[6] = {2, 3, 7, 4, 5, 6};
int *ptr1 = &arr[1];
int *ptr2 = &arr[4];
cout << (ptr2 - ptr1);

  1. 3
  2. 9
  3. 12
  4. 2
Show answer

Answer: (a) 3

Q235. C is a/an ______ language

  1. low level
  2. object based
  3. object oriented
  4. function oriented
Show answer

Answer: (d) function oriented
Why: Course wording (C is a procedural, function-oriented language).

Q236. What is wrong with the following for-loop header?

for (int k = 2, k <= 12, k++)

  1. the increment should always be ++k
  2. the variable must always be the letter i when using a for loop
  3. there should be a semicolon at the end of the statement
  4. Commas must be replaced with semicolons
Show answer

Answer: (d) Commas must be replaced with semicolons

Q237. The reserved words public and private comes from

  1. structures
  2. strings
  3. accessibility modifiers
  4. types of functions
Show answer

Answer: (c) accessibility modifiers

Q238. Which aspect of a class should be hidden from clients to preserve encapsulation?

  1. Functions name within class
  2. Class implementation of Functionality and Identifiers
  3. Methods Functionality within class
  4. None of the given options
Show answer

Answer: (b) Class implementation of Functionality and Identifiers

Q239. seekg() and write() are functionally _________________

  1. Different
  2. Identical
  3. Two names of same function
  4. None of the above
Show answer

Answer: (a) Different

Q240. Suppose a program contains an array declared as int arr[100]; what will be the size of array?

  1. 0
  2. 99
  3. 100
  4. 101
Show answer

Answer: (c) 100

Q241. Which of the following statements is true about new when dynamically allocating memory for an object?

  1. The new operator determines the size of an object
  2. Allocates memory to object and returns pointer of valid type
  3. Creates an object and calls the constructor to initialize the object
  4. All of the given options
Show answer

Answer: (d) All of the given options

Q242. For non-member operator function, object on left side of the operator may be

  1. Object of operator class
  2. Object of different class
  3. Built-in data type
  4. All of the given options
Show answer

Answer: (d) All of the given options

Q243. Class is a user defined___________

  1. data type
  2. memory reference
  3. value
  4. none of the given options
Show answer

Answer: (a) data type

Q244. In obj3 = obj1 + obj2;, if operator+ is overloaded as a non-member function, how are the operands supplied?

  1. obj1 will be passed as an argument to + operator whereas obj2 will drive the + operator
  2. obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator
  3. Both objects (obj1, obj2) will be passed as arguments to the + operator
  4. Any of the objects (obj1, obj2) can drive the + operator
Show answer

Answer: (c) Both objects (obj1, obj2) will be passed as arguments to the + operator

Q245. Which of the following function cannot be overloaded?

  1. Member functions
  2. Utility functions
  3. Constructor
  4. Destructor
Show answer

Answer: (d) Destructor

Q246. The code is written to __________ the program

  1. implement
  2. design
  3. analysis
  4. none of the given options
Show answer

Answer: (a) implement

Q247. In C/C++ if we define an array of size eight ( i.e. int Arr [8]; then the last element of this array will be stored at,

  1. Arr[0]
  2. Arr[8]
  3. Arr[7]
  4. Arr[-1]
Show answer

Answer: (c) Arr[7]

Q248. Array is a data structure which store

  1. Memory addresses
  2. Variables
  3. Data Type
  4. Data
Show answer

Answer: (d) Data

Q249. The default value of a parameter can be provided inside the ________________

  1. function prototype
  2. function definition
  3. both function prototype or function definition
  4. none of the given options
Show answer

Answer: (c) both function prototype or function definition

Q250. When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are

  1. Zero
  2. One
  3. Two
  4. N arguments
Show answer

Answer: (b) One

Q251. "new" and "delete" keywords are _____________ in C++ language

  1. Built-in- Function
  2. Operators
  3. Memory Allocation Function
  4. None of the given options
Show answer

Answer: (b) Operators

Q252. Which two values are printed, in order, by the cout statements in this program?

int func(int);
int num = 10;
int main() {
    int num;
    num = 5;
    cout << num;
    cout << func(num);
}
int func(int x) {
    return num;
}

  1. 5, 5
  2. 10, 5
  3. 5, 10
  4. 10, 10
Show answer

Answer: (c) 5, 10

Q253. Every class contains _______________

  1. Constructor
  2. Destructor
  3. Both a constructor and a destructor
  4. None of the given options
Show answer

Answer: (c) Both a constructor and a destructor

Q254. What does 5 ^ 6 , evaluate to in decimal where ‘^’ is Exclusive OR operator?

  1. 1
  2. 2
  3. 3
  4. 4
Show answer

Answer: (c) 3

Q255. In C++, what are the default access levels of struct members and class members, respectively?

  1. private, public
  2. public, private
  3. private, protected
  4. public, protected
Show answer

Answer: (b) public, private

Q256. Which of the following is true for the C++ statement given below?

int &ref = val;

  1. It creates a synonym for variable ‘val’
  2. It creates an alias for variable ‘val’
  3. It’s a new name for variable ‘val’
  4. All of the given options
Show answer

Answer: (d) All of the given options

Q257. Unary operator implemented as member function takes ____ arguments whereas non- member function takes _____ arguments

  1. One, zero
  2. Zero, one
  3. One, two
  4. Two, one
Show answer

Answer: (b) Zero, one

Q258. Given the following operator+ declaration in class M, how is the operator implemented for p + q?

class M {
public:
    M& operator+(const M&);
};

  1. Member function
  2. Non-member function
  3. Friend function
  4. None of the given options
Show answer

Answer: (a) Member function

Q259. What does (*this) represents?

  1. The current function of the class
  2. The current pointer of the class
  3. The current object of the class
  4. A value of the data member
Show answer

Answer: (c) The current object of the class

Q260. What purpose do classes serve?

  1. Data encapsulation
  2. Providing a convenient way of modeling real-world objects
  3. Simplifying code reuse
  4. All of the given options
Show answer

Answer: (d) All of the given options

Q261. new operator is used to allocate memory from the free store during

  1. Compile Time
  2. Run Time
  3. Link Time
  4. None of the given options
Show answer

Answer: (b) Run Time

Q262. && is -------------------- operator

  1. An arithmetic
  2. Logical
  3. Relational
  4. Unary
Show answer

Answer: (b) Logical

Q263. Which of the following function(s) is/are included in ctype.h header file?

  1. isdigit(int c)
  2. isxdigit(int c )
  3. tolower(int c)
  4. All of the above
Show answer

Answer: (d) All of the above

Q264. The object _______________may be used both for file input and file output

  1. fstream
  2. ifstream
  3. ofstream
  4. none of the given options
Show answer

Answer: (a) fstream

Q265. If there is more than one statement in the block of a for loop, which of the following must be placed at the beginning and the ending of the loop block?

  1. parentheses ( )
  2. braces { }
  3. brackets [ ]
  4. arrows < >
Show answer

Answer: (b) braces { }

Q266. A pointer variable can be,

  1. Decremented
  2. Incremented
  3. Multiplied
  4. Both incremented and Decremented
Show answer

Answer: (d) Both incremented and Decremented

Q267. What are the values of a and b after executing these statements?

int a = 3;
int b = a++;

  1. 3,4
  2. 4,4
  3. 3,3
  4. 4,3
Show answer

Answer: (d) 4,3

Q268. Consider the following code segment. What will be the output of this code?

int addValue(int *a) {
    int b = (*a) + 2;
    return b;
}
int main() {
    int x = 6;
    cout << x << ",";
    cout << addValue(&x) << ",";
    cout << x;
}

  1. 6,8,6
  2. 6,6,8
  3. 6,8,8
  4. 6,6,6
Show answer

Answer: (a) 6,8,6

Q269. When ever dynamic memory allocation is made in C/C++, it is freed_____________

  1. Explicitly
  2. Implicitly
  3. Both explicitly and implicitly
  4. None of the given options
Show answer

Answer: (a) Explicitly

Q270. What happens when the following array of String objects is initialized (assuming both constructors exist)?

String str[5] = {String("Programming"), String("CS201")};

  1. Default constructor will call for all objects of array
  2. Parameterized constructor will call for all objects of array
  3. The parameterized constructor is called for the first two objects and the default constructor for the remaining three.
  4. Default constructor will call for first 3 objects and Parameterized constructor for remaining
Show answer

Answer: (c) The parameterized constructor is called for the first two objects and the default constructor for the remaining three.

Q271. Overloaded member operator function is always called by _______

  1. Class
  2. Object
  3. Compiler
  4. Primitive data type
Show answer

Answer: (b) Object

Q272. The static data members of a class can be accessed by ________

  1. only class
  2. only object
  3. both class and objects
  4. none of given options
Show answer

Answer: (c) both class and objects

Q273. Given the following operator! declaration in class M, how is the operator implemented for !s?

class M {
    friend int operator!(const M&);
};

  1. Member function
  2. Non-member function
  3. Binary operator function
  4. None of the given options
Show answer

Answer: (b) Non-member function

Q274. The variables having a name, type and size are just like empty boxes

  1. True
  2. False
Show answer

Answer: (a) True

Q275. setw is a parameterized manipulator

  1. True
  2. False
Show answer

Answer: (a) True

Q276. The operator functions of << and >> operators are always the member function of a class

  1. True
  2. False
Show answer

Answer: (b) False

Q277. Reusing the variables in program helps to save the memory

  1. True
  2. False
Show answer

Answer: (a) True

Q278. We can also define a user-defines manipulators

  1. True
  2. False
Show answer

Answer: (a) True

Q279. An array stores the numbers into consecutive memory locations

  1. True
  2. False
Show answer

Answer: (a) True

Q280. The template functions do not promote the code reuse

  1. True
  2. False
Show answer

Answer: (b) False

Q281. The default copy constructor provides member wise assignment

  1. True
  2. False
Show answer

Answer: (a) True

Q282. We can also create an array of user defined data type

  1. True
  2. False
Show answer

Answer: (a) True

Q283. Assignment operator is used to initialize a newly declared object from existing object

  1. True
  2. False
Show answer

Answer: (b) False

Q284. eof( ), bad( ), good( ), clear( ) all are manipulators

  1. True
  2. False
Show answer

Answer: (b) False

Q285. The return type of operator function must always be void

  1. True
  2. False
Show answer

Answer: (b) False

Q286. Heap is constantly changing in size

  1. True
  2. False
Show answer

Answer: (a) True

Q287. We can use New keyword inside of Class Constructor

  1. True
  2. False
Show answer

Answer: (a) True

Q288. Array is passed by value to a function by default

  1. True
  2. False
Show answer

Answer: (b) False

Q289. When an operator function is define as member function then operand on the left side of operator must be an object

  1. True
  2. False
Show answer

Answer: (a) True

Q290. We cannot define a variable of user-defined data type in the class

  1. True
  2. False
Show answer

Answer: (b) False

Q291. C++ is a case-sensitive language

  1. True
  2. False
Show answer

Answer: (a) True

Q292. Template class cannot have static variables

  1. True
  2. False
Show answer

Answer: (b) False

Q293. It is possible to define a class within another class

  1. True
  2. False
Show answer

Answer: (a) True

Q294. Set-precision is a parameter less manipulator

  1. True
  2. False
Show answer

Answer: (b) False

Q295. We can do arithmetic with references like pointers

  1. True
  2. False
Show answer

Answer: (b) False

Q296. Overloaded delete operator function takes the same parameter as an argument returned by new operator function

  1. True
  2. False
Show answer

Answer: (a) True

Q297. We cannot make a member function of a class as template function

  1. True
  2. False
Show answer

Answer: (b) False

Q298. Templates are not type safe

  1. true
  2. false
Show answer

Answer: (b) false

Q299. Dealing with structures and functions passing by reference is the most economical method

  1. True
  2. False
Show answer

Answer: (a) True

Q300. Constructors cannot be overloaded like ordinary functions

  1. True
  2. False
Show answer

Answer: (b) False

Q301. Computer can understand only machine language code

  1. True
  2. False
Show answer

Answer: (a) True

Q302. We can change a Unary operator to Binary operator through operator overloading

  1. False
  2. True
Show answer

Answer: (a) False

Q303. Compiler is a

  1. System software
  2. Application Software
  3. Driver
  4. Editor
Show answer

Answer: (a) System software

Q304. If most significant bit of un-signed number is 1 then it represents a positive number

  1. True
  2. False
Show answer

Answer: (a) True

Q305. A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

  1. Write a separate class to handle each
  2. Use templates
  3. Use strings to store all types
  4. None of the given options
Show answer

Answer: (b) Use templates

Q306. Function prototype is written,

  1. Within main function
  2. After the return statement in main
  3. Before the return statement in main
  4. Before call of that function
Show answer

Answer: (d) Before call of that function

Q307. If int sum = 54, what is the value of sum after executing sum = sum - 3;?

  1. 52
  2. 50
  3. 51
  4. 57
Show answer

Answer: (c) 51

Q308. In while loop the loop counter must be initialized,

  1. With in the loop
  2. Before entering the loop
  3. At the end of the loop
  4. None of the given options
Show answer

Answer: (b) Before entering the loop

Q309. Computer can understand only __________language code

  1. High level
  2. Low level
  3. Machine
  4. Fourth generation
Show answer

Answer: (c) Machine

Q310. < , <= , > , >= are called __________ operators

  1. Arithmetic
  2. Logical
  3. Relational
  4. Conational
Show answer

Answer: (c) Relational

Q311. If int a = 50; then the value of a/= 3; will be,

  1. 15
  2. 18
  3. 16
  4. 17
Show answer

Answer: (c) 16

Q312. From the following; which one is used as an assignment operator?

  1. Equal sign ‘=’
  2. Double equal sign ‘==’
  3. Both equal and double equal sign
  4. None of the given options
Show answer

Answer: (a) Equal sign ‘=’

Q313. Which statement about C++ static member functions is accurate?

  1. must be declared inside the class definition, but defined outside it
  2. must be declared private
  3. have multiple copies for the entire class
  4. Can directly access static data members; an explicit object is needed to access non-static data
Show answer

Answer: (d) Can directly access static data members; an explicit object is needed to access non-static data

Q314. If Num is an integer variable then Num++ means,

  1. Add 1 two times with Num
  2. Add 1 with Num
  3. Add 2 with Num
  4. Subtract 2 from Num
Show answer

Answer: (b) Add 1 with Num

Q315. If the return type of a function is void then it means that it will,

  1. Return any type of data
  2. Return some specific type of data
  3. Return no data
  4. Return just character data
Show answer

Answer: (c) Return no data

Q316. Assuming int occupies 4 bytes and double occupies 8 bytes, what is sizeof(VAZ) for the following union?

union intorDouble {
    int ival;
    double charvar;
};
int main() {
    intorDouble VAZ;
    int size = sizeof(VAZ);
}

  1. 2
  2. 4
  3. 8
  4. 12
Show answer

Answer: (c) 8

Q317. The member functions of a class occupy _____ region in memory for ____ object(s) of class

  1. separate, each
  2. common, all
  3. different, each
  4. different, all
Show answer

Answer: (b) common, all

Q318. Which of the following is true while overloading operators?

  1. Precedence of an operator can be changed
  2. The arity (number of operands) can be changed
  3. No new operators can be created
  4. Associativity of an operator can be changed
Show answer

Answer: (c) No new operators can be created

Q319. What will be the output of the following statement?

cout << setbase(16) << 52;

  1. 74
  2. 52
  3. 34
  4. 64
Show answer

Answer: (c) 34

Q320. Within the statement obj1=obj2; obj1 will call the assignment operator function and obj2 will be passed as an argument to function

  1. True
  2. False
Show answer

Answer: (a) True

Q321. Which is NOT a protection level provided by classes in C++?

  1. protected
  2. hidden
  3. private
  4. public
Show answer

Answer: (b) hidden

Q322. We cannot use ______________ pointer for storing and reading data from it

  1. NULL
  2. integer
  3. double
  4. zero
Show answer

Answer: (a) NULL

Q323. We cannot increment ________________

  1. pointers
  2. arrays
  3. references
  4. variables
Show answer

Answer: (b) arrays

Q324. From the following; which on is the correct syntax of an array declaration: array size is 5 and it is of float data type?

  1. float [5] name;
  2. name[5] float;
  3. float name[5];
  4. None of the given options
Show answer

Answer: (c) float name[5];

Q325. If an array has 50 elements, what is allowable range of subscripts?

  1. 0 – 49
  2. 1 – 49
  3. 0 – 50
  4. 1 – 50
Show answer

Answer: (a) 0 – 49

Q326. C++ is a_________________ language

  1. High level
  2. Low level
  3. Machine
  4. Fourth Generation
Show answer

Answer: (a) High level

Q327. In the course examples, what type of displacement is commonly supplied to seekg() and seekp()?

  1. int
  2. short
  3. long
  4. double
Show answer

Answer: (c) long

Q328. Which of the following is used for allocating space for static variables?

  1. Heap
  2. Static storage area
  3. Free store
  4. Stack
Show answer

Answer: (b) Static storage area

Q329. Truth tables are used for analyzing ___________

  1. logical expressions
  2. arithmetic expressions
  3. both logical and arithmetic expressions
  4. none of the given options
Show answer

Answer: (a) logical expressions

Q330. When operator function is implemented as member function then return type of function ____

  1. Can be any data type
  2. Must be built – in data type
  3. Must be user defined data type
  4. Must be an object of same class
Show answer

Answer: (a) Can be any data type

Q331. We cannot define friends for the _____ that are already written and not in our control

  1. Classes
  2. Functions
  3. Destructor
  4. Constructors
Show answer

Answer: (a) Classes

Q332. What will be the output of the following code segment?

float pi = 32.3996;
cout << fixed;
cout << setprecision(2) << pi;

  1. 32.00
  2. 32.39
  3. 32.40
  4. 32.310
Show answer

Answer: (c) 32.40

Q333. Consider the following code segment. Which of the following will be called while executing the second line?

String s1, s2;
s1 = s2;

  1. Default operator
  2. Copy constructor
  3. Assignment operator
  4. Parameterized constructor
Show answer

Answer: (c) Assignment operator

Q334. Consider the following code. The printed value will be converted into:

int n = 10;
cout << oct << n;

  1. base 2
  2. base 8
  3. base 10
  4. decimal number system
Show answer

Answer: (b) base 8

Q335. Which of the following is normally a destination for cin stream?

  1. Mouse
  2. Printer
  3. Variable
  4. Keyboard
Show answer

Answer: (c) Variable

Q336. This reference to a variable can be obtained by preceding the identifier of a variable with ________________

  1. ^ sign
  2. Operator
  3. Dot operator
  4. Ampersand sign &
Show answer

Answer: (d) Ampersand sign &

Q337. Once an object is declared as a friend, _________________________

  1. It has access to public members only
  2. It has to protected data members only
  3. It has no access to data members of the class
  4. It has access to all non-public members as if they were public
Show answer

Answer: (d) It has access to all non-public members as if they were public

Q338. Structure is a collection of _ under a single name

  1. Only function
  2. Only variables
  3. Both data members and member functions
  4. None
Show answer

Answer: (c) Both data members and member functions
Why: In C++ a struct can hold member functions as well as data (in C it holds only data).

Q339. What does the C++ statement cout << expression normally do?

  1. Read the data from keyboard
  2. Read the data from file
  3. Write into a file
  4. Display information on the screen
Show answer

Answer: (d) Display information on the screen

Q340. What quality describes an interface that is easy to understand and use?

  1. Scalability
  2. Reliability
  3. sustainability
  4. usability
Show answer

Answer: (d) usability

Q341. Which of the following if missing would result in infinite recursion in case of recursive function?

  1. Recursive call
  2. Base case
  3. Function parameters
  4. Local variables
Show answer

Answer: (b) Base case

Q342. Which of the given operator is not a valid compound assignment operator?

  1. >>=
  2. =>>
  3. *=
  4. &=
Show answer

Answer: (b) =>>

Q343. What is the term for reusing an existing segment of code with little or no modification?

  1. Code efficiency
  2. Code reusability
  3. Code notification
  4. Code compatibility
Show answer

Answer: (b) Code reusability

Q344. By default, how is an individual scalar array element passed to a C++ function?

  1. By reference
  2. By data type
  3. By value
  4. By data
Show answer

Answer: (c) By value

Q345. Which statement correctly declares an array of ten int elements?

  1. int[10] name;
  2. name[10] int;
  3. int name[10];
  4. int name[];
Show answer

Answer: (c) int name[10];

Q346. We cannot define a function as a friend of a Template class

  1. True
  2. False
Show answer

Answer: (b) False

Q347. Reserve words cannot be used as a variable name

  1. True
  2. False
Show answer

Answer: (a) True

Q348. We can also define a variable of user define data type (object) as static

  1. True
  2. False
Show answer

Answer: (a) True

Q349. Most efficient method of dealing with structure variables is to define the structure globally

  1. True
  2. False
Show answer

Answer: (b) False

Q350. Operating system falls under the category of ______types of software

  1. System Software
  2. Application Software
  3. Device drivers
  4. None of the above
Show answer

Answer: (a) System Software

Q351. The preprocessor runs before which phase of C/C++ translation?

  1. Compiler
  2. Execution
  3. Linking
  4. None of the given options
Show answer

Answer: (a) Compiler

Q352. cerr and clog are the objects of stream class

  1. True
  2. False
Show answer

Answer: (a) True

Q353. A template function can not have static variables

  1. True
  2. False
Show answer

Answer: (b) False

Q354. A C++ friend function must always be defined outside its class definition.

  1. True
  2. False
Show answer

Answer: (b) False

Q355. Assuming ptr points to the first element of an array, which expression obtains the fourth element?

  1. ptr[3]
  2. (ptr + 3)
  3. *(ptr + 3)
  4. Both A and C
Show answer

Answer: (d) Both A and C

Part 2: Subjective Questions

Short Questions

Short answers of 2-5 marks.

Q1. Why is the loop header for (int i = 0; j < numRows; i--) incorrect, and how can it be repaired?

The loop declares and initializes i but tests j, which is not necessarily declared or related to the loop. It also decrements i when a traversal from 0 up to numRows normally needs incrementing. The corrected header is: for (int i = 0; i < numRows; ++i).

Q2. What are the main types of operators based on the number of operands?

Unary operators act on one operand (for example, ++x or !flag). Binary operators act on two operands (for example, a + b). The conditional operator ?: is a ternary operator acting on three operands.

Q3. Write the general syntax of a switch statement.

The general form of the switch statement (the expression must be an integer or character type; each case label is a constant):

switch (expression) {
    case value1:
        // statements
        break;
    case value2:
        // statements
        break;
    default:
        // statements (runs when no case matches)
}

break stops execution from falling through into the next case.

Q4. Which bitwise operator produces 1 when two corresponding bits are different?

The bitwise exclusive OR operator (^). A bit in a ^ b is 1 exactly when the corresponding bits of a and b differ.

Q5. Differentiate between std::endl and the newline character \n.

Both move output to the next line, but std::endl also flushes the output stream. The character \n only inserts a newline; it does not force a flush.

Q6. If inner and outer blocks declare variables with the same name, which variable is used inside the inner block?

The variable declared in the inner block shadows the one declared in the outer block until the inner scope ends.

Q7. How many operands do unary and binary operators require? Can operator overloading change this number?

A unary operator takes one operand and a binary operator takes two operands. Operator overloading cannot change the number of operands (arity) prescribed by C++.

Q8. Compare if/else and switch selection statements.

if/else evaluates general Boolean conditions, including ranges and compound expressions. switch selects among case labels based on an integral or enumeration expression; each case label is a constant expression, and break prevents unintentional fall-through.

Q9. Define a static local variable and explain its lifetime.

A static local variable has static storage duration. It is initialized once, retains its value between function calls, and exists until program termination.

Q10. Which arithmetic operator cannot accept floating-point operands in C++?

The remainder/modulus operator (%) requires integral operands. It cannot be directly applied to float or double values.

Q11. When is the result of the bitwise exclusive OR (XOR) operation equal to 1 for a pair of bits?

XOR yields 1 when its two input bits differ: 0 XOR 1 = 1 and 1 XOR 0 = 1. It yields 0 if both bits are identical.

Q12. What is a truth Table?

A truth table lists every possible combination of the truth values (true/false) of the operands and shows the result of a logical expression for each combination. It is used to analyse and check complicated logical conditions, and it is the basic tool of logic design.

ABA && BA || B!A
00001
01011
10010
11110

Q13. What does code optimization mean?

It is process by which we make our code such a way that it improves the speed of program. By use of optimization we refine program codes in such a way that it run faster and consume less memory. We do it in such a way that output quality is not compromised.

Q14. What is a language translator? Name two main kinds used in programming.

A language translator is a program that converts a program written in a programming language into a form the computer can execute (machine language). The two main kinds are:

  • Compiler: translates the whole program at once and produces an executable file; it reports all syntax errors and generates no executable if there is an error.
  • Interpreter: translates and executes the program one statement at a time, stopping at the first error; execution is slower because translation is repeated on every run.

Q15. Explain the role of testing when designing a program.

Testing checks whether the implemented program meets its specified requirements. Prepare test cases covering normal inputs, boundary values, invalid inputs and expected outputs; run the tests, investigate failures, correct errors and repeat. Independent testing can reveal assumptions the programmer missed.

Q16. What is a runtime error?

A runtime error occurs during execution rather than compilation, such as a division by zero, out-of-range memory access or failure to open a required file. Some runtime errors terminate the program; others produce incorrect behavior. Error checking and exception handling can mitigate them.

Q17. Classify LINUX, DISK CLEANUP, WORD PROCESSOR, WINDOWS, and STUDENT INFORMATION SYSTEM as system or application software.

System software: Linux (operating system), Windows (operating system), Disk Cleanup (system utility). Application software: Word processor; Student Information System.

Q18. Define automatic variables and explain why they are called automatic.

An automatic local variable is created when its block is entered and its lifetime normally ends when the block exits. Storage duration is managed automatically by the language, unlike manually allocated dynamic objects. Example: void f() { int x=0; }

Q19. What steps should be followed to design a good computer program?

Analyze the requirements and inputs/outputs; decompose the problem into manageable parts; choose algorithms and data structures; write clear code with meaningful names and comments; test against normal, boundary, and invalid cases; debug, refine, and make reusable components where appropriate.

Q20. Explain the C++ statement const int *ptr = &x;.

ptr points to an integer whose value cannot be modified through ptr. The pointer itself can be reassigned. The declaration is valid if x is an int object whose address can be taken.

Q21. How much memory is allocated by char name[] = "Hello World";, and why?

The array has 12 char elements: 11 visible characters (including the space) plus the terminating null character (\0). Under the C++ definition, sizeof(char) is 1 byte, so sizeof(name) is 12 bytes.

Q22. What does the C++ new operator return, and what happens if allocation fails?

A successful new expression returns a pointer to the newly created object. An ordinary new expression throws std::bad_alloc on allocation failure; new (std::nothrow) returns nullptr instead.

Q23. What is the output of the following C++ code?

int x = 10;
int &y = x;
y = 5;
cout << x;

The output is 5 because y is a reference to x. Assigning 5 through y changes the same object x.

Q24. What happens when malloc, calloc, or new cannot allocate the requested memory?

On allocation failure, malloc() and calloc() return a null pointer. In standard C++, ordinary new throws std::bad_alloc; the nothrow form of new returns nullptr. These failure mechanisms must be handled appropriately.

Q25. Declare a float array of ten elements and initialize its first four elements to 1.0, 2.0, 3.0, and 4.0.

float values[10] = {1.0f, 2.0f, 3.0f, 4.0f};

The remaining six elements are initialized to zero.

Q26. What two arguments does the C calloc function take?

calloc(count, size) takes the number of elements (count) and the size of each element in bytes (size). It allocates count × size bytes and initializes the allocated bytes to zero.

Q27. What is the benefit of references and when should they be used?

References allow existing objects to be accessed under another name without making copies. Passing a large object as const T& can avoid copying while preventing modification; passing T& permits intentional changes to the caller’s object.

Q28. What is a memory leak?

A memory leak occurs when dynamically allocated storage remains allocated but the program no longer has a usable way to release it. For example, reassigning an owning raw pointer without deleting its previous allocation can leak memory. Prefer automatic storage or RAII types such as std::vector and std::unique_ptr.

Q29. Write the general syntax for dynamically allocating an array using the C++ new operator.

The syntax is Type* ptr = new Type[number_of_elements];. For example, int* values = new int[10]; allocates ten int elements and returns a pointer to the first. Release the allocation with delete[] values;.

Q30. Distinguish an ordinary variable from a pointer variable.

An ordinary variable such as int x = 5; stores a value of its declared type. A pointer variable such as int* p = &x; stores the memory address of an object; *p accesses that object. A pointer has a type that describes the object to which it points.

Q31. Declare a 3 × 3 integer matrix and initialize all nine elements to zero.

int matrix[3][3] = {};

The empty initializer value-initializes every element, so all nine integers are zero. The equivalent traditional form is int matrix[3][3] = {0};.

Q32. How can a C++ function accept an argument by reference without allowing it to modify the caller’s object?

Declare the parameter as a const reference, for example void display(const Student& s);. This avoids an unnecessary copy and prevents the function from modifying s through that reference. Other aliases to a non-const object may still modify it independently.

Q33. Explain the advantages and disadvantages of reading a text file character by character versus line by line.

Character-by-character input is useful when every character must be examined, but may require more function calls and manual handling of line boundaries. Line-by-line input using getline() conveniently preserves each line as a unit and is generally clearer for processing line-oriented records. The appropriate approach depends on the required parsing and buffering.

Q34. Give the general syntax of a C++ class template.

template <typename T>
class Box {
    T value;
public:
    explicit Box(T x) : value(x) {}
};

Q35. Show how to call a function template returning int when its argument x is double.

template <typename T, typename U>
T func(U value) { return static_cast<T>(value); }
double x = 3.5;
int result = func<int, double>(x);

Q36. What is returned when reciprocal(5) instantiates the following template with T = int?

template <typename T> T reciprocal(T x) { return 1 / x; }

The result is 0. Integer division truncates 1/5 to zero. With a floating-point argument, the result would be fractional.

Q37. What is printed when the following functions are each called three times?

void func1() { int x=0; ++x; cout << x << endl; }
void func2() { static int x=0; ++x; cout << x << endl; }

func1 prints 1, 1, 1 on separate lines because x is recreated each time. func2 prints 1, 2, 3 because static x persists between calls.

Q38. What is the main advantage of function overloading?

Function overloading makes programs more readable and expressive by allowing related operations on different parameter types to share a descriptive function name.

Q39. Why does a C++ compiler use name mangling?

Name mangling encodes a function’s name and relevant type/signature information in a unique symbol so overloaded functions and other external entities can be distinguished during linking.

Q40. What distinguishes two overloaded functions in C++?

Overload resolution distinguishes functions using their parameter types, parameter count, and (where applicable) their order. Changing only a function’s return type or parameter names does not constitute overloading.

Q41. Can two C++ functions be overloaded if they have the same parameter types but different parameter names? Justify your answer.

No. Parameter names are not part of a function signature for overload resolution. For example, f(int x, int y) and f(int a, int b) have identical parameter types and therefore cannot coexist as two distinct overloads.

Q42. How do we provide the default values of function parameters?

The default value of a parameter is given in the function prototype or in the function definition (but only in one of them), using = after the parameter, and only for the right-most parameters.

int add(int a, int b = 10);     // prototype with a default
// add(5)  -> 15,   add(5, 2) -> 7

Q43. Name the two kinds of templates provided by C++.

C++ provides function templates and class templates. A function template defines a family of functions parameterized by types (or values); a class template defines a family of class types.

Q44. Can the C++ functions func(int, int) and func(int&, int&) both be declared? What happens if they are called with two non-const int lvalues?

Both overloads can be declared because their parameter types are different. But a call such as func(a, b), where a and b are non-const int lvalues, is ambiguous: both signatures are viable and neither is preferred. The overload accepting values can be called with integer literals; the reference overload cannot bind non-const int& parameters to literals.

void func(int x, int y);
void func(int& x, int& y);

Q45. Write a C++ function template named Add() that adds two values, and call it using integer variables.

#include <iostream>
using namespace std;
template <typename T>
T Add(T a, T b) { return a + b; }
int main() {
    int x = 12, y = 25;
    cout << "Sum = " << Add(x, y) << "\n";
    return 0;
}

Q46. Write a C++ program that reads a radius and calculates the area of a circle.

#include <iostream>
using namespace std;
int main() {
    const double PI = 3.141592653589793;
    double radius;
    cin >> radius;
    double area = PI * radius * radius;
    cout << "Area = " << area << "\n";
    return 0;
}

Q47. Define a default function argument and give an example.

A default argument is a value given in the function declaration that is used when the caller leaves that argument out. Default arguments must be the right-most parameters.

int power(int base, int exponent = 2);   // prototype with a default

power(5);      // uses exponent = 2 -> 25
power(5, 3);   // uses exponent = 3 -> 125

Q48. Write a C++ function template Square that returns the square of its argument. Read a float in main and call Square; for 12.3 the expected output is approximately 151.29.

#include <iostream>
using namespace std;
template <typename T> T Square(T x) { return x * x; }
int main() {
    float x; cin >> x;
    cout << Square(x) << '\n';
}

Q49. Write a C++ program that reads an entire sentence using getline() and prints it.

#include <iostream>
#include <string>
using namespace std;
int main() {
    string sentence;
    getline(cin, sentence);
    cout << sentence << "\n";
    return 0;
}

Q50. Write C++ code to read characters into a string until a comma is encountered.

#include <iostream>
#include <string>
using namespace std;
int main() {
    string text;
    getline(cin, text, ',');
    cout << text << "\n";
    return 0;
}

Q51. Write C++ output statements that display the value 1.2345678 to three decimal places, using a field width of nine padded with leading zeros.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double value = 1.2345678;
    cout << setfill('0') << setw(9) << fixed << setprecision(3) << value;
    return 0;
}

Output: 00001.235

Q52. Demonstrate decimal, hexadecimal, and octal output using C++ manipulators.

#include <iostream>
using namespace std;
int main() {
    int value = 25;
    cout << dec << value << '\n';
    cout << hex << value << '\n';
    cout << oct << value << '\n';
}

Output: 25, 19, 31 (on separate lines).

Q53. If the user enters 123 in octal, what does the following program print in hexadecimal?

int input;
cin >> oct >> input;
cout << hex << input;

53. Octal 123 equals decimal 83, which is hexadecimal 53.

Q54. Explain the interpretation of cout << a << b << c; in C++.

Insertion operators associate left to right: ((cout << a) << b) << c. Each insertion returns a stream reference, allowing the next value to be inserted. The variables must be declared and supported by the output operator.

Q55. What are the usual source and destination of input read through cin in C++?

By default, cin reads characters from standard input (normally the keyboard) and extracts them into a destination variable, such as int age; cin >> age;. Input redirection can change the source without changing the program.

Q56. Write a C++ program demonstrating setprecision() with three double variables.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double a = 12.12345, b = 13.123456, c = 14.1234567;
    cout << fixed << setprecision(2) << a << "\n";
    cout << fixed << setprecision(3) << b << "\n";
    cout << fixed << setprecision(4) << c << "\n";
    return 0;
}

Q57. Write a C++ program that prints two integer variables in decimal, hexadecimal, and octal.

#include <iostream>
using namespace std;
int main() {
    int var1 = 25, var2 = 50;
    cout << dec << var1 << ' ' << var2 << '\n';
    cout << hex << var1 << ' ' << var2 << '\n';
    cout << oct << var1 << ' ' << var2 << '\n';
}

Q58. What happens to an automatic local variable during recursive function calls?

Each recursive call creates its own activation record and a separate instance of each ordinary local variable. Values for one invocation do not overwrite another invocation’s local variables. The instances cease to exist as their corresponding calls return.

Q59. Are friend functions members of a class? How do they access its data and functions?

A friend function is not a member of the class granting friendship. A declaration inside the class using the friend keyword grants the function access to that class’s private and protected members. It is ordinarily called as a regular non-member function, not through a class object.

Q60. Explain the difference between an ordinary function declaration and a member-function declaration inside a class.

Both declarations specify a name, return type, and parameters. A non-static member-function declaration also establishes a class member with an implicit this pointer when called on an object, and access to the class’s members. Member functions can use public, protected, or private access control.

Q61. Explain the expression if (&m != this) in a copy-assignment operator.

The address &m is compared with this, which points to the current object. If the addresses differ, the right-hand object is not the same as the destination object and it is safe to perform the normal copy-assignment steps. The check prevents self-assignment problems, especially when a class manages dynamic resources.

Q62. What are the two types of conversion involving user-defined class types in C++?

Conversions may go from a built-in type to a class type, commonly through a converting constructor, or from a class type to another type, commonly through a conversion function. Converting constructors can also enable class-to-class conversions. Explicit conversions may be provided to avoid unintended implicit conversions.

Q63. Distinguish between a default constructor and a parameterized constructor.

A default constructor can be called with no arguments and initializes a newly created object. A parameterized constructor accepts arguments to initialize an object with specified values. A class may overload constructors to provide both forms.

Q64. Why may class data members contain indeterminate values when an object is created, and how can this be prevented?

Built-in data members of a default-initialized object are not necessarily initialized automatically. Initialize each member using default member initializers or a constructor initializer list, for example: class A { int x = 0; };

Q65. How are static data members declared and initialized in a C++ class?

A static data member is shared by all objects of the class. It is declared inside the class with static and defined (initialised) once outside the class, at file scope:

class Counter {
public:
    static int count;      // declaration
};
int Counter::count = 0;    // definition and initialisation at file scope

In C++17 an inline static int count = 0; inside the class is also allowed. It can be accessed as Counter::count or through an object.

Q66. Define a class and an object, with an example.

A class defines a user-defined type containing data and member functions. An object is an instance of the class.

class Student { public: int id; };
Student s;   // s is an object

Q67. What is the this pointer in C++? Explain its uses.

Inside a non-static member function, this is a pointer to the object on which the function was called. It can disambiguate a member name (this->x = x), return the current object (*this), and pass the current object to another function.

Q68. State the limitations of C++ friendship between classes.

Friendship is granted explicitly by the class being accessed. It is neither reciprocal nor transitive nor inherited. A friend can access private and protected members but is not itself a member function solely because it is a friend.

Q69. Explain the principle of friendship in C++.

A class may grant a non-member function or another class access to its private and protected members by declaring it as friend. Other functions cannot declare themselves friends of a class.

Q70. How can a containing class initialize an object member when its constructor runs?

Use the member initializer list in the containing class constructor. Example: Widget(int x) : member(x) {}. Object members are initialized in declaration order.

Q71. If class B contains a member object of class A, which class constructs and destroys first?

The A member is constructed before the body of B’s constructor runs. B’s destructor body runs first, then the A member is destroyed. Destruction happens in reverse order of construction.

Q72. When is a class constructor called?

A constructor is called automatically when an object of the class is created (when the object is declared, when it comes into scope, or when it is created with new). For an array of objects it is called once for each element. It initialises the data members of the new object.

Q73. Is a C++ constructor a function, and can it be overloaded?

Yes. A constructor is a special member function: it has the same name as the class and no return type. It can be overloaded, i.e. a class may have several constructors with different parameter lists (default, parameterized, copy), and the compiler selects one from the arguments used when the object is created.

Q74. What is a class in C++?

A class is a user-defined data type (a blueprint) that groups data members and the member functions that operate on them under one name, with access control (public, private, protected) to hide the implementation. Objects are created from it. Example: class Student { int id; public: void setId(int i) { id = i; } };

Q75. How does the linker produce an executable from the compiled object code?

The linker combines the compiled program’s object code with the required object code of library functions, resolves external references, and produces an executable.

Q76. Differentiate between a struct and a class in C++.

Both can contain data, constructors, and member functions. The primary language differences are their default member access and default inheritance access: struct defaults to public; class defaults to private.

Q77. What happens when new and delete are used to create and destroy a class object?

An ordinary new expression obtains storage, initializes the object by calling its constructor, and returns a pointer. A matching delete expression destroys the object by calling its destructor and then releases the storage. Arrays allocated by new[] must be released by delete[] so all their elements are destroyed.

Q78. Give the syntax of a class declaring a non-member function as its friend, including the function definition.

class Box {
    int value = 5;
    friend int readValue(const Box& b);
};
int readValue(const Box& b) { return b.value; }

Q79. What is a nested (inner) class? Give an example.

A nested class is declared within another class and has its own members and access control.

class Outer {
public:
    class Inner { public: int value = 0; };
};
Outer::Inner item;

Q80. Is it possible to change the meaning of subtraction between two built-in integers by operator overloading?

No. An overloaded operator must involve at least one operand of a class or enumeration type. C++ does not allow the built-in subtraction operation between two int values to be redefined.

Q81. Can the new and delete operators be overloaded?

Yes. A class may provide class-specific operator new and operator delete functions, and global forms can also be defined. These functions customize allocation and deallocation; construction and destruction remain separate stages.

Q82. Identify the copy-construction and assignment operations in these statements:
(a) Matrix m1(m2);
(b) Matrix m1, m2; m1 = m2;
(c) Matrix m1 = m2;

(a) Copy constructor; (b) copy assignment operator; (c) copy constructor (copy-initialization of a new object).

Q83. Give the declarations of pre-increment and post-increment member operator functions.

Member operator functions of a class ClassName:

ClassName& operator++();      // prefix  (++obj)
ClassName  operator++(int);   // postfix (obj++): the unused int parameter only distinguishes it

Q84. Write the declarations of overloaded input and output stream operators for a Date class.

class Date {
    friend std::ostream& operator<<(std::ostream&, const Date&);
    friend std::istream& operator>>(std::istream&, Date&);
};

Q85. Explain the difference between unary and binary operators and show how they are overloaded in C++.

A unary operator acts on one operand; a binary operator on two. In a member overloaded unary operator there are no explicit operands; in a member binary operator there is one explicit operand. A non-member unary operator has one argument; a non-member binary operator has two. C++ does not permit changing an operator’s arity or precedence.

class Number {
    int value;
public:
    explicit Number(int n=0):value(n) {}
    Number operator-() const { return Number(-value); }
    Number operator+(const Number& b) const { return Number(value+b.value); }
};

Q86. Differentiate function overloading from operator overloading.

Function overloading defines multiple functions with the same name but different parameter lists. The compiler selects an appropriate overload based on arguments. Operator overloading defines behavior for existing C++ operators when applied to user-defined types. Operator overloads are special functions named operator+, operator==, etc.; they cannot create new operators or change precedence/arity.

Q87. Why must the first parameter of a non-member output stream insertion operator be passed by reference?

The first parameter is the output stream. Streams cannot be copied, and the operator has to write into the existing stream (for example cout), not into a copy of it. So the parameter is std::ostream&, and the function returns the same reference so that calls can be chained (cout << a << b).

std::ostream& operator<<(std::ostream& os, const Date& d);

Long Questions

Longer questions that need complete code or a full explanation.

Q88. Explain why binary search is more efficient than linear search for a sorted array.

Linear search checks elements one by one until the target is found or the array ends. In the worst case, it requires n comparisons, so its time complexity is O(n). Binary search requires a sorted array; it compares the target with the middle element and discards approximately half of the remaining search interval after each comparison. Therefore its worst-case time complexity is O(log n). For an array of 1,000 sorted elements, linear search may examine all 1,000 elements. Binary search requires at most about 10 comparisons to locate a matching element or determine that it is absent. Binary search is not directly suitable for unsorted data unless the data is sorted or organized appropriately first.

Q89. Write a C++ program that calculates an employee’s salary from days worked and daily pay rate.

#include <iostream>
using namespace std;
int main() {
    int days;
    double rate;
    cout << "Days worked: "; cin >> days;
    cout << "Daily rate: "; cin >> rate;
    double salary = days * rate;
    cout << "Salary: " << salary << '\n';
}

Q90. Discuss the advantages and disadvantages of C++ templates.

Advantages: A single generic function or class can work with multiple data types, reducing duplicated code. The compiler checks instantiated code against concrete types and can optimize it. Disadvantages: Template diagnostics may be complex; heavy instantiation can increase compilation time and executable size; complex template metaprogramming can reduce readability. Templates generally require definitions visible where they are instantiated.

Q91. Write a C++ program that defines a user-defined stream manipulator named Octal and uses it to display a number in octal form.

#include <iostream>
using namespace std;
ostream& Octal(ostream& out) {
    out.setf(ios::oct, ios::basefield);
    return out;
}
int main() {
    int number;
    cin >> number;
    cout << Octal << number << "\n";
    return 0;
}

Q92. Write a C++ program that reads eight integers into a 2 × 4 matrix and displays the matrix.

#include <iostream>
using namespace std;
int main() {
    int a[2][4];
    for (int i=0; i<2; ++i)
    for (int j=0; j<4; ++j) cin >> a[i][j];
    for (int i=0; i<2; ++i) {
        for (int j=0; j<4; ++j) cout << a[i][j] << ' ';
        cout << '\n';
    }
}

Q93. Write a C++ program that displays the salaries 1000, 1500, 20000, 30000, and 60000 in a width of ten, padding with x characters.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    int salary[] = {1000, 1500, 20000, 30000, 60000};
    for (int s : salary)
    cout << setfill('x') << setw(10) << s << '\n';
}

Q94. Write a C++ program that reads a number in octal and displays it in hexadecimal.

#include <iostream>
using namespace std;
int main() {
    int number;
    cin >> oct >> number;
    cout << hex << number << '\n';
}

Q95. Write a C++ program that reads a string of at most ten characters. If it equals "Virtual", print "Correct Password"; otherwise print its first four characters and its length.

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s;
    getline(cin, s);
    if (s.size() > 10) return 1;
    if (s == "Virtual") cout << "Correct Password\n";
    else cout << s.substr(0,4) << " " << s.length() << '\n';
}

Q96. Define a buffer and explain why buffering improves file input/output performance.

A buffer is a temporary region of memory used to hold data while it is transferred between a program and an input/output device. For output, the program accumulates multiple values in memory and writes them to a file as a larger block, reducing the number of relatively expensive device operations. Input buffering similarly reads a block of data and supplies it to the program as needed. A buffer is flushed when its contents are written to the destination; buffered output should be flushed or the stream closed when the data must be safely delivered. Example: Instead of issuing a separate file write for each of 1,000 numbers, a program can accumulate a batch of numbers in a buffer and write the batch. Buffering reduces I/O overhead, although it does not eliminate the need to handle failed writes or ensure that the final partial buffer is flushed.

Q97. List the important steps when implementing an assignment operator for a class that manages dynamic memory.

Check whether the source and destination are the same object; allocate replacement storage if necessary; copy the source data; release or safely replace the old resource; maintain class invariants; and return *this by reference to support chained assignment. A copy-and-swap implementation is another safe approach.

Q98. Write a safe copy-assignment operator for a String class that manages a dynamically allocated character buffer and handles self-assignment.

#include <cstring>
#include <utility>
class String {
    char* buf;
public:
    String() : buf(new char[1]{'\0'}) {}
    explicit String(const char* text) {
        buf = new char[std::strlen(text) + 1];
        std::strcpy(buf, text);
    }
    String(const String& other) : String(other.buf) {}
    ~String() { delete[] buf; }
    String& operator=(const String& other) {
        if (this == &other) return *this;
        char* next = new char[std::strlen(other.buf) + 1];
        std::strcpy(next, other.buf);
        delete[] buf;
        buf = next;
        return *this;
    }
};

Q99. Explain what a Matrix constructor does when it stores row and column counts, allocates a two-dimensional array dynamically, and fills every element with zero.

It stores the requested row and column sizes, allocates an array of row pointers, allocates a column array for each row, and initializes each matrix entry to 0.0. The class must deallocate each row followed by the pointer array in its destructor. In modern C++, std::vector is generally safer than manual ownership of a double**. Correctly written form of the constructor described in the question:

Matrix::Matrix(int row, int col) {
    numRows = row;
    numCols = col;
    elements = new double*[numRows];
    for (int i = 0; i < numRows; ++i) {
        elements[i] = new double[numCols];
        for (int j = 0; j < numCols; ++j)
            elements[i][j] = 0.0;
    }
}

Q100. Do friend functions violate encapsulation? Justify your answer.

Friendship is an explicit, controlled exception to normal access rules. It exposes private members only to the named function or class, not to all callers. Overusing friendship may increase coupling and weaken information hiding, so it should be granted only where necessary.

Q101. What problems can arise when a class with an owning pointer data member is assigned to another object, and one of the objects is deleted?

The compiler-generated copy assignment usually copies the pointer value (shallow copy). Both objects then point to the same allocation. If one deletes that allocation, the other pointer dangles; subsequent dereference or deletion is undefined behavior. Implement appropriate copy/move operations (Rule of Three/Five) or use RAII types such as std::vector or std::unique_ptr.

Q102. Write a C++ program with a class myClass containing two integer data members, a default constructor, a destructor, setters and getters. Ask the user for the number of objects, allocate an array dynamically, and then free it.

#include <iostream>
using namespace std;
class myClass {
    int a, b;
public:
    myClass() : a(0), b(0) {}
    ~myClass() {}
    void setA(int x) { a = x; }
    void setB(int x) { b = x; }
    int getA() const { return a; }
    int getB() const { return b; }
};
int main() {
    int count;
    cin >> count;
    if (count < 0) return 1;
    myClass* items = new myClass[count];
    delete[] items;
}

Q103. Write a C++ program using a Date class and a Person class. Date must contain day, month and year, setters and getters, and a showdate() function. Person must contain Name and Address as char pointers and a Bday member of type Date, with Display() and setdate() functions. In main(), create a Person object and call its member functions.

One complete implementation is shown below. The Person class allocates and frees the character buffers that it owns; its copy operations are disabled to prevent accidental shallow copies.

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class Date {
    int day, month, year;
public:
    Date() : day(1), month(1), year(2000) {}
    void setDay(int d) { day = d; }
    void setMonth(int m) { month = m; }
    void setYear(int y) { year = y; }
    int getDay() const { return day; }
    int getMonth() const { return month; }
    int getYear() const { return year; }
    void showdate() const {
        cout << getDay() << '-' << getMonth() << '-' << getYear();
    }
};
class Person {
    char* Name;
    char* Address;
    Date Bday;
    static char* copyText(const char* text) {
        char* p = new char[strlen(text) + 1];
        strcpy(p, text);
        return p;
    }
public:
    Person(const char* name, const char* address)
        : Name(copyText(name)), Address(copyText(address)) {}
    Person(const Person&) = delete;
    Person& operator=(const Person&) = delete;
    ~Person() { delete[] Name; delete[] Address; }
    void setdate(int d, int m, int y) {
        Bday.setDay(d);
        Bday.setMonth(m);
        Bday.setYear(y);
    }
    void Display() const {
        cout << "Name: " << Name << '\n';
        cout << "Address: " << Address << '\n';
        cout << "Date of Birth: ";
        Bday.showdate();
        cout << '\n';
    }
};
int main() {
    string name, address;
    int day, month, year;
    cout << "Name: "; getline(cin, name);
    cout << "Address: "; getline(cin, address);
    cout << "Birth date (day month year): ";
    if (!(cin >> day >> month >> year)) return 1;
    Person person(name.c_str(), address.c_str());
    person.setdate(day, month, year);
    person.Display();
}

Q104. Write an overloaded stream-insertion operator (<<) for a class Matrix that stores a 3 × 3 integer array Elements.

#include <iostream>
using namespace std;
class Matrix {
    int Elements[3][3]{};
public:
    friend ostream& operator<<(ostream& os, const Matrix& m) {
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j)
            os << m.Elements[i][j] << (j == 2 ? "" : " ");
            os << "\n";
        }
        return os;
    }
};

Q105. Write a non-member stream extraction operator for the following class to input its 3 × 3 integer matrix: class Matrix { int Elements[3][3]; };

#include <iostream>
class Matrix {
    int Elements[3][3];
public:
    friend std::istream& operator>>(std::istream& in, Matrix& m) {
        for (int i=0; i<3; ++i)
        for (int j=0; j<3; ++j) in >> m.Elements[i][j];
        return in;
    }
};

Q106. A math class contains one data member number. Overload + to support the expression obj2 = 10 + obj1.

class Math {
    int number;
public:
    explicit Math(int n=0) : number(n) {}
    friend Math operator+(int n, const Math& m) {
        return Math(n + m.number);
    }
};
Math obj1(5);
Math obj2 = 10 + obj1;

Part 3: Short Notes & Key Terms

Class
A user-defined type that groups data members and member functions under one name, with access control.
Object
An instance of a class; it occupies memory and has its own copy of the non-static data members.
Encapsulation
Binding data and the functions that operate on it into one unit and hiding the details with private/protected access.
Constructor
Special member function with the class name and no return type, called automatically when an object is created; it can be overloaded.
Destructor
Member function named ~ClassName with no parameters and no return type, called automatically when an object is destroyed; used to release resources.
Copy constructor
Constructor of the form ClassName(const ClassName&) that creates a new object as a copy of an existing one; the default one makes a shallow copy.
Shallow vs deep copy
Shallow copy copies pointer values (both objects share the same memory); deep copy allocates new memory and copies the contents.
this pointer
Implicit pointer available inside non-static member functions that points to the current object.
static data member
One copy shared by all objects of the class; defined once outside the class and accessible as Class::name.
static member function
Belongs to the class, not to an object; has no this pointer and can access only static members.
const member function
Declared with const after the parameter list; it promises not to modify the object's data members.
Friend function
A non-member function given access to the private and protected members of a class by a friend declaration inside the class.
Operator overloading
Giving operators such as + , ++ , << a user-defined meaning for class types; precedence and number of operands cannot change.
Prefix vs postfix ++
operator++() is prefix; operator++(int) is postfix, where the unused int only distinguishes it.
Inheritance
A new (derived) class acquires the members of an existing (base) class, enabling code reuse.
Access in inheritance
Private members of the base are never directly accessible in the derived class; protected members are accessible there but not outside.
Polymorphism
One interface, many forms: compile-time (overloading) and run-time (virtual functions).
Virtual function
A base class member function declared virtual so that the call is resolved at run time according to the actual object type.
Pure virtual function / abstract class
A function declared = 0; a class with at least one pure virtual function is abstract and cannot be instantiated.
Function overloading
Several functions with the same name but different parameter lists; the return type alone is not enough.
Default arguments
Values given in the prototype that are used when the caller omits trailing arguments.
Pass by value vs reference
By value copies the argument; by reference (or pointer) lets the function change the caller's variable.
Pointer
A variable that stores a memory address; dereferenced with *, address taken with &.
new and delete
new allocates memory on the heap and calls the constructor; delete frees it and calls the destructor; use delete[] for arrays.
Array
A fixed-size collection of elements of the same type in contiguous memory; indexes start at 0 and C-strings need one extra byte for the null character.
Stream operators
<< (insertion) writes to an output stream and >> (extraction) reads from an input stream; overloaded versions take the stream by reference and return it for chaining.
Compiler vs interpreter
A compiler translates the whole program into an executable at once; an interpreter translates and runs one statement at a time.
Truth table
A table of all combinations of true/false inputs and the result of a logical expression.
switch statement
Multi-way selection on an integer or character expression; break prevents fall-through and default handles the unmatched case.
Reference
An alias for an existing variable; it must be initialised when declared and cannot be re-seated.

No comments:

Post a Comment

Please give us your feedback & help us to improve this site.