242). Arrays in Java are implemented as?
[A]class
[B]object
[C]variable
[D]none of the mentioned
Show Answer
Correct Answer: object
Notes:
Answer: b
Explanation: None.
243). Which of these keywords is used to prevent content of a variable from being modified?
[A]final
[B]last
[C]constant
[D]static
Show Answer
Correct Answer: final
Notes:
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.
244). Which of these cannot be declared static?
[A]class
[B]object
[C]variable
[D]method
Show Answer
Correct Answer: object
Notes:
Answer: b
Explanation: static statements are run as soon as class containing then is loaded, prior to any object declaration.
245). 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
Show Answer
Correct Answer: when object of class is declared, each object contains its own copy of static variables
Notes:
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.
246). 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
Show Answer
Correct Answer: Variables declared as final occupy memory
Notes:
Answer: a
Explanation: None.
247). Which of these methods must be made static?
[A]main()
[B]delete()
[C] run()
[D]finalize()
Show Answer
Correct Answer: main()
Notes:
Answer: a
Explanation: main() method must be declared static, main() method is called by Java runtime system before any object of any class exists.
248). What will be the output of the following Java program?
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);
}
}
[A]1 2
[B]2 3
[C]3 2
[D]1 5
Show Answer
Correct Answer: 1 5
Notes:
Answer: d
Explanation: None.
output:$ javac static_specifier.java
$ java static_specifier
1 5
249). What will be the output of the following Java program?
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);
}
}
[A]1 2
[B]1 1
[C]2 2
[D] Compilation Error
Show Answer
Correct Answer: 2 2
Notes:
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
250). What will be the output of the following Java program?
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);
}
}
[A]7 7
[B] 6 6
[C] 7 9
[D]9 7
Show Answer
Correct Answer: 7 9
Notes:
Answer: c
Explanation: None.
output:
$ javac static_use.java
$ java static_use
7 9
251). What will be the output of the following Java program?
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] + " ");
}
}
[A] 1 2
[B] 1 2 3
[C]1 2 3 4
[D]1 2 3 4 5
Show Answer
Correct Answer: 1 2 3
Notes:
Answer: b
Explanation: arr.length() is 5, so the loop is executed for three times.
output:
$ javac Output.java
$ java Output
1 2 3
252). What will be the output of the following Java program?
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);
}
}
[A]10 5
[B]5 10
[C]0 10
[D] 0 5
Show Answer
Correct Answer: 10 5
Notes:
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