0% found this document useful (0 votes)
36 views35 pages

Cs304 Mid Term Preparation by MR - ART

In this Document I have covered Lecture 1-18 of CS304 (Object Oriented Programming) with 150+ Most Important MCQs with answers. You can see My YouTube video for more details and Concepts

Uploaded by

Ali Raza Tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views35 pages

Cs304 Mid Term Preparation by MR - ART

In this Document I have covered Lecture 1-18 of CS304 (Object Oriented Programming) with 150+ Most Important MCQs with answers. You can see My YouTube video for more details and Concepts

Uploaded by

Ali Raza Tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Contact For Services: 03251076071

CS304
MIDTERM PREPARATION
PART 1
YouTube video link

Lecture 1:

1. Which of the following is not a programming paradigm?


o A) Procedural Programming
o B) Functional Programming
o C) Linear Programming
o D) Object-Oriented Programming
Answer: C) Linear Programming
2. What does OOP stand for?
o A) Oriented Object Programming
o B) Object-Oriented Programming
o C) Object Operation Programming
o D) Operational Object Programming
Answer: B) Object-Oriented Programming
3. Which of the following is a principle of OOP?
o A) Inheritance

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:

1. Which of the following defines a class in C++?


o A) class ClassName {};
o B) struct ClassName {};
o C) int ClassName {};
o D) void ClassName {};
Answer: A) class ClassName {};
2. What is the purpose of encapsulation?
o A) To increase code redundancy
o B) To hide implementation details
o C) To define a class structure
o D) To enforce a specific method order
Answer: B) To hide implementation details
3. An instance of a class is known as:
o A) A function
o B) A method
o C) An object

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:

1. Which of the following is a correct syntax to declare a class in C++?


o A) class MyClass {};
o B) class MyClass();
o C) new class MyClass {};
o D) void class MyClass {};
Answer: A) class MyClass {};
2. In C++, a constructor is used to:
o A) Deallocate memory
o B) Initialize objects
o C) Create classes
o D) Destroy objects
Answer: B) Initialize objects
3. Which keyword is used to access a member of a class in C++?
o A) ::
o B) ->
o C) .
o D) ,
Answer: C) .
4. What is the purpose of a destructor in C++?
o A) Allocate memory
o B) Perform data hiding
o C) Release resources held by an object
o D) Create new instances of a class
Answer: C) Release resources held by an object

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:

1. Inheritance in C++ allows:


o A) Using the same function name for different purposes
o B) Creating new classes from existing classes
o C) Accessing private data members directly
o D) Deleting base class methods
Answer: B) Creating new classes from existing classes
2. Which of the following is not a type of inheritance in C++?
o A) Single Inheritance
o B) Multiple Inheritance
o C) Double Inheritance
o D) Multilevel Inheritance
Answer: C) Double Inheritance
3. What does a derived class inherit from a base class?
o A) All private members
o B) Only constructors
o C) Protected and public members
o D) All data members regardless of access specifiers
Answer: C) Protected and public members
4. Which of the following access specifiers makes a class member inaccessible outside
the class?
o A) Public
o B) Protected
o C) Private
o D) Static
Answer: C) Private
5. What is the term used when a derived class has two or more base classes?
o A) Multilevel Inheritance

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:

1. Function overloading allows:


o A) Using the same function name with different parameter types
o B) Using multiple names for the same function
o C) Assigning a different return type to the same function
o D) Calling functions with the same arguments
Answer: A) Using the same function name with different parameter types
2. Which of the following is an example of function overloading?
o A) int add(int a, int b), float add(float a, float b)
o B) int add(int a), int add(int a, int b)
o C) void print(), int print(int a)
o D) All of the above
Answer: D) All of the above
3. What is operator overloading?
o A) Using operators with different data types
o B) Providing new meanings to existing operators
o C) Redefining the syntax of operators
o D) Creating custom operators for specific tasks
Answer: B) Providing new meanings to existing operators
4. Which of the following operators cannot be overloaded?
o A) +
o B) =
o C) ::
o D) -
Answer: C) ::
5. Which keyword is used to declare an inline function in C++?
o A) inline
o B) static
o C) virtual

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) {

CS304 MID TERM


Part 2
Lecture 6:

1. Which of the following is not a type of constructor?


o A) Default constructor
o B) Copy constructor
o C) Static constructor
o D) Parameterized constructor
Answer: C) Static constructor
2. What is the primary purpose of a destructor in C++?
o A) To initialize object members
o B) To deallocate memory and resources
o C) To overload operators
o D) To copy objects
Answer: B) To deallocate memory and resources
3. Which of the following statements about destructors is true?
o A) They can be overloaded
o B) They can be virtual
o C) They take parameters
o D) They have a return type
Answer: B) They can be virtual
4. Which symbol is used to define a destructor in C++?

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:

1. What is a static data member in a class?


o A) A data member shared by all instances of a class
o B) A data member that cannot be changed
o C) A data member accessible only within its class
o D) A data member that is constant
Answer: A) A data member shared by all instances of a class
2. Which keyword is used to declare a static member function in C++?
o A) static
o B) friend
o C) virtual
o D) protected
Answer: A) static
3. What is a friend function in C++?
o A) A function that can access private and protected members of a class
o B) A function that must be a member of the class
o C) A function that is only accessible within its class
o D) A function that cannot access any data members
Answer: A) A function that can access private and protected members of a
class
4. Which of the following can be declared as a friend of a class?
o A) A class
o B) A function

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:

1. Which of the following operators cannot be overloaded?


o A) ++
o B) ?:
o C) *
o D) =
Answer: B) ?:
2. What is the main advantage of operator overloading?
o A) It allows custom implementation of operators for user-defined types
o B) It reduces code readability
o C) It changes the syntax of operators
o D) It allows the creation of new operators
Answer: A) It allows custom implementation of operators for user-defined
types
3. Which of the following is a valid way to declare an overloaded operator as a
member function?
o A) returnType operator symbol(parameters) {}
o B) symbol operator(parameters) {}
o C) returnType operator(parameters) symbol {}
o D) returnType symbol operator(parameters) {}
Answer: A) returnType operator symbol(parameters) {}
4. How can the unary operator - be overloaded for a class in C++?
o A) returnType operator-() {}
o B) returnType - operator() {}
o C) returnType operator() - {}
o D) returnType () operator- {} Answer: A) returnType operator-() {}

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:

1. What is a key benefit of using inheritance in OOP?


o A) Increased redundancy
o B) Code reuse and organization
o C) Enhanced performance
o D) Reduced flexibility
Answer: B) Code reuse and organization
2. In multiple inheritance, a derived class:
o A) Can inherit from multiple base classes
o B) Inherits only from one base class
o C) Cannot override base class methods
o D) Must override all inherited methods
Answer: A) Can inherit from multiple base classes
3. Which keyword is used to define a derived class from a base class in C++?
o A) inherits
o B) extends
o C) :
o D) implements
Answer: C) :
4. Which type of inheritance involves a derived class inheriting from multiple base
classes?
o A) Multilevel Inheritance
o B) Single Inheritance
o C) Hierarchical Inheritance
o D) Multiple Inheritance
Answer: D) Multiple Inheritance

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

Lecture 10: Polymorphism

1. What is polymorphism in OOP?


o A) The ability to define multiple functions with the same name
o B) The ability to take multiple forms
o C) The ability to override a function
o D) The ability to overload operators
Answer: B) The ability to take multiple forms
2. Which of the following is a type of polymorphism?
o A) Compile-time polymorphism
o B) Runtime polymorphism
o C) Both A and B
o D) None of the above
Answer: C) Both A and B
3. What is required for function overriding in C++?
o A) Inheritance
o B) Operator overloading
o C) Function overloading
o D) A friend function
Answer: A) Inheritance
4. Which keyword is used to declare a virtual function in C++?
o A) virtual
o B) override
o C) friend
o D) static
Answer: A) virtual
5. What is the primary purpose of a virtual destructor?

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

Lecture 11: Abstract Classes

1. What is an abstract class in C++?


o A) A class that has at least one pure virtual function
o B) A class that cannot have data members
o C) A class with only static members
o D) A class that cannot be inherited
Answer: A) A class that has at least one pure virtual function
2. Which of the following statements is true about abstract classes?
o A) They cannot be instantiated directly
o B) They must have all virtual functions
o C) They cannot be inherited
o D) They cannot have any member functions
Answer: A) They cannot be instantiated directly
3. How is a pure virtual function declared in C++?
o A) virtual returnType functionName() = 0;
o B) pure virtual returnType functionName();
o C) virtual returnType functionName();
o D) returnType functionName() = 0;
Answer: A) virtual returnType functionName() = 0;
4. What is the main purpose of an abstract class?
o A) To provide a base class for other classes
o B) To overload operators
o C) To create standalone objects
o D) To implement friend functions
Answer: A) To provide a base class for other classes
5. Which of the following can be included in an abstract class?
o A) Pure virtual functions
o B) Member data
o C) Constructors and destructors

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:

1. What is the primary purpose of templates in C++?


o A) To overload operators
o B) To allow functions and classes to operate with generic types
o C) To implement polymorphism
o D) To create abstract classes
Answer: B) To allow functions and classes to operate with generic types
2. Which keyword is used to define a template in C++?
o A) template
o B) generic
o C) typename
o D) class
Answer: A) template
3. What is a key benefit of using templates?
o A) Code reusability for different data types
o B) Improved encapsulation
o C) Simplified syntax
o D) Faster execution time
Answer: A) Code reusability for different data types
4. Which of the following correctly defines a template function?
o A) template <typename T> T add(T a, T b) { return a + b; }
o B) typename <template T> T add(T a, T b) { return a + b; }
o C) generic <class T> T add(T a, T b) { return a + b; }
o D) template <class T> T add(T a, T b) { return a + b; }
Answer: D) template <class T> T add(T a, T b) { return a + b; }
5. What is the difference between typename and class in template definitions?
o A) typename is used for function templates; class is used for class templates
o B) There is no difference; they can be used interchangeably
o C) typename is used for type deduction; class is not
o D) typename is used for abstract classes; class is not
Answer: B) There is no difference; they can be used interchangeably
6. How can a template class be instantiated in C++?

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:

1. What is the Standard Template Library (STL) in C++?


o A) A library of template classes and functions

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

CS304 MID TERM


Part 3
Lecture 14:

1. What is exception handling in C++?


o A) A mechanism to handle runtime errors
o B) A way to perform file I/O
o C) A method for function overloading
o D) A tool for debugging
Answer: A) A mechanism to handle runtime errors
2. Which keyword is used to throw an exception in C++?
o A) try
o B) catch
o C) throw
o D) raise
Answer: C) throw
3. Which of the following blocks is used to handle exceptions in C++?
o A) catch
o B) finally
o C) except
o D) handle
Answer: A) catch
4. What is the purpose of a try block in C++?
o A) To define a template function

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:

1. What is file handling in C++?


o A) A mechanism to perform input and output operations on files
o B) A way to overload operators
o C) A tool for exception handling
o D) A method for class inheritance
Answer: A) A mechanism to perform input and output operations on files
2. Which library is used for file handling in C++?
o A) <iomanip>
o B) <iostream>
o C) <fstream>
o D) <cstring>
Answer: C) <fstream>
3. What is the purpose of the ifstream class in C++?
o A) To read from files
o B) To write to files

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

Lecture 16: Namespaces

1. What is a namespace in C++?


o A) A way to define classes
o B) A way to avoid name conflicts
o C) A template for creating objects
o D) A memory allocation technique

Answer: B) A way to avoid name conflicts

2. How do you declare a namespace in C++?


o A) namespace name { /* code */ }
o B) space name { /* code */ }

29
Contact For Services: 03251076071
o C) namespace::name { /* code */ }
o D) name::space { /* code */ }

Answer: A) namespace name { /* code */ }

3. Which keyword is used to access members of a namespace?


o A) . (dot)
o B) :: (scope resolution operator)
o C) -> (arrow)
o D) : (colon)

Answer: B) :: (scope resolution operator)

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

Answer: A) Using using namespace

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();

6. What is the main advantage of using namespaces?


o A) They improve runtime efficiency
o B) They provide better encapsulation
o C) They allow code to be executed faster
o D) They reduce the likelihood of name collisions

Answer: D) They reduce the likelihood of name collisions

7. Which namespace is automatically included in every C++ program?

30
Contact For Services: 03251076071
o A) std
o B) default
o C) global
o D) main

Answer: A) std

8. How can you define a nested namespace in C++?


o A) namespace outer::inner { /* code */ }
o B) namespace outer.inner { /* code */ }
o C) namespace outer { namespace inner { /* code */ } }
o D) namespace outer,inner { /* code */ }

Answer: C) namespace outer { namespace inner { /* code */ } }

Lecture 17:

1. What is the role of the preprocessor in C++?


o A) To compile the code
o B) To execute the program
o C) To process directives before compilation
o D) To link object files

Answer: C) To process directives before compilation

2. Which of the following is a preprocessor directive?


o A) #include
o B) #define
o C) #ifdef
o D) All of the above

Answer: D) All of the above

3. What is the purpose of the #include directive?


o A) To define a constant
o B) To include the contents of a file
o C) To conditionally compile code

31
Contact For Services: 03251076071
o D) To allocate memory

Answer: B) To include the contents of a file

4. How do you define a macro in C++?


o A) macro NAME value
o B) #define NAME value
o C) const NAME = value;
o D) define NAME = value;

Answer: B) #define NAME value

5. What is the result of using the #undef directive?


o A) It defines a new macro
o B) It deletes a macro definition
o C) It includes a header file
o D) It creates a namespace

Answer: B) It deletes a macro definition

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

Answer: D) Both A and B

7. How can you conditionally compile a block of code in C++?


o A) Using #if, #else, #endif
o B) Using #ifdef, #else, #endif
o C) Using #ifndef, #else, #endif
o D) All of the above

Answer: D) All of the above

8. What is the purpose of the #error directive in C++?


o A) To throw a runtime exception

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

Answer: B) To display an error message during preprocessing

9. Which of the following is a valid way to create a function-like macro?


o A) #define macro(a, b) (a + b)
o B) #macro function(a, b) { return a + b; }
o C) #function(a, b) (a + b)
o D) #def macro(a, b) (a + b)

Answer: A) #define macro(a, b) (a + b)

10. What happens when a macro is defined without a value?


o A) It results in a compile-time error
o B) The macro acts as a flag or indicator
o C) The macro is ignored
o D) The macro is initialized to zero

Answer: B) The macro acts as a flag or indicator

Lecture 18:

1. What is dynamic memory allocation in C++?


o A) Allocating memory at compile-time
o B) Allocating memory at runtime
o C) Allocating memory for global variables
o D) Allocating memory for constants

Answer: B) Allocating memory at runtime

2. Which operator is used to allocate memory dynamically in C++?


o A) malloc
o B) calloc
o C) new
o D) alloc

33
Contact For Services: 03251076071
Answer: C) new

3. What is the purpose of the delete operator in C++?


o A) To deallocate memory allocated by new
o B) To remove an element from an array
o C) To terminate a program
o D) To delete a file

Answer: A) To deallocate memory allocated by new

4. What is the result of failing to deallocate dynamically allocated memory?


o A) Memory leak
o B) Compilation error
o C) Faster program execution
o D) Automatic memory deallocation

Answer: A) Memory leak

5. How can you allocate memory for an array dynamically in C++?


o A) int* arr = new int[size];
o B) int* arr = alloc(int, size);
o C) int* arr = malloc(size * sizeof(int));
o D) int* arr = calloc(size, sizeof(int));

Answer: A) int* arr = new int[size];

6. Which of the following correctly deallocates memory for a dynamically allocated


array?
o A) delete arr;
o B) delete[] arr;
o C) free(arr);
o D) dealloc(arr);

Answer: B) delete[] arr;

7. What happens if you try to delete the same memory twice?


o A) The program crashes

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

Answer: D) It results in undefined behavior

8. Which of the following is a correct way to handle memory allocation failure?


o A) Checking if the pointer is nullptr after allocation
o B) Using a try-catch block for std::bad_alloc
o C) Checking if new returns zero
o D) Both A and B

Answer: D) Both A and B

9. What is the main advantage of dynamic memory allocation?


o A) Faster execution of programs
o B) Efficient memory usage
o C) Easier code readability
o D) Increased portability of code

Answer: B) Efficient memory usage

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

Answer: B) Memory must be manually deallocated using delete or delete[]

35

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy