Cognizant Coding and Programming Round 1 — Questions and Answers
Question 1: What will be the output of the following Python code? ```python x = [1, 2, 3] y = x y.append(4) print(x) ```
- [1, 2, 3]
- [1, 2, 3, 4] (Correct answer)
- [4, 1, 2, 3]
- Error
Correct answer: [1, 2, 3, 4]
In Python, when `y = x` is executed, `y` does not create a new list; instead, it creates a new reference that points to the *same* list object that `x` refers to. Therefore, when `y.append(4)` modifies the list, it modifies the single list object shared by both `x` and `y`. Consequently, printing `x` will display the modified list `[1, 2, 3, 4]`.
Question 2: Which sorting algorithm has the worst-case time complexity of O(n^2)?
- Merge Sort
- Quick Sort
- Bubble Sort (Correct answer)
- Heap Sort
Correct answer: Bubble Sort
Bubble Sort is a simple sorting algorithm known for its worst-case time complexity of O(n^2). This occurs when the array is sorted in reverse order, requiring the algorithm to perform the maximum number of comparisons and swaps. While easy to understand, its inefficiency makes it impractical for large datasets compared to more advanced sorting algorithms.
Question 3: What does the following Java code print? ```java int x = 5; System.out.println(x++ + ++x); ```
- 10
- 11
- 12 (Correct answer)
- 13
Correct answer: 12
The expression `x++` is a post-increment operator, meaning `x`'s current value (5) is used in the expression first, then `x` is incremented to 6. The `++x` is a pre-increment operator, so `x` is incremented first (from 6 to 7), and then its new value (7) is used. Therefore, the calculation becomes `5 + 7`, resulting in 12.
Question 4: What is the purpose of the 'break' statement in a loop?
- To skip the next iteration
- To exit the loop immediately (Correct answer)
- To reset the loop counter
- To jump to the end of the program
Correct answer: To exit the loop immediately
The 'break' statement is a control flow statement used to terminate the execution of the innermost loop (e.g., `for`, `while`, `do-while`) or `switch` statement immediately. When encountered, program control jumps to the statement directly following the loop or `switch` block. Its primary purpose is to exit a loop prematurely based on a specific condition.
Question 5: What does the following C++ code snippet do? ```cpp #include <iostream> using namespace std; int main() { int x = 10; int *ptr = &x; cout << *ptr; return 0; } ```
- Prints the address of x
- Prints the value of x (Correct answer)
- Prints a compilation error
- Prints an undefined value
Correct answer: Prints the value of x
In this C++ code, `int *ptr = &x;` declares a pointer `ptr` and initializes it with the memory address of the integer variable `x`. The statement `cout << *ptr;` then uses the dereference operator (`*`) to access the value stored at the memory address pointed to by `ptr`. Therefore, it prints the value of `x`, which is 10.
Question 6: What will be the result of the following SQL query? ```sql SELECT COUNT(*) FROM employees WHERE salary > 50000; ```
- The sum of salaries over 50,000
- The count of employees with salary over 50,000 (Correct answer)
- A list of salaries over 50,000
- An error in query syntax
Correct answer: The count of employees with salary over 50,000
The `SELECT COUNT(*)` clause in SQL is an aggregate function that returns the total number of rows that satisfy the specified criteria. In this query, the `WHERE salary > 50000` clause filters the `employees` table to include only those records where the salary is greater than 50,000. Thus, the query will output the exact count of employees whose salary exceeds 50,000.
What will be the output of the following Python code?
```python
x = [1, 2, 3]
y = x
y.append(4)
print(x)
```