Output of Java Programs | Set 12

11 Nov, 2021
1) What is the output of the following program?
public class Test implements Runnable{    public void run()    {        System.out.printf("%d",3);    }    public static void main(String[] args) throws InterruptedException    {        Thread thread = new Thread(new Test());        thread.start();        System.out.printf("%d",1);        thread.join();        System.out.printf("%d",2);    }  }
a) 123b) 213c) 132d) 321
Ans: (c)
Explanation: The parent thread waits for the newly created thread to complete using join. join() method allows one thread to wait until another thread completes its execution. So, parent thread prints 1 and wait for the child thread to complete. The child thread prints 3 on console and finally the parent thread prints 2.
2) What is the output of the following program?
public class Test{    private static int value = 20;    public int s = 15;    public static int temp = 10;      public static class Nested    {        private void display()      {          System.out.println(temp + s + value);      }      }             public static void main(String args[])    {        Test.Nested inner = new Test.Nested();        inner.display();      } }
a) Compilation errorb) 1020c) 101520d) None of the above
Ans: (a)
Explanation: A non-static variable can not be accessed in static nested inner class. “Nested” cannot access non-static variables[variable s in this case]. Therefore the error:
10: error: non-static variable s cannot be referenced from a static context
            System.out.println(temp + s + value);
                                      ^
3) What is the output of the following program?
import java.io.*;public class Test{    public void display() throws IOException    {        System.out.println("Test");    }  }  class Derived extends Test{    public void display() throws IOException    {        System.out.println("Derived");    }    public static void main(String[] args) throws IOException    {        Derived object = new Derived();        object.display();    }}
a) Testb) Derivedc) Compilation errord) Runtime error
Ans: (b)
Explanation: If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
4) What is the output of the following program?
public class Test extends Thread{    public void run()    {        System.out.printf("Test ");    }    public static void main(String[] args)    {        Test test = new Test();        test.run();        test.start();    }}
a) Compilation errorb) Runtime errorc) Testd) Test Test
Ans: (d)
Explanation: test.run() executes the run method. test.start() creates a new thread and executes the overridden run method of the Thread class. The Thread.start() method always starts a new thread, and the entry point for this thread is the run() method. If you are calling run() directly it will execute in the same thread BUT it is always recommendable logically calling Thread.start() to start a new thread of execution followed by the run() method.
5) What is the output of the following program?
public class Test extends Thread{    public static void main(String[] args)    {        String a = "GeeksforGeeks";        String b = new String(a);        int value = 0;        value = (a==b) ? 1:2;        if(value == 1)        {            System.out.println("GeeksforGeeks");        }        else if(value == 2)        {            System.out.println("Geeks for Geeks");        }        else        {            System.out.println("GFG");        }              }}
a) GeeksforGeeksb) Geeks for Geeksc) GFGd) None of the above
Ans: (b) 
Explanation: == operator checks if two variable refer to the same object. Here a and brefers to two different objects. ?: is another form of if else statement that could be read as, condition : if true then do this : else do this.
6) What is the output of the following program?
public class Test{    try    {        public Test()        {            System.out.println("GeeksforGeeks");            throw new Exception();        }    }    catch(Exception e)    {        System.out.println("GFG");    }    public static void main(String[] args)    {        Test test = new Test();    }}
a) GeeksforGeeksb) GFGc) Compilation errord) None of the above
Ans: (c)
Explanation: Constructors cannot be enclosed in try/catch block.
7) For the given code select the correct answer.
public interface Test{    public int calculate();    protected interface NestedInterface    {        public void nested();    }}
a) Compile time error due to NestedInterfaceb) Compile time error due to access modifier of NestedInterfacec) No Compile time errord) NestedInterface cannot hold any function declaration.
Ans: (b)
Explanation: Access modifier of NestedInterface can only be public. Therefore the error:
4: error: illegal combination of modifiers: public and protected
    protected interface NestedInterface
              ^
1 error
8) Which of the following are true about constructor declaration?a) Constructors can be declared final.b) Constructors can be surrounded by try/catch blocks.c) Constructor cannot throw exception.d) Constructors can hold synchronized code(so that each thread can access constructor sequentially).
Ans: (d)
Explanation: Constructors allows a sequential access of data between threads.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
chhabradhanvi
Java-Output
Java
Program Output
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
ArrayList in Java
Collections in Java
Stream In Java
Singleton Class in Java
Arrow operator -> in C/C++ with Examples
Output of Java Program | Set 1
Output of C Programs | Set 1
delete keyword in C++
Output of C++ programs | Set 50