This set of Java Question Bank on “Arrays Revisited & Keyword static”.
1. Arrays in Java are implemented as? Answer: b 2. Which of these keywords is used to prevent content of a variable from being modified? Answer: a 3. Which of these cannot be declared static? Answer: b 4. Which of the following statements are incorrect? Answer: d 5. Which of the following statements are incorrect? Answer: a 6. Which of these methods must be made static? Answer: a 7. What will be the output of the following Java program? a) 1 2 8. What will be the output of the following Java program? a) 1 2 9. What will be the output of the following Java program? a) 7 7 10. What will be the output of the following Java program? a) 1 2 11. What will be the output of the following Java program? a) 10 5
a) class
b) object
c) variable
d) none of the mentioned
Clarification: None.
a) final
b) last
c) constant
d) static
Clarification: A variable can be declared final, doing so prevents its content from being modified. Final variables must be initialized when it is declared.
a) class
b) object
c) variable
d) method
Clarification: static statements are run as soon as class containing then is loaded, prior to any object declaration.
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
Clarification: 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.
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
Clarification: None.
a) main()
b) delete()
c) run()
d) finalize()
Clarification: main() method must be declared static, main() method is called by Java runtime system before any object of any class exists.
class access
{
public int x;
static int y;
void cal(int a, int b)
{
x += a ;
y += b;
}
}
class static_specifier
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.y = 0;
obj1.cal(1, 2);
obj2.x = 0;
obj2.cal(2, 3);
System.out.println(obj1.x + " " + obj2.y);
}
}
b) 2 3
c) 3 2
d) 1 5
Clarification: None.
output:
$ javac static_specifier.java
$ java static_specifier
1 5
class access
{
static int x;
void increment()
{
x++;
}
}
class static_use
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.increment();
obj2.increment();
System.out.println(obj1.x + " " + obj2.x);
}
}
b) 1 1
c) 2 2
d) Compilation Error
Clarification: 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
class static_out
{
static int x;
static int y;
void add(int a , int b)
{
x = a + b;
y = x + b;
}
}
class static_use
{
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
b) 6 6
c) 7 9
d) 9 7
Clarification: None.
output:
$ javac static_use.java
$ java static_use
7 9
class Output
{
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5};
for ( int i = 0; i < arr.length - 2; ++i)
System.out.println(arr[i] + " ");
}
}
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
Clarification: arr.length() is 5, so the loop is executed for three times.
output:
$ javac Output.java
$ java Output
1 2 3
class Output
{
public static void main(String args[])
{
int a1[] = new int[10];
int a2[] = {1, 2, 3, 4, 5};
System.out.println(a1.length + " " + a2.length);
}
}
b) 5 10
c) 0 10
d) 0 5
Clarification: 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