Menu
Topics Index
...
`

Arrays Of Object References


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays Of Object References
What will be the output of the following program?
public class College {
    public static void main(String[] args) {
        Student[] student = new Student[1];
        student[0] = new Student();
        student[0].name = "Khan";
        student[0].marks = 92;
        student[0].section = 'A';
        for (Student element : student) {
            System.out.print(element.name + " ~ ");
            System.out.print(element.marks + " ~ ");
            System.out.print(element.section);
        }
    }
}
class Student {
    String name;
    int marks;
    char section;
}

A. null ~ 0 ~ 
B. Khan ~ 92 ~ A
C. Compilation Error
D. Runtime Error
Topic: Creating Array Of Objects In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

c compilation error is the answer

Posted by Asad Ahmed    2014-11-18 03:47:16


ans is B  student array creates the array which can hold  reference to one Student object,,,
In Each array memory space,the address of the student object is stored which is nothing but object reference we are creating objects with the help of constructor,,but if we try to access the student objects before creating them,,run time errors occur and throws a null pointer exception

Posted by Sai Veerendra    2014-11-18 04:49:03


c-error

Posted by Trinesh Kumar    2014-11-18 10:52:04


B is the ans

Posted by Dereje Goshu    2014-11-18 11:50:14


ans b.arrays are capable of storing objects also. For example, we can create an array of Strings which is a reference type variable. However, using a String as a reference type to illustrate the concept of array of objects isn't too appropriate due to the immutability of String objects. Therefore, for this purpose, we will use a class Student containing a 3 instance variables. Following is the definition of this class.

class Student {string name;
   int marks;                                                                                                                               char section
}

An array of objects is created just like an array of primitive type data items in the following way.

Student[] studentArray = new Student[7];

The above statement creates the array which can hold references to 1 Student objects. It doesn't create the Student objects themselves. They have to be created separately using the constructor of the Student class. The studentArray contains 1 memory spaces in which the address of 1 Student objects may be stored. If we try to access the Student objects even before creating them, run time errors would occur. For instance, the following statement throws a NullPointerException during runtime which indicates that studentArray[0] isn't yet pointing to a Student object.

studentArray[0].marks = 100;

The Student objects have to be instantiated using the constructor of the Student class and their references should be assigned to the array elements in the following way.

studentArray[0] = new Student();                                                                                    thn instances variables assigned with respective values.thn for loop executed thn sop prints student details

Posted by Maheshwari Natarajan    2014-11-18 18:31:34


Ans is B..here we have the Concept of the Array Of References...Array is an Collection of similar data elements which are stored in adjacent memory locations...

here we have class Student which have 3 instance variables name,marks and  section and these class has Default Constructor which is created by system...

Coming to the program here we create Array of Student object with size 1...
here we create reference only...we didnt create any objects till now...

to create objects we use the keyword new...

In next statement we are creating Student Objecta nd Storing that Object in 0 Location....

now the default Constructor get invoked and Memory allocation will done for the instance variables in class Student

here we didnt initilize any values to these variables so that JVM will give default values for it...next we are assigning values to this variables using the Object....

Student[0]---->name="Khan";
Student[0]---->marks=92;
Student[0]-->section='A';

Posted by Uday Kumar    2014-11-19 00:41:07


This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Maheshwari Natarajan, Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Asad Ahmed. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-19 01:30:29


© meritcampus 2019

All Rights Reserved.

Open In App