πŸ“Š JAVA
Q. What is the output of the following program?
public class Test{
static int x = 10 ;
public static void main(String[] a){
Test test = new Test( ) ;
Test test1 = new Test( ) ;
test.x += 1 ;
System.out.println( test.x + test1.x ) ;
}
}
  • (A) 20
  • (B) 21
  • (C) 22
  • (D) 22 D. Compilation Error
πŸ’¬ Discuss
βœ… Correct Answer: (C) 22

Explanation: Static variable have a single copy of memory. That means all the objects will share the same memory location. So, if the object test increase the value of x by 1, then object test1 will access that incremented value of x

Explanation by: Praveen Singh
Static variable have a single copy of memory. That means all the objects will share the same memory location. So, if the object test increase the value of x by 1, then object test1 will access that incremented value of x

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
173
Total Visits
πŸ“½οΈ
12 mo ago
Published
πŸŽ–οΈ
Praveen Singh
Publisher
πŸ“ˆ
92%
Success Rate