Output of Java Programs | Set 48 (Static keyword) Last Updated : 10 Oct, 2017 Comments Improve Suggest changes Like Article Like Report Prerequisite : Static keyword in Java Question 1. what is the output of this question? JAVA 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 method. If we declare a Static block in java class it is executed when class loads. Question 2. what is the output of this question? JAVA class Test1 { int x = 10; public static void main(String[] args) { System.out.println(x); } static { System.out.print(x + " "); } } Option A) 10 10 B) Error C) Exception D) none Output: B Explanation : If we are trying to print the instance variable inside the static block or static method without creating class instance then it will give the error : non-static variable x cannot be referenced from a static context. Question 3. what is the output of this question? JAVA class Test1 { int x = 10; public static void main(String[] args) { Test1 t1 = new Test1(); System.out.println(t1.x); } static { int x = 20; System.out.print(x + " "); } } Option A) 10 20 B) 20 10 C) 10 10 D) Error Output: B Explanation : We can print the instance variable inside the static method after creating the class reference. Question 4. what is the output of this question? JAVA class Test1 { int x = 10; public static void main(String[] args) { System.out.println(Test1.x); } static { int x = 20; System.out.print(x + " "); } } Option A)10 10 B) 20 20 C) 20 10 D) Error Output: D Explanation : We can not access the instance variable with class name. otherwise it will give the error : non-static variable x cannot be referenced from a static context Question 5. what is the output of this question? JAVA class Test1 { static 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: B Explanation : static variable is class level variable. if we do update in any reference then automatically all pointing reference value are changed. Comment More infoAdvertise with us Next Article Output of Java Programs | Set 48 (Static keyword) S Shivakant Jaiswal Improve Article Tags : Java Java-Output Static Keyword Practice Tags : Java Similar Reads Output of Java Program | Set 8 Difficulty level : IntermediatePredict the output of following Java Programs.Program 1: Java class GfG { public static void main(String args[]) { String s1 = new String("geeksforgeeks"); String s2 = new String("geeksforgeeks"); if (s1 == s2) System.out.println("Equal"); 3 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 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 Explanat 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 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 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 Programs | Set 54 (Vectors) Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java 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 6 min read Output of Java Programs | Set 50 Q 1. What is the output of this program? Java class Test { public final int a; } class Example { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.a); } } Option A. 0 B. Garbage value C. Compile time error : variable is not initialized D. Run time error : a is th 3 min read Output of Java Program | Set 9 Difficulty level : Intermediate Predict the output of following Java Programs. Program 1: Java class Gfg { // constructor Gfg() { System.out.println("Geeksforgeeks"); } static Gfg a = new Gfg(); //line 8 public static void main(String args[]) { Gfg b; //line 12 b = new Gfg(); } } Output: G 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 Like