C++ में खाली class का आकार 0 नहीं होता, बल्कि 1 बाइट होता है। वजह यह है कि हर object का मेमोरी में अलग पता होना ज़रूरी है। अगर आकार 0 होता तो एक ही class के दो objects का पता एक जैसा हो सकता था। इसलिए compiler एक बाइट जोड़ देता है। 0 बाइट इसी नियम से ग़लत है। 2 बाइट ज़रूरत से ज़्यादा है, और Null कोई आकार ही नहीं है।
In C++, an empty class does not have size 0. Its size is 1 byte. Every object must have a unique address in memory. If the size were 0, two objects of the same class could share the same address. So the compiler adds one byte to keep them distinct. That is why 0 byte is wrong, 2 byte is more than needed, and Null is not a size at all.
inheritance में protected और private access specifiers में क्या अंतर है?
What is the difference between protected and private access specifiers in inheritance?
Aprivate member न तो inherit होता है और न derived class में access होता हैA private member is not inheritable and not accessible in the derived class
Bprotected member inherit होता है और derived class में access भी होता हैA protected member is inheritable and also accessible in the derived class
Cदोनों inherit होते हैं, पर derived class में private access होता हैBoth are inheritable, but private is accessible in the derived class
Dदोनों inherit होते हैं, पर derived class में protected access नहीं होताBoth are inheritable, but protected is not accessible in the derived class
सही उत्तर / Answer: (B)
हल / Solution
base class के private और protected दोनों members derived class के object में मौजूद रहते हैं, यानी दोनों inherit होते हैं। फ़र्क access में है। protected member को derived class के functions सीधे use कर सकते हैं, पर private member को नहीं। इसलिए विकल्प 2 सही है। विकल्प 1 ग़लत है क्योंकि private member inherit तो होता है, बस access नहीं होता। विकल्प 3 और 4 ने दोनों का access उलटा बता दिया है।
Both private and protected members of a base class are present in a derived class object, which means both are inherited. The difference is in access. Functions of the derived class can use a protected member directly, but not a private member. So option 2 is correct. Option 1 is wrong because a private member is inherited but not accessible. Options 3 and 4 reverse the access rules of the two specifiers.
कौन-सी programming भाषा multiple inheritance (एकाधिक वंशानुक्रम) का समर्थन नहीं करती?
Which programming language does not support multiple inheritance?
AC++
BCLOS
CJava
Dइनमें से कोई नहींNone of these
सही उत्तर / Answer: (C)
हल / Solution
multiple inheritance में एक class एक साथ दो या अधिक parent classes से गुण लेती है। C++ में यह सीधे होता है, जैसे class C : public A, public B। CLOS (Common Lisp Object System) भी इसे पूरा समर्थन देता है। Java में classes के बीच multiple inheritance नहीं होता, ताकि diamond problem जैसी उलझन न हो। Java यही काम interfaces की मदद से करता है। इसलिए सही उत्तर Java है।
In multiple inheritance, one class inherits from two or more parent classes at the same time. C++ supports it directly, for example class C : public A, public B. CLOS, the Common Lisp Object System, also fully supports it. Java does not allow multiple inheritance between classes, so that ambiguity such as the diamond problem is avoided. Java achieves a similar effect through interfaces instead. So the correct answer is Java.
constructors की तरह, क्या एक class में एक से अधिक destructor हो सकते हैं?
Like constructors, can there be more than one destructor in a class?
AहाँYes
BनहींNo
CशायदMay be
Dकह नहीं सकतेCan't say
सही उत्तर / Answer: (B)
हल / Solution
एक class में कई constructors हो सकते हैं, क्योंकि constructor को overload किया जा सकता है। पर destructor overload नहीं होता। destructor कोई argument नहीं लेता और उसका कोई return type नहीं होता, इसलिए उसके दो अलग रूप बन ही नहीं सकते। हर class में सिर्फ़ एक destructor होता है, जो object के नष्ट होते समय चलता है। इसलिए उत्तर 'नहीं' है। 'शायद' या 'कह नहीं सकते' यहाँ लागू नहीं होते, क्योंकि नियम पक्का है।
A class can have many constructors because constructors can be overloaded. A destructor, however, cannot be overloaded. It takes no arguments and has no return type, so two different versions of it cannot exist. Every class has only one destructor, and it runs when the object is destroyed. So the answer is No. Options like May be or Can't say do not apply, because the language rule is fixed.
destructor का नाम constructor जैसा ही होता है, और उसके आगे कौन-सा चिह्न लगता है?
A destructor has the same name as the constructor, and it is preceded by which symbol?
A!
B?
C~
D$
सही उत्तर / Answer: (C)
हल / Solution
destructor भी class के नाम से ही बनता है, बस उसके आगे tilde चिह्न ~ लगाया जाता है। जैसे class Student का destructor ~Student() होगा। यह object के नष्ट होते समय अपने आप चलता है और memory या file जैसे संसाधन छोड़ता है। ! चिह्न logical NOT operator है, ? conditional operator का हिस्सा है, और $ का C++ में ऐसा कोई काम नहीं है। इसलिए सही उत्तर ~ है।
A destructor is also named after the class, but a tilde sign ~ is placed before the name. For example, the destructor of class Student is written as ~Student(). It runs automatically when an object is destroyed and releases resources such as memory or files. The ! sign is the logical NOT operator, ? is part of the conditional operator, and $ has no such role in C++. So the correct answer is ~.
class A की परिभाषा (class body) के अंदर उसका constructor define करने का syntax क्या है?
What is the syntax of defining the constructor of class A inside the class body?
AA(){}
B~A(){}
CA::A(){}
D~A(){};
सही उत्तर / Answer: (A)
हल / Solution
constructor का नाम class के नाम जैसा होता है और उसका कोई return type नहीं होता। class के अंदर इसे सीधे A(){} लिखकर define करते हैं, इसलिए विकल्प 1 सही है। A::A(){} वाला रूप तब लिखा जाता है जब constructor को class के बाहर scope resolution operator :: से define करना हो। class के अंदर यह रूप ग़लत है। ~A(){} और ~A(){}; destructor के रूप हैं, constructor के नहीं।
A constructor has the same name as the class and has no return type. Inside the class body it is defined directly as A(){}, so option 1 is correct. The form A::A(){} is used only when the constructor is defined outside the class with the scope resolution operator ::, and writing it inside the class is an error. ~A(){} and ~A(){}; are forms of a destructor, not a constructor, because the tilde marks a destructor.
default constructor को कितने parameters की आवश्यकता होती है?
How many parameters does a default constructor require?
A1
B2
C0
D3
सही उत्तर / Answer: (C)
हल / Solution
default constructor वह constructor है जिसे बिना कोई argument दिए बुलाया जा सकता है। जैसे class A में A() { } लिखा हो, तो A obj; लिखते ही यही चलता है। इसे कोई parameter नहीं चाहिए, इसलिए उत्तर 0 है। जिस constructor को 1, 2 या 3 arguments चाहिए हों, वह parameterized constructor कहलाता है। ध्यान दीजिए, अगर हर parameter का default मान दिया हो, तब भी वह बिना argument के बुलाया जा सकता है।
A default constructor is a constructor that can be called without passing any argument. For example, if class A has A() { }, then writing A obj; calls it. It needs no parameter, so the answer is 0. A constructor that needs 1, 2 or 3 arguments is called a parameterized constructor. Note that if every parameter has a default value, the constructor can still be called with no argument, so it also works as a default constructor.
Aज़रूरत पड़ने पर data को बदलनाTo modify the data whenever required
Bobject को नष्ट करनाTo destroy an object
Cobject बनते समय उसके data members को शुरुआती मान (initialize) देनाTo initialize the data members of an object when it is created
Dबाहर से private functions को बुलानाTo call private functions from the outside world
सही उत्तर / Answer: (C)
हल / Solution
constructor एक विशेष member function है जिसका नाम class के नाम जैसा होता है। जैसे ही object बनता है, यह अपने आप चलता है और data members को शुरुआती मान देता है। इसलिए विकल्प 3 सही है। data को बाद में बदलने का काम साधारण member functions (जैसे setter) करते हैं। object को नष्ट करते समय destructor चलता है, constructor नहीं। private functions को बाहर से बुलाना constructor का काम नहीं है।
A constructor is a special member function whose name is the same as the class name. It runs automatically as soon as an object is created and gives initial values to the data members. So option 3 is correct. Changing data later is the job of ordinary member functions such as setters. When an object is destroyed, the destructor runs, not the constructor. Calling private functions from outside the class is not the purpose of a constructor.
array घोषित (declare) करने का सही syntax कौन-सा है?
Which of the following is the correct syntax for declaring an array?
Ainit array []
Bint array [5];
CArray[5];
Dइनमें से कोई नहींNone of the above
सही उत्तर / Answer: (B)
हल / Solution
array घोषित करने का नियम है: पहले datatype, फिर array का नाम, फिर square bracket में आकार, और अंत में semicolon। int array[5]; में int प्रकार है, array नाम है और 5 आकार है। यह पाँच int रखने वाला array बनाता है। init कोई datatype नहीं है और उसमें आकार भी नहीं दिया गया। Array[5]; में datatype ही नहीं लिखा, इसलिए compiler इसे घोषणा नहीं मानेगा। इसलिए विकल्प 2 सही है।
The rule for declaring an array is: data type, then the array name, then the size inside square brackets, and finally a semicolon. In int array[5]; the type is int, the name is array and the size is 5. In option 1, init is not a data type and no size is given. Array[5]; has no data type at all, so the compiler does not accept it as a declaration. Hence option 2 is correct.
निम्नलिखित में से कौन-सा कथन array (सरणी) की विशेषता बताता है?
Which of the following refers to the characteristics of an array?
Aarray एक ही प्रकार के data items का समूह हैAn array is a set of similar data items
Barray अलग-अलग (भिन्न) data items का समूह हैAn array is a set of distinct data items
Carray में अलग-अलग datatype के मान रखे जा सकते हैंAn array can hold different types of datatypes
Dइनमें से कोई नहींNone of the above
सही उत्तर / Answer: (A)
हल / Solution
C++ में array एक ही प्रकार के कई मानों का समूह होता है। जैसे int marks[5] में पाँचों मान int ही होंगे। सारे तत्व memory में लगातार रखे जाते हैं और index 0 से गिने जाते हैं। इसलिए विकल्प 1 सही है। array के मान अलग-अलग होना ज़रूरी नहीं, एक जैसे मान भी रखे जा सकते हैं, इसलिए विकल्प 2 ग़लत है। एक array में अलग-अलग datatype नहीं रख सकते, उसके लिए structure या class चाहिए।
In C++, an array is a collection of elements of the same data type. For example, in int marks[5] all five elements are of type int. The elements are stored in consecutive memory locations and are accessed by an index starting from 0. So option 1 is correct. The values need not be distinct, since repeated values are allowed, so option 2 is wrong. An array cannot mix data types; for that we use a structure or a class.
आगे के 10 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
यदि multilevel इनहेरिटेंस में class C, class B से इनहेरिट करती है और class B, class A से इनहेरिट करती है, तो class C का object बनाने पर उनके destructors किस क्रम में चलेंगे?
In multilevel inheritance, class C inherits class B, and class B inherits class A. In which sequence are their destructors called if an object of class C was declared?