Mettl Fundamental Coding Skills Questions and Answers — Questions and Answers
Question 1: A developer is implementing the 'Back' button functionality for a web browser. Each time a user visits a new page, the URL is stored. When the user clicks 'Back,' the most recently visited URL must be retrieved. Which data structure is the most suitable for managing the browsing history for this feature?
- Queue
- Stack (Correct answer)
- Array List
- Linked List
Correct answer: Stack
A Stack operates on a Last-In, First-Out (LIFO) principle. The last page visited (the most recent) is the first one to be retrieved when the 'Back' button is pressed, which perfectly matches the behavior of a stack. A Queue operates on a First-In, First-Out (FIFO) basis and would be incorrect.
Question 2: What is the worst-case time complexity for searching for an element in an unsorted array of size 'n' using a linear search?
- O(log n)
- O(n^2)
- O(n) (Correct answer)
- O(1)
Correct answer: O(n)
In the worst-case scenario for a linear search on an unsorted array, the algorithm must check every single element to determine if the target is present. This occurs when the element is at the very end of the array or not in the array at all. Therefore, the time taken is directly proportional to the number of elements, 'n', resulting in a linear time complexity of O(n).
Question 3: Review the following pseudo-code snippet. What will be the final value of the `count` variable? ```pseudo-code count = 0 FOR i FROM 1 TO 5 IF i % 2 == 0 THEN count = count + i ELSE count = count - 1 END IF END FOR ```
- 3 (Correct answer)
- 6
- 9
- 15
Correct answer: 3
The loop iterates five times: - i=1 (odd): count becomes 0 - 1 = -1 - i=2 (even): count becomes -1 + 2 = 1 - i=3 (odd): count becomes 1 - 1 = 0 - i=4 (even): count becomes 0 + 4 = 4 - i=5 (odd): count becomes 4 - 1 = 3 The final value of `count` is 3.
Question 4: Which of the following Object-Oriented Programming (OOP) principles allows a class to have multiple methods with the same name but different parameter lists (signatures)?
- Inheritance
- Encapsulation
- Polymorphism (Correct answer)
- Abstraction
Correct answer: Polymorphism
Polymorphism, which means 'many forms,' allows objects to be treated in multiple ways. A key aspect of this is method overloading (a form of compile-time polymorphism), which is the ability to define multiple methods with the same name within the same class, distinguished by the number or type of their parameters.
Question 5: What is the primary risk associated with writing a recursive function that does not have a well-defined base case?
- A syntax error during compilation.
- Slower execution compared to an iterative solution.
- An infinite recursion leading to a stack overflow error. (Correct answer)
- The function returning an incorrect value.
Correct answer: An infinite recursion leading to a stack overflow error.
A recursive function calls itself. A base case is a condition that terminates the recursion. Without a proper base case, the function will call itself indefinitely. Each function call adds a new frame to the call stack, which has a finite amount of memory. Eventually, the stack runs out of space, causing a stack overflow error and crashing the program.
Question 6: A developer declares a variable inside a function. Another function in the same program tries to access this variable but fails, resulting in a reference error. What is the most likely reason for this failure?
- The variable was declared as a constant.
- The variable has local scope. (Correct answer)
- The variable has a data type mismatch.
- The variable was not initialized before use.
Correct answer: The variable has local scope.
Variables declared inside a function have local scope by default. This means they only exist and are accessible within that specific function. They cannot be seen or accessed by other functions. To be accessible across multiple functions, a variable would need to be declared in a broader (e.g., global) scope.
A developer is implementing the 'Back' button functionality for a web browser.
Each time a user visits a new page, the URL is stored.
When the user clicks 'Back,' the most recently visited URL must be retrieved.
Which data structure is the most suitable for managing the browsing history for this feature?