Test Series - oops

Test Number 14/78

Q: Which among the following is false for a constructor?
A. Constructors doesn’t have a return value
B. Constructors are always user defined
C. Constructors are overloaded with different signature
D. Constructors may or may not have any arguments being accepted
Solution: The constructors are not always user defined. The construct will be provided implicitly from the compiler if the used doesn’t defined any constructor. The implicit constructor makes all the string values null and allocates memory space for each data member.
Q: When is the constructor called for an object?
A. As soon as overloading is required
B. As soon as class is derived
C. As soon as class is created
D. As soon as object is created
Solution: The constructor is called as soon as the object is created. The overloading comes into picture as to identify which constructor have to be called depending on arguments passed in the creation of object.
Q: Which among the following function can be used to call default constructor implicitly in java?
A. this()
B. that()
C. super()
D. sub()
Solution: 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.
Q: Why do we use constructor overloading?
A. To use different types of constructors
B. Because it’s a feature provided
C. To initialize the object in different ways
D. To differentiate one constructor from another
Solution: The constructors are overloaded to initialize the objects of a class in different ways. This allows us to initialize the object with either default values or used given values. If data members are not initialized then program may give unexpected results.
Q: If programmer have defined parameterized constructor only, then __________________
A. Default constructor will not be created by the compiler implicitly
B. Default constructor will be created by the compiler implicitly
C. Default constructor will not be created but called at runtime
D. Compile time error
Solution: When the programmer doesn’t specify any default constructor and only defines some parameterized constructor. The compiler doesn’t provide any default constructor implicitly. This is because it is assumed that the programmer will create the objects only with constructors.
Q: Which among the following is not valid in java?
A. Constructor overloading
B. Recursive constructor call
C. Default value constructors
D. String argument constructor
Solution: Java doesn’t provide the feature to recursively call the constructor. This is to eliminate the out of memory error in some cases that arises unexpectedly. That is an predefined condition for constructors in java.
Q: 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)
Solution: 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.
Q: 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
Solution: 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.
Q: Which among the following is false?
A. Constructor can’t be overloaded in Kotlin
B. Constructors can’t be called recursively in java
C. Constructors can be overloaded in C++
D. Constructors overloading depends on different signatures
Solution: Kotlin language allows constructor overloading. This is a basic feature for the constructors. The constructor overloading allows the object to be initialized according to the user.
Q: Which is correct syntax?
A. classname objectname= new() integer;
B. classname objectname= new classname;
C. classname objectname= new classname();
D. classname objectname= new() classname();
Solution: The syntax for object creating in java with calling a constructor for is it is as in option c. The syntax must contain the classname followed by the object name. The keyword new must be used and then the constructor call with or without the parameters as required.

You Have Score    /10