Object Oriented MCQ Set 1
1. Which among the following best describes a nested class?
a) Class inside a class
b) Class inside a function
c) Class inside a package
d) Class inside a structure
Answer
Answer: a [Reason:] If a class is defined inside another class, the inner class is termed as nested class. The inner class is local to the enclosing class. Scope matters a lot here.
2. Which feature of OOP reduces the use of nested classes?
a) Encapsulation
b) Inheritance
c) Binding
d) Abstraction
Answer
Answer: b [Reason:] Using inheritance we can have the security of the class being inherited. The sub class can access the members of parent class. And have more feature than a nested class being used.
3. How many categories are nested classes divided into?
a) 2
b) 3
c) 4
d) 5
Answer
Answer: a [Reason:] The nested classes are divided into two main categories. Namely, Static and non-static. The categories define how the classes can be used inside another class.
4. Non-static nested classes have access to _______ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) All the members
Answer
Answer: d [Reason:] The non-static nested class can access all the members of the enclosing class. All the data members and member functions can be accessed from the nested class. Even if the members are private, they can be accessed.
5. Static nested classes doesn’t have access to _____ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) Any other members
Answer
Answer: d [Reason:] The static nested class doesn’t have access to any other members of the enclosing class. This is a rule that is made to ensure that only the data which can be common to all the object is being accessed by the static nested class.
6. The nested class can be declared ___
a) Public
b) Private
c) Protected
d) Public, Protected, Private or Package private
Answer
Answer: d [Reason:] The nested class can be declared with any specifier, unlike the outer classes which can only be declared public or package private. This is a flexibility given for the nested class being a member of enclosing class.
7. Use of nested class ______ encapsulation.
a) Increases
b) Decreases
c) Doesn’t affect
d) Slightly decreases
Answer
Answer: a [Reason:] The use of nested class increases encapsulation as the inner class is getting even more grouped into the enclosing class. Firstly the class encapsulate the data, having nested classes can increase the encapsulation even further.
8. Which among the following is correct advantage/disadvantage of nested classes?
a) Makes the code more complex
b) Makes the code unreadable
c) Makes the code efficient and readable
d) Makes the code multithreaded
Answer
Answer: c [Reason:] The use of nested classes make the code more streamed towards a single concept. This allows to group the most similar and related classes together and makes it even more efficient and readable.
9. How to access static nested classes?
a) OuterClass.StaticNestedClass
b) OuterClass->StaticNestedClass
c) OuterClass(StaticNestedClass)
d) OuterClass[StaticNestedClass].
Answer
Answer: a [Reason:] Like any other member of the class, the static nested class uses the dot operator to be accessed. The reason behind is, the static classes can’t work with instances, hence we use enclosing class name to access static nested class.
10. A nested class can have its own static members.
a) True
b) False
Answer
Answer: b [Reason:] The nested classes are associated with the object of the enclosing class. Hence have direct access to the members of that object. Hence the inner class can’t have any static members of its own. Otherwise the rule of static members would be violated using enclosing class instance.
11. How to create object of the inner class?
a) OuterClass.InnerClass innerObject = outerObject.new InnerClass();
b) OuterClass.InnerClass innerObject = new InnerClass();
c) InnerClass innerObject = outerObject.new InnerClass();
d) OuterClass.InnerClass = outerObject.new InnerClass();
Answer
Answer: a [Reason:] An instance of inner class can exist only within instance of outer class. To instantiate the inner class, one must instantiate the outer class first. This can be done by the correct syntax above.
12. What will be the output of the following code?
public class Test { public int a=0; class innerClass { public int a=1; void innermethod(int x) { System.out.println(“value of x = ” + x); System.out.println(“value of this.x = ” + this.x); System.out.println(“value of Test.this.x = ” + Test.T=this.x); } } } public static void main( String args[] ) { Test t=new Test(); Test.innerClass im=t.new innerClass(); im.innermethod(55); }
a) value of x = 55
value of this.x = 0
value of Test.this.x = 1
b) value of x = 1
value of this.x = 0
value of Test.this.x = 55
c) value of x = 55
value of this.x = 1
value of Test.this.x = 0
d) value of x = 0
value of this.x = 55
value of Test.this.x = 1
Answer
Answer: c [Reason:] The variable x denotes the parameter of the function. And this.x is the variable of the inner class. Test.this.x is the variable of the outer class. Hence we get this output.
13. Instance of inner class can exist only ___ enclosing class.
a) Within
b) Outside
c) Private to
d) Public to
Answer
Answer: a [Reason:] The class defined inside another class is local to the enclosing class. This means that the instance of inner class will not be valid outside the enclosing class. There is no restriction for instance to be private or public always.
14. If a declaration of a member in inner class has the same name as that in the outer class, then ____ enclosing scope.
a) Outer declaration shadows inner declaration in
b) Inner declaration shadows outer declaration in
c) Declaration gives compile time error
d) Declaration gives run time error
Answer
Answer: b [Reason:] The inner class will have more preference for its local members than those of the enclosing members. Hence it will shadow the enclosing class members. This process is known as shadowing.
15. A static nested class is _______ class in behavior that is nested in another ___ class.
a) Top level, top level
b) Top level, low level
c) Low level, top level
d) Low level, low level
Answer
Answer: a [Reason:] Top level class encloses the other classes or have same preference as that of other top level classes. Having a class inside the top level class is indirectly having a top level class which higher degree of encapsulation.
Object Oriented MCQ Set 2
1. What is array of objects?
a) An array of instances of class represented by single name
b) An array of instances of class represented by more than one name
c) An array of instances which have more than 2 instances
d) An array of instances which have different types
Answer
Answer: a [Reason:] The array of objects an array of instances of a class. The array is represented by a single name. The array name is itself a pointer. Array name represents the first object.
2. Which among the following is a mandatory condition for array of objects?
a) All the objects should be of different class
b) All the objects should be of same program classes
c) All the objects should be of same class
d) All the objects should have different data
Answer
Answer: c [Reason:] The objects of an array must be of same class. This is mandatory because array is set of same type of elements. The objects of same class are considered to be of same type.
3. What is the type of the elements of array of objects?
a) Class
b) Void
c) String
d) Null
Answer
Answer: a [Reason:] The class itself is the type of elements of array of objects. All the objects possess the same properties. Like any other primitive data type, the objects are of their respective class type.
4. If array of objects is declared as given below, which is the limitation on objects?
Class_name arrayName[size];
a) The objects will have same values
b) The objects will not be initialized individually
c) The objects can never be initialized
d) The objects will have same data
Answer
Answer: b [Reason:] If the syntax given, is used to declare array of objects, then the objects can’t be initialized individually. All the objects will have to be initialized after this declaration.
5. Which is the condition that must be followed if the array of objects is declared without initialization, only with size of array?
a) The class should have separate constructor for each object
b) The class must have no constructors
c) The class should not have any member function
d) The class must have a default or zero argument constructor
Answer
Answer: d [Reason:] The class must have a default/zero argument constructor. Since the declaration is done by only specifying the size of array, the class must have default a construct to be called by default to reserve memory for each object. Also, we can’t specify the arguments in this type of declaration hence the class should provide a default initialization.
6. When are the array of objects without any initialization useful?
a) When object data is not required just after the declaration
b) When initialization of object data is to be made by the compiler
c) When object data doesn’t matter in the program
d) When the object should contain garbage data
Answer
Answer: a [Reason:] Sometimes the object data is not mandatory to be used just after the declaration or may be the program requires the data to be updated according to what user inputs. Hence only declaration us also useful.
7. If constructor arguments are passed to objects of array then ______ if the constructors are overloaded.
a) It is mandatory to pass same number of arguments to all the objects
b) It is mandatory to pass same type of arguments to all the objects
c) It is not mandatory to call same constructor for all the objects
d) It is mandatory to call same constructor for all the constructors
Answer
Answer: c [Reason:] It is not mandatory to call the same constructor for all the objects in an array if initialized with the declaration. The objects can be passed with different set of arguments in the same syntax, separated by commas.
8. How the objects of array can be denoted?
a) Indices
b) Name
c) Random numbers
d) Alphabets
Answer
Answer: a [Reason:] Different objects in an array can be denoted with the indices of array. The first object is denoted by 0. And the further indices denote the next objects in sequence of array.
9. The objects in an object array _______
a) Can be created without use of constructor
b) Can be created without calling default constructor
c) Can’t be created with use of constructor
d) Can’t be created without calling default constructor
Answer
Answer: b [Reason:] The objects need some constructor to get the memory spaced reserved for those. If the default constructor is not used then we can use some arguments constructor which will reserve the memory for the objects. The objects can be passed with constructor arguments during declaration.
10. The Object array is created in _____
a) Heap memory
b) Stack memory
c) HDD
d) ROM
Answer
Answer: a [Reason:] If the object arrays are declared dynamically, then the memory will be reserved on heap. The memory for objects will be on stack only if some constructor or some call and return tasks are happening. The program doesn’t run on HDD and ROM is not used for execution of programs.
11. If an array of objects is of size 10 and a data value have to be retrieved from 5th object then ____ syntax should be used.
a) Array_Name[4].data_variable_name;
b) Data_Type Array_Name[4].data_variable_name;
c) Array_Name[4].data_variable_name.value;
d) Array_Name[4].data_variable_name(value);
Answer
Answer: a [Reason:] The array name with the index of fifth element is called, i.e. index 4. Then the dot operator is used to access the data member of that object. This Allows us to access the data members of all the objects in an object array.
12. Can we have two dimensional object array?
a) Yes, always
b) Yes, only if primitive type array
c) No, since two indices are impossible
d) No, never
Answer
Answer: a [Reason:] A two dimensional array can always be created. There is no rule that only primitive type objects can have more than one dimension. The object array can also be made 2 dimensional.
13. From which index does the array of objects start?
a) 0
b) 1
c) 2
d) 3
Answer
Answer: a [Reason:] The index must start from 0. The index ends at size – 1 index. This is because the index is always till n-1 where n is the total number of beads.
14. Two dimensional array can’t be initialized with the declaration.
a) True
b) False
Answer
Answer: b [Reason:] The two dimensional arrays can also be initialized using surly brackets. For each set, values in curly bracket. And then another bracket is added at first and end. This ensure that all the code belongs to the user.
15. Is an array of characters always a string?
a) Yes, always
b) Yes, if each character is terminated by null
c) No, since each character is terminated by null
d) No, never
Answer
Answer: d [Reason:] The character arrays are not same as string. The string once created then remains the same. The character array values may change.
Object Oriented MCQ Set 3
1. What is reference to an object?
a) It is address of an object
b) It is address of where the variables and methods of object are stored
c) It is pointer having address of an object
d) It is address of only variables and not the methods of an object
Answer
Answer: b [Reason:] Reference indicates the address where the object’s variables and methods are stored. It is not actual address of the object. This is done to directly use the variables and methods whenever required.
2. Whenever an object is assigned to a variable or passed to a method, ____
a) Actually the objects aren’t used
b) Actually only the objects are used
c) Actually a pointer to an object is used
d) Actually copy of object is used
Answer
Answer: a [Reason:] Whenever an object is assigned to a variable or passed to a method, we aren’t actually using objects there. Actually the reference to the objects is used. The reference makes a lot of difference as the main object may or may not get affected depending on the code.
3. Does use of object reference in assignment or passing means copy of the object is being used?
a) No, because the copy would create a new temporary variable
b) No, because the copy would not help to make changes to main object
c) Yes, because the reference directly means using address
d) Yes, because the reference directly means the constructors are involved
Answer
Answer: c [Reason:] We can’t say that the reference involves constructors in passing the objects to some method. The reference is used to denote the addresses and hence to point to the main object itself. There is no copy involved.
4. What will be the output of the following code?
import java.awt.Point; class Testing { public static void main(String[] args) { Point p1,p2; p1=new Point(100,100); p2=p1; p1.x=200; p1.y=200; System.out.println(“Point 1: ” + p1.x + ”, “ + p1.y); System.out.println(“Point 2: ” + p2.x + ”, “ + p2.y); } }
a) Point 1: 100, 100
Point 2: 200,200
b) Point 1: 200, 200
Point 2: 100,100
c) Point 1: 100, 100
Point 2: 100,100
d) Point 1: 200, 200
Point 2: 200,200
Answer
Answer: d [Reason:] The expected output would be like p2 with values 100, 100. But this is not the case. The tricky part is assignment used ( p2=p1; ). Here a reference is created from object p1 to p2, and not any new object that would copy p1’s values. Hence when we change the values of p1 object members. There changes are reflected to the object p2 also.
5. Is there any explicit use of pointers in java that would be applicable to objects?
a) Yes, we use reference for this purpose
b) Yes, we use java arrays for this purpose
c) No, implicit pointing is possible
d) No, direct class names should be used
Answer
Answer: c [Reason:] The question clearly asks if there is any explicit use of pointers related to objects. Pointers are not applicable in java first of all. Secondly, the pointing in java is achieved implicitly using the references and object arrays. Now first two options are wrong because they use implicit pointing, and not any explicit pointing.
6. Can a super class object give reference to a subclass method?
a) No, it is not possible
b) May be, it is possible
c) No, it’s not possible
d) No, It’s not possible in few cases only
Answer
Answer: c [Reason:] The object of a super class can never refer to methods of a subclass. Whereas vice versa is possible. If there is an overridden function in subclass, then the object of super class will execute the method of itself and not from the subclass.
7. What will be the output of the following code?
import java.awt.Point; class Testing { public static void main(String[] args) { Point t1,t2,t3; t1=new Point(100,100); t2=t1; t3=t1; t1.x=200; t1.y=200; t2.x=300; t3.y=500; System.out.println(“Point 1: ” + p1.x + ”, “ + p1.y); } }
a) Point 1: 200, 200
b) Point 1: 100,100
c) Point 1: 300, 300
d) Point 1: 300, 500
Answer
Answer: d [Reason:] When references are used, all the variables point to the same object. Whenever any of the variable changes any values, it will be reflected to all the variables pointing to the same object.
8. If a reference variable is declared final then, _____
a) It can never be reassigned to refer to a different object.
b) It can be assigned to refer to any object anytime
c) It can never be assigned with any object
d) It can be assigned with 2 or more objects simultaneously
Answer
Answer: a [Reason:] Since the variable is declared final. It will have a constant value throughout the program. It can refer to only one object at a time. And if it was made to refer to none of the object, it would have got no use.
9. Which of the members are referred by this pointer usually (Java)?
a) Members of class where this is used
b) Member of the parent class where this is used
c) Members that are passed as argument to the object
d) Pointers are not applicable in java
Answer
Answer: a [Reason:] We use this pointer to differentiate the members of the class where this is used to the other inherited or passed variables. The local variables are denoted with this. Or specifically the members of class only.
10. How to refer to method of nested class?
a) enclosingClassObject.innerClassObject.method();
b) innerClassObject.method();
c) method();
d) depends on where the method is being called
Answer
Answer: d [Reason:] This depends on where the method is being called. If the method is called inside the enclosing class itself. Then we can’t use object of enclosing class. If the method is being called within the inner class itself, then its object will also be of no use.
11. How many objects can be referenced from same variables?
a) One at a time
b) Many at a time
c) Many using array name
d) 7 at max at same time
Answer
Answer: a [Reason:] There should not be any confusion in how many references can be made from a single variable. A single variable can only point to one object at a time. Even if it’s an array, the name of array is used and is considered one object name only (representing first array element).
12. Java handles memory dynamically and references are deleted as soon as they are out of scope.
a) True
b) False
Answer
Answer: a [Reason:] In Java, it is inbuilt feature that it handles all the memory dynamically. It is not necessary to free or destroy all the references made from a function which is going out of scope. You can call destroy or free methods explicitly but there is no mandatory rule.
13. Which among the following is true?
a) Object referencing refers to methods address
b) Object referencing refers to variable of object
c) Object referencing points to same address, if assigned by variables
d) Object referencing is used to point methods
Answer
Answer: c [Reason:] The object referencing will point to the same address if variables are assigned. All the variables might have a different name but they will point to the same memory location. This is most basic concept of references.
14. Invoking a method on a particular object is ______ sending a message to that object.
a) Different from
b) Same as
c) Somewhat similar
d) Part of
Answer
Answer: b [Reason:] The methods invoked on a particular object is same as sending a message with same values to that object. Message would contain values in a particular format which can be used by the object. And calling a method would be just another way to do the same task.
15. Can reference to an object be returned from a method?
a) Yes, always possible
b) Yes, but not always
c) No, never possible
d) No, Not possible because referred element would be destroyed
Answer
Answer: b [Reason:] This is possible but not always, since the reference being returned may get destroyed with the return of method. This is an undesirable condition, hence it is not always possible to return references. But it is always possible if the referred element is not local to the method.
Object Oriented MCQ Set 4
1. Which definition best describes an object?
a) Instance of a class
b) Instance of itself
c) Child of a class
d) Overview of a class
Answer
Answer: a [Reason:] An object is instance of its class. It can be declared in the same way that a variable is declared, only thing is you have to use class name as the data type.
2. How many objects can be declared of a specific class in a single program?
a) 32768
b) 127
c) 1
d) As many as you want
Answer
Answer: d [Reason:] You can create as many objects of a specific class as you want, provided enough memory is available.
3. Which among the following is false?
a) Object must be created before using members of a class
b) Memory for an object is allocated only after its constructor is called
c) Objects can’t be passed by reference
d) Objects size depends on its class data members
Answer
Answer: c [Reason:] Objects can be passed by reference. Objects can be passed by value also. If object of a class is not created, we can’t use members of that class.
4. Which of the following is incorrect?
a) class student{ }s;
b) class student{ }; student s;
c) class student{ }s[];
d) class student{ }; student s[5];
Answer
Answer: c [Reason:] The array must be specified with a size. You can’t declare object array, or any other linear array without specifying its size. It’s a mandatory field.
5. The object can’t be:
a) Passed by reference
b) Passed by value
c) Passed by copy
d) Passed as function
Answer
Answer: d [Reason:] Object can’t be passed as function as it is an instance of some class, it’s not a function. Object can be passed by reference, value or copy. There is no term defined as pass as function for objects.
6. What is size of the object of following class (64 bit system)?
class student { int rollno; char name[20]; static int studentno; };
a) 20
b) 22
c) 24
d) 28
Answer
Answer: c [Reason:] 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.
7. Functions can’t return objects. (True/False)
a) True
b) False
Answer
Answer: b [Reason:] Functions can always return an object if the return type is same as that of object being returned. Care has to be taken while writing the prototype of function.
8. How members of an object are accessed?
a) Using dot operator/period symbol
b) Using scope resolution operator
c) Using member names directly
d) Using pointer only
Answer
Answer: a [Reason:] Using dot operator after the name of object we can access its members. It is not necessary to use the pointers. We can’t use the names directly because it may be used outside the class.
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.
Answer
Answer: d [Reason:] 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.
10. Which among the following is wrong?
a) class student{ }; student s;
b) abstract class student{ }; student s;
c) abstract class student{ }s[50000000];
d) abstract class student{ }; class toppers: public student{ }; topper t;
Answer
Answer: b [Reason:] We can never create instance of an abstract class. Abstract classes doesn’t have constructors and hence when an instance is created there is no facility to initialize its members. Option d is correct because topper class is inheriting the base abstract class student, and hence topper class object can be created easily.
11. Object declared in main() function:
a) Can be used by any other function
b) Can be used by main() function of any other program
c) Can’t be used by any other function
d) Can be accessed using scope resolution operator
Answer
Answer: c [Reason:] The object declared in main() have local scope inside main() function only. It can’t be used outside main() function. Scope resolution operator is used to access globally declared variables/objects.
12. When an object is returned_____
a) A temporary object is created to return the value
b) The same object used in function is used to return the value
c) The Object can be returned without creation of temporary object
d) Object are returned implicitly, we can’t say how it happens inside program
Answer
Answer: a [Reason:] A temporary object is created to return the value. It is created because object used in function is destroyed as soon as the function is returned. The temporary variable returns the value and then gets destroyed.
13. Which among the following is correct?
a) class student{ }s1,s2; s1.student()=s2.student();
b) class student{ }s1; class topper{ }t1; s1=t1;
c) class student{ }s1,s2; s1=s2;
d) class student{ }s1; class topper{ }t1; s1.student()=s2.topper();
Answer
Answer: c [Reason:] Only if the objects are of same class then their data can be copied from to another using assignment operator. This actually comes under operator overloading. Class constructors can’t be assigned any explicit value as in option b and d.
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 } };
a) student s[3]={ s(394, 9); s(394, 9); s(394,9); };
b) student s[2]={ s(394,9), s(222,5) };
c) student s[2]={ s1(392,9), s2(222,5) };
d) student s[2]={ s[392,9], s2[222,5] };
Answer
Answer: b [Reason:] It is the way we can initialize the data members for an object array using parameterized constructor. We can do this to pass our own intended values to initialize the object array data.
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. (True/False)
a) True
b) False
Answer
Answer: b [Reason:] 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.
Object Oriented MCQ Set 5
1. How many types of constructors are available, in general, in any language?
a) 2
b) 3
c) 4
d) 5
Answer
Answer: b [Reason:] There are 3 types of constructors in general, namely, default constructors, parameterized constructors and copy constructors. Default one is called whenever an object is created without arguments.
2. Choose the correct option for the following code.
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
Answer
Answer: d [Reason:] 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.
3. Which constructor is called while assigning some object with another?
a) Default
b) Parameterized
c) Copy
d) Direct assignment is used
Answer
Answer: c [Reason:] Copy constructor is used while an object is assigned with another. This is mandatory since we can’t decide which member should be assigned to which member value. By using copy constructor, we can assign the values in required form.
4. It’s necessary to pass object by reference in copy constructor because:
a) Constructor is not called in pass by reference
b) Constructor is called in pass by reference only
c) It passes the address of new constructor to be created
d) It passes the address of new object to be created
Answer
Answer: a [Reason:] Object must be passed by reference to copy constructor because constructor is not called in pass by reference. Otherwise, in pass by value, a temporary object will be created which in turn will try to call its constructor that is already being used. This results in creating infinite number of objects and hence memory shortage error will be shown.
5. Which specifier applies only to the constructors?
a) Public
b) Protected
c) Implicit
d) Explicit
Answer
Answer: d [Reason:] The keyword explicit can be used while defining the constructor only. This is used to suppress the implicit call to the constructor. It ensures that the constructors are being called with the default syntax only (i.e. only by using object and constructor name).
6. Which among the following is true?
a) Default constructor can’t be defined by the programmer
b) Default parameters constructor isn’t equivalent to default constructor
c) Default constructor can be called explicitly
d) Default constructor is and always called implicitly only
Answer
Answer: c [Reason:] Default constructors can be called explicitly anytime. They are specifically used to allocate memory space for the object in memory, in general. It is not necessary that these should always be called implicitly.
7. Which type of constructor can’t have a return type?
a) Default
b) Parameterized
c) Copy
d) Constructors don’t have a return type
Answer
Answer: d [Reason:] Constructors don’t return any value. Those are special functions, whose return type is not defined, not even void. This is so because the constructors are meant to initialize the members of class and not to perform some task which can return some value to newly created object.
8. Why do we use static constructors?
a) To initialize the static members of class
b) To initialize all the members with static value
c) To delete the static members when not required
d) To clear all the static members initialized values
Answer
Answer: a [Reason:] Static constructors help in initializing the static members of the class. This is provided because the static members are not considered to be property of the object, rather they are considered as the property of class.
9. When and how many times a static constructor is called?
a) Created at time of object destruction
b) Called at first time when an object is created and only one time
c) Called at first time when an object is created and called with every new object creation
d) Called whenever an object go out of scope
Answer
Answer: b [Reason:] Those are called at very first call of object creation. That is called only one time because the value of static members must be retained and continued from the time it gets created.
10. Which among the following is true for static constructor?
a) Static constructors are called with every new object
b) Static constructors are used initialize data members to zero always
c) Static constructors can’t be parameterized constructors
d) Static constructors can be used to initialize the non-static members also
Answer
Answer: c [Reason:] Static constructors can’t be parameterized constructors. Those are used to initialize the value of static members only. And that must be a definite value. Accepting arguments may make it possible that static members loses their value with every new object being created.
11. Within a class, only one static constructor can be created.
a) True
b) False
Answer
Answer: a [Reason:] Since the static constructors are can’t be parameterized, they can’t be overloaded. Having this case, only one constructor will be possible to be created in a local scope, because the signature will always be same and hence it will not be possible to overload static constructor.
12. Default constructor initializes all data members as:
a) All numeric member with some garbage values and string to random string
b) All numeric member with some garbage values and string to null
c) All numeric member with zero and strings to random value
d) All numeric member with zero and strings to null
Answer
Answer: d [Reason:] Default constructor, which even the programmer doesn’t define, always initialize the values as zero if numeric and null if string. This is done so as to avoid the accidental values to change the conditional statements being used and similar conditions.
13. When is the static constructor called?
a) After the first instance is created
b) Before default constructor call of first instance
c) Before first instance is created
d) At time of creation of first instance
Answer
Answer: c [Reason:] The static constructor is called before creation of first instance of that class. This is done so that even the first instance can use the static value of the static members of the class and manipulate as required.
14. If constructors of a class are defined in private access, then:
a) The class can’t be inherited
b) The class can be inherited
c) Instance can be created only in another class
d) Instance can be created anywhere in the program
Answer
Answer: a [Reason:] If the constructors are defined in private access, then the class can’t be inherited by other classes. This is useful when the class contains static members only. The instances can never be created.
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 initilizations
c) This program works fine
d) This program produces errors
Answer
Answer: c [Reason:] 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.