Constructors

Question 1
Given a class named student, which of the following is a valid constructor declaration for the class?
A
Student student(){}
B
Private final student(){}
C
Student(student s){}
D
Void student(){}
       OOPS       Constructors       Nielit Scientist-B IT 4-12-2016
Question 1 Explanation: 
A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.
Question 2
Which two are valid constructors for Thread?
1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4.Thread(Runnable r, ThreadGroup g)
5.Thread(Runnable r, int priority)
A
1 and 3
B
2 and 4
C
1 and 2
D
2 and 5
       OOPS       Constructors       Nielit Scientific Assistance IT 15-10-2017
Question 2 Explanation: 
(1) and (2) are both valid constructors for Thread.
(3), (4), and (5) are not legal Thread constructors
Question 3
Which constructor will be called by the following lines of code?
(i) Student S1;
(ii) Student S2=S1;
A
First copy constructor, then default constructor
B
First default constructor, then copy constructor
C
Default constructor for both lines of code
D
Copy constructor for both lines of code
       OOPS       Constructors       KVS DEC-2013
Question 3 Explanation: 
→ Student S1:----whenever we create object to the class , automatically default constructor will be called.
→ Student S2=S1:------The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.
Question 4
Implicit return type of a class constructor is:
A
not of class type itself
B
class type itself
C
a destructor of class type
D
a destructor not of class type
       OOPS       Constructors       UGC NET CS 2016 July- paper-3
Question 4 Explanation: 
Implicit return type of a class constructor is class type itself.

Syntax For Constructors:

Access_Modifier No Return_Type Class(...)

Example:

public static main(String [ ]args)

Constructor rules:

1. Constructors are always used with a new.

2. Have the same name as that of Class

3. Does not have a return type

4. Can be Overloaded but not Overridden.

Question 5
Which of the following is NOT a type of constructor
A
Copy constructor
B
Friend constructor
C
Default constructor
D
Parameterized constructor
       OOPS       Constructors       APPSC-2016-DL-CS
Question 5 Explanation: 
Parameterized constructors: Constructors that can take at least one argument are termed as parameterized constructors. When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function.
Default constructors: If the programmer does not supply a constructor for an instantiable class, Java/C++ compiler inserts a default constructor into your code on your behalf. This constructor is known as default constructor.
Copy Constructor: Copy constructors define the actions performed by the compiler when copying class objects. A Copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object). It is used to create a copy of an existing object of the same class. Even though both classes are the same, it counts as a conversion constructor.
Question 6
What should be the name of the constructor
A
same as object
B
same as member
C
same as class
D
same as function
       OOPS       Constructors       APPSC-2016-DL-CS
Question 6 Explanation: 
Constructor: a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring class. They have the task of initializing the object's data members
There are 6 questions to complete.
PHP Code Snippets Powered By : XYZScripts.com
error: Content is protected !!