Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Constructors are the cornerstones of object creation in Java. Imagine them as the blueprints that come to life when you instantiate a new object.
These special methods, unlike regular methods, don’t have a return type and share the same name as the class itself. Invoked automatically during object creation, constructors are entrusted with a critical task: initializing an object’s state. This initialization process ensures that your objects are born in a valid and consistent condition, ready to fulfill their purpose within your program. By carefully crafting constructors, you lay the foundation for robust and maintainable Java applications.
This DataFlair quiz will put your understanding of constructors to the test, helping you solidify your grasp of this essential concept in object-oriented programming. So, are you ready to dive in and challenge your knowledge?
Time limit: 0
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Which of the following keywords is used to create an object to invoke a constructor ?
Correct
Incorrect
Question 2 of 15
2. Question
class Constructor {
String a = “DataFlair”;
public Constructor ( ) {
System.out.println(a);
}
public static void main ( String args [ ] )
{
Constructor c = new Constructor();
}
}
Correct
Incorrect
Question 3 of 15
3. Question
class Demo {
public Demo ( int a , int b )
{
System.out.println( a*b);
}
}
class Main {
public static void main ( String args [ ] )
{
Demo d = new Demo();
d.Demo(1,10);
}
}
Correct
Incorrect
Question 4 of 15
4. Question
Which of the following cannot be used in a Constructor ?
Correct
Incorrect
Question 5 of 15
5. Question
class Sample {
static void Sample ( ) {
System.out.println(“Constructor”);
}
public static void main ( String args [ ] )
{
Sample s = new Sample();
s.Sample();
}
}
Correct
Incorrect
Question 6 of 15
6. Question
class Sample {
int a;
public Example ( int x )
{
a=x;
}
}
class Main {
public static void main ( String args [ ] )
{
Sample s = new Sample ( 10 );
System.out.println(a);
}
}
Correct
Incorrect
Question 7 of 15
7. Question
Which of the following is not a syntax of Constructor ?
Correct
Incorrect
Question 8 of 15
8. Question
class Constructor {
String a;
Constructor ( )
{
a = “DataFlair”;
}
}
class Main {
public static void main ( String args [ ] )
{
System.out.println(a);
Constructor c = new Constructor();
System.out.println(c.Constructor);
}
}
Correct
Incorrect
Question 9 of 15
9. Question
class ConstructorSample {
int a;
String b;
public ConstructorSample ( int c , String d )
{
a = c;
b = d;
}
void display( )
{
System.out.println(a );
System.out.println(b);
}
}
class Main {
public static void main ( String args [ ] )
{
ConstructorSample s = new ConstructorSample(1,One);
s.display();
}
}
Correct
Incorrect
Question 10 of 15
10. Question
Which of the following methods can have different arguments using constructors ?
Correct
Incorrect
Question 11 of 15
11. Question
class Function {
int p;
Function ( )
{
p=100;
}
public static void main ( String args [ ] )
{
Function f = new Function();
System.out.println(f.Function);
}
}
Correct
Incorrect
Question 12 of 15
12. Question
class Main {
Main ( )
{
System.out.println(“Default Constructor”);
}
Main ( int x )
{
this(1);
System.out.println(x);
}
}
Correct
Incorrect
Question 13 of 15
13. Question
Which of the following is used while using Constructor ?
Correct
Incorrect
Question 14 of 15
14. Question
class ConstructorOverloading {
int x , y , z ;
ConstructorOverloading ( int a , int b )
{
x=a;
y=b;
System.out.println(x+y);
}
ConstructorOverloading ( int a , int b , int c )
{
x=a;
y=b;
z=c;
System.out.println(a+b+c);
}
public static void main ( String args [ ] )
{
ConstructorOverloading c = new ConstructorOverloading( 2 , 4 , 8 );
}
}
Correct
Incorrect
Question 15 of 15
15. Question
class Example {
public Example {
System.out.println(a);
}
public static void main ( String args [ ] )
{
Example e = new Example();
e.Example();
}
}
Correct
Incorrect
Summary:
Have you just conquered the DataFlair quiz on Constructors in Java? If so, congratulations! This quiz was designed to test your understanding of the fundamentals of constructors, including the keywords used to create objects (`new`) and invoke constructors themselves.
By tackling these questions, you’ve taken a significant step towards mastering this essential concept in Java. But your journey with constructors doesn’t end here!
There’s a whole world of constructors waiting to be explored. Delve deeper into resources that cover advanced constructor types, such as default constructors (automatically generated by the compiler if you don’t define your own) and parameterized constructors (allowing you to provide initial values for object properties during creation).
Additionally, explore best practices for constructor design to ensure your objects are consistently initialized in a robust and efficient way. Remember, the more you practice, the more comfortable you’ll become with constructors. So keep at it, experiment with different scenarios, and watch your Java programming skills flourish!
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.