Interviews - C# Corner

Sandeep Soni
What happens when the following elements get inserted in an ArrayList?
  1. al.Add(1);
  2. al.Add('1');
  3. al.Add("1");

a) Compilation Error
b) Code works perfectly - Prints 1 ‘1’ “1” if an Console.Write statement is written.
c) System.IndexOutOfRangeException
d) System.InvalidCastException
e) System.StackOverflowException
f) System.IO.IOException

By Sandeep Soni in C# onNov 18 2018
  • Kenny Dong
    Nov, 2018 23

    I used foreach and print every elements with the tostring method, there is no exception and displayed 1 1 1

    • 4
  • Tushar Dikshit
    Apr, 2019 18

    Unlike arrays, Arraylist can have objects of different types. So it will work. b.

    • 3
  • Jignesh Kumar
    Mar, 2019 11

    it will print if it loop trough 1 1 1

    • 1
  • Mangesh Kanche
    Dec, 2018 24

    Since ArrayList is class belongs to Collection, it will hold the heterogeneous types of data. So No problem will occur and all the values will get stored into ArrayList.

    • 1
  • Alan Moore
    Nov, 2018 24

    b) Since ArrayList is not generically typed it should work perfectly

    • 1
  • Muhammad Zubair
    Aug, 2019 4

    please post correct answer… I think code works perfectly.

    • 0
  • Bidyasagar Mishra
    Jul, 2019 11

    print 1,1,1

    • 0
  • sushil kumar
    Mar, 2019 6

    b) code works perfectly- print 1,'1',"1"

    • 0
  • Amir Khan
    Mar, 2019 6

    in c language array hold only same data elements .But in c sharp we store any type of data in arraylist

    • 0
  • Deepak Vishwakarma
    Feb, 2019 22

    b) Code works perfectly & Prints 1 1 1

    • 0
  • Nikki Nagarkar
    Feb, 2019 5

    It prints 1 1 1

    • 0
  • Amit Prakash
    Jan, 2019 2

    System.InvalidCastException

    • 0
  • Amit Prakash
    Jan, 2019 2

    No problem will occur and all the values will get stored into ArrayList

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS