Infinite Loop Debugging
In the following Python code, which change is needed to prevent an infinite loop? nn i = 0nwhile i u003C 5:n print(i)
- Add i += 1 inside the loop
- Change while to for
- Remove print(i)
- Add break statement before the loop
- Replace i u003C 5 with i u003E 5
Off-by-One Error Identification
Given this Java code: for(int i = 0; i u003C= arr.length; i++) { sum += arr[i]; }, what is the logical bug present?
- The loop will access an index out of bounds
- sum should be initialized outside the loop
- arr.length should be replaced with arr.size()
- i should start from 1
- The loop should be in reverse
Memory Leak Detection
In a C program, what could cause a memory leak?
- Allocating memory with malloc but not calling free
- Using ++ instead of += in a loop
- Not including stdio.h
- Forgetting to declare variables
- Returning 0 at the end of main
Choosing the Right Data Structure
When repeatedly searching for an item by key, which is the most optimal data structure in Python?
- Dictionary
- List
- Tuple
- Set
- String
SQL Query Optimization
How can you speed up queries on a large SQL table with millions of rows?
- Create an index on commonly searched columns
- Use SELECT * instead of specific columns
- Run VACUUM after every query
- Rename the table
- Drop unused columns
Debugging Variable Scope Issues
What is the issue if a function cannot access a variable defined inside another function in Python?
- The variable has local scope
- The variable name is too short
- The function was not called
- Python does not support variables
- The code needs more comments
Identifying Redundant Computation
What is one way to optimize this code snippet?nfor i in range(len(data)):n y = expensive_computation()n print(data[i], y)
- Move expensive_computation() outside the loop
- Use print() twice
- Replace range(len(data)) with while
- Convert data to a string
- Replace print with log
Fixing Typographical Errors
Which of the following is a correct Python function definition?
- def func():
- fnc def():
- def func[]:
- def fun():-
- define func():
Resolving Race Conditions
What does using a lock in multithreaded code help to prevent?
- Race conditions
- Syntax errors
- Compilation warnings
- Memory address leaks
- Misaligned pointers
Eliminating Unnecessary Loops
How can you optimize this code for summing list elements in Python?nresult = 0nfor i in range(len(nums)):n result += nums[i]
- Use result = sum(nums)
- Use result = nums.sum()
- Replace range with enumerate
- Change for loop to a while loop
- Convert nums to a set before summing