VBA Interview questions - Part 3 - Array concept

Hello Readers,

Welcome to Coding by Learning !!! 😀 

Lets see interview questions on VBA array concepts.

1. What is array?

  • Array is a group of elements of similar data types

2. How to declare a array variable?

  • Syntax : Dim arrayVariableName(size) As Datatype
  • Ex : Dim fruits(5) As String

3. What is ReDim keyword in VBA?

  • Using ReDim keyword developer can redefine the size of an array

4. How to use ReDim in VBA?

  • Syntax : ReDim arrayVariableName(size) As Datatype 
  • Ex : ReDim fruits(10) As String

5. I have already stored some elements in the array, what will happen if I redefine the size?

  • When you redefine the size of an array without preserve keyword, then already stored elements will be erased from the array

6. What is preserve keyword in VBA?

  • Preserve keyword in VBA helps to retain already stored elements in the array even though it is redefined.
  • Example :  ReDim Preserve fruits(10) As String

7. What is option base in VBA?

  • Option base is related with array concepts. It defines what is Lbound i.e lower bound value/elements  limit of an array.
  • If developer has not declared option base, then default lower bound value is 0. So the array index will start with value 0.

8. What is LBOUND  and UBOUND in VBA?

  • LBOUND  and UBOUND helps to define lower bound and upper bound limit of array elements.
9. How to find size of an array?
  • UBOUND – LBOUND  + 1  gives the size of an array.
  • Example :


Output :


10. What error VBA gives if developer tries to insert elements beyond array size?

  • VBA gives subscription out of range error when developer inserts array elements more than the size of it.
  • Example : Lets try to add 4th element to fruits array which size is 3

Output:

When run the program, will get following error





Happy learning & Happy coding!!!

Comment and share this post to your friends and colleagues 😀

Comments

  1. Thanks! Pls give example program in upcoming blogs to use 'preserve' keyword sir.

    ReplyDelete

Post a Comment

Popular posts from this blog

VBA Interview Questions - Part 2 - Variables and Data types

VBA Interview questions - screen updating