Object Oriented Programming using C
Object Oriented Programming using C
1. Which was the first purely object oriented programming language developed?
a) Java
b) C++
c) SmallTalk
d) Kotlin
View Answer
Answer: c
Explanation: SmallTalk was the first programming language developed which was purely
object oriented. It was developed by Alan Kay. OOP concept came into the picture in
1970’s.
6. Pure OOP can be implemented without using class in a program. (True or False)
a) True
b) False
View Answer
Answer: b
Explanation: It’s false because for a program to be pure OO, everything must be written
inside classes. If this rule is violated, the program can’t be labelled as purely OO.
3. What is default access specifier for data members or member functions declared within a
class without any specifier, in C++?
a) Private
b) Protected
c) Public
d) Depends on compiler
View Answer
Answer: a
Explanation: The data members and member functions are Private by default in C++
classes, if none of the access specifier is used. It is actually made to increase the privacy of
data.
advertisement
4. Which is most appropriate comment on following class definition?
class Student
{
int a;
public : float a;
};
a) 20
b) 22
c) 24
d) 28
View Answer
Answer: c
Explanation: The size of any object of student class will be of size 4+20=24, because static
members are not really considered as property of a single object. So static variables size
will not be added.
9. If a local class is defined in a function, which of the following is true for an object of that
class?
a) Object is accessible outside the function
b) Object can be declared inside any other function
c) Object can be used to call other class members
d) Object can be used/accessed/declared locally in that function
View Answer
Answer: d
Explanation: For an object which belongs to a local class, it is mandatory to declare and use
the object within the function because the class is accessible locally within the class only.
14. Which among following is correct for initializing the class below?
class student{
int marks;
int cgpa;
public: student(int i, int j){
marks=I;
cgpa=j
}
};
15. Object can’t be used with pointers because they belong to user defined class, and
compiler can’t decide the type of data may be used inside the class.
a) True
b) False
View Answer
Answer: b
Explanation: The explanation given is wrong because object can always be used with
pointers like with any other variables. Compiler doesn’t have to know the structure of the
class to use a pointer because the pointers only points to a memory address/stores that
address
2. If a function can perform more than 1 type of tasks, where the function name remains
same, which feature of OOP is used here?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
View Answer
Answer: c
Explanation: For the feature given above, the OOP feature used is Polymorphism. Example
of polymorphism in real life is a kid, who can be a student, a son, a brother depending on
where he is.
3. If different properties and functions of a real world entity is grouped or embedded into a
single element, what is it called in OOP language?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
View Answer
Answer: d
Explanation: It is Encapsulation, which groups different properties and functions of a real
world entity into single element. Abstraction, on other hand, is hiding of functional or exact
working of codes and showing only the things which are required by the user.
advertisement
4. Which of the following is not a feature of pure OOP?
a) Classes must be used
b) Inheritance
c) Data may/may not be declared using object
d) Functions Overloading
View Answer
Answer: c
Explanation: Data must be declared using objects. Object usage is mandatory because it in
turn calls its constructors, which in turn must have a class defined. If object is not used, it is
a violation of pure OOP concept.
a) Inheritance
b) Polymorphism
c) Inheritance and polymorphism
d) Encapsulation and Inheritance
View Answer
Answer: d
Explanation: Encapsulation is indicated by use of classes. Inheritance is shown by inheriting
the student class into topper class. Polymorphism is not shown here because we have
defined the constructor in the topper class but that doesn’t mean that default constructor is
overloaded.
8. How many basic features of OOP are required for a programming language to be purely
OOP?
a) 7
b) 6
c) 5
d) 4
View Answer
Answer: a
Explanation: There are 7 basic features that define whether a programing language is pure
OOP or not. The 4 basic features are inheritance, polymorphism, encapsulation and
abstraction. Further, one is, object use is must, secondly, message passing and lastly,
Dynamic binding.
9. The feature by which one object can interact with another object is _____________
a) Data transfer
b) Data Binding
c) Message Passing
d) Message reading
View Answer
Answer: c
Explanation: The interaction between two object is called the message passing feature.
Data transfer is not a feature of OOP. Also, message reading is not a feature of OOP.
11. Which feature in OOP is used to allocate additional function to a predefined operator in
any language?
a) Operator Overloading
b) Function Overloading
c) Operator Overriding
d) Function Overriding
View Answer
Answer: a
Explanation: The feature is operator overloading. There is not a feature named operator
overriding specifically. Function overloading and overriding doesn’t give addition function to
any operator.
14. Which among the following, for a pure OOP language, is true?
a) The language should follow 3 or more features of OOP
b) The language should follow at least 1 feature of OOP
c) The language must follow only 3 features of OOP
d) The language must follow all the rules of OOP
View Answer
Answer: d
Explanation: The language must follow all the rules of OOP to be called a purely OOP
language. Even if a single OOP feature is not followed, then it’s known to be a partially OOP
language.
3. Which among the following is the language which supports classes but not
polymorphism?
a) SmallTalk
b) Java
c) C++
d) Ada
View Answer
Answer: d
Explanation: Ada is the language which supports the concept of classes but doesn’t support
the polymorphism feature. It is an object-based programming language. Note that it’s not an
OOP language.
advertisement
4. If same message is passed to objects of several different classes and all of those can
respond in a different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
View Answer
Answer: c
Explanation: The feature defined in question defines polymorphism features. Here the
different objects are capable of responding to the same message in different ways, hence
polymorphism.
class student
{
public : int marks;
void disp()
{
cout<<”its base class”
};
class topper:public student
{
public :
void disp()
{
cout<<”Its derived class”;
}
}
void main() { student s; topper t;
s.disp();
t.disp();
}
a) Its base classIts derived class
b) Its base class Its derived class
c) Its derived classIts base class
d) Its derived class Its base class
View Answer
Answer: a
Explanation: You need to focus on how the output is going to be shown, no space will be
given after first message from base class. And then the message from derived class will be
printed. Function disp() in base class overrides the function of base class being derived.
class education
{
char name[10];
public : disp()
{
cout<<”Its education system”;
}
class school:public education
{
public: void dsip()
{
cout<<”Its school education system”;
}
};
void main()
{
school s;
s.disp();
}
}
a) Its school education system
b) Its education system
c) Its school education systemIts education system
d) Its education systemIts school education system
View Answer
Answer: a
Explanation: Notice that the function name in derived class is different from the function
name in base class. Hence when we call the disp() function, base class function is
executed. No polymorphism is used here.
15. If 2 classes derive one base class and redefine a function of base class, also overload
some operators inside class body. Among these two things of function and operator
overloading, where is polymorphism used?
a) Function overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either function overloading or operator overloading because polymorphism can be
applied only once in a program
View Answer
Answer: c
Explanation: Both of them are using polymorphism. It is not necessary that polymorphism
can be used only once in a program, it can be used anywhere, any number of times in a
single program.
2. If data members are private, what can we do to access them from the class object?
a) Create public member functions to access those data members
b) Create private member functions to access those data members
c) Create protected member functions to access those data members
d) Private data members can never be accessed from outside the class
View Answer
Answer: a
Explanation: We can define public member functions to access those private data members
and get their value for use or alteration. They can’t be accessed directly but is possible to
be access using member functions. This is done to ensure that the private data doesn’t get
modified accidentally.
advertisement
9. Which among the following violates the principle of encapsulation almost always?
a) Local variables
b) Global variables
c) Public variables
d) Array variables
View Answer
Answer: b
Explanation: Global variables almost always violates the principles of encapsulation.
Encapsulation says the data should be accessed only by required set of elements. But
global variable is accessible everywhere, also it is most prone to changes. It doesn’t hide
the internal working of program.
10. Which among the following would destroy the encapsulation mechanism if it was
allowed in programming?
a) Using access declaration for private members of base class
b) Using access declaration for public members of base class
c) Using access declaration for local variable of main() function
d) Using access declaration for global variables
View Answer
Answer: a
Explanation: If using access declaration for private members of base class was allowed in
programming, it would have destroyed whole concept of encapsulation. As if it was
possible, any class which gets inherited privately, would have been able to inherit the
private members of base class, and hence could access each and every member of base
class.
11. Which among the following can be a concept against encapsulation rules?
a) Using function pointers
b) Using char* string pointer to be passed to non-member function
c) Using object array
d) Using any kind of pointer/array address in passing to another function
View Answer
Answer: d
Explanation: If we use any kind of array or pointer as data member which should not be
changed, but in some case its address is passed to some other function or similar variable.
There are chances to modify its whole data easily. Hence Against encapsulation.
12. Consider the following code and select the correct option.
class student
{
int marks;
public : int* fun()
{
return &marks;
}
};
main()
{
student s;
int *ptr=c.fun();
return 0;
}
a) This code is good to go
b) This code may result in undesirable conditions
c) This code will generate error
d) This code violates encapsulation
View Answer
Answer: d
Explanation: This code violates the encapsulation. By this code we can get the address of
the private member of the class, hence we can change the value of private member, which
is against the rules.
class hero
{
char name[10];
public : void disp()
{
cout<<name;
}
};
a) This maintains encapsulation
b) This code doesn’t maintain encapsulation
c) This code is vulnerable
d) This code gives error
View Answer
Answer: a
Explanation: This code maintains encapsulation. Here the private member is kept private.
Outside code can’t access the private members of class. Only objects of this class will be
able to access the public member function at maximum.
advertisement
7. Which among the following can be viewed as combination of abstraction of data and
code.
a) Class
b) Object
c) Inheritance
d) Interfaces
View Answer
Answer: b
Explanation: Object can be viewed as abstraction of data and code. It uses data members
and their functioning as data abstraction. Code abstraction as use of object of inbuilt class.
12. If two classes combine some private data members and provides public member
functions to access and manipulate those data members. Where is abstraction used?
a) Using private access specifier for data members
b) Using class concept with both data members and member functions
c) Using public member functions to access and manipulate the data members
d) Data is not sufficient to decide what is being used
View Answer
Answer: c
Explanation: It is the concept of hiding program complexity and actual working in
background. Hence use of public member functions illustrates abstraction here.
13. A phone is made up of many components like motherboard, camera, sensors and etc. If
the processor represents all the functioning of phone, display shows the display only, and
the phone is represented as a whole. Which among the following have highest level of
abstraction?
a) Motherboard
b) Display
c) Camera
d) Phone
View Answer
Answer: d
Explanation: Phone as a whole have the highest level of abstraction. This is because the
phone being a single unit represents the whole system. Whereas motherboard, display and
camera are its components.
advertisement
4. In which access should a constructor be defined, so that object of the class can be
created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer
Answer: a
Explanation: Constructor function should be available to all the parts of program where the
object is to be created. Hence it is advised to define it in public access, so that any other
function is able to create objects.
5. How many types of constructors are available for use in general (with respect to
parameters)?
a) 2
b) 3
c) 4
d) 5
View Answer
Answer: a
Explanation: Two types of constructors are defined generally, namely, default constructor
and parameterized constructor. Default constructor is not necessary to be defined always.
8. If class C inherits class B. And B has inherited class A. Then while creating the object of
class C, what will be the sequence of constructors getting called?
a) Constructor of C then B, finally of A
b) Constructor of A then C, finally of B
c) Constructor of C then A, finally B
d) Constructor of A then B, finally C
View Answer
Answer: d
Explanation: While creating the object of class C, its constructor would be called by default.
But, if the class is inheriting some other class, firstly the parent class constructor will be
called so that all the data is initialized that is being inherited.
9. In multiple inheritance, if class C inherits two classes A and B as follows, which class
constructor will be called first?
class A{ };
class B{ };
class C: public A, public B{ };
a) A()
b) B()
c) C()
d) Can’t be determined
View Answer
Answer: a
Explanation: Constructor of class A will be called first. This is because the constructors in
multiple inheritance are called in the sequence in which they are written to be inherited.
Here A is written first, hence it is called first.
class student
{
int marks;
};
student s1, s2, s3;
a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) all are created at same time
View Answer
Answer: a
Explanation: The objects are created in the sequence of how they are written. This happens
because the constructors are called in the sequence of how the objects are mentioned. This
is done in sequence.
Implicit calls: The compiler might implicitly call a default constructor (if available) when you
declare an object without arguments. However, these are not temporary instances as they
are assigned to a variable.
Explicit calls: You explicitly use the new keyword followed by the constructor name and
arguments to create a new object. This is the most common way to create temporary
objects.
Copy constructors: These constructors are used to create a copy of an existing object.
They are not typically used for temporary instances.
Therefore, only an explicit call to a constructor allows you to create a temporary instance
that isn’t assigned to a variable and exists momentarily within the expression or statement.
14. Which among the following is correct for the class defined below?
class student
{
int marks;
public: student(){}
student(int x)
{
marks=x;
}
};
main()
{
student s1(100);
student s2();
student s3=100;
return 0;
}
a) Object s3, syntax error
b) Only object s1 and s2 will be created
c) Program runs and all objects are created
d) Program will give compile time error
View Answer
Answer: c
Explanation: It is a special case of constructor with only 1 argument. While calling a
constructor with one argument, you are actually implicitly creating a conversion from the
argument type to the type of class. Hence you can directly specify the value of that one
argument with assignment operator.
15. For constructor overloading, each constructor must differ in ___________ and
__________
a) Number of arguments and type of arguments
b) Number of arguments and return type
c) Return type and type of arguments
d) Return type and definition
View Answer
Answer: a
Explanation: Each constructor must differ in the number of arguments it accepts and the
type of arguments. This actually defines the constructor signature. This helps to remove the
ambiguity and define a unique constructor as required.
class student
{
int marks;
}
student s1;
student s2=2;
a) Object s1 should be passed with argument
b) Object s2 should not be declared
c) Object s2 will not be created, but program runs
d) Program gives compile time error
View Answer
Answer: d
Explanation: The object s2 can be assigned with one value only if a single argument
constructor is defined in class, but here, it can’t be done as no constructor is defined. Hence
every object must be declare or created without using arguments.
advertisement
15. Which among the following is correct, based on the given code below?
class student
{
int marks;
public : student()
{
cout<<”New student details can be added now”;
}
};
student s1;
a) Cout can’t be used inside the constructor
b) Constructor must contain only initializations
c) This program works fine
d) This program produces errors
View Answer
Answer: c
Explanation: This program will work fine. This is because it is not mandatory that a
constructor must contain only initialization only. If you want to perform a task on each
instance being created, that code can be written inside the constructor.
3. If two classes have exactly same data members and member function and only they
differ by class name. Can copy constructor be used to initialize one class object with
another class object?
a) Yes, possible
b) Yes, because the members are same
c) No, not possible
d) No, but possible if constructor is also same
View Answer
Answer: c
Explanation: The restriction for copy constructor is that it must be used with the object of
same class. Even if the classes are exactly same the constructor won’t be able to access all
the members of another class. Hence we can’t use object of another class for initialization.
advertisement
10. Out of memory error is given when the object _____________ to the copy constructor.
a) Is passed with & symbol
b) Is passed by reference
c) Is passed as <classname &obj>
d) Is not passed by reference
View Answer
Answer: d
Explanation: All the options given, directly or indirectly indicate that the object is being
passed by reference. And if object is not passed by reference then the out of memory error
is produced. Due to infinite constructor call of itself.
12. The deep copy is possible only with the help of __________
a) Implicit copy constructor
b) User defined copy constructor
c) Parameterized constructor
d) Default constructor
View Answer
Answer: b
Explanation: While using explicit copy constructor, the pointers of copied object point to the
intended memory location. This is assured since the programmers themselves manipulate
the addresses.
advertisement
5. Which constructor will be called from the object created in the code below?
8. Which among the following function can be used to call default constructor implicitly in
java?
a) this()
b) that()
c) super()
d) sub()
View Answer
Answer: a
Explanation: The function this() can be used to call the default constructor from inside any
other constructor. This helps to further reuse the code and not to write the redundant data in
all the constructors.
12. Which constructor will be called from the object obj2 in the following program?
class A
{
int i;
A()
{
i=0;
}
A(int x)
{
i=x+1;
}
A(int y, int x)
{
i=x+y;
}
};
A obj1(10);
A obj2(10,20);
A obj3;
a) A(int x)
b) A(int y)
c) A(int y, int x)
d) A(int y; int x)
View Answer
Answer: c
Explanation: The two argument constructor will be called as we are passing 2 arguments to
the object while creation. The arguments will be passed together and hence compiler
resolves that two argument constructor have to be called.
13. What are we only create an object but don’t call any constructor for it in java?
a) Implicit constructor will be called
b) Object is initialized to some null values
c) Object is not created
d) Object is created but points to null
View Answer
Answer: d
Explanation: The object becomes a reference object which can be initialized will another
object. Then this object will indirectly become another name of the object being assigned. If
not assigned, it simply points to null address.
advertisement
advertisement
4. If in multiple 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?
a) ~C() then ~B() then ~A()
b) ~B() then ~C() then ~A()
c) ~A() then ~B() then ~C()
d) ~C() then ~A() then ~B()
View Answer
Answer: a
Explanation: The destructors are always called in the reverse order of how the constructors
were called. Here class A constructor would have been created first if Class C object is
declared. Hence class A destructor is called at last.
5. Choose the correct sequence of destructors being called for the following code.
10. Global destructors execute in ___________ order after main function is terminated.
a) Sequential
b) Random
c) Reverse
d) Depending on priority
View Answer
Answer: c
Explanation: The destructors are always called in reverse order no matter which destructor
it is. This is done to ensure that all the resources are able to get free. And no resource is
kept busy.
12. Which among the following is correct for the destructors concept?
a) Destructors can be overloaded
b) Destructors can have only one parameter at maximum
c) Destructors are always called after object goes out of scope
d) There can be only one destructor in a class
View Answer
Answer: d
Explanation: This is so because the destructors can’t be overloaded. And the destructor
must have the same name as that of class with a tilde symbol preceding the name of the
destructor. Hence there can be only one destructor in a class. Since more than one function
with same name and signature can’t be present in same scope.
13. Which class destructor will be called first, when following code go out of scope?
class A{ };
class B{ };
class C: public B{ };
A a;
B b;
C c;
a) ~A()
b) ~B()
c) ~C()
d) ~B() and ~C()
View Answer
Answer: c
Explanation: The constructor that would have created at last, its destructor will be called first
when the code goes out of scope. This will help the program to manage the resources more
efficiently.
14. When an object is passed to a function, its copy is made in the function and then
______________
a) The destructor of the copy is called when function is returned
b) The destructor is never called in this case
c) The destructor is called but it is always implicit
d) The destructor must be user defined
View Answer
Answer: a
Explanation: When an object is passed to a function, its copy is made in the function. This
copy acts as a real object till the function is live. When the function is returned, the copy’s
destructor is called to free the resources held by it.