Q. What will be the output for the below code?
public class Test{
int i=8;
int j=9;
public static void main(String[] args){
add();
}
public static void add(){
int k = i+j;
System.out.println(k);
}
}

  • (A) 17
  • (B) 0
  • (C) Compilation fails with an error at line 5
  • (D) 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 5 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: Admin
i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
292
Total Visits
πŸ“½οΈ
2 y ago
Published
πŸŽ–οΈ
Admin
Publisher
πŸ“ˆ
89%
Success Rate