Java Multiple Choice Questions & Answers on Arrays & static Keyword for Freshers

1. Arrays in Java are implemented as?

a) class
b) object
c) variable
d) None of the mentioned
Answer: b
2. Which of these keywords is used to prevent content of a variable from being modified?

a) final
b) last
c) constant
d) static
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
Answer: b

Explanation: static statements are run as soon as class containing then is loaded, prior to any object declaration.
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.
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.
Answer: a
6. Which of these methods must be made static?

a) main()
b) delete()
c) run()
d) finalize()
Answer: a

Explanation: main() method must be declared static, main() method is called by Java’s run time system before any object of any class exists.
6. What is the output of this program?
  1.     class access{
  2.         public int x;
  3.   static int y;
  4.         void cal(int a, int b){
  5.             x +=  a ;
  6.             y +=  b;
  7.         }        
  8.     }    
  9.     class static_specifier {
  10.         public static void main(String args[])
  11.         {
  12.             access obj1 = new access();
  13.             access obj2 = new access();   
  14.             obj1.x = 0;
  15.             obj1.y = 0;
  16.             obj1.cal(1, 2);
  17.             obj2.x = 0;
  18.             obj2.cal(2, 3);
  19.             System.out.println(obj1.x + " " + obj2.y);     
  20.         }
  21.    }
a) 1 2
b) 2 3
c) 3 2
d) 1 5
Answer: d
7. What is the output of this program?
  1.     class access{
  2.        static int x;
  3.        void increment(){
  4.            x++;
  5.        }   
  6.      }   
  7.     class static_use {
  8.         public static void main(String args[])
  9.         {
  10.             access obj1 = new access();
  11.             access obj2 = new access();
  12.             obj1.x = 0;   
  13.             obj1.increment();
  14.             obj2.increment();
  15.             System.out.println(obj1.x + " " + obj2.x);
  16.             }
  17.    }
a) 1 2
b) 1 1
c) 2 2
d) Compilation Error
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.
8. What is the output of this program?
  1.     class static_out {
  2.         static int x;
  3.   static int y;
  4.         void add(int a , int b){
  5.             x = a + b;
  6.             y = x + b;
  7.         }
  8.     }    
  9.     class static_use {
  10.         public static void main(String args[])
  11.         {
  12.             static_out obj1 = new static_out();
  13.             static_out obj2 = new static_out();   
  14.             int a = 2;
  15.             obj1.add(a, a + 1);
  16.             obj2.add(5, a);
  17.             System.out.println(obj1.x + " " + obj2.y);     
  18.         }
  19.    }
a) 7 7
b) 6 6
c) 7 9
d) 9 7
Answer: c
9. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[])
  3.         {
  4.             int arr[] = {1, 2, 3, 4, 5};
  5.             for ( int i = 0; i < arr.length - 2; ++i)
  6.                 System.out.println(arr[i] + " ");
  7.         } 
  8.     }
a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
Answer: b

Explanation: arr.length() is 5, so the loop is executed for three times.
10. What is the output of this program?
  1.     class Output {
  2.         public static void main(String args[])
  3.         {
  4.             int a1[] = new int[10];
  5.             int a2[] = {1, 2, 3, 4, 5};
  6.             System.out.println(a1.length + " " + a2.length);
  7.         } 
  8.     }
a) 10 5
b) 5 10
c) 0 10
d) 0 5
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.

Related

Multiple Choice Questions 2698641719831702002

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item