1. Assume you have two instances of Student Object. Which is following array declaration is correct ?
- Student students(student1, student2});
- Student [] students(student1,student2);
- Student students = [student1,student2];
- Student [] students = {student1, student2};
Answer: 4
2. A Product class contains an encapsulated array field of Product items. How would you write a getProductItems method in the Product class to return these items?
A) public Product getProductItems(){ return items; } B) public void getProductItems(Product items){ return items; } C) public void getProductItems(){ return items; } D) public Product[] getProductItems(){ return items; }
Answer: D
3. What is the output of the following program?
public static final int size = 3; public static void main(String[] args) { int[] data = new int[size]; data[3] = 30; for(int i=0;i<data.length;i++) { System.out.println(data[i]); } }
- 0, 0 ,30
- Compilation Error
- Runtime Error
- Non Of the above
Answer: 3 throws java.lang.ArrayIndexOutOfBoundsException