The Wayback Machine - https://web.archive.org/web/20240912162639/https://www.geeksforgeeks.org/output-java-programs-set-12/
Open In App

Output of Java Programs | Set 12

Last Updated : 07 Oct, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

1) What is the output of the following program? 

Java




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) 123 b) 213 or 231 c) 132 or 312 d) 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. However, we do not know if the parent will execute the first print before the child or not, so on one hand there is the possibility to display 3, followed by 1, and on the other hand there is the possibility to display 1 followed by 3, different runs of the program leading to different results. What is clear is that the parent will wait for the children to complete the execution before it prints 2. So the 2 possible results are 132 and 312. 

2) What is the output of the following program? 

Java




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 error b) 1020 c) 101520 d) 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? 

Java




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) Test b) Derived c) Compilation error d) 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? 

Java




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 error b) Runtime error c) Test d) 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? 

Java




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) GeeksforGeeks b) Geeks for Geeks c) GFG d) None of the above

Ans: (b) 

Explanation: == operator checks if two variable refer to the same object. Here a and b refers 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? 

Java




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) GeeksforGeeks b) GFG c) Compilation error d) None of the above

Ans: (c)

Explanation: Constructors cannot be enclosed in try/catch block. 

7) For the given code select the correct answer. 

Java




public interface Test
{
    public int calculate();
    protected interface NestedInterface
    {
        public void nested();
    }
}


a) Compile time error due to NestedInterface b) Compile time error due to access modifier of NestedInterface c) No Compile time error d) 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 allow a sequential access of data between threads. 



Similar Reads

Output of Java Programs | Set 52 (Strings Class)
Prerequisite : Basics of Strings class in java 1. What is the Output Of the following Program class demo1 { public static void main(String args[]) { String str1 = "java"; char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; String str2 = new String(arr); System.out.println(str1); System.out.prin
5 min read
Output of Java Programs | Set 53 (String Comparison)
Prerequisite : String Comparison in Java 1. What should be the output of this program? class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) true b) false c) 0 d) 1 Output: b) false Explanation: The
3 min read
Output of Java Programs | Set 54 (Vectors)
Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java Code import java.util.*; class demo1 { public static void main(String[] args) { Vector v = new Vector(20); System.out.println(v.capacity()); System.out.println(v.size()); } } Output: 20 0 Explanation: function - int capacity( ) Returns the capacity of the vect
6 min read
Output of Java programs | Set 10 (Garbage Collection)
Prerequisite - Garbage Collection in Java Difficulty level : Intermediate In Java, object destruction is taken care by the Garbage Collector module and the objects which do not have any references to them are eligible for garbage collection. Below are some important output questions on Garbage collection. Predict the output of following Java Progra
4 min read
Output of Java programs | Set 13 (Collections)
Prerequisite - Collections in Java 1) What is the output of the following program? Java Code import java.util.*; public class priorityQueue { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<>(); queue.add(11); queue.add(10); queue.add(22); queue.add(5); queue.add(12); queue.add(2); while (queue.
3 min read
Output of Java Programs | Set 21 (Type Conversions)
Prerequisite - Type Conversions in Java with Examples 1) What is the output of the following program? public class Test { public static void main(String[] args) { int value = 554; String var = (String)value; //line 1 String temp = "123"; int data = (int)temp; //line 2 System.out.println(data + var); } } a) 677 b) Compilation error due to
3 min read
Output of Java programs | Set 24 (Final Modifier)
Difficulty level : Easy Prerequisite : final keyword in java Predict the output of following Java Programs: What will be output of following program? class Test { final int MAXIMUM; final double PI; public Test(int max) { MAXIMUM = max; } public Test(double pi) { PI = pi; } public static void main(String[] args) { Test t1 = new Test(1500); Test t2
3 min read
Output of Java programs | Set 29
Question 1. What is the output of the following question? class Test1 { public static void main(String[] args) { int String = 65; int Runnable = 97; System.out.print(String + " : " + Runnable); } } Option A) Error B) A : a C) 65 : 97 D) None Output: C Explanation : We can use all predefined Java class name and interface name as identifier
2 min read
Output of Java Programs | Set 30
QUE.1 What is the output of this program ? public class Prg { public static void main(String args[]) { System.out.print('A' + 'B'); } } OPTION a) AB b) 195 c) 131 d) Error Answer: c Explanation: Here, ‘A’ and ‘B’ are not strings they are characters. ‘A’ and ‘B’ will not concatenate. The ASCII of the ‘A’ and ‘B’ will be added. The value of ‘A’ is 65
2 min read
Output of Java Programs | Set 31
Prerequisite : Arrays in Java 1. What will be the output of the following program? Java Code public class Test { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println(arr); } } Options: 1. 1 2. Compile-time error 3. 1 2 3 4 5 4. [I@Hashcode_in_Hexadecimal Output: The answer is option(4) Explanation : In the abov
3 min read
Output of Java Programs | Set 32
Prerequisite : Arrays in Java Question 1. What is the output of following program? class ArrayDemo { public static void main(String[] args) { int arr1[] = { 1, 2, 3, 4, 5 }; int arr2[5] = { 1, 2, 3, 4, 5 }; for (int i = 0; i < 5; i++) System.out.print(arr1[i] + " "); System.out.println(); for (int i = 0; i < 5; i++) System.out.print
3 min read
Output of Java Programs | Set 33 (Collections)
Prerequisite: Java - Collections 1. What is the output of following Java Program? import java.util.ArrayList; class Demo { public void show() { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(7); list.add(1); for (int number : list) { System.out.print(number + " "); } } } public class Main { public st
3 min read
Output of Java Programs | Set 36 (do-while loop)
Prerequisite : while loop in Java 1. What will be the output for the following program? class Test { public static void main(String[] args) { do while (true) System.out.println("HELLO"); while (false); } } Options: 1. HELLO 2. Compile time error 3. HELLO (infinitely) 4. No Output The answer is option (3) Explanation: Think the above examp
3 min read
Output of Java Programs | Set 37 (If-else)
Prerequisite: if else, for loops 1. What will be the output for the following program? public class Test { public static void main(String[] args) { for (;;) System.out.println("GEEKS"); } } Options: 1.GEEKS 2.Compile time error 3.Run time Exception 4.GEEKS (Infinitely) The answer is option (4) Explanation: In the above example, we are usi
3 min read
Output of Java Programs | Set 38 (Arrays)
Prerequisite : Arrays in Java Question 1. What is the output of this question class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; System.out.print(arr[-2]); } } Option A) 11 33 B) Error C) exception D) 11 -33 Output: C Explanation : We will get java.lang.ArrayIndexOutOfBoundsException because [-2] index is out of rang
3 min read
Output of Java Programs | Set 39 (throw keyword)
Prerequisite : Exception Handling, Throws 1. What will be the output of the following program? class Geeks { public static void main(String[] args) { throw new ArithmeticException(); } } Options: 1. RuntineException:java.lang.ArithmeticExcetion 2. RuntineException:/ by zero 3. RuntineException:java.lang.ArithmeticExcetion:/ by zero 4. RuntineExcept
3 min read
Output of Java Programs | Set 40 (for loop)
Prerequisite : Loops in Java 1. what will be the output of the following program? public class Test { public static void main(String[] args) { for (int i = 0; i < 10; i++) int x = 10; } } Options: 1. No Output 2. 10 3. Compile time error 4. 10 (10 times) The answer is option (3) Explanation: Curly braces are optional and without curly braces we
3 min read
Output of Java Programs | Set 41 (try-catch)
Prerequisite : try-catch, Exception Handling1. What will be the output of the following program? Java Code class Geeks { public static void main(String[] args) { try { System.out.println(1/0); } catch(ArithmeticException e) { System.out.println(e.getMessage()); } } } Options: 1. java.lang.ArithmeticExcetion 2. / by zero 3. java.lang.ArithmeticExcet
3 min read
Output of Java Programs | Set 42 (Arrays)
Prerequisite : Java Arrays Question 1. What is the output of this question? class Test1 { public static void main(String[] args) { int arr[] = new int[5]; int arr2[] = new int['a']; byte bt = 10; int arr3[] = new int[bt]; System.out.println(arr.length); System.out.println(arr2.length); System.out.println(arr3.length); } } Option A) Error B) Runtime
3 min read
Output of Java Programs | Set 43 (Conditional statements & Loops)
Prerequisite: Decision Control and Loops 1. What will be the output of the following program? class Test { public static void main(String[] args) { int i = 0, j = 9; do { i++; if (j-- < i++) { break; } } while (i < 5); System.out.println(i + "" + j); } } Options: 1.44 2.55 3.66 4.77 The answer is option (3) Explanation : In the abov
3 min read
Output of Java Programs | Set 44 (throws keyword)
Prerequisite : throws 1. What will be the output of the following program? class Geeks throws ArithmeticException { public static void main(String[] args) throws ArithmeticException { System.out.printl(10 / 0); System.out.println("Hello Geeks"); } } Options: 1. Hello Geeks 2. No Output 3. Run-time Exception 4. Compile time error Output: T
3 min read
Output of Java Programs | Set 45 (static and instance variables)
Prerequisite : static and instance variables Question 1. What is the output of this question? class Test1 { int x = 10; public static void main(String[] args) { Test1 t1 = new Test1(); Test1 t2 = new Test1(); t1.x = 20; System.out.print(t1.x + " "); System.out.println(t2.x); } } Option A) 10 10 B) 20 20 C) 10 20 D) 20 10 Output: D Explana
3 min read
Output of Java Programs | Set 46 (Multi-Threading)
Prerequisite : Multi-threading in Java 1. What will be the output of the program? class Test extends Thread { public void run() { System.out.println("Run"); } } class Myclass { public static void main(String[] args) { Test t = new Test(); t.start(); } } Options: 1. One thread created 2. Two thread created 3. Depend upon system 4. No threa
3 min read
Output of Java Programs | Set 47 (Arrays)
Prerequisite : Arrays in Java Question 1. What is the output of this question? class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); int arr2[] = new int[3]; arr2[] = { 11, 22, 33 }; for (int i = 0; i < arr2.length
2 min read
Output of Java Programs | Set 48 (Static keyword)
Prerequisite : Static keyword in Java Question 1. what is the output of this question? class Test1 { public static void main(String[] args) { int x = 20; System.out.println(x); } static { int x = 10; System.out.print(x + " "); } } Option A) 10 20 B) 20 10 C) 10 10 D) 20 20 Output: A Explanation : Static block is executed before main metho
2 min read
Output of Java Programs | Set 49
Find the output of below java program Q 1. What is the output of this program? Java Code public class Example { int x = 10; public static void main(String args[]) { Example obj; System.out.println(obj.x); } } Option A. 10 B. 0 C. Compile time error D. Run time error Output: C. Compile time error Explanation : In java, memory of object is created by
3 min read
Output of Java Programs | Set 51 (Regular Inner class)
Prerequisite : Inner Classes in Java 1. What will be the .class file name of the Inner class? C/C++ Code class Outer { class Inner { public void m1() { System.out.println("Hello Geeks"); } } } Options: 1.Outer.Inner.class 2.Inner.class 3.Outer.class 4.Outer$Inner.class Output: The answer is option (4) Explanation : The java compil
3 min read
Output of Java Programs | Set 34 (Collections)
1. What is the Output of the following Java Program? [GFGTABS] Java import java.util.LinkedList; class Demo { public void show() { LinkedList<Integer> list = new LinkedList<Integer>(); list.add(1); list.add(4); list.add(7); list.add(5); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } } publi
3 min read
Output of Java Programs | Set 35 (Decision Making)
Prerequisite: Decision Making in JavaPredict the Output of the following programs 1. What will be the output of the following program? [GFGTABS] JAVA public class Test { public static void main(String[] args) { int x = 10; if (x) { System.out.println("HELLO GEEKS"); } else { System.out.println("BYE"); } } } [/GFGTABS]Options: 1.
7 min read
Output of Java Programs | Set 50
Q 1. What is the output of this program? [GFGTABS] Java class Test { public final int a; } class Example { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.a); } } [/GFGTABS]Option A. 0 B. Garbage value C. Compile time error : variable is not initialized D. Run time error : a is the blank variable Output: C. Co
3 min read
Practice Tags :