🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Untitled document (6).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

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

Use Quizgecko on...
Browser
Browser