CS304 Object Oriented Programming Midterm MCQs

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which part of an object exhibits its state?

  • Operations
  • Data (correct)
  • Any private part
  • Any public part

Inheritance is a way to

  • pass arguments to objects of classes.
  • organize data.
  • add features to existing classes without rewriting them. (correct)
  • improve data-hiding and encapsulation.

Suppose you have been given the following design: "A person has a name, age, address and sex. You are designing a class to represent a type of person called a patient. This kind of person may be given a diagnosis, have a spouse and may be alive". Given that the person class has already been created, what of the following would be appropriate to include when you design the patient class?

  • sex and diagnosis
  • age and sex
  • diagnosis and age
  • registration date and diagnosis (correct)

What problem(s) may occur when we copy objects without using deep copy constructor?

<p>All of the given (D)</p> Signup and view all the answers

This pointers are not accessible for static member functions.

<p>True (A)</p> Signup and view all the answers

A static member function cannot be declared.

<p>Explicit (A)</p> Signup and view all the answers

Remain in memory even when all objects of a class have been destroyed.

<p>Static variables (B)</p> Signup and view all the answers

Friend functions are functions of a class.

<p>non-member (B)</p> Signup and view all the answers

Which means if A declares B as its friend it DOES NOT mean that A can access private data of B. It only means that B can access all data of A.

<p>Friendship is one way only (A)</p> Signup and view all the answers

The statement objA=objB; will cause a compiler error if the objects are of different classes.

<p>True (A)</p> Signup and view all the answers

Which operator can not be overloaded?

<p>Conditional operator (? : ) (B)</p> Signup and view all the answers

To convert from a user-defined class to a basic type, you would most likely use

<p>a conversion operator that's a member of the class. (A)</p> Signup and view all the answers

The technique in which we visualize our programming problems according to real life's problems is called

<p>object oriented Programming (B)</p> Signup and view all the answers

In object orientated programming, a class of objects can properties from another class of objects.

<p>Inherit (B)</p> Signup and view all the answers

A C++ class is similar to

<p>Structure (B)</p> Signup and view all the answers

Suppose that the Test class does not have an overloaded assignment operator. What happens when an assignment a=b; is given for two Test objects a and b?

<p>The automatic assignment operator is used (B)</p> Signup and view all the answers

A'A static member function can be called, even when a class is not

<p>Instantiated (C)</p> Signup and view all the answers

Identify which of the following overloaded operator function's declaration is appropriate for the given call? Rational_number_1 + 2.325 Where Rational_number_1 is an object of user defined class Rational_number.

<p>Rational_number operator+(Rational_number &amp;obj, double&amp; num); (B)</p> Signup and view all the answers

Provide the facility to access the data member.

<p>accesser function (D)</p> Signup and view all the answers

Constant objects cannot change their state,

<p>True (A)</p> Signup and view all the answers

Relationship indicates that an object contains other objects.

<p>'has-a' (B)</p> Signup and view all the answers

Which one of the following features of OOP is used to derive a class from another?

<p>Inheritance (A)</p> Signup and view all the answers

Is a relationship

<p>Inheritance (C)</p> Signup and view all the answers

Satisfy the condition of polymorphism

<p>all of the given (D)</p> Signup and view all the answers

A generalization-specialization relation between classes are implemented using

<p>inheritance (D)</p> Signup and view all the answers

The >= operator can be overloaded.

<p>True (A)</p> Signup and view all the answers

In order to free the memory occupied by the object, we use

<p>Destructor (B)</p> Signup and view all the answers

Which of the following is not an example of multiple inheritances?

<p>Woman (B)</p> Signup and view all the answers

Static variable can be initialized more than once.

<p>False (B)</p> Signup and view all the answers

A generic class showing all the common attributes and a behavior of other classes represents a very important feature in OOP called

<p>Inheritance (D)</p> Signup and view all the answers

We can get only one unique value which can be used by all the objects of that class by the use of,

<p>static variables (A)</p> Signup and view all the answers

A member function having the same name as that of a class and a ~ sign with it is called,

<p>Destructor (A)</p> Signup and view all the answers

Using encapsulation we can achieve

<p>All of given options (A)</p> Signup and view all the answers

For classes with common behavior, you can save effort by placing the common behavior in a

<p>Base class (B)</p> Signup and view all the answers

Which of the following are an advantage of OOP?

<p>All of the given options (D)</p> Signup and view all the answers

Static variables act like a global variable in the context or scope of the class.

<p>True (A)</p> Signup and view all the answers

The compiler won't object if you overload the * operator to perform division.

<p>False (B)</p> Signup and view all the answers

We can use "this" pointer in the constructor in the body and even in the initialization list of any class if we are careful.

<p>True (A)</p> Signup and view all the answers

An overloaded operator always requires one less argument than its number of operands.

<p>False (B)</p> Signup and view all the answers

In OOP a class is an example of

<p>User Defined Type (A)</p> Signup and view all the answers

A class can be identified from a statement by

<p>Noun (D)</p> Signup and view all the answers

The members of a class that can be accessed without creating the object of the class is called

<p>Static (B)</p> Signup and view all the answers

Suppose there is an object of type Person, which of the following can be considered as one of its attributes

<p>Both Name and Age (A)</p> Signup and view all the answers

What a derived class can add?

<p>All of given (C)</p> Signup and view all the answers

Is/are used to access information hidden within an object?

<p>Private member functions (D)</p> Signup and view all the answers

Which one of the following terms best represents the statement given below, " Hiding details of an object from the other parts of a program"

<p>Encapsulation (D)</p> Signup and view all the answers

Flashcards

What exhibits an object's state?

The data portion of an object encompasses the properties and values that define its current state.

What is inheritance for?

Inheritance allows you to create new classes (derived classes) that inherit properties and behaviors from existing classes (base classes) without rewriting them. This helps in code reuse and organization.

What would you include in a 'Patient' class derived from a 'Person' class?

The Patient class would inherit from the Person class and add attributes specific to patients, such as diagnosis, registration date, and perhaps spouse information. Inheriting from Person avoids duplicating 'name', 'age', and 'sex' attributes.

What issues may occur when copying objects without a deep copy constructor?

Copying objects without a deep copy constructor can lead to dangling pointers (pointing to freed memory) and memory leaks. Deep copy constructors ensure that all data members are duplicated, avoiding shared memory and potential issues when original objects are modified.

Signup and view all the flashcards

Are 'this' pointers available for static member functions?

The 'this' pointer is unavailable inside static member functions because static functions belong to the class itself, not to individual objects. Since 'this' refers to the current object, it doesn't apply to static functions.

Signup and view all the flashcards

What kind of function is not declared explicitly?

Static member functions are associated with a class rather than individual objects. They lack the 'this' pointer and can be invoked directly using the class name without creating an object.

Signup and view all the flashcards

What remains in memory even after destroying all objects of a class?

Static variables are declared within a class but belong to the class itself, not individual objects. Their values persist even when all class objects are destroyed, acting as a shared memory space for all instances of that class.

Signup and view all the flashcards

Friend functions are ______________ functions of a class.

Friend functions are defined outside a class but have access to its private and protected members. They are not member functions of the class but are granted special privileges by the class itself.

Signup and view all the flashcards

How does friendship work between two classes?

Friendship relationship is one-way. Class A declaring B as its friend doesn't mean A can access B's private data. Instead, B can access all of A's data.

Signup and view all the flashcards

What happens with object assignment if the objects are of different classes?

The statement "objA = objB" will cause a compiler error if objA and objB belong to different classes because they have different data structures. A direct assignment is not allowed between objects of incompatible types.

Signup and view all the flashcards

What is the correct operator overloading declaration for adding a double to a class object?

The correct declaration for overloading the + operator to add a double value to a Rational_number object is: Rational_number operator+(double& obj); This allows the compiler to automatically perform the conversion when a Rational_number object is added to a double value.

Signup and view all the flashcards

Which operator cannot be overloaded?

The conditional operator (? :) cannot be overloaded in C++ because its behavior is fixed by the language and cannot be modified by user-defined operators.

Signup and view all the flashcards

How to convert from a user-defined class to a basic type?

To convert from a user-defined class to a basic type, you would use a conversion operator declared as a member of the class. This operator allows you to explicitly or implicitly convert an object of that class into the desired basic type.

Signup and view all the flashcards

What technique visualizes programming problems like real-life problems?

Object-oriented programming (OOP) encourages modeling programming problems based on real-world objects and their relationships. This approach focuses on entities, their attributes (data), and their actions (behavior).

Signup and view all the flashcards

What can a class of objects do with properties from another class in OOP?

In OOP, a class can inherit properties from another class through inheritance. This allows you to create specialized classes (derived classes) that extend the functionality of existing classes (base classes).

Signup and view all the flashcards

What is a C++ class similar to?

A C++ class is similar to a structure in that both define a blueprint for data and functions. However, classes offer more features like data hiding, inheritance, and polymorphism, making them more versatile and powerful.

Signup and view all the flashcards

What problem(s) may occur when copying objects without using a deep copy constructor?

Copying objects without a deep copy constructor can cause dangling pointers, memory leaks, and potentially system crashes because the copied object shares the same memory as the original. This can lead to unexpected behavior if one object is modified.

Signup and view all the flashcards

What happens when assigning objects without an overloaded assignment operator?

If a class does not have an overloaded assignment operator, the automatic assignment operator provided by the language is used. This typically performs a shallow copy, which copies references, potentially leading to the shared memory issues mentioned earlier.

Signup and view all the flashcards

When can a static member function be called?

A static member function can be called without instantiating an object of the class, because it is associated with the class itself rather than a specific object. This is similar to using static methods in other languages.

Signup and view all the flashcards

What is the correct declaration for overloading the + operator to add a double to a class object?

The correct overload declaration for adding a double value to a Rational_number object is: Rational_number operator+(double& obj); This allows the compiler to convert the double value to a Rational_number automatically.

Signup and view all the flashcards

What provides the facility to access data members?

Accessor functions, also known as getter functions, provide controlled access to private data members, ensuring data integrity and encapsulation. They provide a safe way to retrieve the values of private attributes.

Signup and view all the flashcards

Can constant objects change their state?

A constant object's state cannot be modified after its initialization. This enforces immutability, preventing accidental changes to the object's data.

Signup and view all the flashcards

What relationship indicates an object containing other objects?

The 'has-a' relationship in OOP indicates that one object contains another object as a member, representing a whole-part association.

Signup and view all the flashcards

Which OOP feature is used to derive a class from another?

Inheritance allows a new class to inherit properties and behaviors from an existing class, implementing a generalization-specialization relationship. This builds upon existing functionality and promotes code reuse.

Signup and view all the flashcards

What kind of relationship is inheritance?

Inheritance is a relationship in OOP where a derived class inherits properties from a base class. This allows for code reuse and the creation of specialized classes based on existing ones.

Signup and view all the flashcards

What satisfies the condition of polymorphism?

Polymorphism allows an object to take on multiple forms. This enables the same function call to behave differently depending on the actual type of the object at runtime.

Signup and view all the flashcards

How are generalization-specialization relations between classes implemented?

Inheritance is used to implement a generalization-specialization relationship between classes. It allows a derived class to inherit properties and behaviors from a base class, forming a hierarchical structure.

Signup and view all the flashcards

Can the >= operator be overloaded?

The >= operator can be overloaded in C++ to define a custom behavior for comparison between objects of user-defined classes. This allows you to define how objects of your class compare to each other.

Signup and view all the flashcards

What is used to free memory occupied by an object?

The destructor of a class is called automatically when an object of that class goes out of scope or is explicitly deleted using the 'delete' operator. This ensures the object's resources are properly cleaned up.

Signup and view all the flashcards

What is not an example of multiple inheritances?

Multiple inheritance allows a class to inherit from multiple base classes, inheriting attributes and behaviors from each. This can be useful for modeling complex relationships with multiple parent classes.

Signup and view all the flashcards

Can a static variable be initialized more than once?

Static variables are initialized only once, when the class is loaded. Subsequent initialization attempts are ignored.

Signup and view all the flashcards

What does a generic class representing common attributes and behavior of other classes represent?

Abstraction in OOP aims to create a generic representation of a concept by hiding unnecessary details, focusing only on the essential properties and behaviors. This allows for code reuse and creates a simplified view of complex entities.

Signup and view all the flashcards

Where do static variables act like global variables?

Static variables are declared within a class but belong to the class itself, not individual objects. Their values persist for all objects of that class, allowing for shared data across instances.

Signup and view all the flashcards

Will the compiler object if the overloaded * operator is used for division?

The compiler doesn't check the correctness of the overloaded operator's implementation. You can, for example, overload the * operator to perform division, but this could lead to unintended consequences and confusion.

Signup and view all the flashcards

Can the 'this' pointer be used within the constructor?

While the 'this' pointer is generally available within constructors, it's generally considered best practice to avoid using it directly in the constructor body or initialization list to prevent potential confusion and errors with the 'this' pointer related to uninitialized data members.

Signup and view all the flashcards

What is a C++ class similar to?

A C++ class is similar to a structure in that both define a blueprint for data and functions. However, classes offer more features like data hiding, inheritance, and polymorphism, making them more versatile and powerful.

Signup and view all the flashcards

Which operator can not be overloaded?

The conditional operator (? :) cannot be overloaded in C++ because its behavior is fixed by the language and cannot be modified by user-defined operators.

Signup and view all the flashcards

How many arguments does an overloaded operator require?

An overloaded operator always takes one less argument than the number of operands it represents. For example, an overloaded binary operator (like + or *) takes one argument, but represents two operands.

Signup and view all the flashcards

How are generalization-specialization relations between classes implemented?

Inheritance is used to implement a generalization-specialization relationship between classes. It allows a derived class to inherit properties and behaviors from a base class, forming a hierarchical structure.

Signup and view all the flashcards

In OOP, a class is an example of _____

A class is a user-defined data type that represents a blueprint for objects. It defines the attributes (data members) and behaviors (member functions) that objects of that class will have.

Signup and view all the flashcards

How can a class be identified from a statement?

Classes are typically named using nouns, reflecting the real-world entity they represent. This helps in understanding the purpose and role of the class within your program.

Signup and view all the flashcards

What kind of class member can be accessed without creating an object?

Public members of a class are accessible directly from outside the class, through object instances or using the scope resolution operator. They provide the interface for interacting with the object from other parts of your code.

Signup and view all the flashcards

What can be considered as one of an object's attributes?

An object's attributes are its properties or characteristics, representing the specific data that defines its state. By understanding these attributes, we can capture the essential features of an object.

Signup and view all the flashcards

What can a derived class add?

A derived class can add new data members, member functions (including friend functions), constructors, and destructors, enhancing its functionality and specializing its behavior. It builds upon the base class's capabilities.

Signup and view all the flashcards

What is/are used to access information hidden within an object?

Public member functions, which are part of a class's interface, provide controlled access to private data members, ensuring data integrity and encapsulation. They act as the gateway for interaction with the object's internal state.

Signup and view all the flashcards

Are 'this' pointers accessible for static member functions?

The 'this' pointer is unavailable inside static member functions because static functions belong to the class itself, not to individual objects. Since 'this' refers to the current object, it doesn't apply to static functions.

Signup and view all the flashcards

What kind of function cannot be declared?

Static member functions are associated with a class rather than individual objects. They lack the 'this' pointer and can be invoked directly using the class name without creating an object.

Signup and view all the flashcards

Does the C++ compiler allow for dynamic allocation of memory for objects?

In C++, objects can be dynamically allocated using the 'new' operator, allowing for flexible memory management and the creation of objects at runtime.

Signup and view all the flashcards

How to improve the 'Base' class with respect to accessing the field 'Age'?

To improve the 'Base' class with respect to accessing the 'Age' field, it's highly recommended to make 'Age' private and define getter and setter methods (get_age() and set_age()) to provide controlled access. This ensures data encapsulation and proper modification.

Signup and view all the flashcards

Can friend classes and friend functions be used as alternatives?

Both friend classes and friend functions can provide alternative ways to access the private members of a class. However, they serve different purposes and often have distinct advantages depending on the specific situation.

Signup and view all the flashcards

Which operators always take no arguments if overloaded?

When overloading the increment (++), decrement (--), or dereference (*) operators, they usually do not take explicit arguments. The arguments are implicitly provided by the context of the operator.

Signup and view all the flashcards

What happens when assigning objects without an overloaded assignment operator?

If a class does not have an overloaded assignment operator, the default assignment operator, provided by the language, is used. This typically performs a shallow copy, which copies references, potentially leading to shared memory issues.

Signup and view all the flashcards

What must an overloaded '-' operator do to work correctly?

For the expression "obj3 = obj1 - obj2" to work, the overloaded '-' operator must return a value. This value is used to assign the result to the 'obj3' object, completing the operation.

Signup and view all the flashcards

Which operator can not be overloaded?

The conditional operator (? :) cannot be overloaded in C++ because its behavior is fixed by the language and cannot be modified by user-defined operators.

Signup and view all the flashcards

How do we achieve independence of internal implementation from its external interface?

Abstraction allows us to focus on the essential aspects of a concept rather than its detailed implementation. This promotes code reuse and simplifies the interface for other parts of the program.

Signup and view all the flashcards

Which one is not an object association?

Inheritance is a specialized form of object association, representing a 'is-a' relationship. Other forms include simple association (objects can interact without a parent-child relationship), aggregation (a 'has-a' relationship where parts can exist independently), and composition (parts cannot exist without the whole).

Signup and view all the flashcards

How do we capture object attributes and behavior in OOP?

A class encapsulates the attributes (data members) and behaviors (member functions) of a real-world object within a single unit, defining its characteristics and actions.

Signup and view all the flashcards

What is the return type of a constructor?

Constructors do not have an explicit return type. They are implicitly understood to construct objects and their return type is determined by the type of object they create.

Signup and view all the flashcards

Study Notes

CS304 - Object Oriented Programming Midterm Solved MCQs

  • Question 1 (Marks: 1): Which part of an object exhibits its state?

    • Data (Page 23)
  • Question 2 (Marks: 1): Inheritance is a way to:

    • add features to existing classes without rewriting them (Page 27)
  • Question 3 (Marks: 1): What should be included in a 'Patient' class (already having a 'Person' class)?

    • diagnosis and age
    • age and sex
    • sex and diagnosis
    • registration date and diagnosis
  • Question 4 (Marks: 1): What problem(s) occur when copying objects without a deep copy constructor?

    • Dangling pointer
    • Memory leakage
    • All of the given (Page 147)
    • System crash
  • Question 5 (Marks: 1): Are this pointers accessible for static member functions?

    • False (Page 114)
  • Question 6 (Marks: 1): Can a static member function be declared?

    • static
  • Question 7 (Marks: 1): What remains in memory even after all class objects are destroyed?

    • Static variables (Page 111)
  • Question 8 (Marks: 1): What are friend functions?

    • non-member functions of a class (Page 136)
  • Question 9 (Marks: 1): If A declares B as its friend, does A access B's private data?

    • No, only B can access A's private data.
  • Question 10 (Marks: 1): Does objA=objB; cause a compiler error with different classes?

    • True
  • Question 11 (Marks: 1): Which overloaded operator declaration is appropriate for Rational_number_1 + 2.325?

    • Rational_number operator+(Rational_number& obj);
    • Rational_number operator+(double& obj);
    • Rational_number operator+(Rational_number &obj, double& num);
    • operator+(double& obj);
  • Question 12 (Marks: 1): Which operator cannot be overloaded?

    • Conditional operator (? :) (Page 141)
  • Question 13 (Marks: 1): How do you convert from user-defined class to basic types?

    • Built-in conversion operator
  • Question 14 (Marks: 1): What is the programming technique that visualizes problems in real-world terms?

    • Object Oriented Programming (Page 9)
  • Question 15 (Marks: 1): What can classes of objects do in OOP?

    • Inherit properties from another class
  • Question 16 (Marks: 1): What is a C++ class similar to?

    • Structure
  • Question 17 (Marks: 1): What happens when a=b; where 'a' and 'b' are Test objects but the Test class lacks an overloaded assignment operator?

    • The automatic assignment operator is used.
  • Question 18 (Marks: 1): How can a class be identified in a statement?

  • Noun (Page 58)

  • Question 19 (Marks: 1): What are members that can be accessed without object creation?

    • Public members
  • Question 20 (Marks: 1): What are attributes of a Person object?

    • Name, Age
  • Question 21 (Marks: 1): What is/are used to access hidden information in an object?

    • Private data members
    • Private member functions (Page 69)
  • Question 22 (Marks: 1): Can this pointers be accessed with static member functions?

    • False (Page 14)
  • Question 23 (Marks: 1): Can a static member function be declared?

  • Yes, a static member function can be declared.

  • Question 24 (Marks: 1): Can memory dynamically be allocated to objects in C++?

    • False
  • Question 25 (Marks: 1): How can variable "Age" in class Base be accessed appropriately? -Define the variable Age as private and create a get method that returns it and a set method that updates it.

  • Question 26 (Marks: 1): What is not a keyword in C++?

  • B_op

  • operator

  • Question 27 (Marks: 1): Can the >= operator be overloaded?

    • True (Page 140)
  • Question 28 (Marks: 1): How can a sub-object's life be independent of the master class's life?

    • Composition
    • Aggregation (Page 134)
  • Question 29 (Marks: 1): What is a way to make general classes more specific in OOP?

    • Inheritance (Page 27)
  • Question 30 (Marks: 1): What is the return type of a constructor in C++ ?

    • No type
  • Question 31 (Marks: 1): What is the process for hiding object details from users?

  • Encapsulation

  • Abstraction(Page 16)

  • Question 32 (Marks: 1): What are the ways to declare a variable to be static in class?

  • Static variable keyword before the variable declaration.

  • Question 33 (Marks: 1): Should static variables be initialized more than once?

    • False (Page 107)
  • Question 34 (Marks: 1): What is the best approach to implement functions with identical behavior on different data types?

  • Templates

  • Overloading

  • Question 35 (Marks: 1): Is class Vector<char>* an example of partial specialization?

    • True (Page 281)
  • **Question 36 (Marks: 1):**Classes like TwoDimensionalShape and ThreeDimensionalShape, should they be abstract?

  • True

  • Question 37 (Marks: 1): Classes like Sphere and Cube should they be abstract?

    • True
  • Question 38 (Marks: 1): What is important in OOP for object identification?

    • State, Behavior (and other properties)
  • Question 39 (Marks: 1): A member function that has the same name as the class and a '~' followed by the name is called?

    • Destructor
  • Question 40 (Marks: 1): What is not an advantage of inheritance?

  • Avoiding code rewriting (and providing frameworks).

  • Expanding classes.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Object-Oriented Programming Concepts
30 questions
Object-Oriented Programming Concepts
13 questions
CS304 Object-Oriented Programming Final Exam
45 questions
Use Quizgecko on...
Browser
Browser