Menu
Topics Index
...
`

Print items for List


If you need explanation Read this topic

If you need Answer Take test on this topic

Print items for List
What will be the output of the following program?
import java.util.*;
public class Hackers
{
    public static void main(String args[])
    {
        int i = 225;
        List list = new ArrayList();
        list.add(new Object());
        list.add("Hackers Hacks");
        list.add(i);
        System.out.println(list.get(1));
    }
}

A. 225
B. Hackers Hacks
C. Compilation Error
D. Runtime Error
Topic: Java ArrayList

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.

here option:B
execution start from main method  in main one variable created i=225
and  a list object was created (its like an array but in array we store similiar type of data but in list we can store different type of data)
and to store data we use a method called add(); ex:l.add(1); (l is list object reference and l.add(1) mean 1 will store in in list at index '0')
in program:
list.add(new Object());(l[0]={reference of object})
list.add("Hackers Hacks");(l[1]={reference of string("Hackers Hacks")})
list.add(i); (l[2]={its store value of i means 225})
to access the values from list object used a method get()
and next statement System.out.println(list.get(1));=>its will print l[1] value called  "Hackers Hacks"
thats why option is B.

Posted by Goutham Singarapu    2015-02-10 14:10:00


ANS IS B (hacers hack)

--here concept of List is used.

--First of all integer I is declared with the value 255.

--After that list is created by passing it the object of arraylist().

--Then,add() method of arraylist is used to add the elements in list.

--At 0th location empty object is added.
1st location string "hacers hack" is added.
2nd location integer I I.e. 255 is added.

--get() method is used to access any element in the list by passing the index value at which element u want to access is present.

--here,index passed is 1...so,"hackers hack" will get printed.

Posted by Mânïshå Mùlchåndânï    2015-02-10 14:22:10


c

Posted by Dinesh Saini    2015-02-10 14:41:18


Ans is B..here we have the AL Concept
AL is Advanced version of the Array where arrays are fixed in the size and arrays can store  homogeneous elements where as AL are Growble in size and can store heterogeneous elements and AL can allow the null insertion and allows Duplicate elements and insertion order is preserved and AL implements RandomAccess interface

Execution:
here we create the instance for the AL class and adding the different types of elements int and object and string..here int is primitive but internally it will perform autoboxing..ad we are accessing the elements at 1 location using the get() as i said insertion order is preserved so it element at 1st index is Hackers Hacks

Posted by Uday Kumar    2015-02-10 14:41:37


b

Posted by Shubham Bansal    2015-02-10 18:59:04


ans b.. ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.add method Inserts the specified element at the specified position index in this list. get () method Returns the element at the specified position in this list. coming to this program i is initialized with 225,thn array list is created. by using add() we added 3 elements. it is accessed from index 0 to 2.index 0 has object, 1 has string, 2 has i value.sop prints index 1 value by using get() method

Posted by Maheshwari Natarajan    2015-02-10 19:55:50


This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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    2015-02-12 04:40:53


© meritcampus 2019

All Rights Reserved.

Open In App