Inner Classes(Computer Science > Java Program ) Questions and Answers

Question 1.

Which of the following statements are valid array declarations?
A. int number();
B. int number[];
C. double[] marks;
D. counter int[];


  1.    A,B,C
  2.    B,C
  3.    C,D
  4.    A,B,D
Explanation:-
Answer: Option B. -> B,C

Option A is incorrect because array cannot be declared using Parentheses but square brackets.



Question 2.

Which three are legal array declarations?
1.int [] myScores [];
2. char [] myChars;
3. int [6] myScores;
4. Dog myDogs [];
5. Dog myDogs [7];


  1.    1, 2, 4
  2.    2, 4, 5
  3.    2, 3, 4
  4.    All are correct.
Explanation:-
Answer: Option A. -> 1, 2, 4



Question 3.

Which one of the following will declare an array and initialize it with five numbers?


  1.    Array a = new Array(5);
  2.    int [] a = {23,22,21,20,19};
  3.    int a [] = new int[5];
  4.    int [5] array;
Explanation:-
Answer: Option B. -> int [] a = {23,22,21,20,19};

Option B is the legal way to declare and initialize an array with five elements.



Question 4.

int number[] = new int[5]; After execution of this statement, which of the following are true?
(A) number[0] is undefined
(B) number[5] is undefined
(C) number[4] is null
(D) number[2] is 0
(E) number.length() is 5


  1.    (A) & (E)
  2.    (C) & (E)
  3.    (E)
  4.    (B), (D) & (E)
Explanation:-
Answer: Option D. -> (B), (D) & (E)



Question 5.


Which of the following statements are valid array declaration ?


(A) int number();
(B) float average[];
(C) double[] marks;
(D) counter int[];
  1.    (A)
  2.    (A) & (C)
  3.    (B) & (C)
  4.    (D)
Explanation:-
Answer: Option C. -> (B) & (C)



Question 6.


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);
}
}
  1.    10 5
  2.    5 10
  3.    0 10
  4.    0 5
Explanation:-
Answer: Option A. -> 10 5

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.



Question 7.
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]);
}
}
  1.    1 2
  2.    1 2 3
  3.    1 2 3 4
  4.    1 2 3 4 5
Explanation:-
Answer: Option B. -> 1 2 3

arr.length() is 5, so the loop is executed for three times.



Question 8.

Arrays in Java are implemented as?


  1.    class
  2.    object
  3.    variable
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> object



Question 9.


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);
}
}
  1.    3
  2.    0
  3.    6
  4.    1
Explanation:-
Answer: Option D. -> 1

Array arr contains 10 elements. n contains 6 thus in next line n is given value 2 printing

arr[2]/2 i:e 2/2 = 1.



Question 10.


What is the output of this program?


class array_output {
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = 'i';
System.out.print(array_variable[i] + "");
}
}
}
  1.    1 2 3 4 5 6 7 8 9 10
  2.    0 1 2 3 4 5 6 7 8 9 10
  3.    i j k l m n o p q r
  4.    i i i i i i i i i i
Explanation:-
Answer: Option D. -> i i i i i i i i i i