Machine-translated by Google. Questions are authored in English.
Top

Classes and Objects Questions and Answers

1.

A/An ______ is a user-defined datatype which has data members and member functions.

Exam Year(s)

DSSSB TGT - 2023,

Answer: A

A class is a user-defined data type that holds data members and member functions. An object is an instance of a class, created from it, not the type itself. A package is a way of grouping related classes in languages such as Java, and a method is a single function inside a class. Only the class fits the definition. In Java and C++ the keyword class is used to define such a type, and objects are then created from it.

Please log in or register to report this question.

2.

The structure of C++ programming brings together a group of :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: A

A C++ structure brings together related data items, variables.

A struct groups variables that belong together, which may be of different types, under one name: for example a student's roll number (int), name (char array) and marks (float). In C++ a structure can also contain member functions.

Items of the same data type are grouped by an array, not a structure, and integers with user-defined names describe an enumeration (enum). Since those two are wrong, 'All of the above' is wrong too.

Please log in or register to report this question.

Answer: B

A static function can be called using the class name and function name.

A static member function belongs to the class as a whole, not to any particular object. It has no this pointer, can directly access only static members, and is called with the class name and the scope resolution operator, as in Counter::count(), without needing an object.

The function called when an object is destroyed is the destructor. A static function needs no dummy object, which is the whole point, and the statement that it is closely connected with an individual object describes ordinary (non-static) member functions.

Please log in or register to report this question.

4.

In C++ .................. operator is used for Dynamic memory allocation.

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

Dynamic memory is allocated with the new operator.

In C++, new allocates memory on the free store (heap) at run time, constructs the object there, and returns a pointer to it; for example int *p = new int[10];. The memory is released with delete or delete[].

The scope resolution operator :: names a member or global identifier, the conditional operator ?: chooses between two expressions, and the member access operators (dot and arrow) select a member of an object. None of them allocates memory.

Please log in or register to report this question.

5.

Objects life time generally corresponds to which storage allocation mechanism from the following :

(i) Static objects
(ii) Stack objects
(iii) Heap objects
(iv) Serialised objects

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: B

The answer is (i), (ii) and (iii).

An object's lifetime is tied to how its storage is allocated. Static objects live for the whole run of the program, stack (automatic) objects live from their declaration until their block exits, and heap (dynamic) objects live from explicit allocation until they are freed or garbage collected. These are the three classic storage allocation strategies.

Serialisation turns an object into a byte stream for storage or transmission. It is not a storage allocation mechanism, so options naming (iv) are wrong, and (ii) and (iii) alone leaves out static objects.

Please log in or register to report this question.

6.

Which of the following properties makes user defined class good ?

(i) Something that has a small and well defined set of operations.
(ii) Something that can be seen as a "Black Box" manipulated exclusively through that set of operations.
(iii) Something whose actual representation could conceivable be modified without affecting the way that set of operation is used.
(iv) Something one might want more than one of.

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: B

All four properties make a good class, so the answer is (i), (ii), (iii) and (iv).

Stroustrup describes a good user-defined type as something with a small, well-defined set of operations; something that can be treated as a black box, used only through those operations; something whose representation could be changed without changing how the operations are used; and something of which one might want more than one instance.

Each of the narrower options leaves out properties from that list.

Please log in or register to report this question.

Answer: A

A shallow copy duplicates only references, while a deep copy duplicates the entire object.

A shallow copy copies the object's fields as they are, so any field that refers to another object still points to the same object; changes through one copy are seen through the other. A deep copy recursively copies the referenced objects too, so the two copies share nothing.

Option B reverses the two. The distinction has nothing to do with system versus user-defined variables, so C and D are wrong.

Please log in or register to report this question.

8.

What happens if delete is used on a pointer not allocated with new in C++ ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: C

The result is undefined behavior.

The C++ standard allows delete only on a pointer obtained from new (or on a null pointer). Applying it to anything else, such as the address of a local variable, memory from malloc or a pointer into the middle of an array, is undefined behaviour: the program may crash, corrupt the heap and fail much later, or appear to work by chance.

That is why 'memory is freed safely' and 'program runs normally' are wrong; even if it seems to run, nothing is guaranteed. delete never resets the pointer to NULL by itself, even in the valid case; the programmer must do that.

Please log in or register to report this question.

9.

Match the following with respect to C++ data types :

a. User defined type1. Qualifier
b. Built in type2. Union
c. Derived type3. Void
d. Long double4. Pointer

Code : a b c d

Exam Year(s)

UGC NET - 2012,

Answer: A

The correct matching is a-2, b-3, c-4, d-1. A union is a user defined type (like struct and class), void is a built-in (fundamental) type, a pointer is a derived type made from other types, and long double is a built-in type with the qualifier long, listed here as the qualifier.

The other codes give Void to the user defined types or the pointer to the built-in type, which is wrong. So the code 2 3 4 1 is the answer.

Please log in or register to report this question.

Answer: D

The advantage of new and delete is both allocation of memory and freeing of memory previously allocated, and in addition new can initialize the object. Option D combines the two functions new (allocate) and delete (free), which are the key purpose of the operators.

Options A and B each name only half of the pair, so they are incomplete. Option C names a possible benefit, initialization, but not the main function pair described in D.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Classes and Objects Questions and Answers || Classes and Objects MCQ Questions and Answers || Classes and Objects GK Questions and Answers || Classes and Objects GK MCQ Questions || Classes and Objects Multiple Choice Questions and Answers || Classes and Objects GK || GK on Classes and Objects || Object Oriented Programming Questions and Answers || Object Oriented Programming MCQ Questions and Answers || Object Oriented Programming GK Questions and Answers || GK on Object Oriented Programming