Q. What will be the output for the below code ?

Code:
1. public class Test{
2.       int i=8;
3.       int j=9;
4.       public static void main(String[] args){
5.             add();
6.       }
7.       public static void add(){
8.             int k = i+j;
9.             System.out.println(k);
10.      }
11. }
  • (A) 17
  • (B) 0
  • (C) Compilation fails with an error at line 5
  • (D) Compilation fails with an error at line 8
πŸ’¬ Discuss
βœ… Correct Answer: (D) Compilation fails with an error at line 8
Explanation: i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.
Explanation by: Jatin Dubey
i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
226
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Jatin Dubey
Publisher
πŸ“ˆ
84%
Success Rate