Examveda
Examveda

Determine Output:
class MyClass{
      static final int a = 20;

      static final void call(){
            System.out.println("two");
      }
	
      static{
            System.out.println("one");
      }
}

public class Test{
      public static void main(String args[]){
            System.out.println(MyClass.a);
      }
}

A. one

B. one two

C. one two 20

D. 20

E. one 20

Answer: Option E


This Question Belongs to Java Program >> Declaration And Access Control

Join The Discussion

Comments ( 7 )

  1. Anoop Kumar
    Anoop Kumar :
    11 months ago

    How

  2. Tanuja Thube
    Tanuja Thube :
    1 year ago

    Static block executes before the main method, then the control is given to main method so the output is one 20

  3. Manoj Mani
    Manoj Mani :
    3 years ago

    The answer should be 20. Java will first look for a static block inside the class containing the main method. In this question, unless we create an instance of class MyClass (i.e. using new MyClass() inside the main method), the static block of MyClass will not be called. So the answer should be just 20.

  4. Pavi Nandy
    Pavi Nandy :
    3 years ago

    Static block will be executed first. So, it prints "one". Then the main method calls the static varibale (a). As, it is static varibale it can be called by class name. So 20 gets printed. So, the answer is "one 20"

  5. Prajun Magar
    Prajun Magar :
    5 years ago

    why the static method wasn't called over here?

  6. AK8421 Jagtap
    AK8421 Jagtap :
    6 years ago

    Option-E
    In Java Every time Static block always executed first.That-why first answer is "one" then Execute static variables in Class level.
    If only static variable available instead of Static{} block then ans is 20

  7. BhuvNesh TiwAri
    BhuvNesh TiwAri :
    7 years ago

    Answer is wrong of this question, the output should be "one 20" please check this question again carefully. Thank You !!

Related Questions on Declaration and Access Control