Output of Java Programs | Set 14 (Constructors) - GeeksforGeeks

18 May, 2017
Prerequisite – Java Constructors
1) What is the output of the following program?
class Helper{    private int data;    private Helper()    {        data = 5;    }}public class Test{    public static void main(String[] args)    {        Helper help = new Helper();        System.out.println(help.data);    }}
a) Compilation errorb) 5c) Runtime errord) None of these
Ans. (a)Explanation: A private constructor cannot be used to initialize an object outside the class that it is defined within because it is no longer visible to the external class.
2) What is the output of the following program?
public class Test implements Runnable{    public void run()    {        System.out.printf(" Thread's running ");    }      try    {        public Test()        {            Thread.sleep(5000);        }       }     catch (InterruptedException e)     {        e.printStackTrace();    }          public static void main(String[] args)    {        Test obj = new Test();        Thread thread = new Thread(obj);        thread.start();        System.out.printf(" GFG ");    }}
a) GFG Thread’s runningb) Thread’s running GFGc) Compilation errord) Runtimer error
Ans. (c)Explanation: A constructor cannot be enclosed inside a try/catch block.
3) What is the output of the following program?
class Temp{    private Temp(int data)    {        System.out.printf(" Constructor called ");    }    protected static Temp create(int data)    {        Temp obj = new Temp(data);        return obj;    }    public void myMethod()    {        System.out.printf(" Method called ");    }}  public class Test{    public static void main(String[] args)    {        Temp obj = Temp.create(20);        obj.myMethod();    }}
a) Constructor called Method calledb) Compilation errorc) Runtime errord) None of the above
Ans. (a)Explanation: When a constructor is marked as private, the only way to create a new object of that class from some external class is using a method that creates a new object, as defined above in the program. The method create() is responsible for creation of Temp object from some other external class. Once the object is created, its method can be invoked from the class in which the object is created.4) Whats is the output of the following program?
public class Test{    public Test()    {        System.out.printf("1");        new Test(10);        System.out.printf("5");    }    public Test(int temp)    {        System.out.printf("2");        new Test(10, 20);        System.out.printf("4");    }    public Test(int data, int temp)    {        System.out.printf("3");              }          public static void main(String[] args)    {        Test obj = new Test();              }      }
a) 12345b) Compilation errorc) 15d) Runtime error
Ans. (a)Explanation: Constructors can be chained and overloaded. When Test() is called, it creates another Test object calling the constructor Test(int temp).
5) What is the output of the following program?
class Base{    public static String s = " Super Class ";    public Base()    {        System.out.printf("1");    }}public class Derived extends Base{    public Derived()    {        System.out.printf("2");        super();    }          public static void main(String[] args)    {        Derived obj = new Derived();        System.out.printf(s);    }}
a) 21 Super Classb) Super Class 21c) Compilation errord) 12 Super ClassAns. (c)Explanation: Constructor call to super class must be the first statement in the constructor of the Derived class.privateThis article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-Output
Java
Program Output
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
ArrayList in Java
Stack Class in Java
LinkedList in Java
Overriding in Java
Arrow operator -> in C/C++ with Examples
delete keyword in C++
Output of C Programs | Set 1
Output of Java Program | Set 1
Different ways to copy a string in C/C++