Cognizant Technical Screening 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 iteration, yielding O(log n) time complexity.
Question 2: Which SQL clause is used to filter groups after a GROUP BY operation?
- WHERE
- HAVING (Correct answer)
- FILTER
- ON
Correct answer: HAVING
HAVING filters rows after GROUP BY aggregation, while WHERE filters before grouping.
Question 3: In object-oriented programming, what does 'encapsulation' mean?
- Inheriting properties from a parent class
- Bundling data and methods that operate on that data within one unit (Correct answer)
- Using one interface for many data types
- Hiding implementation by using abstract classes only
Correct answer: Bundling data and methods that operate on that data within one unit
Encapsulation bundles data (fields) and behavior (methods) together and restricts direct access to internal state.
Question 4: What does the 'volatile' keyword guarantee in Java?
- The variable is immutable
- Reads and writes to the variable are always done from main memory (Correct answer)
- The variable is thread-local
- The variable is garbage-collected immediately
Correct answer: Reads and writes to the variable are always done from main memory
volatile ensures visibility across threads by bypassing CPU cache and always reading/writing to main memory.
Question 5: Which data structure follows the Last-In-First-Out (LIFO) principle?
- Queue
- Stack (Correct answer)
- Deque
- Priority Queue
Correct answer: Stack
A stack removes the most recently added element first, following LIFO order.
Question 6: What is the output of 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; bitwise AND yields 001 which equals 1.
Question 7: In a relational database, what does a foreign key enforce?
- Uniqueness of column values
- Referential integrity between two tables (Correct answer)
- Non-null constraint on a column
- Indexed access to rows
Correct answer: Referential integrity between two tables
A foreign key ensures that the value in one table's column exists as a primary key in another table, maintaining referential integrity.
What is the time complexity of binary search on a sorted array of n elements?