MCQs on Tables: The Core Data Structure | Lua

1. Introduction to Tables

  1. What is a table in Lua?
    • a) A data structure used to store data
    • b) A type of variable
    • c) A function in Lua
    • d) A type of loop
  2. Which symbol is used to define a table in Lua?
    • a) []
    • b) {}
    • c) ()
    • d) ||
  3. How do you create an empty table in Lua?
    • a) table = {}
    • b) table = []
    • c) table = ()
    • d) table = ||
  4. What is the default index type for elements in a Lua table?
    • a) Integer
    • b) String
    • c) Boolean
    • d) Any type
  5. How do you add a key-value pair to a table in Lua?
    • a) table[key] = value
    • b) table.key = value
    • c) add(table, key, value)
    • d) table.add(key, value)
  6. Can Lua tables hold different types of data in the same table?
    • a) Yes, tables can store mixed data types
    • b) No, tables can only store one type of data
    • c) Yes, but only integers
    • d) Yes, but only strings
  7. How do you initialize a table with multiple elements in Lua?
    • a) table = {1, 2, 3}
    • b) table = [1, 2, 3]
    • c) table = (1, 2, 3)
    • d) table = {1; 2; 3}
  8. What happens if you try to access a table key that doesn’t exist?
    • a) Lua throws an error
    • b) Lua returns nil
    • c) Lua returns false
    • d) Lua creates the key with a default value
  9. Can you use tables to represent objects in Lua?
    • a) Yes, tables are used as objects
    • b) No, tables can only store data
    • c) Yes, but only for arrays
    • d) No, Lua doesn’t support objects

2. Indexing and Iterating Through Tables

  1. How do you access the value of a table element with a specific key?
    • a) table[key]
    • b) table.get(key)
    • c) table:key
    • d) table.getValue(key)
  2. Which method is commonly used to iterate through all keys and values in a table?
    • a) for loop with pairs()
    • b) for loop with next()
    • c) while loop with pairs()
    • d) repeat...until loop with pairs()
  3. How can you iterate through the indices of an array-like table in Lua?
    • a) for i, v in ipairs(table)
    • b) for i, v in pairs(table)
    • c) for i = 1, table
    • d) for i in table
  4. What is the function used to retrieve the next key-value pair in an iteration?
    • a) next()
    • b) pairs()
    • c) get()
    • d) nextkey()
  5. What does the ipairs() function iterate through?
    • a) Only the numerical indices of a table
    • b) All keys in a table
    • c) Only the string keys of a table
    • d) None of the above
  6. What will the pairs() function return when iterating over a table?
    • a) Key-value pairs
    • b) Only the keys
    • c) Only the values
    • d) Table size
  7. Which of the following will correctly iterate through a table using a for loop?
    • a) for i, v in pairs(table)
    • b) for i, v in ipairs(table)
    • c) Both of the above
    • d) None of the above
  8. In Lua, what does the next() function return?
    • a) The next key-value pair in a table
    • b) The first key in a table
    • c) A boolean value
    • d) A string
  9. Can a table in Lua have both numerical indices and string keys?
    • a) Yes, tables can have both
    • b) No, a table can only have one type of key
    • c) Yes, but only string keys
    • d) Yes, but only numerical indices
  10. What is the output of iterating through a table with for k, v in pairs(table)?
    • a) Key-value pairs
    • b) Only the keys
    • c) Only the values
    • d) An error

3. Using Tables as Arrays

  1. How do you use a table in Lua as an array?
    • a) By using consecutive integer indices
    • b) By using string indices
    • c) By using the array keyword
    • d) By defining a table inside another table
  2. What happens when you assign an array element in Lua without using indices?
    • a) It creates a new key-value pair
    • b) It causes an error
    • c) It creates a new index
    • d) It overwrites the entire table
  3. How do you remove an element from a table when using it as an array?
    • a) table.remove()
    • b) table.delete()
    • c) table.clear()
    • d) table.pop()
  4. Which function can be used to get the length of a table when used as an array?
    • a) #table
    • b) table.length()
    • c) table.size()
    • d) table.count()
  5. What happens if you try to access an array element with an index beyond the table’s length in Lua?
    • a) Lua returns nil
    • b) Lua throws an error
    • c) Lua returns false
    • d) Lua creates a new index
  6. How do you iterate through a table used as an array?
    • a) for i = 1, #table do
    • b) for i, v in ipairs(table)
    • c) for i, v in pairs(table)
    • d) Both a and b
  7. How do you initialize an empty array in Lua?
    • a) table = {}
    • b) array = []
    • c) array = ()
    • d) array = ||
  8. What is the default index of the first element when using a table as an array in Lua?
    • a) 0
    • b) 1
    • c) -1
    • d) nil
  9. How do you insert an element at the beginning of a table used as an array?
    • a) table.insert(table, 1, value)
    • b) table.add(1, value)
    • c) table.unshift(value)
    • d) table.push(1, value)
  10. Can a Lua table be resized when used as an array?
    • a) Yes, tables are dynamic
    • b) No, tables have fixed size
    • c) Only when using array type
    • d) No, only arrays in C can be resized
  11. How do you find the index of an element in a Lua array?
    • a) table.indexOf(value)
    • b) table.find(value)
    • c) table.index(value)
    • d) Lua does not provide a built-in function for this

Answer Key

QnoAnswer
1a) A data structure used to store data
2b) {}
3a) table = {}
4a) Integer
5a) table[key] = value
6a) Yes, tables can store mixed data types
7a) table = {1, 2, 3}
8b) Lua returns nil
9a) Yes, tables are used as objects
10a) table[key]
11a) for loop with pairs()
12a) for i, v in ipairs(table)
13a) next()
14a) Only the numerical indices of a table
15a) Key-value pairs
16c) Both of the above
17a) The next key-value pair in a table
18a) Yes, tables can have both
19a) Key-value pairs
20a) By using consecutive integer indices
21a) It creates a new key-value pair
22a) table.remove()
23a) #table
24a) Lua returns nil
25d) Both a and b
26a) table = {}
27b) 1
28a) table.insert(table, 1, value)
29a) Yes, tables are dynamic
30d) Lua does not provide a built-in function for this

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top