Java Questions & Answers – Arrays Revisited & Keyword Static

This set of Java Question Bank focuses on “Arrays Revisited & Keyword Static”.

1. Arrays in Java are implemented as?
a) class
b) object
c) variable
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

2. Which of these keywords is used to prevent content of a variable from being modified?
a) final
b) last
c) constant
d) static
View Answer

Answer: a
Explanation: A variable can be declared final, doing so prevents its content from being modified. Final variables must be initialized when it is declared.

3. Which of these cannot be declared static?
a) class
b) object
c) variable
d) method
View Answer

Answer: b
Explanation: static statements are run as soon as class containing then is loaded, prior to any object declaration.
advertisement
advertisement

4. Which of the following statements are incorrect?
a) static methods can call other static methods only
b) static methods must only access static data
c) static methods can not refer to this or super in any way
d) when object of class is declared, each object contains its own copy of static variables
View Answer

Answer: d
Explanation: All objects of class share same static variable, when object of a class are declared, all the objects share same copy of static members, no copy of static variables are made.

5. Which of the following statements are incorrect?
a) Variables declared as final occupy memory
b) final variable must be initialized at the time of declaration
c) Arrays in java are implemented as an object
d) All arrays contain an attribute-length which contains the number of elements stored in the array
View Answer

Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of these methods must be made static?
a) main()
b) delete()
c) run()
d) finalize()
View Answer

Answer: a
Explanation: main() method must be declared static, main() method is called by Java runtime system before any object of any class exists.

7. What will be the output of the following Java program?

advertisement
  1.     class access
  2.     {
  3.         public int x;
  4.  	static int y;
  5.         void cal(int a, int b)
  6.         {
  7.             x +=  a ;
  8.             y +=  b;
  9.         }        
  10.     }    
  11.     class static_specifier 
  12.     {
  13.         public static void main(String args[])
  14.         {
  15.             access obj1 = new access();
  16.             access obj2 = new access();   
  17.             obj1.x = 0;
  18.             obj1.y = 0;
  19.             obj1.cal(1, 2);
  20.             obj2.x = 0;
  21.             obj2.cal(2, 3);
  22.             System.out.println(obj1.x + " " + obj2.y);     
  23.         }
  24.    }

a) 1 2
b) 2 3
c) 3 2
d) 1 5
View Answer

Answer: d
Explanation: None.
output:

advertisement
$ javac static_specifier.java
$ java static_specifier
1 5

8. What will be the output of the following Java program?

  1.     class access
  2.     {
  3.        static int x;
  4.        void increment()
  5.        {
  6.            x++;
  7.        }   
  8.      }   
  9.     class static_use 
  10.     {
  11.         public static void main(String args[])
  12.         {
  13.             access obj1 = new access();
  14.             access obj2 = new access();
  15.             obj1.x = 0;   
  16.             obj1.increment();
  17.             obj2.increment();
  18.             System.out.println(obj1.x + " " + obj2.x);
  19.          }
  20.    }

a) 1 2
b) 1 1
c) 2 2
d) Compilation Error
View Answer

Answer: c
Explanation: All objects of class share same static variable, all the objects share same copy of static members, obj1.x and obj2.x refer to same element of class which has been incremented twice and its value is 2.
output:

$ javac static_use.java
$ java static_use 
2 2

9. What will be the output of the following Java program?

  1.     class static_out 
  2.     {
  3.         static int x;
  4.  	static int y;
  5.         void add(int a , int b)
  6.         {
  7.             x = a + b;
  8.             y = x + b;
  9.         }
  10.     }    
  11.     class static_use 
  12.     {
  13.         public static void main(String args[])
  14.         {
  15.             static_out obj1 = new static_out();
  16.             static_out obj2 = new static_out();   
  17.             int a = 2;
  18.             obj1.add(a, a + 1);
  19.             obj2.add(5, a);
  20.             System.out.println(obj1.x + " " + obj2.y);     
  21.         }
  22.    }

a) 7 7
b) 6 6
c) 7 9
d) 9 7
View Answer

Answer: c
Explanation: None.
output:

$ javac static_use.java
$ java static_use
7 9

10. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int arr[] = {1, 2, 3, 4, 5};
  6.             for ( int i = 0; i < arr.length - 2; ++i)
  7.                 System.out.println(arr[i] + " ");
  8.         } 
  9.     }

a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
View Answer

Answer: b
Explanation: arr.length() is 5, so the loop is executed for three times.
output:

$ javac Output.java
$ java Output
1 2 3

11. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int a1[] = new int[10];
  6.             int a2[] = {1, 2, 3, 4, 5};
  7.             System.out.println(a1.length + " " + a2.length);
  8.         } 
  9.     }

a) 10 5
b) 5 10
c) 0 10
d) 0 5
View Answer

Answer: a
Explanation: Arrays in java are implemented as objects, they contain an attribute that is length which contains the number of elements that can be stored in the array. Hence a1.length gives 10 and a2.length gives 5.
output:

$ javac Output.java
$ java Output
10 5

Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.