Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

What will be output of following program?

  1. public class Q2 {
  2.  static int class_var = 2;
  3.  
  4.  public Q2(){
  5.   class_var = 5;
  6.  }
  7.  public static void main(String args[]){
  8.   System.out.println(class_var);
  9.  }
  10. }
a. 2
b. 5
c. Compile time error
d. None of the above.

  Answer:

A is the correct choice. class_var is declared as a static variable and initialized to 2. When main is called, it's value would be 2 since there is no object of the class created, so constructor is not called.
No votes yet
Share this

Post new comment

2 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.