Tuesday, July 15, 2014

Constructor multiple choice questions in Java

Constructor in Java with examples
In this post, I have created a few questions on understanding of constructors in Java. If you are new to constructor concept in Java and you want to read about constructor before taking this test please check constructor article or otherwise you can refer to it later after the test. Please let me know your feedback after the test. All the Best!


1. What is the return type of Constructors?
    int
    float
    void
    None of the above
The correct answer is the None of the above. Constructors does not have any return type, not even void.

2. Which operator is used by Java to free the memory of an object when it is no longer needed?
    null
    free
    new
    None of the above
The correct answer is the None of the above. Garbage collection take care of memory management in java.

3. Predict the output of the following program?


class employee{
private String name;
public employee(String name) {
this.name=name;
}
}

public class Constructor1 {

public static void main(String[] JavaLatte) {
employee e = new employee("JavaDeveloper");
System.out.println("Emp Name : "+e.name);
}
}
    Emp Name : JavaDeveloper
    JavaDeveloper
    Compile time error
    Run time error
The correct answer is the Compile time error because String name is private.

4. Predict the output of the following program?


class person{
person(){
System.out.println("Hello constructor!");
return;
}
}

public class Constructor2 {

public static void main(String[] JavaLatte) {
person p = new person();
}
}
    Hello constructor!
    No output
    Compile time error
    Run time error
The correct answer is the Hello constructor! because we are not returning anything from constructor

5. Predict the output of the following program?

class Animal{
}

public class Constructor4 {

public static void main(String[] JavaLatte) {
Animal a = new Animal();
}
}
    Run with error
    Run without error
    Compile time error
    Run time error
The correct answer is the Run without error because default constructor will be created if we are thinking that constructor is not there

6. Predict the output of the following program?

class manager{
String name;
public manager(String name) {
this.name=name;
}
void welcomeMessage(){
System.out.println("Welcome to Java-Latte");
}
}

public class Constructor3 {

public static void main(String[] args) {
manager m = new manager();
}
}
    Welcome to Java-Latte
    No output
    Compile time error
    Run time error
The correct answer is the Compile time error because in this case default constructor in not created.

7. Will this class compile?


class Overloaded{
Overloaded(){
System.out.println("Constructor is initialised");
}
protected Overloaded(){
System.out.println("Constructor is initialised");
}
}
    Yes
    No
The correct answer is the No because Overloaded constructors can’t be defined by just a change in the access modifiers

8. Predict the output of the following program?

class Rectangle{
int len;
int width;
void Rectangle(int len, int width){
this.len=len;
this.width=width;
}
}
public class Constructor5 {

public static void main(String[] JavaLatte) {

Rectangle r = new Rectangle();
r.Rectangle(5, 7);
System.out.println(r.width+" "+r.len);
}
}
    5 7
    7 5
    0 0
    Error
The correct answer is the 7 5.

9. Can we define a constructor using all four access modifiers: public,protected, default, and private.
    Yes
    No
The correct answer is the Yes.

10. Predict the output of the following program?

class Square{
Square(){
System.out.println("Constructor is initialised");
}
{
System.out.println("Square is initialised");
}
}

public class Constructor6 {

public static void main(String[] JavaLatte) {
new Square();
}
}
    Square is initialised
         Constructor is initialised
    Constructor is initialised
         Square is initialised
    Compile time error
    Run time error
The correct answer is the A An initializer block is defined within a class, not as a part of a method. It executes for every object that’s created for a class

11. Predict the output of the following program?

class Square{
Square(){
System.out.println("Constructor is initialised");
}
{
System.out.println("Square is initialised");
}
}

public class Constructor7 {

public static void main(String[] JavaLatte) {
new Square();
new Square();
}
}
    Square is initialised
         Square is initialised
         Constructor is initialised
         Constructor is initialised
    Constructor is initialised
         Constructor is initialised
         Square is initialised
         Square is initialised
    Square is initialised
         Constructor is initialised
         Square is initialised
         Constructor is initialised
    Square is initialised   
         Constructor is initialised
The correct answer is the C because An initializer block is defined within a class, not as a part of a method. It executes for every object that’s created for a class

12. Predict the output of the following program?

class Employe{
String name;
Employe(){
Employe("JavaLatte");
}
Employe(String name){
this.name = name;
}
}

public class Constructor8 {

public static void main(String[] JavaLatte) {
new Employe();
}
}
    JavaLatte
    No output
    Compile time error
    Run time error
The correct answer is the Won’t compile—you can’t invoke a constructor within a class by using the class’s name. Because a constructor is defined using the name of its class, it’s a common mistake to try to invoke a constructor from another constructor using the class’s name


13.  Predict the output of the following program?
class Employe{
String name;
Employe(){
System.out.println("JavaLatte");
}
Employe(String name){
this.name = name;
this();
}
}

public class Constructor9 {
public static void main(String[] JavaLatte) {
new Employe();
}
}

    JavaLatte
    No output
    Compile time error
    Run time error
The correct answer is the Won’t compile— the call to the overloaded constructor must be the first statement in a constructor.


Show me the answers!

You are suppose to answer all the question!

Well done! You answered them all right!

If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

2 comments: