Menu
Topics Index
...
`

Arrays Of Object References2834


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays Of Object References2834
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].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.

Ans is D..here we have the class Student with the following properties
name,marks,section
here we create the array of student references in the main class with size 1 but we did not create any instance for the class Student here...creating reference wont allocate memory for the Properties of the class

so next we are assigning the values for the properties in the Student class..but here Jvm will throw the NullPointerException  Bcz here we did not create the instance for the class Student,just we create the reference for the class..so if we try to access the  properties of class without creating the object so these Exception will thrown..

Posted by Uday Kumar    2014-12-10 09:11:16


ans d.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(); but here no such object created so nullPointerException get thrown

Posted by Maheshwari Natarajan    2014-12-10 15:35:11


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. 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-12-11 05:28:00


© meritcampus 2019

All Rights Reserved.

Open In App