Static Output Questions
https://www.scientecheasy.com/2021/10/java-static-interview-questions.html/
1)
public class Myclass
{
private int x = 10;
static int m1() {
int y = x;
return y;
}
public static void main(String[] args) {
m1();
}
}
Ans: No, the above code will not be compiled because x is an instance variable and instance member cannot be accessed from static region.
2)
public class Myclass
{
private int x = 10;
static int m1()
{
Myclass obj = new Myclass();
int y = obj.x;
return y;
}
public static void main(String[] args) {
System.out.println(m1());
}
}
Ans: There is no error in the above code snippet. Output: 10.
3)
public class Myclass
{
static int a = 20;
static int b = 30;
static int c = 40;
Myclass()
{
a = 200;
}
static void m1() {
b = 300;
}
static {
c = 400;
}
public static void main(String[] args) {
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Ans: Output: 20, 30, 400.
4)
public class Myclass {
static int a = 20;
Myclass() {
a = 200;
}
public static void main(String[] args) {
new Myclass();
System.out.println(a);
}
}
Output: 200.
5)
public class Myclass {
static int a = 20;
Myclass() {
a++;
}
void m1() {
a++;
System.out.println(a);
}
public static void main(String[] args)
{
Myclass obj = new Myclass();
Myclass obj2 = new Myclass();
Myclass obj3 = new Myclass();
obj3.m1();
}
}
Ans: Output: 24.
6)
public class Test
{
Test() {
m2();
}
void m1() {
System.out.println("Instance method");
}
static void m2() {
System.out.println("Static method");
m1();
}
public static void main(String[] args)
{
new Test();
}
}
There is an error inside the static method because we cannot make a static reference to the non-static method m1() from the type Test.
7)
public class Test
{
Test(Test t) {
m1();
System.out.println("Constructor");
}
void m1() {
m2();
System.out.println("Instance method");
}
static void m2() {
System.out.println("Static method");
}
public static void main(String[] args)
{
new Test(null);
}
}
Yes, the above code will be compiled successfully. There is no problem. Output: Static method, Instance method, Constructor.
8)
public class Test
{
void m1(Test test) {
System.out.println("Instance method");
}
static void m1(Test t) {
System.out.println("Static method");
}
}
Yes, Duplicate method error. This is because we cannot declare a static method and instance method with the same signature in the same class.
9)
public class Myclass {
static int a = 20;
static void m2() {
a++;
}
public static void main(String[] args) {
System.out.println(a);
}
}
Output: 20.
10)
public class Myclass {
static int a = 20;
static {
a++;
}
{
a++;
System.out.println(a);
}
public static void main(String[] args)
{
Myclass obj = new Myclass();
Myclass obj2 = new Myclass();
Myclass obj3 = new Myclass();
}
}
Ans: Output: 22, 23, 24.
11)
public class Myclass {
Myclass() {
System.out.println("constructor");
}
static void m1() {
System.out.println("static method");
}
void m2(){
System.out.println("instance method");
}
static {
System.out.println("static block");
}
{
System.out.println("instance block");
}
public static void main(String[] args)
{
Myclass obj = new Myclass();
m1();
obj.m2();
}
}
Ans: Output: static block, instance block, constructor, static method, instance method.
12)
public class Myclass {
static {
System.out.println("static block");
}
{
System.out.println("instance block");
}
public static void main(String[] args) {
Myclass obj = new Myclass();
Myclass obj2 = new Myclass();
Myclass obj3 = new Myclass();
}
}
Ans: Output: static block, instance block, instance block, instance block.
13)
public class Myclass {
private static int x = 10;
static {
x++;
}
static {
++x;
}
{
x--;
}
public static void main(String[] args) {
Myclass obj = new Myclass();
Myclass obj2 = new Myclass();
Myclass obj3 = new Myclass();
System.out.println(x);
}
}
Ans: Yes, the code will be compiled fine. The output is 9.
14)
class demo {
static{
System.out.println("hello");
}
public static final int i =10;
}
public class test3 {
public static void main(String[] args) {
System.out.println(demo.i);
}
}
Ans : it will print 10 (compiler optimization : in common memeory location so it will not load the class)
15)
16)
17)
18)
19)
20)
21)
22)
23)
Comments
Post a Comment