Mettl Coding Skills 4 — Questions and Answers
Question 1: Which design pattern ensures only one instance of a class exists throughout an application?
- Factory
- Observer
- Singleton (Correct answer)
- Decorator
Correct answer: Singleton
The Singleton pattern restricts instantiation to a single object and provides a global access point.
Question 2: What is the output of the following C code snippet: `int x = 5; printf("%d", x++);`?
- 6
- 5 (Correct answer)
- 4
- Undefined behavior
Correct answer: 5
Post-increment returns the original value before incrementing, so 5 is printed.
Question 3: Which of the following best describes a deadlock in concurrent programming?
- A process running indefinitely without producing output
- Two or more processes each waiting for the other to release a resource (Correct answer)
- A thread executing too quickly for the CPU
- A race condition causing data corruption
Correct answer: Two or more processes each waiting for the other to release a resource
Deadlock occurs when processes circularly wait for resources held by each other, halting all progress.
Question 4: In object-oriented programming, what does 'encapsulation' mean?
- Inheriting behavior from a parent class
- Hiding internal state and exposing only necessary interfaces (Correct answer)
- Allowing a method to behave differently based on input type
- Grouping related classes into packages
Correct answer: Hiding internal state and exposing only necessary interfaces
Encapsulation bundles data and methods while restricting direct access to internal state.
Question 5: What is the result of evaluating `5 & 3` in most programming languages (bitwise AND)?
- 8
- 2
- 1 (Correct answer)
- 15
Correct answer: 1
5 is 101 and 3 is 011 in binary; ANDing them yields 001, which equals 1.
Question 6: Which HTTP method is idempotent and used to fully replace a resource?
- POST
- PATCH
- PUT (Correct answer)
- DELETE
Correct answer: PUT
PUT replaces the entire resource and is idempotent — calling it multiple times yields the same result.
Question 7: In Python, what does `dict.get('key', 'default')` return when 'key' is absent?
- None
- KeyError
- 'default' (Correct answer)
- False
Correct answer: 'default'
dict.get() returns the second argument as a fallback when the key does not exist.
Which design pattern ensures only one instance of a class exists throughout an application?