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?
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
Results
Results
0 of 15 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- Current
- Review / Skip
- Answered
- Correct
- Incorrect
-
Question 1 of 15
1. Question
Which of the following keywords is used to create an object to invoke a constructor ?
CorrectIncorrect -
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();
}
}
CorrectIncorrect -
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);
}
}
CorrectIncorrect -
Question 4 of 15
4. Question
Which of the following cannot be used in a Constructor ?
CorrectIncorrect -
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();
}
}
CorrectIncorrect -
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);
}
}
CorrectIncorrect -
Question 7 of 15
7. Question
Which of the following is not a syntax of Constructor ?
CorrectIncorrect -
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);
}
}
CorrectIncorrect -
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();
}
}
CorrectIncorrect -
Question 10 of 15
10. Question
Which of the following methods can have different arguments using constructors ?
CorrectIncorrect -
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);
}
}
CorrectIncorrect -
Question 12 of 15
12. Question
class Main {
Main ( )
{
System.out.println(“Default Constructor”);
}
Main ( int x )
{
this(1);
System.out.println(x);
}
}
CorrectIncorrect -
Question 13 of 15
13. Question
Which of the following is used while using Constructor ?
CorrectIncorrect -
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 );
}
}
CorrectIncorrect -
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();
}
}
CorrectIncorrect
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!
