What will the following code print and Why<br /> <br /> class Class1 {<br /> static{<br /> System.out.println("Wow");<br /> }<br /> public static final int xyz=1;<br /> }<br /> <br /> class Class2{ <br /> public static void main(String[] args) {<br /> System.out.println(String.valueOf(Class1.xyz));<br /> }<br /> }
Javasearch.buggybread.com
Share

Search Interview Questions


 3356 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Java - Interview Questions and Answers

 Q1. What will the following code print and Why

class Class1 {
static{
System.out.println("Wow");
}
public static final int xyz=1;
}

class Class2{
public static void main(String[] args) {
System.out.println(String.valueOf(Class1.xyz));
}
}
Core Java2018-03-24 12:29:42

Ans. 1

Reason being that Class1.xyz is replaced with 1 during pre compilation only and a reference to Class1 is never made.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     static final   pre compilation


Related Questions

  Interface can only have ...
 What is the following code doing ?

private static final String[] COLORS = new String[] { "Red", "Green", "Blue" };
  Will the static block be executed in the following code ? Why ?
  What are the conventions regarding constant variables or static final variables ?
  Why is a constant defined as a static final in Java?
 Can you assign value to a static final type at runtime ?
 What is the advantage of using static final or constant variables in Java ?
 What are the trade offs between public constructor and static final method ?
 If we have to maintain a value "7" denoting "number of days in a week" as constant variable, Which of the following is the best constant name and Why ?

SEVEN
NUMBER_OF_DAYS_IN_WEEK
NUMBER_OF_DAYS_IN_WEEK_7
 If we have to maintain a value "31" denoting "number of days in a month" as constant variable, Which of the following is the best constant name and Why ?

THIRTY_ONE
NUMBER_OF_DAYS_IN_MONTH
NUMBER_OF_DAYS_IN_MONTH_31




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this