Output of Java Program | Set 6

Difficulty level : Intermediate

Predict the output of following Java Programs.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

class First
{
    public First() {  System.out.println("a"); }
}
   
class Second extends First
{
    public Second()  {  System.out.println("b"); }
}
   
class Third extends Second
{
    public Third()   {  System.out.println("c"); }
}
   
public class MainClass
{
    public static void main(String[] args)
    {
        Third c = new Third();
    }
}

chevron_right


Output:

a
b
c


Explanation:
While creating a new object of ‘Third’ type, before calling the default constructor of Third class, the default constructor of super class is called i.e, Second class and then again before the default constructor of super class, default constructor of First class is called. And hence gives such output.



Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

class First
{
    int i = 10;
   
    public First(int j)
    {
        System.out.println(i); 
        this.i = j * 10;
    }
}
   
class Second extends First
{
    public Second(int j)
    {
        super(j); 
        System.out.println(i); 
        this.i = j * 20;
    }
}
   
public class MainClass
{
    public static void main(String[] args)
    {
        Second n = new Second(20); 
        System.out.println(n.i);
    }
}

chevron_right


Output:

10
200
400


Explanation:
Since in ‘Second’ class it doesn’t have its own ‘i’, the variable is inherited from the super class. Also, the constructor of parent is called when we create an object of Second.

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*; 
class
{
    public static void main (String[] args) 
    {
        Object i = new ArrayList().iterator(); 
        System.out.print((i instanceof List) + ", "); 
        System.out.print((i instanceof Iterator) + ", "); 
        System.out.print(i instanceof ListIterator); 
    
}

chevron_right


Output:

false, true, false

Explanation:
The iterator() method returns an iterator over the elements in the list in proper sequence, it doesn’t return a List or a ListIterator object. A ListIterator can be obtained by invoking the listIterator method.

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

class ThreadEx extends Thread
{
    public void run()
    {
        System.out.print("Hello...");
    }
    public static void main(String args[])
    {
        ThreadEx T1 = new ThreadEx();
        T1.start();
        T1.stop();
        T1.start();
    }
}

chevron_right


Output:

Run Time Exception


Explanation:
Exception in thread “main” java.lang.IllegalThreadStateException at java.lang.Thread.start
Thread cannot be started twice.

This article is contributed by Pratik Agrawal. 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.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.