Mettl Coding Skills 5 — Questions and Answers
Question 1: What is the primary purpose of an abstract class in Java?
- To allow multiple inheritance
- To define a template with some methods left for subclasses to implement (Correct answer)
- To prevent any instantiation permanently
- To provide static utility methods
Correct answer: To define a template with some methods left for subclasses to implement
Abstract classes define partial implementations and enforce contracts via abstract methods that subclasses must implement.
Question 2: Which of the following correctly identifies a stack overflow?
- Accessing an index beyond array bounds
- Exceeding the call stack limit, often due to infinite recursion (Correct answer)
- Writing past the end of a heap-allocated buffer
- Running out of disk space during file I/O
Correct answer: Exceeding the call stack limit, often due to infinite recursion
A stack overflow occurs when recursive (or deeply nested) calls exhaust the fixed-size call stack.
Question 3: In CSS, which selector has the highest specificity?
- Element selector (e.g., `p`)
- Class selector (e.g., `.btn`)
- ID selector (e.g., `#header`) (Correct answer)
- Universal selector (`*`)
Correct answer: ID selector (e.g., `#header`)
ID selectors carry the highest specificity weight (0,1,0,0) among standard CSS selectors.
Question 4: What is the difference between '==' and '===' in JavaScript?
- No difference; both check value and type
- '==' checks value only with type coercion; '===' checks value and type strictly (Correct answer)
- '===' checks value only; '==' checks value and type
- '===' is used only for objects
Correct answer: '==' checks value only with type coercion; '===' checks value and type strictly
'==' performs type coercion before comparing, while '===' requires both value and type to match.
Question 5: Which of the following is an example of a linear data structure?
- Binary Tree
- Graph
- Hash Map
- Linked List (Correct answer)
Correct answer: Linked List
A linked list is linear because elements are arranged sequentially, each pointing to the next.
Question 6: In Big O notation, what does O(1) represent?
- The algorithm runs in one second
- Performance degrades linearly with input size
- The algorithm's runtime is constant regardless of input size (Correct answer)
- The algorithm uses one byte of memory
Correct answer: The algorithm's runtime is constant regardless of input size
O(1) means the operation takes the same amount of time no matter how large the input is.
Question 7: Which concept allows a function to call itself with a smaller sub-problem until a base case is reached?
- Iteration
- Polymorphism
- Recursion (Correct answer)
- Abstraction
Correct answer: Recursion
Recursion is the technique where a function calls itself, breaking the problem down until a base case terminates the calls.
What is the primary purpose of an abstract class in Java?