Cs304 Mid Term Preparation by MR - ART
Cs304 Mid Term Preparation by MR - ART
CS304
MIDTERM PREPARATION
PART 1
YouTube video link
Lecture 1:
1
Contact For Services: 03251076071
o B) Compilation
o C) Interpretation
o D) Scripting
Answer: A) Inheritance
4. Encapsulation in OOP refers to:
o A) Combining data and functions that operate on the data
o B) Hiding data implementation
o C) Inheriting properties from one class to another
o D) None of the above
Answer: A) Combining data and functions that operate on the data
5. What is the main advantage of polymorphism in OOP?
o A) It increases code execution speed.
o B) It allows methods to do different things based on the object it is acting upon.
o C) It provides more memory space.
o D) It improves the readability of code.
Answer: B) It allows methods to do different things based on the object it is
acting upon.
6. Which OOP concept focuses on exposing only the necessary parts of an object?
o A) Abstraction
o B) Encapsulation
o C) Polymorphism
o D) Inheritance
Answer: A) Abstraction
7. Which of the following best describes a class in OOP?
o A) A template for creating objects
o B) An instance of an object
o C) A function that returns a value
o D) A variable that holds data
Answer: A) A template for creating objects
8. In OOP, what does inheritance allow?
o A) Creating new classes from existing ones
o B) Hiding class methods from other classes
o C) Running multiple threads simultaneously
2
Contact For Services: 03251076071
o D) Directly accessing memory addresses
Answer: A) Creating new classes from existing ones
9. Which principle of OOP is demonstrated by method overloading?
o A) Encapsulation
o B) Abstraction
o C) Inheritance
o D) Polymorphism
Answer: D) Polymorphism
10. What does the term 'object' refer to in OOP?
o A) A pre-defined function
o B) A collection of data and methods
o C) A memory location
o D) A class attribute
Answer: B) A collection of data and methods
Lecture 2:
3
Contact For Services: 03251076071
o D) A module
Answer: C) An object
4. Data abstraction is achieved through:
o A) Using functions to perform operations
o B) Hiding the implementation details from the user
o C) Using a different programming language
o D) Implementing multiple classes
Answer: B) Hiding the implementation details from the user
5. Which keyword is used to create an object in C++?
o A) create
o B) new
o C) instantiate
o D) construct
Answer: B) new
6. What is a method in the context of OOP?
o A) A function associated with a class
o B) A standalone function
o C) A variable within a class
o D) A pre-processor directive
Answer: A) A function associated with a class
7. How do objects communicate in OOP?
o A) Through data encapsulation
o B) Using messages or method calls
o C) By inheriting each other's properties
o D) By sharing global variables
Answer: B) Using messages or method calls
8. What is the term for combining data and methods in a single unit in OOP?
o A) Inheritance
o B) Polymorphism
o C) Abstraction
o D) Encapsulation
Answer: D) Encapsulation
9. In C++, how is a public data member of a class accessed?
4
Contact For Services: 03251076071
o A) Using dot operator with an object
o B) Directly, without any operator
o C) Using the address-of operator
o D) With the help of pointers only
Answer: A) Using dot operator with an object
Lecture 3:
5
Contact For Services: 03251076071
5. Which of the following is a default constructor?
o A) MyClass() {}
o B) MyClass(int a) {}
o C) ~MyClass() {}
o D) MyClass(MyClass &obj) {}
Answer: A) MyClass() {}
6. How many times is a destructor called for an object during its lifecycle?
o A) Once
o B) Twice
o C) Multiple times
o D) Depends on the object size
Answer: A) Once
7. What is the syntax for defining a destructor in C++?
o A) ~ClassName() {}
o B) #ClassName() {}
o C) @ClassName() {}
o D) *ClassName() {}
Answer: A) ~ClassName() {}
8. In C++, if no constructor is defined, which type of constructor is provided by
default?
o A) Copy constructor
o B) Parameterized constructor
o C) Default constructor
o D) Private constructor
Answer: C) Default constructor
9. Which function allows an object to initialize itself?
o A) Destructor
o B) Constructor
o C) Copy function
o D) Static function
Answer: B) Constructor
10. What is a copy constructor in C++?
o A) A constructor that copies data from another object
6
Contact For Services: 03251076071
o B) A constructor that initializes data with zero
o C) A constructor that is called explicitly
o D) A constructor that deletes an object
Answer: A) A constructor that copies data from another object
Lecture 4:
7
Contact For Services: 03251076071
o B) Single Inheritance
o C) Multiple Inheritance
o D) Hierarchical Inheritance
Answer: C) Multiple Inheritance
6. Generalization and specialization in OOP refer to:
o A) Hiding class members
o B) Extending and restricting class functionalities
o C) Overloading operators
o D) Using friend functions
Answer: B) Extending and restricting class functionalities
7. In C++, the syntax to declare inheritance is:
o A) class Derived : base
o B) class Derived -> Base
o C) class Derived : public Base
o D) class Derived inherits Base
Answer: C) class Derived : public Base
8. Which of the following statements is true about inheritance?
o A) A derived class cannot have its own members.
o B) A derived class can override base class methods.
o C) A base class must be abstract.
o D) Inheritance increases data redundancy.
Answer: B) A derived class can override base class methods.
9. What is the purpose of the protected keyword in inheritance?
o A) To make data inaccessible
o B) To allow access within the class and derived classes
o C) To define a private data member
o D) To declare a virtual function
Answer: B) To allow access within the class and derived classes
10. Which of the following is an example of multilevel inheritance?
o A) Class A inherits Class B, Class B inherits Class C
o B) Class A inherits Class B and Class C
o C) Class A inherits Class B
8
Contact For Services: 03251076071
o D) Class A and Class B inherit Class C
Answer: A) Class A inherits Class B, Class B inherits Class C
Lecture 5:
9
Contact For Services: 03251076071
o D) friend
Answer: A) inline
6. What is the main benefit of using inline functions?
o A) To reduce function call overhead
o B) To increase the execution time
o C) To create more flexible programs
o D) To improve memory management
Answer: A) To reduce function call overhead
7. In operator overloading, what is required?
o A) At least one operand must be a user-defined type
o B) All operands must be built-in types
o C) Only arithmetic operators can be overloaded
o D) Only unary operators can be overloaded
Answer: A) At least one operand must be a user-defined type
8. Which of the following function prototypes demonstrates function overloading?
o A) void display(int a);
o B) int display(float a);
o C) void display(int a, int b);
o D) All of the above
Answer: D) All of the above
9. What is the key difference between function overloading and function overriding?
o A) Overloading occurs within the same class; overriding occurs in inherited
classes
o B) Overloading requires inheritance; overriding does not
o C) Overloading changes return types; overriding changes parameter types
o D) There is no difference; they are synonyms
Answer: A) Overloading occurs within the same class; overriding occurs in
inherited classes
10. How can an overloaded operator be declared as a member function in C++?
o A) returnType operator symbol(parameters) {}
o B) symbol operator(parameters) {}
o C) returnType operator() symbol(parameters) {}
10
Contact For Services: 03251076071
o D) returnType symbol operator(parameters) {}
Answer: A) returnType operator symbol(parameters) {
11
Contact For Services: 03251076071
o A) ~
o B) !
o C) #
o D) %
Answer: A) ~
5. When is a destructor called in C++?
o A) When an object is created
o B) When an object is deleted or goes out of scope
o C) When a constructor is called
o D) When a program starts
Answer: B) When an object is deleted or goes out of scope
6. Which of the following is a parameterized constructor?
o A) MyClass() {}
o B) MyClass(int a, int b) {}
o C) ~MyClass() {}
o D) MyClass(MyClass &obj) {}
Answer: B) MyClass(int a, int b) {}
7. How many destructors can a class have in C++?
o A) One
o B) Two
o C) Unlimited
o D) None
Answer: A) One
8. What is a copy constructor used for?
o A) To copy an object's data members from another object
o B) To delete an object
o C) To overload an operator
o D) To initialize static members
Answer: A) To copy an object's data members from another object
9. Which statement correctly defines a default constructor in C++?
o A) MyClass() {}
o B) MyClass(int a) {}
12
Contact For Services: 03251076071
o C) MyClass(MyClass &obj) {}
o D) ~MyClass() {}
Answer: A) MyClass() {}
10. What happens if a destructor is declared virtual in a base class?
o A) It prevents memory leaks in derived classes
o B) It stops derived class destructors from being called
o C) It forces the use of the default destructor
o D) It has no effect
Answer: A) It prevents memory leaks in derived classes
Lecture 7:
13
Contact For Services: 03251076071
o C) Both A and B
o D) Only member functions
Answer: C) Both A and B
5. How can a static member function be called in C++?
o A) Using the class name
o B) Using an object of the class
o C) Using a pointer to the function
o D) Both A and B
Answer: D) Both A and B
6. What is a characteristic of static member functions?
o A) They can access only static data members
o B) They can access both static and non-static data members
o C) They cannot access any data members
o D) They are always private
Answer: A) They can access only static data members
7. Why are friend functions used in C++?
o A) To improve encapsulation
o B) To access private data of a class from non-member functions
o C) To make functions virtual
o D) To create more flexible polymorphism
Answer: B) To access private data of a class from non-member functions
8. Which of the following statements about friend functions is true?
o A) They violate encapsulation
o B) They cannot access private members
o C) They must be a part of the class
o D) They cannot be virtual
Answer: A) They violate encapsulation
9. What is the correct syntax to declare a friend function?
o A) friend returnType functionName(parameters);
o B) returnType friend functionName(parameters);
o C) friend functionName(parameters) returnType;
o D) functionName friend(parameters) returnType;
Answer: A) friend returnType functionName(parameters);
14
Contact For Services: 03251076071
10. What is a key limitation of static member functions?
o A) They cannot be called without an object
o B) They cannot be overloaded
o C) They cannot access non-static data members
o D) They can only be called once
Answer: C) They cannot access non-static data members
Lecture 8:
15
Contact For Services: 03251076071
5. Which keyword is used to define a friend function for operator overloading?
o A) friend
o B) static
o C) virtual
o D) extern
Answer: A) friend
6. When overloading the ++ operator as a member function, what should its signature
be for the prefix form?
o A) ClassName& operator++()
o B) ClassName operator++(int)
o C) ClassName& operator++(int)
o D) ClassName operator++()
Answer: A) ClassName& operator++()
7. Which operator should return a reference when overloaded?
o A) []
o B) ++
o C) +=
o D) *
Answer: C) +=
8. What is required for binary operator overloading?
o A) Both operands must be of the same type
o B) At least one operand must be a user-defined type
o C) Both operands must be user-defined types
o D) Only one operand is required
Answer: B) At least one operand must be a user-defined type
9. What is a limitation of operator overloading?
o A) Not all operators can be overloaded
o B) Operators can only be overloaded globally
o C) Operators cannot be overloaded as member functions
o D) Only arithmetic operators can be overloaded
Answer: A) Not all operators can be overloaded
10. Which of the following operators can be overloaded for user-defined types?
o A) +
16
Contact For Services: 03251076071
o B) -
o C) *
o D) All of the above
Answer: D) All of the above
Lecture 9:
17
Contact For Services: 03251076071
5. What happens if two base classes have a method with the same signature in multiple
inheritance?
o A) The derived class must override the method
o B) The derived class cannot inherit the method
o C) The derived class uses virtual inheritance
o D) The derived class will have an ambiguity error
Answer: D) The derived class will have an ambiguity error
6. Which of the following is true about virtual inheritance?
o A) It allows sharing of data members between base classes
o B) It prevents multiple instances of a base class
o C) It requires all base classes to be abstract
o D) It restricts the use of polymorphism
Answer: B) It prevents multiple instances of a base class
7. What is the term for a derived class inheriting from another derived class?
o A) Single Inheritance
o B) Multilevel Inheritance
o C) Multiple Inheritance
o D) Hierarchical Inheritance
Answer: B) Multilevel Inheritance
8. In C++, which access specifier allows members to be inherited but not accessed by
derived class objects?
o A) Public
o B) Protected
o C) Private
o D) Static
Answer: B) Protected
9. What is the role of the virtual keyword in inheritance?
o A) To allow function overloading
o B) To enable runtime polymorphism
o C) To make a class abstract
o D) To hide data members
Answer: B) To enable runtime polymorphism
10. Which of the following statements about inheritance is false?
18
Contact For Services: 03251076071
o A) Inheritance increases encapsulation
o B) Inheritance reduces code redundancy
o C) Inheritance supports polymorphism
o D) Inheritance allows code reuse
Answer: A) Inheritance increases encapsulation
19
Contact For Services: 03251076071
o A) To prevent memory leaks when deleting derived class objects
o B) To initialize base class data members
o C) To override a constructor
o D) To create an abstract class
Answer: A) To prevent memory leaks when deleting derived class objects
6. Which of the following allows for dynamic binding of functions at runtime?
o A) Virtual functions
o B) Static functions
o C) Inline functions
o D) Friend functions
Answer: A) Virtual functions
7. What is function overriding?
o A) Redefining a base class function in a derived class
o B) Using the same function name with different parameters
o C) Changing the return type of a function
o D) Creating a friend function for a class
Answer: A) Redefining a base class function in a derived class
8. Which of the following statements is true about abstract classes?
o A) They cannot have data members
o B) They cannot be used to create objects directly
o C) They must have all virtual functions
o D) They cannot be inherited
Answer: B) They cannot be used to create objects directly
9. What is the purpose of a pure virtual function?
o A) To create an interface for derived classes
o B) To overload an operator
o C) To initialize data members
o D) To define a default implementation
Answer: A) To create an interface for derived classes
10. Which of the following is a characteristic of runtime polymorphism?
o A) It occurs during compile-time
o B) It involves virtual functions and pointers
20
Contact For Services: 03251076071
o C) It requires function overloading
o D) It is achieved through friend functions
Answer: B) It involves virtual functions and pointers
21
Contact For Services: 03251076071
o D) All of the above
Answer: D) All of the above
6. What is required when a derived class inherits from an abstract class?
o A) It must implement all pure virtual functions
o B) It must override all base class functions
o C) It cannot have its own members
o D) It must be declared as abstract
Answer: A) It must implement all pure virtual functions
7. Which of the following statements is true about pure virtual functions?
o A) They must have a body
o B) They can have default arguments
o C) They are not allowed in derived classes
o D) They define an interface for derived classes
Answer: D) They define an interface for derived classes
8. What happens if a derived class does not implement all pure virtual functions?
o A) The derived class becomes abstract
o B) The program will not compile
o C) The base class must provide default implementations
o D) The derived class can still be instantiated
Answer: A) The derived class becomes abstract
9. Which of the following can be declared as abstract?
o A) Class
o B) Structure
o C) Union
o D) All of the above
Answer: A) Class
10. Which keyword is used to define a pure virtual function in C++?
o A) virtual
o B) pure
o C) abstract
o D) friend
Answer: A) virtual
22
Contact For Services: 03251076071
Lecture 12:
23
Contact For Services: 03251076071
o A) By specifying the type in angle brackets, e.g., ClassName<int>
o B) By using the template keyword
o C) By calling the constructor with type arguments
o D) By using the new keyword
Answer: A) By specifying the type in angle brackets, e.g., ClassName<int>
7. Which of the following statements is true about template specialization?
o A) It allows customizing behavior for specific data types
o B) It prevents the use of generic types
o C) It is only applicable to functions, not classes
o D) It requires using the abstract keyword
Answer: A) It allows customizing behavior for specific data types
8. What is the purpose of a non-type template parameter?
o A) To restrict template arguments
o B) To define constants within a template
o C) To allow templates to accept different data types
o D) To implement function overloading
Answer: B) To define constants within a template
9. Which of the following is an example of a non-type template parameter?
o A) template <int N>
o B) template <typename T>
o C) template <class T>
o D) template <float F>
Answer: A) template <int N>
10. What is a common use case for template metaprogramming?
o A) Compile-time computation and optimization
o B) Runtime polymorphism
o C) Function overloading
o D) Operator overloading
Answer: A) Compile-time computation and optimization
Lecture 13:
24
Contact For Services: 03251076071
o B) A set of macros for code generation
o C) A set of predefined constants
o D) A debugging tool
Answer: A) A library of template classes and functions
2. Which of the following is a component of the STL?
o A) Containers
o B) Algorithms
o C) Iterators
o D) All of the above
Answer: D) All of the above
3. What is a container in the context of STL?
o A) A class that stores a collection of objects
o B) A function that operates on arrays
o C) A keyword for defining data types
o D) A built-in data type
Answer: A) A class that stores a collection of objects
4. Which STL component provides a way to access elements in a container?
o A) Algorithms
o B) Iterators
o C) Functions
o D) Classes
Answer: B) Iterators
5. What is the role of algorithms in the STL?
o A) To provide debugging information
o B) To define new data types
o C) To perform operations on containers
o D) To implement polymorphism
Answer: C) To perform operations on containers
6. Which of the following is an example of a sequence container in STL?
o A) vector
o B) map
o C) set
25
Contact For Services: 03251076071
o D) queue
Answer: A) vector
7. What is the primary advantage of using STL?
o A) Improved operator overloading
o B) Reduced encapsulation
o C) Increased code reusability and efficiency
o D) Faster compilation times
Answer: C) Increased code reusability and efficiency
8. Which of the following statements is true about associative containers?
o A) They store elements in key-value pairs
o B) They maintain elements in a sequential order
o C) They do not support iterators
o D) They require all elements to be unique
Answer: A) They store elements in key-value pairs
9. What is the purpose of a vector in STL?
o A) To manage memory allocation
o B) To implement binary trees
o C) To perform arithmetic operations
o D) To provide dynamic array functionality
Answer: D) To provide dynamic array functionality
10. Which STL component is used to iterate over a range of elements in a container?
o A) Iterators
o B) Pointers
o C) Functions
o D) Variables
Answer: A) Iterators
26
Contact For Services: 03251076071
27
Contact For Services: 03251076071
o B) To catch exceptions
o C) To perform memory allocation
o D) To contain code that might throw an exception
Answer: D) To contain code that might throw an exception
5. Which of the following statements is true about exception handling?
o A) It always improves program performance
o B) It requires all exceptions to be caught
o C) It separates error-handling code from regular code
o D) It is only used for syntax errors
Answer: C) It separates error-handling code from regular code
6. What is the purpose of a try-catch block?
o A) To perform arithmetic operations
o B) To handle exceptions and prevent program crashes
o C) To implement polymorphism
o D) To create template classes
Answer: B) To handle exceptions and prevent program crashes
Lecture 15:
28
Contact For Services: 03251076071
o C) To perform arithmetic operations
o D) To handle exceptions
Answer: A) To read from files
4. Which of the following classes is used to write to files in C++?
o A) file
o B) istream
o C) fstream
o D) ofstream
Answer: D) ofstream
5. What is the purpose of the fstream class in C++?
o A) To perform both input and output operations on files
o B) To handle runtime errors
o C) To perform arithmetic operations
o D) To define template classes
Answer: A) To perform both input and output operations on files
6. Which of the following is a valid mode for opening a file in C++?
o A) ios::in
o B) ios::out
o C) ios::app
o D) All of the above
Answer: D) All of the above
29
Contact For Services: 03251076071
o C) namespace::name { /* code */ }
o D) name::space { /* code */ }
4. How can you use all members of a namespace without qualifying their names?
o A) Using using namespace
o B) Using import namespace
o C) Using include namespace
o D) Using namespace use
5. Which of the following is a correct way to use a specific function from a namespace?
o A) namespace::functionName();
o B) namespace::functionName();
o C) using namespace::functionName();
o D) using functionName::namespace();
Answer: B) namespace::functionName();
30
Contact For Services: 03251076071
o A) std
o B) default
o C) global
o D) main
Answer: A) std
Lecture 17:
31
Contact For Services: 03251076071
o D) To allocate memory
6. Which directive is used to prevent multiple inclusions of the same header file?
o A) #ifndef
o B) #pragma once
o C) #endif
o D) Both A and B
32
Contact For Services: 03251076071
o B) To display an error message during preprocessing
o C) To stop program execution
o D) To delete a macro definition
Lecture 18:
33
Contact For Services: 03251076071
Answer: C) new
34
Contact For Services: 03251076071
o B) The memory is deleted safely
o C) The program ignores the second delete
o D) It results in undefined behavior
10. Which of the following statements is true about dynamic memory allocation?
o A) Memory is automatically deallocated when a program ends
o B) Memory must be manually deallocated using delete or delete[]
o C) Memory is allocated at compile-time
o D) Memory allocation always succeeds
35