Question 1Medium
What is the output of this 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);
}
}
- 10 5
- 5 10
- 0 10
- 0 5
Answer: A. 10 5
Explanation
a1 is an array of 10 zeros, so a1.length is 10. a2 has 5 values, so a2.length is 5. The output is 10 5.
Step-by-step solution
A1 is an array of size 10, so a1.length = 10.A2 is initialized with 5 elements, so a2.length = 5.System.out.println prints the values of a1.length and a2.length as 10 and 5.- The output is 10 5.
Why the other options are wrong
- B. a1 has length 10 and a2 has length 5, so the order printed is 10 then 5, not 5 then 10.
- C. a1 is created with size 10, so its length is 10, not 0.
- D. a1 is created with 10 elements, so its length is 10 and not 0.
Question 2Medium
Which of these keywords is used to prevent content of a variable from being modified?
- final
- last
- constant
- static
Answer: A. final
Explanation
A variable can be declared final, doing so prevents its content from being modified. Final variables must be initialized when it is declared.
Step-by-step solution
- Final keyword makes a variable unchangeable.
- Variables declared as final cannot be reassigned.
- Final is used to prevent modification of variable content.
- Other options do not serve this purpose.
- The correct answer is final.
Why the other options are wrong
- B. last is not a Java keyword for constant values, so it cannot stop a variable being changed.
- C. constant is not a Java keyword; Java uses final to make a variable unchangeable.
- D. static makes a variable belong to the class; it does not stop its value from changing.
Tip: Use 'final' to make variables unchangeable in Java.
Question 3Medium
Which of these is an incorrect Statement?
- It is necessary to use new operator to initialize an array.
- Array can be initialized using comma separated expressions surrounded by curly braces.
- Array can be initialized when they are declared.
- None of the mentioned
Answer: A. It is necessary to use new operator to initialize an array.
Explanation
Array can be initialized using both new and comma separated expressions surrounded by curly braces example:
intarr[5] = new int[5];
and
intarr[] = { 0, 1, 2, 3, 4};
Step-by-step solution
- Option A says the new operator is necessary to initialise an array.
- But an array can be initialised with curly braces without new, for example int[] a = {1, 2, 3};.
- Option B correctly describes initialising with comma-separated values in braces.
- Option C is correct too: an array can be initialised when it is declared.
- So option A is the incorrect statement.
Why the other options are wrong
- B. This statement is true, since braces with comma-separated values initialize an array, so it is not the incorrect one.
- C. This statement is true, since arrays can be initialized at declaration, so it is not the incorrect one.
- D. None is wrong because option A is an incorrect statement, so one option is the answer.
Tip: Remember: arrays can be initialized with curly braces without using new.
Question 4Medium
Which of these operators is used to allocate memory to array variable in Java?
- malloc
- alloc
- new
- newmalloc
Answer: C. new
Explanation
The new operator allocates memory for an array in Java. malloc is a C function, not a Java operator.
Step-by-step solution
- In Java, 'new' operator creates an object and allocates memory for it.
- 'new' is used with array syntax like new int[5] to allocate memory.
- Malloc is a C function, not a Java operator.
- Java uses 'new' for memory allocation of arrays and objects.
- The correct operator for allocating memory to an array in Java is 'new'.
Why the other options are wrong
- A. malloc is a C library function and Java has no malloc operator; memory is allocated with new.
- B. alloc is not a Java operator or keyword for allocating memory to arrays.
- D. newmalloc is not a Java keyword or operator; only new allocates memory.
Tip: 'new' is the only valid operator for array memory allocation in Java.
Question 5Medium
Arrays in Java are implemented as?
- class
- object
- variable
- None of the mentioned
Answer: B. object
Explanation
char array_variable [] = new int[10];
in this statement new operator is use to initialize the array.
We know that new is use to initialize Object of the class. So array is the class and the array_name is the object of that class.
Step-by-step solution
- In Java, arrays are created with new.
- They have fields like length and are treated as objects.
- So option B is correct, which is object.
Why the other options are wrong
- A. An array is not a class.
- C. An array is not just a variable.
- D. One option is correct.
Tip: Java arrays are objects.
Question 6Medium
What is the output of this program?
class evaluate {
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}
- 3
- 0
- 6
- 1
Answer: D. 1
Explanation
arr[6] is 6, and 6 / 2 is 3, so n becomes arr[3], which is 3. Then arr[3] / 2 is 3 / 2, which is 1 in integer division, so the output is 1.
Step-by-step solution
- Arr holds 0 to 9, so arr[i] = i, and n starts at 6.
- arr[n] = arr[6] = 6, and 6 / 2 = 3.
- n = arr[3] = 3.
- Print arr[n] / 2 = arr[3] / 2 = 3 / 2 = 1 in integer division. Option D is the answer.
Why the other options are wrong
- A. 3 is arr[3] before the division by 2, not the printed value.
- B. The printed value is 1, not 0, because arr[3] is 3.
- C. 6 is arr[6], the value before the index is changed.
Tip: Solve nested array indexes from the inside out, and integer division drops the fraction.
Question 7Medium
Which of these is necessary to specify at time of array initialization?
- Row
- Column
- Both Row and Column
- None of the mentioned
Answer: A. Row
Explanation
Explanation: Its necessary to specify rows at time of initialization to give memory allocation request to compiler.
If array is a 2 Dimensional then Both row and column necessary to specify.
Step-by-step solution
- When an array is created, its size must be given so memory can be reserved. For a 2D array the first dimension, the number of rows, is the one that is needed.
- The column size can be left out when each row is created separately (a jagged array).
- So only the row size is required.
Why the other options are wrong
- B. The column size can be left out, so it is not required.
- C. Only the row size is required, so 'Both Row and Column' is too much.
- D. A row size is always required, so 'None of the mentioned' is wrong.
Tip: Giving both sizes fixes a rectangular shape. Giving only the rows leaves each row's length to be set later.
Question 8Medium
What is the output of this program?
class multidimention_array {
public static void main(String args[])
{
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum += arr[i][j];
System.out.print(sum);
}
}
- 11
- 10
- 13
- 14
Answer: B. 10
Explanation
arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1 element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of array is given j + 1 value in loop. sum contains addition of all the elements of the array.
output:
$ javac multidimention_array.java
$ java multidimention_array
10
Step-by-step solution
- Arr is a jagged array: row 0 has 1 cell, row 1 has 2 cells and row 2 has 3 cells.
- The first loop fills each row with 1, 2, 3, so row 0 is [1], row 1 is [1, 2] and row 2 is [1, 2, 3].
- The second loop adds every cell: row sums are 1, 3 and 6.
- The total is 1 + 3 + 6 = 10, the correct answer.
Why the other options are wrong
- A. 11 would need one extra 1; the row sums are 1, 3 and 6.
- C. 13 is 3 more than the total of 10.
- D. 14 does not match the row sums 1, 3 and 6, which add to 10.
Tip: For jagged arrays, add row by row: a row filled with 1 to n adds up to n times (n + 1) divided by 2.
Question 9Medium
Which of these methods must be made static?
main()delete()run()finalize()
Answer: A. main()
Explanation
main() method must be declared static, main() method is called by Java's run time system before any object of any class exists.
Step-by-step solution
- main() is the entry point for Java programs, so it must be static.
- Java runtime calls main() before any objects are created.
- Static methods can be called without an object instance.
- Only static methods can be invoked by the JVM at startup.
- Thus, main() method must be made static.
Why the other options are wrong
- B. delete() is an ordinary instance method and does not need to be static.
- C. run() is an instance method from Runnable and does not have to be static.
- D. finalize() is an instance method called on objects, not a static method.
Tip: Remember: main() is always static because it's the entry point.
Question 10Medium
What is the output of this 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);
}
}
- 1 2
- 1 1
- 2 2
- Compilation Error
Answer: C. 2 2
Explanation
x is static, so both objects share one copy of it. Each increment call adds 1 to that shared x, so after two calls it is 2. Both obj1.x and obj2.x print 2, so the output is 2 2.
Step-by-step solution
- X is static, so obj1 and obj2 share one copy of x.
- obj1.x = 0 sets that shared copy to 0.
- obj1.increment() and obj2.increment() each add 1 to the same x.
- X is now 2, so both prints show 2. Output 2 2 is option C.
Why the other options are wrong
- A. The value is shared, so both objects print the same number, not 1 and 2.
- B. Both calls add to the same shared x, which reaches 2, not 1.
- D. The code compiles; a static field can be used through an object reference.
Tip: A static field has one copy shared by all objects of the class.