|
2653 | 2653 | "explanation": "The function 'foo' creates a list comprehension that iterates over a range of 'n'. In each iteration, it performs a constant time operation of appending the current value 'i' to the new list. As the number of iterations is directly proportional to the input 'n', the time complexity of the function is linear. Therefore, for each increase in 'n', the number of operations increases linearly, making the time complexity O(n).",
|
2654 | 2654 | "correctAnswer": "O(n)",
|
2655 | 2655 | "incorrectAnswers": ["O(1)", "O(n^2)", "O(log n)", "O(n log n", "O(n^3)"]
|
| 2656 | + }, |
| 2657 | + { |
| 2658 | + "difficulty": "hard", |
| 2659 | + "question": "What will the following Python code output using a list comprehension?", |
| 2660 | + "codeSnippet": "x = [i ** 2 if i % 2 == 0 else i for i in range(5)]\nprint(x)", |
| 2661 | + "explanation": "The list comprehension creates a list where each even number is squared and each odd number is left as is, resulting in [0, 1, 4, 3, 16].", |
| 2662 | + "correctAnswer": "[0, 1, 4, 3, 16]", |
| 2663 | + "incorrectAnswers": [ |
| 2664 | + "[1, 2, 3, 4, 5]", |
| 2665 | + "[0, 2, 4, 6, 8]", |
| 2666 | + "[25, 16, 9, 4, 1]", |
| 2667 | + "TypeError", |
| 2668 | + "None" |
| 2669 | + ] |
| 2670 | + }, |
| 2671 | + { |
| 2672 | + "difficulty": "hard", |
| 2673 | + "question": "What does this Python code snippet with a generator expression print?", |
| 2674 | + "codeSnippet": "gen = (x ** 2 for x in range(10) if x % 2 == 0)\nprint(next(gen))", |
| 2675 | + "explanation": "The generator expression generates squares of even numbers from 0 to 9. The 'next' function retrieves the first element, which is 0^2, resulting in 0.", |
| 2676 | + "correctAnswer": "0", |
| 2677 | + "incorrectAnswers": ["1", "4", "16", "StopIteration", "None"] |
| 2678 | + }, |
| 2679 | + { |
| 2680 | + "difficulty": "hard", |
| 2681 | + "question": "What is the output of this code using dictionary comprehension?", |
| 2682 | + "codeSnippet": "d = {i: i ** 2 for i in range(5)}\nprint(d.get(3))", |
| 2683 | + "explanation": "The dictionary comprehension creates a dictionary where each key is a number from 0 to 4 and each value is its square. The 'get' method returns the value for key 3, which is 9.", |
| 2684 | + "correctAnswer": "9", |
| 2685 | + "incorrectAnswers": ["3", "16", "None", "KeyError", "12"] |
| 2686 | + }, |
| 2687 | + { |
| 2688 | + "difficulty": "hard", |
| 2689 | + "question": "What will the following code output?", |
| 2690 | + "codeSnippet": "def foo(n):\n if n == 0:\n return 'End'\n else:\n return foo(n-1)\n\nprint(foo(3))", |
| 2691 | + "explanation": "The function 'foo' is a recursive function that calls itself, decreasing 'n' by 1 each time, until 'n' becomes 0. When 'n' is 0, it returns 'End'.", |
| 2692 | + "correctAnswer": "End", |
| 2693 | + "incorrectAnswers": ["3", "2", "1", "RecursionError", "None"] |
| 2694 | + }, |
| 2695 | + { |
| 2696 | + "difficulty": "hard", |
| 2697 | + "question": "What is the output of this Python code using tuple unpacking?", |
| 2698 | + "codeSnippet": "a, *b, c = range(5)\nprint(b)", |
| 2699 | + "explanation": "The code uses extended unpacking where 'a' is assigned the first value, 'c' the last value, and 'b' is a list of the remaining values in the range, resulting in [1, 2, 3].", |
| 2700 | + "correctAnswer": "[1, 2, 3]", |
| 2701 | + "incorrectAnswers": [ |
| 2702 | + "[0, 1, 2]", |
| 2703 | + "[2, 3, 4]", |
| 2704 | + "[0, 1, 2, 3, 4]", |
| 2705 | + "TypeError", |
| 2706 | + "1" |
| 2707 | + ] |
| 2708 | + }, |
| 2709 | + { |
| 2710 | + "difficulty": "hard", |
| 2711 | + "question": "What is the output of this Python code snippet using a lambda function within a function?", |
| 2712 | + "codeSnippet": "def foo(x):\n return (lambda y: y + x)(x * 2)\n\nprint(foo(3))", |
| 2713 | + "explanation": "The function 'foo' returns a lambda function that adds 'y' to 'x'. The lambda function is immediately invoked with 'x * 2'. For 'foo(3)', the lambda receives 6, and returns 6 + 3, resulting in 9.", |
| 2714 | + "correctAnswer": "9", |
| 2715 | + "incorrectAnswers": ["6", "12", "3", "TypeError", "None"] |
2656 | 2716 | }
|
2657 | 2717 | ],
|
2658 | 2718 | "expert": [
|
|
0 commit comments