Mettl Coding Skills 2 — Questions and Answers
Question 1: What will the following Python code output? x = [1, 2, 3, 4, 5] print(x[1:4])
- [1, 2, 3]
- [2, 3, 4] (Correct answer)
- [2, 3, 4, 5]
- [1, 2, 3, 4]
Correct answer: [2, 3, 4]
Python slicing x[1:4] returns elements at indices 1, 2, 3 (not including index 4): x[1]=2, x[2]=3, x[3]=4. Result: [2, 3, 4].
Python list slicing follows the pattern list[start:end:step]. x[1:4] starts at index 1 (value=2) and goes up to but NOT including index 4 (value=5). So it returns elements at indices 1, 2, 3: [2, 3, 4]. Key rules: indices are 0-based, the end index is exclusive, negative indices count from the end. List slicing is frequently tested in Mettl coding assessments for Python roles.
Question 2: What is the time complexity of binary search?
- O(n)
- O(log n) (Correct answer)
- O(n²)
- O(n log n)
Correct answer: O(log n)
Binary search halves the search space with each comparison, giving O(log n) time complexity.
Binary search operates on sorted arrays. At each step, it compares the target to the middle element and eliminates half the array. With n=1024 elements, it takes at most log₂(1024) = 10 comparisons. General formula: O(log n). Compare: Linear search = O(n), Bubble sort = O(n²), Merge sort = O(n log n). The prerequisite for binary search is a sorted array. Algorithm complexity is a core topic in Mettl coding and technical assessments.
Question 3: Which SQL statement is used to retrieve unique values from a column?
- SELECT UNIQUE
- SELECT DISTINCT (Correct answer)
- SELECT DIFFERENT
- SELECT SINGLE
Correct answer: SELECT DISTINCT
SELECT DISTINCT eliminates duplicate rows from the result set.
SQL's SELECT DISTINCT statement returns only unique values: SELECT DISTINCT column_name FROM table_name. Without DISTINCT, duplicate values appear for each row. Example: If a 'City' column has values [Delhi, Mumbai, Delhi, Chennai], SELECT DISTINCT City returns [Delhi, Mumbai, Chennai]. SQL knowledge — including SELECT, WHERE, GROUP BY, JOIN, DISTINCT — is tested in Mettl coding assessments for database and software development roles.
Question 4: In object-oriented programming, what is 'encapsulation'?
- Inheriting properties from a parent class
- Bundling data and methods into a single unit and restricting access (Correct answer)
- A class having multiple forms
- Breaking a problem into smaller functions
Correct answer: Bundling data and methods into a single unit and restricting access
Encapsulation bundles data (attributes) and the methods that operate on that data into a single unit (class) and controls access through access modifiers.
The four pillars of OOP: Encapsulation, Inheritance, Polymorphism, Abstraction. Encapsulation: bundles data and methods into a class, uses access modifiers (private, protected, public) to control visibility, prevents direct access to data (data hiding). Example: A BankAccount class with private balance and public deposit()/withdraw() methods. Inheritance: child class inherits parent properties. Polymorphism: same interface, different implementations. OOP concepts are fundamental in Mettl coding assessments for software development roles.
Question 5: What does the following pseudocode return for input n=5? function factorial(n): if n == 0: return 1 return n * factorial(n-1)
- 24
- 120 (Correct answer)
- 60
- 720
Correct answer: 120
factorial(5) = 5 × factorial(4) = 5 × 4 × 3 × 2 × 1 = 120.
Recursive functions call themselves with modified parameters until reaching a base case. Tracing factorial(5): factorial(5) = 5 × factorial(4) = 5 × 4 × factorial(3) = 5 × 4 × 3 × factorial(2) = 5 × 4 × 3 × 2 × factorial(1) = 5 × 4 × 3 × 2 × 1 × factorial(0) = 5 × 4 × 3 × 2 × 1 × 1 = 120. Base case: factorial(0) = 1. Recursion tracing and algorithm output prediction are key skills in Mettl coding sections.
Question 6: What is the output of: print(type(3.14)) in Python?
- <class 'int'>
- <class 'float'> (Correct answer)
- <class 'double'>
- <class 'decimal'>
Correct answer: <class 'float'>
3.14 is a floating-point number. In Python, type() returns <class 'float'> for floating-point values.
Python's type system: int (integers: 1, -5, 0), float (decimals: 3.14, -2.7, 1.0), str (strings: 'hello'), bool (True/False), list, dict, tuple. The type() function returns the data type of an object. Python uses 'float' (not 'double' as in Java/C++) for all floating-point numbers. Note: type(3) → <class 'int'>, type('3') → <class 'str'>, type(True) → <class 'bool'>. Data type questions are frequently tested in Mettl Python assessments.
What will the following Python code output?
x = [1, 2, 3, 4, 5]
print(x[1:4])