Nothing Special   »   [go: up one dir, main page]

Resit-Question-Ct006 3 3 Aplc

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CT006-3-3-APLC Resit Exam Page 1 of 1

SECTION A
Answer ALL questions.
Each question carries ONE (1) mark.

1. Which of the following programming paradigm that focuses on binding functions for
computations like mathematical functions and avoid changing state and mutable data?
(a) Imperative programming
(b) Functional programming
(c) Logic programming
(d) Object oriented programming

2. A ___________ is a function that takes a function as an argument or returns a function.


(a) pure function
(b) impure function
(c) first order function
(d) higher order function

3. The following statements are correct for side-effect in functional programming paradigm
except
(a) the function that performs a recursive call
(b) the function that modifies a data structure in place
(c) the function that changes the file system
(d) the function that mutate the external data

4. Which of the following is incorrect statement?


(a) As the pure functions have numerous benefits, it is realistic to only have pure functions
in our computer program.
(b) As the pure functions are deterministic by nature, writing the unit tests for the function
can be a lot simpler.
(c) As the pure functions only depends on their input and side effects are prohibited thus
they are great for scenarios where the parallel threads run and use shared memory.
(d) As the pure functions have a small amount of logic, we can easily reuse functions
between different parts of our codebase or different projects altogether.

5. The following statements demonstrate the differences between functional (FP) and object-
oriented programming (OOP) except
(a) FP uses immutable data while OOP uses mutable data
(b) FP follows declarative programming model while OOP follows imperative
programming model
(c) FP focuses on “how you are doing” while OOP focuses on “what you are doing”
(d) FP supports parallel programming while OOP is not suitable for parallel programming

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 2 of 1

6. Which of the following programming concept describes turning a function with multiple
arity into a function with less arity?
(a) Closure
(b) Currying
(c) Partial application
(d) Monad

7. Given the following code below. Which one is higher order function?

Optional<String> line = Stream.of("101","Book1", "23", "9.9")


.reduce( (e1, e2) -> String.join("|", e1, e2) );

if( line.isPresent() ) System.out.println( line.get() );

(a) Stream.of()
(b) reduce()
(c) get()
(d) Optional.isPresent()

8. Given the f() method below. Which of the following curried function is correctly defining
the f() method?

double f(int a, int b, int c, int d) {


return (a+b) * (c+d);
}

(a)
Function<Integer, Function<Integer, Integer>> f(int a, int
b) {
return c -> d -> (a+b) * (c+d);
}

(b)
Function<Integer, Function<Integer, Integer>> f(int a) {
return b -> c -> d -> (a+b) * (c+d);
}

(c)
Function<Integer, Function<Integer, Function<Integer,
Function<Integer, Integer>>>> f = a -> b -> c -> d -> (a+b)
* (c+d);

(d)
Function<Integer, Function<Integer, Function<Integer,
Function<Integer, Integer>>>> f = (a+b) * (c+d);

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 3 of 1

9. Based on the given multiply() method. Which of the following code is valid to call the
method?

class Demo{

public Function<Integer, Integer> multiply(Integer x){


return new Function<Integer, Integer>(){
@Override
public Integer apply(Integer y) {
return x * y;
}
};
}
}

(a) new Demo().multiply(10,20);


(b) new Demo().multiply.apply(10).apply(20);
(c) new Demo().multiply(10).apply(20);
(d) new Demo().multiply(10).apply(20).result();

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 4 of 1

10. Given the code snippet below. Which lambda expression best represents the
factorial(n) method?

class Demo{
static final int factorial(int n) {
// stop condition
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
// recursive call
}
}

(a)
static final UnaryOperator<int> factorial = n -> n == 0 ? 1 :
n * Demo.factorial.apply(n - 1);

(b)
static final UnaryOperator<Integer> factorial = n -> n == 0 ?
1 : n * Demo.factorial.apply(n - 1);

(c)
static final UnaryOperator<Integer> factorial = n -> n == 0 ->
1 -> n * Demo.factorial.apply(n - 1);

(d)
static final Function<Integer, Integer> factorial = n -> n ==
0 -> 1 -> n * Demo.factorial.apply(n - 1);

11. Which of the following is correct explains parametric polymorphism?


(a) Parametric polymorphism is a programming technique that enables the generic
definition of functions and types without a great deal of concern for type-based errors.
(b) Parametric polymorphism is a way to make a computer program more effective at
runtime while still maintaining full performance in executing functions.
(c) Parametric polymorphism is a specific principle that is derived from the Object-
oriented programing techniques emphasizing the functions dynamic.
(d) None of the above

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 5 of 1

12. Generics means,


(a) Generic is a technique that makes coding faster and concise.
(b) Generics add stability to your code by making more of your bugs detectable at runtime.
(c) Generics add stability to your code by making more of your bugs detectable at compile
time.
(d) Generics operate on objects of a specific type only while providing compile-time type
safety.

13. Which of the following is incorrect statement regarding the use of generics and
parameterized types in Java language?
(a) Generics provide type safety by shirting more type checking responsibilities to the
compiler.
(b) Generics and parameterized types eliminate the need for down casts when using Java
collections
(c) When designing your own collections class, say LinkedList, generics and
parameterized types allow you to achieve type safety with just a single class definition
as opposed to defining multiple classes.
(d) All of the above

14. Choose the correct option based on the following program.

import java.util.*;

public class OnlineTest {


public static void main(String[] args) {
List<int> lst = new ArrayList<>();
lst.add( 100 );
lst.add( 200 );
System.out.println( “The list:” + lst );
}
}

(a) It prints the following: The list: [100, 200]


(b) It prints the following: The list: [200, 100]
(c) It results in a compilation error
(d) It results in a runtime exception

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 6 of 1

15. In light of parametric polymorphism, generics introduce type inference. Which of the
following explains type inference in Java correctly?
(a) With type inferencing engine, the JVM restrict the types that can be used as generic
type arguments.
(b) Type inference enables the compiler look at the type of a method argument to infer a
generic type.
(c) Type inference means that generic type information is not available to the JVM at
runtime, only compile time.
(d) None of the above

16. The lambda expression is the first step of functional programming in Java. Choose a correct
statement for the lambda expression.
(a) Lambda expressions are considered to be anonymous functions that are bound to
identifiers.
(b) Lambda expressions implement functional interfaces with multiple abstract methods.
(c) Lambda expressions are functions that can be created belonging to a class.
(d) Lambda expressions enable passing functionality as an argument to another method
such as what action should be taken when event occurred.

17. Given the following lambda expression. Choose the correct return type of this lambda
expression.

BinaryOperator<String> f = (s1, s2) -> String.join("|", s1, s2);

(a) String
(b) Object
(c) void
(d) Function
(e) None of the above

18. Given the following lambda expression. Choose the correct return type of this lambda
expression.

Function<Double, Function<Double, Double>> f = u -> v -> u * v;

(a) String
(b) Object
(c) void
(d) Function
(e) None of the above

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 7 of 1

19. Consider the following statement. Which of the following best describes the programming
practice used?

String version = computer.getSoundCard().getUSB().getVersion();

(a) The statement will compile and reasonable practice in programming.


(b) The statement may return null reference to indicate the absence of a sound card
resulting null object exception at runtime.
(c) The statement may return null reference to indicate the absence of a sound card
resulting null object exception at compile time.
(d) All of the above

20. Assume that Holder, Account and Rate classes are predefined. Which of the
following is the most reasonable discussion on the given code?

Holder holder = new Holder();


double value = 0;
if(holder != null){
Account acc = holder.getAccount();
if(acc != null){
Rate rate = acc.getRate();
if(rate != null){
value = rate.get();
}
}
}

(a) The program code design looks well-formatted with all possible null object checking.
It is an essential code pattern to meet the requirements.
(b) Multiple checks included for nullable objects may result in a lot of boilerplate code to
make sure no null exception occurs. This way, coding can be cumbersome.
(c) It is not an ideal approach to check the nullable cases or absence/presence of a value
at runtime. The object can be modelled in a container to optionally contain a value
during compilation.
(d) All of the above

21. Strict function means,


(a) Strict functions always evaluate its arguments.
(b) Strict functions evaluate some of its arguments.
(c) Strict functions evaluate the first argument only.
(d) None of the above.

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 8 of 1

22. Non-strict function means,


(a) Non-strict functions always evaluate its arguments.
(b) Non-strict functions evaluate some of its arguments.
(c) Non-strict functions evaluate the first argument only.
(d) None of the above.

23. Which of the following are the effects of eager evaluation strategy except
(a) Code that is easily understandable in terms of execution order that does not potentially
change its behaviour based on a change of execution context.
(b) An easier debug process compared to other evaluation strategies
(c) Responsibility for code performance is however shifted towards the programmer thus
requiring a careful code optimization process.
(d) The ability of define control flow as abstractions instead of primitives.

24. The following explains the lazy evaluation strategy in functional programming paradigms
except
(a) Lazily allows the language runtime to mandatorily evaluate subexpressions that are
not directly linked to the final result of the expression.
(b) Lazily increases the time complexity of an algorithm by evaluating the temporary
computations and conditionals.
(c) Lazily is easy to find its performance because it contains thunks/delayed objects of
expression before their executions.
(d) None of the above.

25. Choose a correct statement of function composition.


(a) Programmers can create smaller functions that only take care of one piece of logic at a
time. They then compose more complex functions by using different smaller functions.
(b) Function composition technique reduces bugs and makes our code hard to understand.
(c) Higher order function and function composition used interchangeably in designing
program functions.
(d) All of the above.

[25 marks]

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021


CT006-3-3-APLC Resit Exam Page 9 of 1

SECTION B
Answer ALL questions.
Each question carries TWENTY-FIVE (25) marks.

Question B1

Object-oriented programming, or OOP, is a variety of languages that create instances of classes


for objects. This helps to determine their type. Many programming languages that are in common
use today support OOP to some degree. Although, OOP allows parallel development, modular
design, maintainability and etc. These provide value-added features in software design and
development. However, OOP tends to use more CPU power in turn it can be inefficient. Due to
various reasons, some programmers are not having much favour into OOP style but the logic
programming can be an option to solve the problem. In your opinion, explain FIVE (5) constructive
reasons to use logic programming paradigm like Prolog, instead of OOP such as C/C++/Java for
software developments.

[25 marks]

Level 3 Asia Pacific University College of Technology & Innovation Oct-2021

You might also like