Cognizant Coding and Programming Round 2 ā Questions and Answers
Question 1: What is the time complexity of binary search on a sorted array of n elements?
- O(n)
- O(log n) (Correct answer)
- O(n log n)
- O(1)
Correct answer: O(log n)
Binary search halves the search space each step, yielding O(log n) time complexity.
Question 2: Which data structure is used to implement a recursive function call stack?
- Queue
- Heap
- Stack (Correct answer)
- Linked List
Correct answer: Stack
Function calls are pushed onto a call stack and popped when functions return, following LIFO order.
Question 3: In Python, what does the 'self' parameter in a class method refer to?
- The class itself
- The current instance of the class (Correct answer)
- The parent class
- A static variable
Correct answer: The current instance of the class
'self' refers to the current object instance, allowing access to its attributes and methods.
Question 4: What will be the output of: print(type(1/2)) in Python 3?
- <class 'int'>
- <class 'float'> (Correct answer)
- <class 'double'>
- <class 'fraction'>
Correct answer: <class 'float'>
In Python 3, the / operator always returns a float, so 1/2 yields 0.5 of type float.
Question 5: Which sorting algorithm has the best average-case time complexity?
- Bubble Sort
- Selection Sort
- Merge Sort (Correct answer)
- Insertion Sort
Correct answer: Merge Sort
Merge Sort has O(n log n) average-case complexity, better than O(n²) algorithms like Bubble, Selection, or Insertion Sort.
Question 6: What is a dangling pointer in C/C++?
- A pointer to a null value
- A pointer to freed or out-of-scope memory (Correct answer)
- A pointer that points to another pointer
- An uninitialized pointer
Correct answer: A pointer to freed or out-of-scope memory
A dangling pointer references memory that has been freed or gone out of scope, leading to undefined behavior.
Question 7: In Java, which keyword prevents a method from being overridden in a subclass?
- static
- private
- final (Correct answer)
- abstract
Correct answer: final
The 'final' keyword applied to a method prevents subclasses from overriding it.
What is the time complexity of binary search on a sorted array of n elements?