Output of Java Programs | Set 45 (static and instance variables) Last Updated : 02 Oct, 2017 Comments Improve Suggest changes Like Article Like Report Prerequisite : static and instance variables Question 1. What is the output of this question? JAVA 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 Explanation : instance variable is object level variable means for every object a separate copy of instance variable will be created. Question 2. What is the output of this question? JAVA class Test1 { static int i = 1; public static void main(String[] args) { for (int i = 1; i < 10; i++) { i = i + 2; System.out.print(i + " "); } } } Option A) 3 6 9 B) 3 6 9 .... 27 C) Error D) none Output: A Explanation : Here local variables are printed after execution. If we want to execute static variables, then we write Test1.i or we write Test1 object.i. Question 3. What is the output of this question? JAVA class Test1 { static int i = 1; public static void main(String[] args) { int i = 1; for (Test1.i = 1; Test1.i < 10; Test1.i++) { i = i + 2; System.out.print(i + " "); } } } Option A) 1 3 9 B) 1 2 3 ... 9 C) 3 5 7 9 11 13 15 17 19 D) None Output: C Explanation : Here, two different i copies of variable are declared, one is static and other one is local. If we write Test1.i then, static variable is executed and if we write only i, then local variable are executed. Question 4. What is the output of this question? JAVA class Test1 { static int i = 1; public static void main(String[] args) { static int i = 1; for (Test1.i = 1; Test1.i < 10; Test1.i++) { i = i + 2; System.out.print(i + " "); } } } Option A)Error B)1 3 9 C)3 5 7 9 11 13 15 17 19 D)1 2 3 ... 9 Output: A Explanation : We can not declare the static variable inside the block. If we declare static variable inside the block, then we will get the compile time error : illegal start of expression. Question 5. What is the output of this question? JAVA class Test1 { public static void main(String[] args) { static int arr1[] = { 11, 22, 33 }; static int arr2[] = { 11, 22, 33, 44, 55 }; static int ptr[]; ptr = arr1; arr1 = arr2; arr2 = ptr; System.out.print(arr1.length + " "); System.out.println(arr2.length); } } Option A)Error B)5 5 C)5 3 D)3 5 Output: A Explanation :Here we are trying to declare array as static type but we can not declare the local array as static type. If we will try to declare the local variable as static, then will get error : illegal start of expression. Comment More infoAdvertise with us Next Article Output of Java Programs | Set 45 (static and instance variables) S Shivakant Jaiswal Improve Article Tags : Java Java-Output Practice Tags : Java 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 Java 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 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? Java class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) tru 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? JAVA 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 3 min read Output of Java Programs | Set 48 (Static keyword) 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 E 2 min read Output of Java program | Set 15 (Inner Classes) Prerequisite :- Local inner classes , anonymous inner classes 1) What is the output of the following java program? Java public class Outer { public static int temp1 = 1; private static int temp2 = 2; public int temp3 = 3; private int temp4 = 4; public static class Inner { private static int temp5 = 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? JAVA 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 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 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 21 (Type Conversions) Prerequisite - Type Conversions in Java with Examples 1) What is the output of the following program? Java 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.print 3 min read Output of Java Programs | Set 47 (Arrays) Prerequisite : Arrays in Java Question 1. What is the output of this question? JAVA 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 2 min read Output of Java Programs | Set 14 (Constructors) Prerequisite - Java Constructors 1) What is the output of the following program? Java class Helper { private int data; private Helper() { data = 5; } } public class Test { public static void main(String[] args) { Helper help = new Helper(); System.out.println(help.data); } } a) Compilation error b) 3 min read Like