in Technology by

What will be the output of the following program?

public class MyFirst {  

      public static void main(String[] args) {  

         MyFirst obj = new MyFirst(n);  

 }  

 static int a = 10;  

 static int n;  

 int b = 5;  

 int c;  

 public MyFirst(int m) {  

       System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);  

   }  

// Instance Block  

  {  

     b = 30;  

     n = 20;  

  }   

// Static Block  

  static   

{  

          a = 60;  

     }   

 }  

a) 10, 5, 0, 20, 0

b) 10, 30, 20

c) 60, 5, 0, 20

d) 60, 30, 0, 20, 0

🔗Reference: stackoverflow.com

🔗Source: Java Interview Questions and Answers

1 Answer

0 votes
by

Answer: (d) 60, 30, 0, 20, 0

Explanation: In the above code, there are two values of variable a, i.e., 10 and 60. Similarly, there are two values of variable b, i.e., 5 and 30. But in the output, the values of a and b are 60 and 30, respectively. It is because of the execution order of the program.

The execution order of the program is that the static block executes first, then instance block, and then constructor. Hence, the JVM will consider the value of a and b as 60 and 30 concerning the execution order. The value of a = 10 and b = 5 are of no use. And the value of variables c and m is 0 as we have not assigned any value to them.

Hence, the correct answer is an option (d).

Related questions

0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? def foo(x): x[0] = ['def'] x[1] = ['abc'] return id(x) q = ['abc ... (id(q) == foo(q)) a) Error b) None c) False d) True...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? def foo(x): x[0] = ['def'] x[1] = ['abc'] return id(x) q = ['abc ... (id(q) == foo(q)) a) Error b) None c) False d) True...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Java program? public class Test { public static void main( ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    Smriti wants to give output of a program in infinite loop which statement will you suggest to her to use? In qbasic Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    What will be the value of ‘result’ in following Python program? list1 = [1,2,3,4] list2 = [2,4,5,6] list3 = [2,6,7,8] result ... ) [1, 7, 8] c) [1, 2, 4, 7, 8] d) error...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? >>>list1 = [1, 3] >>>list2 = list1 >>>list1[0] = 4 >>>print(list2) a) [1, 4] b) [1, 3, 4] c) [4, 3] d) [1, 3]...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? print('*', "abcde".center(6), '*', sep='') a) * abcde * b) *abcde * c) * abcde* d) * abcde *...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? print("abc. DEF".capitalize()) a) Abc. def b) abc. def c) Abc. Def d) ABC. DEF...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id="224" >>>temp ... >>>print(temp.id) a) 12 b) 224 c) None d) Error...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? print("abc. DEF".capitalize()) a) Abc. def b) abc. def c) Abc. Def d) ABC. DEF...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id="224" >>>temp ... >>>print(temp.id) a) 12 b) 224 c) None d) Error...
asked Jan 2, 2023 in Technology by JackTerrance
...