MCQs on Advanced Table Manipulation | Lua

1. Nested Tables

  1. What is a nested table in Lua?
    • a) A table that contains another table as its element
    • b) A table that stores multiple values
    • c) A table with only numerical indices
    • d) A function that returns a table
  2. How do you access an element of a nested table in Lua?
    • a) table[key][subkey]
    • b) table.subkey[key]
    • c) table[key]
    • d) table.get(subkey)
  3. Can you have tables within tables in Lua?
    • a) Yes, tables can contain other tables
    • b) No, Lua does not allow nested tables
    • c) Yes, but only one level deep
    • d) No, only arrays are allowed
  4. How do you store a table inside another table in Lua?
    • a) table1 = {table2}
    • b) table1 = table2
    • c) table1 = table2[0]
    • d) table1 = table2()
  5. What is the output of accessing a nested table element like table[1][2]?
    • a) The value at the first index of the first table and the second index of the second table
    • b) A syntax error
    • c) The second element of the first table
    • d) The first element of the second table
  6. How can you modify an element of a nested table in Lua?
    • a) table[1][2] = value
    • b) table[1] = value[2]
    • c) table[2][1] = value
    • d) table[1][2].value = value
  7. Is it possible to have tables as values and keys in Lua tables?
    • a) Yes, Lua supports tables as both keys and values
    • b) No, only strings and numbers can be keys
    • c) Yes, but only when the keys are strings
    • d) No, tables can only be values
  8. Can you have a table as a key to another table in Lua?
    • a) Yes, but only if the table is a string
    • b) Yes, Lua tables can have other tables as keys
    • c) No, tables cannot be used as keys
    • d) Yes, only if the table is mutable
  9. What is the behavior of table[1][1] when the first table is empty?
    • a) Returns nil
    • b) Throws an error
    • c) Creates a new table
    • d) Returns an empty string
  10. Can you iterate over a nested table using the for loop in Lua?
    • a) Yes, using nested for loops
    • b) No, for loops do not support nested tables
    • c) Yes, using the pairs() function only
    • d) No, you must manually access each element

2. Using Table Library Functions

  1. Which function is used to add an element to the end of a table in Lua?
    • a) table.insert()
    • b) table.push()
    • c) table.add()
    • d) table.append()
  2. How do you remove an element from a table in Lua?
    • a) table.remove()
    • b) table.delete()
    • c) table.pop()
    • d) table.drop()
  3. What is the result of using table.sort() in Lua?
    • a) Sorts the elements of the table in ascending order
    • b) Sorts the keys of the table
    • c) Removes duplicate elements
    • d) Reverses the table
  4. What is the purpose of table.concat() in Lua?
    • a) Joins elements of a table into a single string
    • b) Sorts elements of a table
    • c) Adds elements to a table
    • d) Removes elements from a table
  5. How can you find the length of a table in Lua?
    • a) #table
    • b) table.length()
    • c) table.size()
    • d) table.count()
  6. Which of the following is used to insert a value at the beginning of a table in Lua?
    • a) table.insert(table, 1, value)
    • b) table.push(value)
    • c) table.addFirst(value)
    • d) table.unshift(value)
  7. What happens if you try to remove an element from an empty table using table.remove()?
    • a) Returns nil
    • b) Throws an error
    • c) Does nothing
    • d) Creates a new element
  8. What does table.sort() do when passed a custom comparison function?
    • a) Sorts the table using the custom function’s logic
    • b) Ignores the function and sorts normally
    • c) Sorts the table in reverse order
    • d) Throws an error
  9. Which function in Lua can combine the elements of a table into a string with a delimiter?
    • a) table.concat()
    • b) table.join()
    • c) table.merge()
    • d) table.mergeBy()
  10. How do you clear all elements from a table in Lua?
    • a) table.clear()
    • b) table.removeAll()
    • c) table = {}
    • d) table.empty()

3. Key-Value Pairs

  1. What is the basic structure of a key-value pair in a Lua table?
    • a) key = value
    • b) value = key
    • c) key -> value
    • d) value -> key
  2. How can you access the value of a specific key in a Lua table?
    • a) table[key]
    • b) table.getValue(key)
    • c) table:retrieve(key)
    • d) table.access(key)
  3. What happens if you try to access a non-existent key in a Lua table?
    • a) Returns nil
    • b) Returns false
    • c) Throws an error
    • d) Returns 0
  4. Can you change the value associated with a key in a Lua table?
    • a) Yes, by assigning a new value to the key
    • b) No, once a key is created, its value cannot be changed
    • c) Yes, but only if the key is a number
    • d) Yes, but only if the value is a string
  5. Which function is used to get all the keys of a table in Lua?
    • a) table.keys()
    • b) pairs()
    • c) table.getKeys()
    • d) table.indexes()
  6. What is the default value returned if you try to access an undefined key in a Lua table?
    • a) nil
    • b) false
    • c) 0
    • d) undefined
  7. Can a Lua table have multiple keys pointing to the same value?
    • a) Yes, tables can have duplicate keys
    • b) No, each key must be unique
    • c) Yes, but only if the values are numbers
    • d) No, keys are automatically unique
  8. How do you check if a key exists in a Lua table?
    • a) if table[key] then
    • b) if key in table then
    • c) if table.has(key)
    • d) if table.contains(key)
  9. Can you use table.insert() to add a key-value pair to a table?
    • a) No, table.insert() is for arrays only
    • b) Yes, but only if the value is a number
    • c) Yes, table.insert() can insert key-value pairs
    • d) No, use table.add() instead
  10. How do you remove a specific key-value pair from a table in Lua?
    • a) table[key] = nil
    • b) table.remove(key)
    • c) table.delete(key)
    • d) table.removeValue(key)

Answer Key

QnoAnswer
1a) A table that contains another table as its element
2a) table[key][subkey]
3a) Yes, tables can contain other tables
4a) table1 = {table2}
5a) The value at the first index of the first table and the second index of the second table
6a) table[1][2] = value
7a) Yes, Lua supports tables as both keys and values
8b) Yes, Lua tables can have other tables as keys
9a) Returns nil
10a) Yes, using nested for loops
11a) table.insert()
12a) table.remove()
13a) Sorts the elements of the table in ascending order
14a) Joins elements of a table into a single string
15a) #table
16a) table.insert(table, 1, value)
17a) Returns nil
18a) Sorts the table using the custom function’s logic
19a) table.concat()
20c) table = {}
21a) key = value
22a) table[key]
23a) Returns nil
24a) Yes, by assigning a new value to the key
25b) pairs()
26a) nil
27a) Yes, tables can have duplicate keys
28a) if table[key] then
29a) No, table.insert() is for arrays only
30a) table[key] = nil

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