Docsity

Programming Paradigms Exam with Answers, Exams of Programming Paradigms

Toggle sidebar
bg1
Programming Paradigms
Exam with Answers
:: operator - ANScalled the cons operator, used to construct a new list by
adding an elm to the front of an existing list
8 major categories of control flow - ANS1. sequential
2. conditional
3. iterative/loop
4. transfer
5. procedural
6. concurrent
7. exceptional
8. co-routine
algebraic data types vs. class-based types - ANSalgebraic data types
represent data structures by combining simpler types, while class-based
types use classes and objects for data representation
applicative order - ANSevaluates function arguments before applying them
assembly language - ANSthere is no better way to understand the hardware
that runs the software than by programming in assembly
assignment (=) - ANSthe process of giving a value to a variable
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Programming Paradigms Exam with Answers and more Exams Programming Paradigms in PDF only on Docsity!

Programming Paradigms

Exam with Answers

:: operator - ANScalled the cons operator, used to construct a new list by adding an elm to the front of an existing list 8 major categories of control flow - ANS1. sequential

  1. conditional
  2. iterative/loop
  3. transfer
  4. procedural
  5. concurrent
  6. exceptional
  7. co-routine algebraic data types vs. class-based types - ANSalgebraic data types represent data structures by combining simpler types, while class-based types use classes and objects for data representation applicative order - ANSevaluates function arguments before applying them assembly language - ANSthere is no better way to understand the hardware that runs the software than by programming in assembly assignment (=) - ANSthe process of giving a value to a variable

associativity - ANSthe order in which operators of the same precedence are evaluated in the absence of parentheses boxing - ANSthe process of converting a value type to a reference type (unboxing: the reverse, converting a reference type to a value type) C language - ANShigh-level assembler, abstracts you away from the implementation details of any particular CPU but allows for programming that is still close to the "bare metal" of the machine. It is almost impossible to understand how operating systems work without understanding the C programming language car - ANSreturns the first elm of a list cdr - ANSreturns the rest of the list (all elms but first) constructors - ANSfunctions used to create and initialize objects or instances of a class in object-oriented programming control abstraction - ANSexpressing control flow structures in a way that hides low-level details from the programmer. achieved through higher-order functions currying - ANStechnique where a function that takes multiple arguments is transformed into a sequence of functions, each taking a single argument. in functional programming, a function that appears to take multiple parameters is often curried. when you provide fewer arguments than the function expects, it returns a new function that expects the remaining arguments.

let divide x y = if y = 0 then None else Some (x / y) record type: type person = { name: string; age: int } let john = { name = "John"; age = 30 } list comprehension: let evens = [for x in [1; 2; 3; 4; 5] do if x % 2 = 0 then yield x] functional programming - ANSformal definition: programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data digestible definition: coding style where functions are first-class citizens, which means they can be assigned to variables, passed as arguments, and returned as values. emphasizes immutability, pure functions, and declarative programming. functional programming languages include - ANSscala, f#, Ocaml, SML golang language - ANS Haskell - ANSfunctional programming language, uses lazy evaluation by default, strong emphasis on immutability, referential transparency, and type safety high-order functions - ANSfunctions that can take other functions as arguments or return functions as results

how to evaluate a function's type - ANS1. find the input type

  1. find the output type
  2. ans = "i-type -> o-type" how to make array address calculations - ANSaddress=base address+ (index×element size) immutability - ANSonce data is created, it cannot be changed. Instead of modifying existing data structures, functional programming encourages creating new ones. imperative programming (paradigm) - ANSstep-by-step instructions to achieve a specific result (C++, C, Java) initialization - ANSthe process of assigning initial values to variables or data structures JVM - ANScompiler that is just as fast as C/C++ Kotlin - ANSstatically-typed programming language developed by JetBrains, interoperable with Java, featuring concise syntax, null safety, extension functions, and aimed at enhancing developer productivity for Android and general-purpose development lazy evaluation (AKA normal order evaluation) - ANSdelaying the evaluation of an expression until its value is actually needed, can improve efficiency by only computing what is necessary left-associativity - ANSif operators are left-associative, they are evaluated from left to right when they have the same precedence. Most operators in

| _ -> "Other" list creation: let numbers = [1; 2; 3; 4; 5];; recursion: let rec factorial n = if n <= 1 then 1 else n * factorial (n - 1);; higher-order functions: let square_all lst = List.map (fun x -> x * x) lst;; option type: let divide x y = if y = 0 then None else Some (x / y);; record type: type person = { name: string; age: int };; let john = { name = "John"; age = 30 };; list comprehension: let evens = List.filter (fun x -> x mod 2 = 0) [1; 2; 3; 4; 5];; (* Lambda function that squares its argument *) let square = fun x -> x * x;; optionals - ANSfeature designed to handle the absence of a value orthagonality - ANSthe property that a relatively small set of primitive constructs can be combined in a relatively small number of ways to build the control and data structures of a language

An orthogonal language has features that can be freely combined without restrictions pattern matching - ANSfeature in functional programming languages that allows you to destructure and match complex data structures, such as lists. It simplifies code by handling different cases based on the structure of the data Ex: (define (classify-value x) (match x ((? number?) 'numeric) ((? string?) 'text) (else 'unknown))) polymorphism - ANSthe ability of a variable, function, or operator to take on multiple forms or behaviors precedence - ANScrucial for determining the order in which different operations or statements are executed programming paradigm - ANSapproach to programming that guides the way developers design, structure, and organize their code Prolog - ANSdeclarative programming language designed for tasks involving rule-based logic programming (uses logical paradigm) prolog syntax - ANS% rules for factorial using recursion factorial(0, 1). factorial(N, Result) :- N > 0,

stack: function call management and local variables rust - ANSsystems programming language known for its focus on memory safety, zero-cost abstractions, and concurrency support Scala - ANSstatically typed, functional programming language that runs on the Java Virtual Machine (JVM) and combines object-oriented and functional programming paradigms features concise syntax, pattern matching, and immutable collections Scheme - ANSLanuage of Lisp dialect, uses prefix notation where the operator precedes its operands scheme syntax - ANS; Define (for binding values) (define x 5) ; Let (for local bindings) (define result (let ((y 10) (z 20)) (+ x y z))) ; Lambda (for defining anonymous functions) (define square (lambda (a) (* a a))) ; Car and Cdr (for accessing elements of a list) (define myList '(1 2 3)) (define firstElement (car myList)) (define restOfList (cdr myList)) ; Cons (for constructing lists)

(define newList (cons 0 myList)) ; Null? (for checking if a list is empty) (define isListEmpty (null? '())) ; If (for conditional branching) (define (abs-value x) (if (< x 0) (- x) x)) scripting languages - ANSmuch slower in execution speed than Java or C, elaborate functionality in just a few lines of code python, perl, ruby, groovy short-circuit evaluation - ANSprogramming language feature where the evaluation of a logical expression is stopped as soon as the result is determined SML (ML) syntax - ANS(* Binding Values ) val x = 10; ( Function Definition ) fun add(x, y) = x + y; ( Pattern Matching ) fun factorial 0 = 1 | factorial n = n * factorial (n - 1); ( Conditional Expression ) fun absolute x = if x < 0 then ~x else x; ( Tuples *)

statically typed - ANSvariable types are checked at compile-time, reducing the likelihood of runtime errors structured flow vs unstructured flow - ANSstructured flow: involves organized, modular programming constructs unstructured flow: lacks such organization and can lead to spaghetti code structuring and destructuring - ANSorganizing and breaking down complex data structures into simpler components Swift - ANSstatically-typed programming language developed by Apple for app development, known for its type safety, modern syntax, optionals, and emphasis on performance tail recursion - ANSform of recursion where the recursive call is the last operation in the function test question (multiple choice): given the OCaml/F# function: let rec f = function | [] -> [] | x :: xs -> (x, x+1) :: f xs. What does the following method call yield: f [2; 4; 6]? a) [] b) [12] c) [4; 8; 12]

d) [(2,3)(4,5)(6,7)] - ANSfunction: used to pattern match on the input argument [] -> []: matches an empty list x :: xs -> (x, x+1) :: f xs: matches a non-empty list with a head element x and a tail xs. It then creates a tuple (x, x+1) and appends it to the result of the recursive call f xs.

  1. 2 -> (2,3) 4 -> (4,5) 6 -> (6,7)
  2. Combine results and you get d) [(2,3)(4,5)(6,7)] test question (multiple choice): nearly all language constructs in the Scheme language are represented in: a) infix notation b) prefix notation d) a mix - ANSprefix notation test question (multiple choice): what is the function type of f in the declaration: let f = fun x y -> (x+1, y+1.0). a) 'a * 'b -> 'a * 'b b) int * float -> int * float c) int -> float -> int * float d) (int, float) - ANSint -> float -> int * float the function f takes two parameters, an integer (x) and a float (y), and returns a tuple of type int * float

f) exception handling - ANSvariable declaration is related to variable and data type handling, not directly a category of control flow test question (T/F): if a language uses "normal order evaluation," it's possible that g(x) may never be evaluated in an expression such as in result = f(x, g(x)). - ANStrue, lazy evaluation delays the evaluation of an expression until its value is actually needed. If the value of g(x) is not used in the computation of result, it may never be evaluated. Haskell, a functional programming language, uses lazy evaluation by default. test question (T/F): in functional programming languages like OCaml/F#, a function requiring more than one parameter will accept only one parameter and yield a new function to accept the next parameter - ANSTrue test question (T/F): in languages that implement tail recursion, it is generally best to avoid recursion altogether for better performance and to avoid potential stack overflows - ANStrue, some programming languages optimize tail-recursive calls by reusing the current function's stack frame, reducing the risk of a stack overflow. However, not all languages or compilers support this, and using iteration may be preferable for performance test question (T/F): in object-oriented languages such as Java, the compiler can verify that every variable is properly initialized to a non-null value before its first use in an expression. - ANSfalse, while some modern languages, including Java, require variables to be initialized before use, the compiler does not always check for null values. It is the responsibility of the programmer to ensure proper initialization to avoid null pointer exceptions during runtime test question (T/F): In Prolog, a function's formal parameters may serve as both "inputs" and "outputs." - ANStrue, formal parameters are often used bidirectionally. Predicates in Prolog can have input, output, or both input and output parameters. The values of parameters are determined by unification

test question (T/F): individual programming languages may be classified as either object-oriented or functional but never have features of both paradigms, as this would create ambiguities for compilers. - ANSfalse, many modern programming languages are multi-paradigm. Languages like Scala, Kotlin, and Swift are considered both object-oriented and functional test question (T/F): unlike the arithmetic operators in most programming languages, Scheme arithmetic operators do not have explicit precedence associated with them - ANStrue, in Scheme, a Lisp dialect, arithmetic expressions are typically written in prefix notation, and there is no need for explicit precedence rules. Instead, Scheme uses parentheses to indicate the order of operations. For example, instead of 3 + 5 * 2, you would write (+ 3 (* 5 2)). test question (T/F): whereas classes can be used to implement "type abstraction," in OO languages, high...functions can be used to implement "control abstraction" in functional languages - ANStrue test question: assume a "pseudo-Java/C" language that DOES NOT employ short-circuit evaluation of Boolean expressions. What would be the output of the following fragment upon invoking main( )? boolean fun1 () { println("Fun1"); return false; } boolean fun2 () { printIn("Fun2"); return true; } void main() {println( fun1() && fun2() ); } - ANSFun Fun false (both functions executed because lack of short-circuit evaluation, evaluates to false at the end because of the && test question: given the following set of Prolog facts/rules:

(starting) address of someArray is memory address 1000, what memory address refers to the element someArray [3] [2]? - ANSstarting address: 1000 size of each row: 10*2=20 bytes (for 10 integers) size of each elm: 2 bytes addr of someArray[3][2] = starting address + (row index * size of each row)

  • (column index * size of each element) addr = 1000+(320)+(22) = 1064 ans: 1054 test question: the following statement will produce a "syntax error" in F#. Why? let f x = if x > 0 then "The value is Positive" - ANSBecause it is missing an else token/branch. In F#, an if statement must have then and else test question: what output would be produced by the execution of the following mini-program: let rec mystery lst = match lst with | x :: y :: XS -> if x=y then mystery (x :: xs) else x :: mystery (y :: xs) | x -> x printfn "Result: %A" (mystery [1;1;2;3;4;4;4;5]) - ANS[1; 2; 3; 4; 5]. The mystery function removes consecutive duplicates from the list.

test question: write a function called valueRange in OCaml/F# which:

  • accepts a function/predicate (that acts as a "filter") and a list of integers
  • resolves to a pair of values (a tuple) containing both the largest and smallest values in the list which are also "accepted" by the function - ANSlet valueRange filter lst = let rec find_range acc_min acc_max = function | [] -> (acc_min, acc_max) | hd :: tl -> if filter hd then find_range (min acc_min hd) (max acc_max hd) tl else find_range acc_min acc_max tl in match lst with | [] -> failwith "Empty list has no range" | hd :: tl -> find_range hd hd tl type abstraction - ANSdetails about data type are hidden, exposing only the essential characteristics. achieved through abstract data types or classes types in OCaml/F# - ANSthe OCaml/F# language supports strong static typing with type inference, enabling the compiler to deduce types without explicit declarations. unification - ANSa process where variables are bound to values based on logical relationships xs - ANSconventional name you'll see in programs to represent the remaining elms of a list