Computer Science Practice Questions PDF
Document Details

Uploaded by NeatestAmetrine
Tags
Summary
This document contains a series of practice questions related to computer science topics such as Haskell, C, and C++. The questions cover topics such as static type systems, memory management, and generic programming.
Full Transcript
Consider the Haskell function definition (++) [] ys = ys (++) (x:xs) ys = x : (xs ++ ys) 1. What is the type of (++) ? (A) (++):: [a] →> [a] -> [a] (B) (++) :: [a] -> [b] -> [c] (C) (++) :: a →> a →> a (D) (++) :: a -> b →> c 2. What does the function foldr (++) [] do? (A) Concatenate two lists. (...
Consider the Haskell function definition (++) [] ys = ys (++) (x:xs) ys = x : (xs ++ ys) 1. What is the type of (++) ? (A) (++):: [a] →> [a] -> [a] (B) (++) :: [a] -> [b] -> [c] (C) (++) :: a →> a →> a (D) (++) :: a -> b →> c 2. What does the function foldr (++) [] do? (A) Concatenate two lists. (B) Count the elements of a list. (C) Nothing. (D) Flatten a list of lists. (E) Reverse a list. 3. A list [1, 2, 3] in Haskell is syntactic sugar for (A) 1:2:3: [] which is equivalent to ( ( [] :1) : 2) : 3 (B) 1:2:3 which is equivalent to (1:2) :3 (C) 1:2:3: [] which is equivalent to 1 : (2 : (3: []) ) (D) 1:2: 3 which is equivalent to 1 : (2:3) Consider the following Haskell function definitions: Foldr _ z [] = z Foldr f z (x:xs) = f x (foldr f z xs) Foldl _z [] = z Foldl f z (x:xs) = foldl f (f z x) xs 4. Which of these functions are tail recursive? (A) both foldr and foldl (B) foldl but not foldr (C) neither foldr nor foldl (D) foldr but not foldl 5. What is the type of foldr? (A) foldr :: (a->a->a)->a->[a]->a (B) foldr :: (a->b->b) ->b-> [a] ->b (C) foldr :: a →> b →> [a] →> [b] (D) foldr :: (a->a->b)->b-> [a]->b (E) foldr :: (b->a->b) ->b-> [a]->b 6. What is the type of foldl? (A) foldl:: a →> b →› [a] →> [b] (B) foldl :: (a-›a->b) ->b-> [a]->b (C) foldl :: (b->a->b) ->b-> [a] ->b (D) foldl:: (a-›a->a)->a->[a]->a (E) foldl :: (a->b->b) ->b> [a]->b 7. Which of these languages does not use a static type def by default? (A) Python (B) C# (C) C++ (D) C (E) Haskell 8. What characterizes a static type system? (A) Only static variables have types. (B) The types of values are being determined at runtime. (C) The use of types is optional. (D) The types of values have to be determined at compile time before runtime. 9. In C, we first declare an int*, then assign it via malloc, then without explicitly deallocating it, assign it another value from malloc. What did we create? (A) memory leak (B) dangling pointer (C) target for automatic garbage collection (D) stack overflow 10. A generator implemented using the yield keyword in C# or Python behaves similar to which Haskell feature? (A) lazy lists (B) pattern matching (C) type classes (D) monads 11. What is the main reduction rule of the operational semantc of the λ-calculus? (A) y-reduction (B) λ-reduction (C) S-reduction (D) a-reduction (E) ß-reduction 12. Runtime stacks enable (A) all of these options (B) unique sets of parameters per subroutine activation (C) unique sets of local variables per subroutine activation (D) recursion 13. What is the main advantage of generic programming? (A) It improves the space complexity. (B) It improves source code re-usability. (C) It improves the time complexity. 14. Which of the following statements about the stack and the heap is incorrect? (A) Stack memory is allocated and deallocated automatically. (B) The stack is usually larger than the heap. (C) The stack is used for local variables. (D) The heap should be used when the size of required memory will only be known at runtime. Match the languages with the way that they are typically exe-cuted. 15. C (A) compiled to native code or interpreted (B) compiled to native code (C) compiled to managed code (that is then executed by the.NET runtime) (D) interpreted (after being compiled to byte code) 16. C++ (A) compiled to native code (B) compiled to native code or interpreted (C) interpreted (after being compiled to byte code) (D) compiled to managed code (that is then executed by the.NET runtime) 17. C# (A) interpreted (after being compiled to byte code) (B) compiled to native code (C) compiled to native code or interpreted (D) compiled to managed code (that is then executed by the.NET runtime) 18. Python (A) compiled to native code or interpreted (B) compiled to native code (C) compiled to managed code (that is then executed by the.NET runtime) (D) interpreted (after being compiled to byte code) 19. Haskell (A) compiled to native code or interpreted (B) compiled to managed code (that is then executed by the.NET runtime) (C) interpreted (after being compiled to byte code) (D) compiled to native code Consider the following C program: #include #include Int main() { Int *p; P = malloc (sizeof(int)); If (p == 0) { fputs(“Out of memory!\n”, stderr); Return 1; } *p = 42; Printf (“%d\n”, *p); free(p); Return 0; } 20. In which lines of this program will heap memory be allocated and/or deallocated? (A) 5, 7, 14, and 17 (B) 7, 14, and 17 (C) 7 and 17 (D) 5, 7, and 17 21. In which lines of this program are we referring to static mem-ory? (A) 5 & 7 (B) 7 & 19 (C) 5, 7, & 19 (D) 10 & 15 22. Is the use of heap memory really necessary in the above program? (A) Yes, because the size of the int is unknown at compile time. (B) Yes, because the stack doesn't allow the flexible allocation and deallocation that we need here. (C) No, since we're only allocating memory for one int. We could have used the stack for that. (D) Yes, because the stack might be too small. 23. How would the program's behavior change if we would remove line 17? (A) The program will print an error message because we didn't free the heap memory. (B) The program will crash. C) The behavior wouldn't change, since the heap memory allocated in line 7 would be freed in line 19 when the process terminates. (D) We will introduce a memory leak. Consider the following generator written in C#: IEnumerable‹int› Quux (IEnumerable‹int> seg) { yield return 1; foreach (varn in seq) yield return 3 * n; } 24. Translate the C# generator Quux into a Haskell function quux :: [Int] →> [Int] using lazy lists. (A) quux xs = 1 : map (3+) xs B) quux xs = ++ foldr (3*) xs (C) quux xs = 1 ++ map (3*) xs (D) quux xs = map (3*) (1 : xs) (E) quux xs = 1 : foldr (3*) xs 25. How many times will the yield return statement be invoked when the following code is being executed? var qs = Quux (Quux (new int[] { 2, 5 })) ; Console.WriteLine (String.Join(", “, qs)); (A) 6 (B) 1 (C) 3 (D) 7 (E) 5 26. What is the output of the following code? var qs = Quux (Quux (new int[] { 2, 5 })) ; Console.WriteLine (String.Join(", “, qs)); (A) 1, 1, 3, 15 (B) 1, 3, 18, 54 (C) 1, 3, 18, 45 (D) 1, 1, 18, 145 27. Translate the C# generator Quux into Python. (A) def quux (seq) : yield return 1 foreach n in seq: yield return 3 * n B) def quux (seq) : yield 1 foreach n in seq: yield 3 * n (C) def quux (seq) : yield 1 for n in seq: yield 3 * n (D) det quux (seq) : yield return 1 for n in seq: yield return 3 * n Evaluate the Haskell λ-expression (\x -> x * x) (1 + 2) step-by-step in normal order, te. leftmost outermost redex, first 28. First step: (A) (1 + 2) * (1 + 2) (B) 3 * (1+ 2) (C) (\x -> x * x) 3 (D) 3 * 3 (E) 9 29-Second stop: (A) 3 * 3 (B) 3 * (1 + 2) (C) 9 (D) (\x -> x * x) 3 (E) (1 + 2) * (1 + 2) 30. Third step: (A) 3 * 3 (B) (\x -> x * x) 3 (C) 3 * (1 + 2) (D) 9 (E) (1 + 2) * (1 + 2) 31. Fourth step: (A) (\x -> x * x) 3 (B) 9 (C) (1 + 2) * (1 + 2) (D) 3 * 3 (E) 3 * (1 + 2) Evaluate the Haskell λ-expression (\x -> x * x) (1 + 2) step-by-step in applicative order, ie. rightmost innermost redex, first 32. First step: (A) (1 + 2) * (1 + 2) (B) (\x →> x * x) 3 (C) 9 (D) 3 * (1 + 2) (E) 3 * 3 33. Second step: (A) 3 * (1 + 2) (B) 9 (C) 3 * 3 (D) (\x → x * x) 3 (E) (1 + 2) * (1 + 2) 34. Third step: (A) (1 + 2) * (1 + 2) (B) 3 * 3 (C) 3 * (1 + 2) (D) 9 (E) (\x →> x * x) 3 Consider the following fragment of C code: int a = 5; int b; int c(int d) { return a * b[d]; } Match the symbol with the memory segment (where memory will be allocated to store its value). 35. dynamic (heap) (A) *c (B) d (C) *b (D) none of these (E) a 36. static, data segment (A) none of these (B) a (C) d (D) *b (E) *c 37. automatic (stack) (A) none of these (B) d (C) *c (D) a (E) *b 38. static, BSS segment (A) *b (B) none of these (C) a (D) d (E) *c 39. static, text segment (A) *b (B) d (C) *c (D) a (E) none of these Match the generic Programming features to the languages 40. polymorphic types and type classes (A) C++ (B) C (C) Haskell (D) C# (E) Python 41. mainly just workarounds with macros or void* (A) Python (B) C (C) Haskell (D) C# (E) C++ 42. templates (A) C# (B) Python (C) C (D) Haskell (E) C++ 43, type parameters and interfaces (A) C++ (B) C (C) C# (D) Haskell (E) Python Match the heap memory allocation feature to the language. 44. malloc/ free (A) C++ (B) Python (C) C (D) Haskell (E) C# 45. new/delete (A) C# (B) C (C) Python (D) C++ (E) Haskell 46. new / garbage collector (A) Python (B) C# (C) C (D) Haskell (E) C++ 47. Which of the following best describes linking? (A) Load the executable into memory for execution. (B) Convert source code into machine code. (C) Combine multiple object files into an executable, (D) Check the syntax of the code. (E) Optimize the machine code for better performance. 48. What is the role of the loader? (A) Remove the comments from the source code. (B) Translate source code to machine code. (C) Optimize the code for faster execution, (D) Place the executable into memory for execution. (E) Connect the program's external dependencies. Match the generic sort functions to the languages. 49. sort :: Ord a => [a] -> [a] (A) C (B) Haskell (C) C# (D) C++ (E) Python 50. void qsort (void *a, size_t n, size_t width, int (*comp) (const void *, const void *) ); (A) C++ (B) C# (C) Python (D) C (E) Haskell 51. template void sort (RandomIt first, RandomIt last, Compare comp) ; (A) Python (B) Haskell (C) C# (D) C (E) C++ 52. public static IOrderedEnumerable OrderBy( this IEnumerable source, Func keySelector) ; (A) C (B) C++ (C) Haskell (D) Python (E) C# 53. sorted (iterablel, cmp[, keyl, reverse]]]) (A) Haskell (B) Python (C) C (D) C# (E) C++