Algorithm & Programming Quiz PDF
Document Details
Uploaded by Deleted User
Tags
Related
Summary
This document contains a quiz on algorithm characteristics, Python code behavior, and computer architecture. The questions cover topics like algorithm properties, time complexity, sorting algorithms, and Python functions.
Full Transcript
Algorithm Characteristics and Properties 1. Which of the following is a property of a well-defined algorithm? (1 Mark) ○ (i) Ambiguity ○ (ii) Finiteness ○ (iii) Non-terminating ○ (iv) Complexity 2. Answer: (ii) Finiteness 3. Which characteristic...
Algorithm Characteristics and Properties 1. Which of the following is a property of a well-defined algorithm? (1 Mark) ○ (i) Ambiguity ○ (ii) Finiteness ○ (iii) Non-terminating ○ (iv) Complexity 2. Answer: (ii) Finiteness 3. Which characteristic ensures that an algorithm produces the correct output for all valid inputs? (1 Mark) ○ (i) Efficiency ○ (ii) Termination ○ (iii) Correctness ○ (iv) Generality 4. Answer: (iii) Correctness 5. Which of the following is not a typical step in the development of an algorithm? (1 Mark) ○ (i) Problem Analysis ○ (ii) Data Collection ○ (iii) Solution Design ○ (iv) Implementation 6. Answer: (ii) Data Collection 7. What is the time complexity of an algorithm that makes a single pass through an array of size n? (1 Mark) ○ (i) O(1) ○ (ii) O(log n) ○ (iii) O(n) ○ (iv) O(n^2) 8. Answer: (iii) O(n) 9. Which of the following is not a type of sorting algorithm? (1 Mark) ○ (i) Bubble Sort ○ (ii) Merge Sort ○ (iii) Binary Search ○ (iv) Quick Sort 10. Answer: (iii) Binary Search 2. Python Code and Function Behavior What will be the output of the following Python code? (1 Mark) python Copy code def increment(x): return x + 1 print(increment(5)) 6. ○ (i) 4 ○ (ii) 5 ○ (iii) 6 ○ (iv) 7 7. Answer: (iii) 6 8. What is the purpose of the return statement in a function? (1 Mark) ○ (i) To stop the function execution ○ (ii) To pass a value back to the caller ○ (iii) To define a function ○ (iv) To display output to the user 9. Answer: (ii) To pass a value back to the caller 10. Which of the following will correctly create a list of numbers from 1 to 5 in Python? (1 Mark) ○ (i) numbers = [1, 2, 3, 4, 5] ○ (ii) numbers = range(1, 6) ○ (iii) numbers = list(range(1, 6)) ○ (iv) All of the above 11. Answer: (iv) All of the above What will be the output of this Python code? (1 Mark) python Copy code for i in range(2, 6, 2): print(i) 12. ○ (i) 2 4 ○ (ii) 2 4 6 ○ (iii) 2 4 6 8 ○ (iv) 2 6 13. Answer: (i) 2 4 What is the result of the following Python expression? (1 Mark) python Copy code 10 % 3 14. ○ (i) 1 ○ (ii) 3 ○ (iii) 10 ○ (iv) 0 15. Answer: (i) 1 3. Block Diagram and Computer Architecture 11. What does the ALU (Arithmetic Logic Unit) do in a computer system? (1 Mark) ○ (i) Manage memory storage ○ (ii) Perform arithmetic and logic operations ○ (iii) Handle input and output operations ○ (iv) Fetch instructions 12. Answer: (ii) Perform arithmetic and logic operations 13. Which component of the computer architecture is responsible for interpreting and executing instructions? (1 Mark) ○ (i) Memory Unit ○ (ii) CPU ○ (iii) Control Unit ○ (iv) ALU 14. Answer: (ii) CPU 15. In a computer's block diagram, what does the Control Unit do? (1 Mark) ○ (i) Stores data ○ (ii) Executes arithmetic operations ○ (iii) Directs the operation of the processor ○ (iv) Manages I/O operations 16. Answer: (iii) Directs the operation of the processor 17. Which of the following is not a function of the computer's Memory Unit? (1 Mark) ○ (i) Store data ○ (ii) Retrieve data ○ (iii) Execute instructions ○ (iv) Buffer data 18. Answer: (iii) Execute instructions 19. What is the role of the bus in a computer system? (1 Mark) ○ (i) Store instructions ○ (ii) Connect different components for data transfer ○ (iii) Perform calculations ○ (iv) Execute programs 20. Answer: (ii) Connect different components for data transfer 4. Compiler and Programming Concepts 16. What does a compiler do? (1 Mark) ○ (i) Executes code line by line ○ (ii) Translates high-level code into machine code all at once ○ (iii) Translates code into bytecode for the virtual machine ○ (iv) Debugs the code 17. Answer: (ii) Translates high-level code into machine code all at once 18. Which of the following is a disadvantage of using an interpreter? (1 Mark) ○ (i) Faster execution ○ (ii) Immediate feedback on code errors ○ (iii) Requires the source code to be translated every time it runs ○ (iv) Generates an executable file 19. Answer: (iii) Requires the source code to be translated every time it runs 20. What is the purpose of the preprocessor in a compiler? (1 Mark) ○ (i) Execute the program ○ (ii) Translate the entire program to machine code ○ (iii) Process directives and macros before compilation ○ (iv) Optimize the machine code 21. Answer: (iii) Process directives and macros before compilation 22. Which of the following is a difference between a compiler and an interpreter? (1 Mark) ○ (i) A compiler translates code at runtime, while an interpreter translates code before execution. ○ (ii) An interpreter translates code line by line, while a compiler translates the entire code at once. ○ (iii) A compiler provides immediate feedback on code errors, while an interpreter does not. ○ (iv) An interpreter generates machine code, while a compiler does not. 23. Answer: (ii) An interpreter translates code line by line, while a compiler translates the entire code at once. 24. What does the term 'lexical analysis' refer to in the context of compiling? (1 Mark) ○ (i) Checking the syntax of the source code ○ (ii) Translating the source code into intermediate code ○ (iii) Breaking the source code into tokens ○ (iv) Optimizing the machine code 25. Answer: (iii) Breaking the source code into tokens 5. Decimal to Hexadecimal Conversion 21. Convert decimal 1024 to hexadecimal. (2 Marks) ○ (i) 1000 ○ (ii) 1024 ○ (iii) 400 ○ (iv) 800 22. Answer: (i) 1000 23. Convert decimal 16 to hexadecimal. (2 Marks) ○ (i) A ○ (ii) 10 ○ (iii) 16 ○ (iv) 1A 24. Answer: (i) A 25. Convert decimal 2047 to hexadecimal. (2 Marks) ○ (i) 7FF ○ (ii) 800 ○ (iii) 3FF ○ (iv) FFF 26. Answer: (i) 7FF 27. Convert decimal 786 to hexadecimal. (2 Marks) ○ (i) 313 ○ (ii) 2F2 ○ (iii) 31A ○ (iv) 3B2 28. Answer: (ii) 2F2 29. Convert decimal 4000 to hexadecimal. (2 Marks) ○ (i) F80 ○ (ii) 9C4 ○ (iii) FA0 ○ (iv) 3E8 30. Answer: (i) FA0 6. Additional Python and Programming Questions What is the output of the following Python code? (1 Mark) python Copy code x = [1, 2, 3] x.append(4) print(x) 26. ○ (i) [1, 2, 3, 4] ○ (ii) [1, 2, 3] ○ (iii) [4, 1, 2, 3] ○ (iv) [1, 2, 3, 4, 4] 27. Answer: (i) [1, 2, 3, 4] What is the result of the following code snippet? (1 Mark) python Copy code x = "Python" print(x[1:4]) 28. ○ (i) "Pyt" ○ (ii) "yth" ○ (iii) "ytho" ○ (iv) "thon" 29. Answer: (ii) "yth" 30. Which of the following statements is correct about Python dictionaries? (1 Mark) ○ (i) Dictionaries are ordered collections. ○ (ii) Dictionaries store values in a key-value pair. ○ (iii) Dictionaries allow duplicate keys. ○ (iv) Dictionaries are immutable. 31. Answer: (ii) Dictionaries store values in a key-value pair. What will be the output of the following Python code? (1 Mark) python Copy code x = {1, 2, 3} x.add(2) print(x) 32. ○ (i) {1, 2, 3} ○ (ii) {1, 2, 2, 3} ○ (iii) {1, 3} ○ (iv) {2, 2, 3} 33. Answer: (i) {1, 2, 3} What is the result of the following Python expression? (1 Mark) python Copy code len("Data Science") 34. ○ (i) 12 ○ (ii) 11 ○ (iii) 13 ○ (iv) 14 35. Answer: (iii) 13 7. Miscellaneous Questions 31. What is the purpose of the 'break' statement in a loop? (1 Mark) ○ (i) Exit the loop immediately ○ (ii) Skip the current iteration of the loop ○ (iii) Continue to the next iteration of the loop ○ (iv) Restart the loop 32. Answer: (i) Exit the loop immediately 33. Which of the following is not a valid Python identifier? (1 Mark) ○ (i) _variable ○ (ii) variable1 ○ (iii) 1variable ○ (iv) variable_1 34. Answer: (iii) 1variable 35. What does the 'pass' statement do in Python? (1 Mark) ○ (i) End the program ○ (ii) Placeholder for future code ○ (iii) Skip the current iteration of a loop ○ (iv) Exit the loop 36. Answer: (ii) Placeholder for future code What will be the output of the following code? (1 Mark) python Copy code def greet(name): print(f"Hello, {name}!") greet("Alice") 37. ○ (i) Hello, Alice! ○ (ii) greet("Alice") ○ (iii) Hello, {name}! ○ (iv) Alice 38. Answer: (i) Hello, Alice! 39. Which of the following keywords is used to define a function in Python? (1 Mark) ○ (i) define ○ (ii) func ○ (iii) def ○ (iv) function 40. Answer: (iii) def 41. In Python, how do you denote a block of code? (1 Mark) ○ (i) Using curly braces {} ○ (ii) Using indentation ○ (iii) Using parentheses () ○ (iv) Using semicolons ; 42. Answer: (ii) Using indentation What is the result of the following operation in Python? (1 Mark) python Copy code 7 // 3 43. ○ (i) 2.33 ○ (ii) 2 ○ (iii) 2.666 ○ (iv) 3 44. Answer: (ii) 2 45. What does the 'continue' statement do in a loop? (1 Mark) ○ (i) Exit the loop ○ (ii) Skip the current iteration and proceed to the next iteration ○ (iii) Restart the loop ○ (iv) Pause the loop 46. Answer: (ii) Skip the current iteration and proceed to the next iteration 47. Which method removes the last item from a list in Python? (1 Mark) ○ (i) pop() ○ (ii) remove() ○ (iii) delete() ○ (iv) discard() 48. Answer: (i) pop() 49. How do you create a comment in Python? (1 Mark) ○ (i) // ○ (ii) # ○ (iii) -- ○ (iv) 50. Answer: (ii) # 8. Advanced Topics What is the output of the following code? (1 Mark) python Copy code def add(a, b=5): return a + b print(add(10)) 41. ○ (i) 5 ○ (ii) 10 ○ (iii) 15 ○ (iv) Error 42. Answer: (iii) 15 What will be the output of this Python code? (1 Mark) python Copy code x = [1, 2, 3] x = x + [4, 5] print(x) 43. ○ (i) [1, 2, 3, 4, 5] ○ (ii) [1, 2, 3] ○ (iii) [4, 5] ○ (iv) [1, 2, 3, [4, 5]] 44. Answer: (i) [1, 2, 3, 4, 5] 45. Which of the following is true about Python sets? (1 Mark) ○ (i) Sets are ordered collections. ○ (ii) Sets allow duplicate elements. ○ (iii) Sets are immutable. ○ (iv) Sets do not allow duplicate elements. 46. Answer: (iv) Sets do not allow duplicate elements. What is the result of the following code? (1 Mark) python Copy code def double(x): return x * 2 print(double(3.5)) 47. ○ (i) 3.5 ○ (ii) 7.0 ○ (iii) 6.0 ○ (iv) 8.0 48. Answer: (ii) 7.0 49. Which of the following is a valid way to create a tuple in Python? (1 Mark) ○ (i) (1, 2, 3) ○ (ii) [1, 2, 3] ○ (iii) {1, 2, 3} ○ (iv) 1, 2, 3 50. Answer: (i) (1, 2, 3) What will be the output of this code snippet? (1 Mark) python Copy code def multiply(x, y=2): return x * y print(multiply(4, 5)) 51. ○ (i) 9 ○ (ii) 8 ○ (iii) 20 ○ (iv) 10 52. Answer: (iii) 20 53. How do you concatenate two strings in Python? (1 Mark) ○ (i) Using + ○ (ii) Using - ○ (iii) Using * ○ (iv) Using / 54. Answer: (i) Using + 55. Which method will you use to remove all items from a list in Python? (1 Mark) ○ (i) clear() ○ (ii) remove() ○ (iii) delete() ○ (iv) discard() 56. Answer: (i) clear() What is the result of this Python expression? (1 Mark) python Copy code 3 ** 2 57. ○ (i) 6 ○ (ii) 9 ○ (iii) 8 ○ (iv) 7 58. Answer: (ii) 9 What will be the result of the following code? (1 Mark) python Copy code def func(x): x.append(10) lst = [1, 2, 3] func(lst) print(lst) 59. ○ (i) [1, 2, 3] ○ (ii) [1, 2, 3, 10] ○ (iii) [10, 1, 2, 3] ○ (iv) [1, 2, 3, ] 60. Answer: (ii) [1, 2, 3, 10] 1. What does it mean to compute in computer science? a) To design algorithms b) To execute an algorithm c) To create hardware d) To debug code Answer: b) To execute an algorithm 2. What is an algorithm? a) A set of random instructions b) A step-by-step procedure for solving a problem c) A piece of hardware d) A data structure Answer: b) A step-by-step procedure for solving a problem 3. What does "input" refer to in computing? a) The output produced by an algorithm b) The data or commands provided to an algorithm c) The code written in a program d) The result of an algorithm Answer: b) The data or commands provided to an algorithm 4. What does "output" refer to in computing? a) The instructions given to an algorithm b) The data processed by the algorithm c) The final result of the algorithm's execution d) The initial input data Answer: c) The final result of the algorithm's execution Functions 5. Define a function in programming. Answer: A function is a block of reusable code that performs a specific task when called. 6. What is the purpose of the return statement in a function? a) To exit the function and optionally return a value b) To print a message c) To define the function d) To loop through code Answer: a) To exit the function and optionally return a value 7. What is the difference between print and return in a function? a) print outputs to the console; return sends a value back to the caller b) print sends a value back to the caller; return outputs to the console c) Both print and return do the same thing d) print and return are not used in functions Answer: a) print outputs to the console; return sends a value back to the caller 8. What will be the output of the following code? python Copy code def max1(x, y): if x > y: return x else: return y print(max1(7, 5)) a) 7 b) 5 c) 0 d) None Answer: a) 7 9. What is the difference between a function that returns a value and one that only prints output? Answer: A function that returns a value can provide data back to the caller for further use, while a function that only prints output displays information to the console but does not return any value. Variable Scope 10. Define variable scope. Answer: Variable scope determines where a variable is accessible within a program, such as within a function or globally. 11. What is a local variable? a) A variable defined inside a function or block b) A variable defined outside any function c) A variable that is accessible globally d) A variable that never changes Answer: a) A variable defined inside a function or block 12. What will be the output of the following code? python Copy code def f(x): x = x + 1 print('In f(x): x =', x) return x x = 3 z = f(x) print(x, z) a) In f(x): x = 4; 3 4 b) In f(x): x = 4; 4 4 c) In f(x): x = 4; 3 3 d) In f(x): x = 4; 4 3 Answer: a) In f(x): x = 4; 3 4 13. How does the scope of a variable created within a function differ from a variable created outside the function? Answer: Variables created within a function are local to that function and cannot be accessed outside it, whereas variables created outside are global and can be accessed throughout the program. Functions as Arguments 14. What is the output of this code? python Copy code def func_a(): print('Inside func_a') def func_b(y): print('Inside func_b') return y def func_c(z): print('Inside func_c') return z() print(func_a()) print(5 + func_b(2)) print(func_c(func_a)) a) Inside func_a; Inside func_b; Inside func_c; None 7 None b) Inside func_a; Inside func_b; Inside func_c; None 7 Inside func_a c) Inside func_a; Inside func_b; None; None 7 d) Inside func_a; Inside func_b; Inside func_c; None None 7 Answer: b) Inside func_a; Inside func_b; Inside func_c; None 7 Inside func_a 15. What does passing a function as an argument to another function allow? a) It allows functions to be executed in parallel b) It allows dynamic behavior based on the function passed c) It prevents the function from executing d) It makes functions immutable Answer: b) It allows dynamic behavior based on the function passed Recursion 16. Define recursion. Answer: Recursion is a technique where a function calls itself to solve smaller instances of the same problem until a base case is reached. 17. What will be the output of the following recursive function? python Copy code def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print(factorial(5)) a) 120 b) 60 c) 30 d) 5 Answer: a) 120 18. What will be the result of the following code? python Copy code def mult(a, b): if b == 1: return a else: return a + mult(a, b-1) print(mult(4, 3)) a) 12 b) 7 c) 10 d) 8 Answer: a) 12 Loops 19. What will be the output of the following while loop? python Copy code i = 0 while i < 4: print(i) i += 1 a) 0 1 2 3 b) 0 1 2 3 4 c) 1 2 3 4 d) 0 1 2 Answer: a) 0 1 2 3 20. What will be the result of the following for loop? python Copy code for i in range(3): for j in range(2): print(i, j) a) 0 0 0 1 0 2 1 0 1 1 b) 0 0 1 0 2 1 1 1 c) 0 0 1 1 2 2 d) 0 0 1 0 1 1 Answer: a) 0 0 0 1 0 2 1 0 1 1 21. What is the purpose of the break statement in a loop? a) To skip the current iteration b) To terminate the loop execution c) To start a new loop d) To restart the loop Answer: b) To terminate the loop execution 22. What does the continue statement do in a loop? a) It exits the loop completely b) It skips the remaining code inside the loop for the current iteration and moves to the next iteration c) It restarts the loop from the beginning d) It terminates the loop execution Answer: b) It skips the remaining code inside the loop for the current iteration and moves to the next iteration Function Scope and Execution 23. How does Python handle the scope of variables in recursive functions? a) Each recursive call creates a new scope with its own set of variables b) All recursive calls share the same scope c) Variables in recursive functions are globally scoped d) Recursive functions do not have their own scope Answer: a) Each recursive call creates a new scope with its own set of variables 24. What will be the output of this code? python Copy code def g(x): def h(): x = 'abc' x = x + 'd' print('g: x =', x) h() return x x = 3 z = g(x) print(z) a) g: x = abcd; 3 b) g: x = abcd; abcd c) g: x = abcd; 3 d) g: x = abc; 3 Answer: a) g: x = abcd; 3 25. What happens if you reference a variable before it is assigned within a function? a) The program executes without errors b) The function will use the global variable c) An UnboundLocalError will be raised d) The variable will be initialized to None Answer: c) An UnboundLocalError will be raised Functions with/without Return Statements 26. What will be the output of this function? python Copy code def max2(x, y): if x > y: print(x) else: print(y) a) The greater of x or y is printed b) The function does not return anything c) It prints the maximum value but does not return it d) Both x and y are printed Answer: a) The greater of x or y is printed 27. What does the following function do? python Copy code def func(x, y=10): return x + y print(func(5)) a) Returns 15 b) Returns 10 c) Returns 5 d) Returns 20 Answer: a) Returns 15 28. What is the purpose of default arguments in functions? a) To provide a default value if no argument is passed b) To create multiple functions c) To override global variables d) To create static variables Answer: a) To provide a default value if no argument is passed Recursion and Base Cases 29. What will be the result of this recursive function? python Copy code def count_down(n): if n