CBSE - MCQ Question Banks (के. मा. शि. बो . -प्रश्नमाला )

PreviousNext

Q. 190001 A class implements polymorphism by


A. restricting access to members.

B. ignoring background details.

C. bundling data and functions together.

D. allowing different behavior for the same function.

Right Answer is: D

SOLUTION

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions.


Q. 190002 If the object is global, then


A. it can be used anywhere in the program by any function.

B. It can be used only within the function that declares it.

C. the member can be accessed only by the member functions of the class.

D. the scope of public members depends upon the referencing object.

Right Answer is: A

SOLUTION

If an object has a global scope, then the objects can be used anywhere in the program by any function. If an object has a local scope, then the objects can be used only within the function that declares it.


Q. 190003 The single most important C++ feature that implements OOP concepts and ties them together is


A. object.

B. class.

C. method.

D. data hiding.

Right Answer is: B

SOLUTION

A class is a way to bind the data describing an entity and its associated functions together. In C++, class makes a data type that is used to create objects of this type.


Q. 190004 The other name for member function is


A. method.

B. data member.

C. object.

D. function.

Right Answer is: A

SOLUTION

Member functions are the set of operations that may be applied to objects of that class. There may be zero or more member functions for a class. They are referred to as the class interface.


Q. 190005 A group of similar objects is known as


A. members.

B. functions.

C. class.

D. methods.

Right Answer is: C

SOLUTION

A class is a way to bind the data describing an entity and its associated functions together.


Q. 190006 A friend function


A. is a non member function.

B. is a member function declared as a friend.

C. data members.

D. is called using the object.

Right Answer is: A

SOLUTION

A friend function is used in object-oriented programming to allow access to private or protected data in a class from outside the class.


Q. 190007 Inline functions are


A. defined inside a member function.

B. defined under public label.

C. defined inside the class.

D. defined inside every object.

Right Answer is: C

SOLUTION

In computer science, an inline function is a programming language construct used to tell a compiler it should perform in-line expansion on a particular function.These functions are defined inside the class.


Q. 190008 Count is a global integer declared outside class X. Its declaration would be


A. X :: Count.

B. :: Count.

C. Count :: X.

D. X :: int Count .

Right Answer is: B

SOLUTION

:: is scope resolution operator. If the Count integer belongs to class X, then it would have been declared as X :: Count. But, since it is global variable, it will be declared as ::Count outside class.


Q. 190009 If a user wants to know the current value of a private data member, he can get it through


A. an accessor function.

B. a mutator function.

C. setters function.

D. getmember() function.

Right Answer is: A

SOLUTION

Accessor functions allow us to access the data number/ field of an object. Example getRollNo()can be an accessor function to roll no. of students.


Q. 190010 Members that can be used only by member functions and friends of class in which it is declared are


A. public members.

B. private members.

C. protected members.

D. hidden members.

Right Answer is: C

SOLUTION

Protected means data members and member functions can be used in same class and its derived class.


Q. 190011 In case of inline functions


A. the class name must precede the function name.

B. function call is replaced by the function code.

C. there must be at least one static member.

D. program size decreases as there is no function call.

Right Answer is: B

SOLUTION

With inline code, the compiler replaces the function call statement with the function code itself and then compiles the entire code. This speeds up the process as the compiler doesnot have to jump to another location to execute the function.


Q. 190012 Out of the following, the false statement is -


A. Passing an object by reference may change the original value.

B. Passing an object by value does not change the original value.

C. Objects can be passed only to member functions.

D. Objects can directly call public members of the class.

Right Answer is: C

SOLUTION

An object may be passed to a member function, a non-member function, and a friend function.


Q. 190013 When a class uses dynamic memory, the member function invoked by the class is


A. an overloaded assignment operator

B. a destructor.

C. a copy constructor.

D. no member functions provided.

Right Answer is: B

SOLUTION

In dynamic memory allocation, whenever the object is out of scope, its memory would be freed and deallocated. So, a destructor is automatically invoked by the class.


Q. 190014 A friend function


A. is a non member function

B. is a member function declared as a friend

C. data members

D. is called using the object

Right Answer is: A

SOLUTION

A friend function is a function that can access the private members of a class as though it were a member of that class. In all other regards, the friend function is just like a normal function. A friend function may or may not be a member of another class.
To declare a friend function, use the friend keyword in front of the prototype of the function you wish to be a friend of the class. It does not matter whether you declare the friend function in the private or public section of the class.


Q. 190015 Private members are


A. directly accessed by any function.

B. hidden from outside world.

C. are inheritable.

D. are accessed by classes in which they are absent.

Right Answer is: B

SOLUTION

Private Members are the class members that are hidden from outside world. The private members implement the OOP concept of data hiding. The private members of a class can be used only by member functions (and friends) of the class in which it is declared.


Q. 190016 In case of inline functions


A. the class name must precede the function name

B. function call is replaced by the function code

C. there must be at least one static member

D. program size decreases as there is no function call

Right Answer is: B

SOLUTION

With inline code, the compiler code replaces the function call statement with the function code itself and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function and then jumps back as the code of the called function is already available to the calling program.


Q. 190017 Disadvantage of inline function is -


A. it speeds up the program.

B. it improves performance.

C. there is a memory penalty.

D. it removes too many function calls.

Right Answer is: C

SOLUTION

Inline functions run a little faster than the normal functions as function-calling overheads are saved, however, there is a memory penalty. If 10 times an inline function is called, there will be 10 copies of the function inserted into the code.


Q. 190018 A girl can be a daughter, a sister or a friend. In C++, this could serve as an example for


A. encapsulation.

B. inheritance.

C. polymorphism.

D. data hiding.

Right Answer is: C

SOLUTION

Polymorphism : An object is in different forms and in each form it exhibits the same functionality but the implementation is different.


Q. 190019 The static data member is


A. visible globally.

B. visible only in two classes.

C. visible only in three classes.

D. visible only within the class.

Right Answer is: D

SOLUTION

A static data member is different from ordinary data members of a class in the following aspects :

1. There is only one copy of this data member maintained for the entire class which is shared by all the objects of that class.
2. It is visible only within the class, however, its lifetime is the entire program.


Q. 190020 A static member function can access


A. only static members

B. all data members

C. other member functions

D. only public members

Right Answer is: A

SOLUTION

A member function that accesses only the static members of a class may be declared as static. A static member function can access only static members of the same class. A static member function is invoked by using the class name instead of its objects as it is shown below:
classname::function-name; 


Q. 190021 Private members implement the concept of


A. polymorphism.

B. data hiding.

C. abstraction.

D. encapsulation.

Right Answer is: B

SOLUTION

Private Members are the class members that are hidden from the outside world. The private members implement the OOP concept of data hiding.


Q. 190022 The number of data members a class can have is


A. only 3.

B. only 2.

C. only 1.

D. 0 or more.

Right Answer is: D

SOLUTION

Data members are the data type properties that describe the characteristics of a class. There may be zero or more data members of any type in a class.


Q. 190023 An object is


A. an instance of a class.

B. a hardware tool.

C. also known as class.

D. is the way in which a class is defined.

Right Answer is: A

SOLUTION

Object is an identifiable entity with some characteristics and behaviour. In OOP, object represents an entity that can store data and has its interface through functions.


Q. 190024 A valid class definition is


A. class X{ private: int a=2; char b = }:

B. Class X (int a=2; public: char b= )

C. -class X{int a=2; char b= }

D. class X
{
   int a;
   public:
   void add();
};

Right Answer is: D

SOLUTION

The general form of a class definition looks like :
class class-name
{
   private:
     [variable declarations;]
     [function declarations;]
  protected:
     [variable declarations;]
     [function declarations;]
   public:
     [variable declarations;]
     [function declarations;]
};  For example,
class Sample
{
    int a;
    double b;
    public:
      void add();
      void print();
      ...
};     


Q. 190025 A class implements polymorphism by-


A. restricting access to members

B. ignoring background details

C. bundling data and functions together

D. allowing different behavior for the same function

Right Answer is: D

SOLUTION

Polymorphisms is the ability to take more than one forms. For example, say a door. A door to a temple, a door to a house, a door to a kitty house,
all are doors, but all look different.


Q. 190026 A class implements abstraction by-


A. restricting access to members

B. storing essential features

C. bundling data and functions together

D. allowing different behavior for the same function

Right Answer is: B

SOLUTION

Abstraction refers to the act of representing essential features without including the background details or explanations.


Q. 190027 A class implements encapsulation by-


A. restricting access to members

B. ignoring background details

C. bundling data and functions together

D. allowing different behavior for the same function

Right Answer is: C

SOLUTION

Encapsulation refers to wrapping up data and associated functions into one single unit.


Q. 190028 By default, members in a class are-


A. data

B. functions

C. private

D. public

Right Answer is: C

SOLUTION

There are generally two types of members in a class : private and public. If no label (public or private) is specified then by default, members are private.


Q. 190029 A class declaration has-


A. keyword Class, followed by class-name, followed by members within braces

B. keyword Class, followed by private members and functions, within braces

C. keyword Class, followed by data members and functions within braces

D. keyword class, followed by class-name, followed by members within braces

Right Answer is: D

SOLUTION

The keyword class specifies that it is a class; class-name is the tagname of the class that acts as a type specifier for it, using which objects of this class type can be created. The braces { and } surround the body of the class. The class body contains the declaration of its members ( data and functions). These are generally two types of members in a class : private and public. 
The class declaration looks like :
class class-name
{
   private:
     [variable declarations;]
     [function declarations;]
  protected:
     [variable declarations;]
     [function declarations;]
   public:
     [variable declarations;]
     [function declarations;]
};         


Q. 190030 The different access levels in a class are


A. protected, personal, public

B. private, public, preserved

C. public, private, protected

D. public, protected, personal

Right Answer is: C

SOLUTION

Program access levels control access to members from within the program. These access levels are private, protected or public. Depending upon the access level of a class member, access to it is allowed or denied.


Q. 190031 A class binds


A. data and structures

B. data and functions

C. objects and structures

D. functions and structures

Right Answer is: B

SOLUTION

A class is a way to bind the data describing an entity and its associated functions together. In C++, class makes a data type that is used to create objects of this type.


Q. 190032 A class implements abstraction by


A. restricting access to members.

B. storing essential features.

C. bundling data and functions together.

D. allowing different behavior for the same function.

Right Answer is: B

SOLUTION

Abstraction is the process or result of generalization by reducing the information content of a concept or an observable phenomenon, typically to retain only information which is relevant for a particular purpose.


Q. 190033 A class implements encapsulation by


A. restricting access to members.

B. ignoring background details.

C. bundling data and functions together.

D. allowing different behavior for the same function.

Right Answer is: C

SOLUTION

Encapsulation is the inclusion of one thing within another thing so that the included thing is not apparent. A class implements encapsulation by bundling data and functions together.


Q. 190034 The set of operations performed on the class are known as


A. data members.

B. mutator functions.

C. manager functions.

D. member functions.

Right Answer is: D

SOLUTION

The member functions are the set of operations that may be applied to objects of that class. There may be zero or more member functions for a class. They are referred to as the class interface.


Q. 190035 Among the following, the valid class definition is


A. class X{ private: int a=2; char b= };

B. Class X{int a=2; public: char b= }

C. -class X{int a=2; char b= }

D. class X{int a; public: char b; )

Right Answer is: D

SOLUTION

The correct syntax for class definition is class classname { data members }


Q. 190036 Out of the following, the correct statement is -


A. A member function can call another member function.

B. Member functions are unable to call non-member functions.

C. A class with one static data must have one static member function.

D. A friend function can access private data without the object.

Right Answer is: A

SOLUTION

Member functions cannot access non-member functions of a class and private data can be accessed only by member functions of class.


Q. 190037 The scope resolution operator is denoted by


A. :: .

B. ; .

C. : .

D. << .

Right Answer is: A

SOLUTION

The scope resolution operator ::, when used with classname depicts the class member as in classname :: functionname and when used only with the variable name as in ::svariablename, depicts the global variable (the one with the file scope).


Q. 190038 The statement which is valid or legal in C++ is


A. A reference can be used without proper initialization.

B. Array of references is legal in C++.

C. Char , int pointers can refer to null pointers but vice versa not true.

D. Array of pointers is not legal in C++.

Right Answer is: C

SOLUTION

A reference has to be properly initialized, otherwise it will result in an error. In C++, special care is taken for assignment of void pointers to other pointer types. While we can assign a pointer of any type to a void pointer the reverse is not true until we do explicit typecasting.


Q. 190039 A class in C++ is known as empty class if it


A. has more than a name.

B. has no member functions.

C. is just a name.

D. has no common properties.

Right Answer is: C

SOLUTION

An empty class has no variable declarations, no functions or methods but the objects of these classes can have non–zero size.


Q. 190040 A class definition


A. is same as class declaration.

B. does not create objects.

C. creates objects.

D. creates objects and initializes them.

Right Answer is: B

SOLUTION

Objects are created separately using new operator. A class definition just defines a class and its data members.


Q. 190041 When the members are accessible from outside the class, they are under


A. public access specifier.

B. private access specifier.

C. protected access specifier.

D. friend access specifier.

Right Answer is: A

SOLUTION

Public member functions can be accessed by any other member or non-member functions of the class as well as outside the class.


Q. 190042 Write a statement declaring an array named Bk of 15 objects of class Book.
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

Book Bk[15];


Q. 190043 What constitutes the public interface of the class?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

Public members, that can be called in any function (using the object) constitute the public interface of the class.


Q. 190044 When should we make a function inline?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

Make only small functions inline. It must fulfill certain other conditions - it must not have a loop, switch or goto if it returns a value, it must not have static variables and must not be recursive.


Q. 190045 What do you understand by the term containership?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

Containership refers to an object of a class being a member of another class.


Q. 190046 What is a nested class?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface.


Q. 190047 How are public members different from private members of a class?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

Public members can be called outside the class but Private members can be accessed only by other members of the class. Also, public members can be inherited while private members cannot be.


Q. 190048 What is nesting of member functions?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

When a member function of a class calls another member function, it is known as nesting of member functions.


Q. 190049 What is the difference between private and protected members of a class?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

The only difference is that private members cannot be inherited outside the class while protected members can be inherited by its derived classes.


Q. 190050 What are objects?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

An object is an instance of a class. For a class that has been defined, objects can be created as variables of that class. Each object will have a set of data members defined in the class.


Q. 190051 What is a friend function?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

A friend function is used in object-oriented programming to allow access to private or protected data in a class from the outside. Normally, a function that is not a member of a class cannot access such information; neither can an external class. Occasionally, such access will be advantageous for the programmer. Under these circumstances, the function or external class can be declared as a friend of the class using the friend keyword.


Q. 190052 How is struct different from class in C++?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

Members of a class are private by default whereas members of a structure are public by default.


Q. 190053 What are static member functions?
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.

Right Answer is:

SOLUTION

A static member function is a member function that can access only static data members. It is called using the class name and not with the object as is the case with other member functions.


Q. 190054 The error in the following code is -

           class file{                      char a,b,c;                       public:                                char file();             };


A. constructor should not be defined publicly.

B. constructor should have arguments.

C. constructor should not have any return type.

D. constructor should be hidden.

Right Answer is: C

SOLUTION

char file(); is incorrect as constructor does not have any return type.


Q. 190055 The syntax error in the following program is  in -

            class ABC {          int x=10;          float y;          ABC( ) {y = 5;}          ~ABC() {} }; void main( ) {         ABC a1,a2; }


A. line 3 and line 10.

B. line 4 and line 5.

C. line 3 and line 5.

D. line 4 and line 10.

Right Answer is: A

SOLUTION

In line 3, the class member cannot be initialized inside a class. In line 10, a1 is assigned a value that is never used.


Q. 190056 The object that will invoke constructor1 in the following code is -

class exam           {                  int month;                  public:                           exam(int y){ month = y};    //constructor1                           exam(exam &t);                    //constructor2           };


A. exam In;

B. exam In();

C. exam (10);

D. exam In(10);  

Right Answer is: D

SOLUTION

exam is the name of the class and In is the name of the object. Inside the brackets, integer value is passed.


Q. 190057 In the following C++ code, function1 is referred to as -

           class Hall             {                              public:                                        Hall( )         //function1                                         { 
                                           cout << “Leaving the hall” <                                        }                                       ~Hall( )            //function2                                       { 
                                          cout << “Entering the hall” <               };


A. constructor.

B. private member.

C. destructor.

D. public member.

Right Answer is: A

SOLUTION

A member function with the same name as its class is called constructor and it is used to initialize the objects of that class type with a legal initial value.


Q. 190058 The default constructor gets called


A. explicitly.

B. automatically.

C. implicitly.

D. locally.

Right Answer is: B

SOLUTION

The object creation is a two-step process. At first, the memory is allocated and then its data members are initialised. For initialisation, constructors are provided which gets called automatically.


Q. 190059 The difference between constructors and the other member function is that


A. constructors cannot be overloaded.

B. constructors have a return type.

C. constructors do not return any value.

D. constructors are called explicitly.

Right Answer is: C

SOLUTION

Constructors are like other member functions of the class and hence, can be overloaded. The only difference is that the constructors do not return any value.


Q. 190060 Static memory allocation takes place during


A. execution.

B. compilation.

C. linking.

D. initialization.

Right Answer is: B

SOLUTION

Static memory allocation takes place during compilation whereas dynamic memory allocation takes place during execution.


Q. 190061 Dynamic memory allocation always takes place during


A. execution.

B. compilation.

C. linking.

D. initialization.

Right Answer is: A

SOLUTION

Static memory allocation takes place during compilation, whereas, dynamic memory allocation takes place during execution.


Q. 190062 The destructor is always preceded with a


A. hash.

B. sigma.

C. tilde.

D. dash.

Right Answer is: C

SOLUTION

The destructor is a member function that has the same name as the class. It is used to deallocate the memory space and always preceded with a tilde sign(~).


Q. 190063 A copy constructor is called when


A. an object is passed by reference.

B. a function returns no argument.

C. an object is passed by value.

D. no object is passed.

Right Answer is: C

SOLUTION

The copy constructor is invoked when an object is passed by value to a function. The pass-by-value method requires a copy of the passed argument to be created for the function to operate upon.


Q. 190064 The constructor of the form classname(classname &) is a


A. parameterized constructor.

B. copy constructor.

C. default constructor.

D. mutual constructor.

Right Answer is: B

SOLUTION

A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. This constructor takes a single argument: a reference to the object to be copied.


Q. 190065 In copy constructor, an argument is passed by


A. value

B. address.

C. value-result.

D. reference.

Right Answer is: D

SOLUTION

A copy constructor is a constructor of the form classname(classname &). The compiler will use the copy constructor whenever you initialize an instance using values of another instance of same type. In copy constructor, if the argument is passed by value instead of reference, the compiler runs out of memory.


Q. 190066 Member functions of a class required to be called


A. explicitly.

B. automatically.

C. implicitly.

D. locally.

Right Answer is: A

SOLUTION

Member functions are used to modify, retrieve or print data whenever required and hence are called explicitly.


Q. 190067 A constructor may not be


A. protected

B. private

C. static

D. dynamic

Right Answer is: C

SOLUTION

One of the main characteristic of constructor is that it cannot be static.


Q. 190068 Default Constructor is also called as


A. copy constructor.

B. special member function.

C. zero-argument constructor.

D. new operator.

Right Answer is: C

SOLUTION

Default constructor does not accept any argument. Thats why it is also called as zero-argument constructor.


Q. 190069 Copy constructor


A. takes one argument.

B. uses different name from the class name.

C. must be a private member.

D. does not allow overloading.

Right Answer is: A

SOLUTION

Copy constructor takes one argument. Also called as one argument constructor. The main use of copy constructor is to initialize the objects while in creation. Also it is used to copy values of one object into another object.


Q. 190070 Initializing variables during runtime is


A. initializing references.

B. initializing constants.

C. compile time initialization.

D. dynamic initialization.

Right Answer is: D

SOLUTION

In dynamic initialization, values are assigned to the variables during run-time.


Q. 190071 Give the output of the given code -
    
class A{      int a,b,c; public:   A(int x, int y){               a=x;               b=y*2;   }   void disp1(){             c=a*b;             cout<< c;   }   A(){               a=12;               b=21;   } void disp2(){               c=a+b;                cout<< c;   } }; void main(){    A a(11,2);    a.disp1();    A a1;    a1.disp2(); }  


A. 44 33

B. 22 33

C. 11 33

D. 11 12

Right Answer is: A

SOLUTION

when disp1() function will be called, it will print multiplication of a and b, i.e., 11 and 4 respectively. So 44 will be printed.   when disp2() function will be called, it will print addition of a and b, i.e. , 12 and 21 respectively. So 33 will be displayed.  


Q. 190072 What will be the output of the given code -

                class A{        int a,b,c; public:   A(int x, int y){               a=x;               b=y*2;   }   void disp(){          c=a*b;          cout<< c;   } }; void main(){    A obj(11,22);    obj.disp(); }        


A. 1122

B. 484

C. 55

D. 2211

Right Answer is: B

SOLUTION

The variables a and b are initialized with 11 and 44 respectively. When object obj called the function disp(), it will display the multiplication of a and b, i.e., 484 


Q. 190073 In object oriented programming, the concept which is illustrated in Function 3 is called: class Travel { int PlaceCode; char Place[20]; float Charges; public: Travel( ) //Function 1 { PlaceCode=1;strcpy(Place,"DELHI");Charges=1000; } void TravelPlan(float C ) // Function 2 { cout<


A. Destructor Overloading

B. Destructor

C. Constructor

D. Constructor Overloading

Right Answer is: B

SOLUTION

Function 3 : Destructor
A destructor is called or invoked when an object of that class is destroyed. When a variable goes out of scope or a dynamically allocated variable is explicitly deleted using the delete keyword, the class destructor is called to help clean up the  class  before it is removed from the memory.  


Q. 190074 What are static data members?
Right Answer is:

SOLUTION

Static data members are class variables. Only one copy of the static data member is maintained and this is shared by all the objects. They are visible only within the class but their lifetime is the entire program. They can be accessed in all member functions.


Q. 190075 Identify errors in the following code :     

    class X{int a;          static int b;        public:          void f1(void) {              a=10;              b=20;          }          static void f2(void) {              cout<< a<< b;          }     };     X ob;     void main(){          ob.f1();          ob.f2();     }
Right Answer is:

SOLUTION

Function f2( ) is a static member function. It is accessing a, which is a non-static data member. Secondly, static data member b is not defined outside the class. 


Q. 190076 Define a class to store bank account details of customers based on the specifications given :    
    Data members:     Account Number                integer     Name of Account Holder  20 characters     Type of Account                 1 character (S - Savings C - Current)     Balance                                float       Member Functions :     init() -  to give data members initial values     disp() - to display account details     deposit() - to deposit money     draw() - to withdraw money, leaving at least Rs.1000 as balance     
All functions must be defined.  
Right Answer is:

SOLUTION

      class AC{ int acc_no;

         char name[20];

         char type;

         float balance;

      public:

        void init(){

             cout<<"nEnter Account No.";

             cin>>acc_no;

             cout<<"nName of Account Holder : ";

             cin.getline(name,20);

             cout<<"nType = S or C ";

             cin>>type;

             type=toupper(type);

             cout<<"nBalance : ";

             cin>>balance;

         }

         void disp(){

             cout<<"nAccount No."<< acc_no;

             cout<<"nName of Account Holder : "<< name;

             cout<<"nType = "<< type;

             cout<<"nBalance : "<< balance;

         }

         void deposit(float d){

             balance = balance + d;

             cout<<"nBalance Updated";

         }

         void draw(float d){

             if(balance - d>=1000)

             { balance=balance-d;

             cout<<"nBalance Updated";

            }

             else

             cout<<"Leaves insufficient balance";

         }

    };                              


Q. 190077 Look at the following code fragment –          int a,b;      class A{int x;          float y;          char z;          void initval(void){              x=0;              y=0.0;              z='0';          }        public:              int a;              int b;              void sums(int n){                  cout<<"Sum1="<< (x + y)*n;                  cout<<"Sum2="<< (a+b)*n;              }              void fn(int i, float j, char k);          };          void A::fn(int i,float j,char k){                 x=i;y=j;z=k;          }     A ob;     void disp(void){          int a=210;          cout<< a;     }       Which variables can be accessed by the following functions?     sums(),   fn(),   disp()
Right Answer is:

SOLUTION

sums( ) can access A::x,   A::y,  A::z,  A::a,  A::b,  ::a,  ::b  


fn( ) can access   A::x,   A::y,  A::z,  A::a,  A::b,  ::a,  ::b


disp( ) can access    ::a,  ::b,   local variable a,   A::a and   A::b through ob.


Q. 190078 Define a class Book with the following specifications :   
        Private members –         book_code        integer         title                    20 characters         author               20 characters         Price                   integer           Public members –         getbook()     –     function that accepts book details         buybook()    –     function that accepts no. of copies to buy and  displays total amount to be paid.         
Function definitions must be given.  
Right Answer is:

SOLUTION

          class Book{

             int book_code;

             char title[20];

             char author[20];

             int Price;

           public:

             void getbook(){

                 cout << "nEnter Book Code : ";

                 cin >> book_code;

                 cout << "nBook Title : ";

                 cin.getline(title,20);

                 cout << "nBook Author : ";

                 cin.getline(author,20);

                 cout << "nPrice : ";

                 cin >> Price;

             }

             void buybook(){

                 int c;

                 cout << "nHow many copies to buy? ";

                 cin >> c;

                 cout << "nPay Rs."<< price*c;

             }

        };                         

 


Q. 190079 Can a member function and a non member function be given the same name? Support your answer with an explanation.
Right Answer is:

SOLUTION

Yes they can.

The scope of a member function is restricted to the class. It may be called by other members of the class or if called in a non member function, it is linked with an object of the class.

Non-member functions have no such restrictions. If defined globally, they have program scope and can be called from any function. If defined inside a function, they have scope local to that function.


Q. 190080 How is memory allocated for a class and its objects?
Right Answer is:

SOLUTION

The class definition gives the data members and member functions. Based on the class definition, objects are declared. Each object will have its own set of data members but there will be one set of functions accessed by all the objects. So, when the class is defined, memory is allocated for member functions and they are stored there. When objects are declared, separate memory space is allocated for each object, sufficient to accommodate all its data members.


Q. 190081 How are member functions different from non member functions?
Right Answer is:

SOLUTION

1. Member functions can access private and protected members of the class while non member functions have access to only public members and that too, through the objects.
2. Member functions don't have scope outside the class while non member functions have scope outside the class.

 


Q. 190082 State the advantages of inline functions?
Right Answer is:

SOLUTION

Advantages of inline function are:-
1) It does not require function calling overhead.
2) It also save overhead of variables push/pop on the stack, while function calling.
3) It also save overhead of return call from a function.
4) It increases locality of reference by utilizing instruction cache.
5) After in-lining compiler can also apply intraprocedural optmization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..


Q. 190083 A dining hall can accommodate only 50 guests. Define a class to store seat number and name of the guests who are seated on first come first seated basis. Define functions to display details of any seat number and to display the current seating situation. Write a program to show the working of this class.
Right Answer is:

SOLUTION

    #include < iostream.h>
   
#include < ctype.h>

    class DH{int seat;

         char name[20];

         static int taken;

      public:

         void getdata(){

             taken++;

             cout<<"nSeat Number = "<< taken;

             cout<<"nName = ";

             cin.getline(name,20);

             seat=taken;

         }

         static void status(){

             cout<<"nTotal Seats = 50";

             cout<<"nSeats Taken = "<< taken;

             cout<<"n Available = "<< 50-taken;

         }

         void disp( ){

             cout<<"nSeat No. = "<< seat;

             cout<<"nName = "<< name;

         }

    };

    DH ob[50];

    int DH::taken;

    void main(){

         int i,s;

         char mo='Y',c;

         for(i=0;mo=='Y' && i<50;i++){

             ob[i].getdata();

             DH::status();

             cout<<"nMore? (Y/N) ";

             cin>>mo;

             mo=toupper(mo);

             c=cin.get();

         }

         cout<<"Enter Seat No. to display";

         cin>>s;

         ob[s-1].disp();

    }             


Q. 190084 Describe the access specifiers of a class in brief.
Right Answer is:

SOLUTION

Three access specifiers are given by C++ :-

1. private : which disallow the accessibility of block outside the class
2. public : which allow the accessibility to outside of the class by any function
3. protected : which restrict the accessibility upto derived class only

With the proper use of access specifier the data can be hidden from unauthorized access.


Q. 190085 How does a class implement Data Hiding, Data Abstraction and Encapsulation?
Right Answer is:

SOLUTION

A class binds together data and functions in a single unit to enforce encapsulation.

The class represents essential features of the object only, thus implementing data abstraction.

In the class, members are classified as private, protected and public. Accessibility of the members depends on which category they are under. Private data are accessed by member functions of a class and its protected data are accessed by its derived classes only.Thus a class enforces data hiding.


Q. 190086 When can a function not be inline?
Right Answer is:

SOLUTION

Some situations where defining a function as inline will not work – 

        1. If the function returns a value and contains a loop, switch or goto statement.

        2. If the function does not return a value and contains a return statement.

        3. If the function contains static variables.

        4. If the function is recursive in nature.     


Q. 190087 The size of object of empty class is


A. 0 byte.

B. 1 byte.

C. 2 bytes.

D. 10 bytes.

Right Answer is: B

SOLUTION

The minimum amount of memory that can be reserved is 1 byte. Hence, the size of the object of empty class will be 1 byte.


Q. 190088 The member functions of a class access global data or global functions with the help of


A. ~ operator.

B. :: operator.

C. ^ operator.

D. & operator.

Right Answer is: B

SOLUTION

The way to access global data and functions is to use the scope resolution operator (::).


Q. 190089 An object can be created dynamically using the keyword


A. dynamic.

B. create.

C. new.

D. fresh.

Right Answer is: C

SOLUTION

The keyword 'new' is used to create an object dynamically. For Example : Sample p = new Sample; Here 'p' is an object of Sample type.


Q. 190090 Destructor can be overloaded for


A. only once.

B. twice.

C. zero times.

D. thrice.

Right Answer is: C

SOLUTION

Destructors cannot be overloaded. It does not make any sense in passing any arguments to the destructors.


Q. 190091 Constructors do not have a return type because


A. it is not a function.

B. it deallocates memory.

C. it is called whenever an object is deallocated.

D. it is called whenever an object gets invoked.

Right Answer is: D

SOLUTION

Constructors are called whenever an object gets invoked. While creating an object it is not required to return a value.


Q. 190092 The number of times an error value can be returned from a constructor is


A. one.

B. two.

C. zero.

D. infinite.

Right Answer is: C

SOLUTION

An error value can never be returned from a constructor. However, when a runtime error occurs, an exception can be thrown from within a constructor.


Q. 190093 To make functions inline, one should use the keyword


A. private.

B. protected.

C. inline.

D. inherited.

Right Answer is: C

SOLUTION

Functions defined in class are by default inline and those defined outside are not inline. Functions can be made inline by writing the keyword inline in function’s definition.


Q. 190094 Destructors:


A. Cannot take arguments

B. Can return only ~ type

C. Cannot be private

D. Can be inherited

Right Answer is: A

SOLUTION

The desructor has specific naming rules as follows:
The destructor has the same name as that of the class, preceded by tilde (~).
The destructor cannot take arguments. 
The destructor has no return type.


Q. 190095 Constructors:


A. Cannot take arguments

B. Cannot have default values

C. Can be inherited

D. Have no return type

Right Answer is: D

SOLUTION

C++ constructors have an implicit return type used by the compiler but we can't declare a return type or return a value.


Q. 190096 The statement given below is an example of:
A(int a, char b)
{
……….
}


A. Copy constructor

B. Default constructor

C. Parameterized constructor

D. Temporary constructor

Right Answer is: C

SOLUTION

Parameterized constructor contains arguments to assign the values to the class elements.


Q. 190097 Which of the following is correct about the code given below?
#include < iostream.h >
class ExtraMarks
{
public:
ExtraMarks()
{
cout<< "Extra";
}
~ExtraMarks()
{
cout<< "Marks";
}
};
int main()
{
ExtraMarks objMarks;
return 0;
}


A. The program will print the output Extra.

B. The program will print the output Marks.

C. The program will print the output ExtraMarks.

D. The program will report compile-time error. 

Right Answer is: C

SOLUTION

As the object is created, it invokes the constructor and then the destructor followed by it. Output:


Q. 190098 A temporary instance


A. is deleted when no longer needed.

B. calls the default constructor.

C. is a temporary class.

D. is created during dynamic initialization.

Right Answer is: A

SOLUTION

A temporary instance is the one that lives in the memory as long as it is being used or referenced in an expression and after this it dies. The temporary instances are anonymous i.e. they do not bear a name.


Q. 190099 The output of the following program would be:


A. 1234

B. 12344321

C. 4321

D. 12341234

Right Answer is: B

SOLUTION

The program will start to print 1 from because the variable is initialized to zero as val = 0. Then, it is incremented by 1, each time constructor is called, till val = 4. After that, destructor is called, which decrements val from 4 to 1. Output:


Q. 190100 A function returning an object


A. uses default arguments.

B. calls the copy constructor.

C. calls the default constructor.

D. simply returns control.

Right Answer is: B

SOLUTION

When an object is returned by a function the copy constructor is called to create a copy of the value returned by the function.


PreviousNext