Output of Java Programs | Set 12
Last Updated :
07 Oct, 2022
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 29
Question 1. What is the output of the following question? JAVA 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 pred
2 min read
Output of Java Programs | Set 31
Prerequisite : Arrays in Java 1. What will be the output of the following program? Java 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
3 min read
Output of Java Program | Set 1
Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java // filename Main.java class Test { protected int x, y; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + " " + t.y); } } Output: 0 0 In Java, a pro
3 min read
Output of Java Programs | Set 32
Prerequisite : Arrays in Java Question 1. What is the output of following program? JAVA 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.pri
3 min read
Output of Java program | Set 27
Ques1. What is the output of the following? Java import java.util.*; public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } Options : A. The program has a compile error because the size of the array wasn't specified whe
3 min read
Output of Java program | Set 26
Ques1: What is the output of this program?Java class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj
3 min read
Output of Java program | Set 28
Question 1. What is the output of the following question? JAVA class Test { int a = 10; static int b = 20; public static void main(String[] args) { Test t1 = new Test(); t1.a = 100; t1.b = 200; Test t2 = new Test(); System.out.println("t1.a =" + t1.a + " t1.b =" + t1.b); System.o
4 min read
Output of Java program | Set 5
Predict the output of following Java Programs.Program 1: Java // Main.java public class Main { public static void gfg(String s) { System.out.println("String"); } public static void gfg(Object o) { System.out.println("Object"); } public static void main(String args[]) { gfg(null);
3 min read
Output of Java Programs | Set 30
QUE.1 What is the output of this program ? JAVA 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 o
2 min read
Output of Java Program | Set 3
Predict the output of the following Java Programs: Example1: Java // filename: Test.java class Test { // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) { // Creating an object of class inside main() Test t = new Test(); // Printin
3 min read