Cognizant Coding and Programming Round 3 ā Questions and Answers
Question 1: What is the output of the following C code: int x = 5; printf("%d", x++);
- 4
- 5 (Correct answer)
- 6
- Undefined
Correct answer: 5
Post-increment (x++) uses the current value of x (5) in the expression, then increments it afterward.
Question 2: Which of the following best describes a hash collision?
- Two keys map to the same hash value (Correct answer)
- A hash function returns a negative value
- The hash table is full
- Two different hash functions produce different results
Correct answer: Two keys map to the same hash value
A hash collision occurs when two distinct keys produce the same hash index, requiring a resolution strategy.
Question 3: What does the SQL statement 'SELECT DISTINCT' do?
- Selects only the first row
- Removes duplicate rows from the result set (Correct answer)
- Sorts the result in ascending order
- Selects rows with NULL values
Correct answer: Removes duplicate rows from the result set
DISTINCT eliminates duplicate rows from the query result, returning only unique values.
Question 4: In object-oriented programming, what is encapsulation?
- Inheriting methods from a parent class
- Bundling data and methods that operate on it within a class (Correct answer)
- Creating multiple objects from one class
- Overriding parent class methods
Correct answer: Bundling data and methods that operate on it within a class
Encapsulation bundles data (fields) and behavior (methods) together in a class while restricting direct external access.
Question 5: Which traversal of a binary search tree produces nodes in sorted order?
- Pre-order
- Post-order
- In-order (Correct answer)
- Level-order
Correct answer: In-order
In-order traversal (left ā root ā right) visits BST nodes in ascending sorted order.
Question 6: What is the space complexity of Merge Sort?
- O(1)
- O(log n)
- O(n) (Correct answer)
- O(n²)
Correct answer: O(n)
Merge Sort requires O(n) auxiliary space for the temporary arrays used during merging.
Question 7: In Python, what is the result of: [] == [] and [] is []?
- True and True
- True and False (Correct answer)
- False and True
- False and False
Correct answer: True and False
'==' checks value equality (both empty lists are equal), while 'is' checks identity (they are different objects in memory).
What is the output of the following C code: int x = 5; printf("%d", x++);