CSCE 314

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What can go into the underlined space? Haskell's _____ is what helps guarantee referential transparency. - pureness - reference type - static type system - laziness - being a functional programming language - side effectful evaluation strategy

pureness

What can go into the underlined space? Haskell's _____ is what helps guarantee referential transparency. pureness reference type static type system laziness being a functional programming language side effectful evaluation strategy

pureness

What algorithm does f implement? f [] = []f (x:xs) = f left ++ [x] ++ f right where left = [ y | y <- xs, y > x ] right = [ z | z <- xs, z <= x ] quicksort in an ascending order reverse with duplicated elements quicksort in a descending order reverse without duplicated elements mergesort in an ascending order mergesort in a descending order

quicksort in a descending order

What is the following function called? "A function that is defined in terms of itself (by calling itself either directly or indirectly)." partially defined function recursive function higher-order function pure function lambda function lazy function

recursive function

Fill in each of the two blanks in the expression below with a function name or a number so that the completed expression will produce the result when evaluated by the Haskell system. Choices for the function names are given below. Result: [10.0,3.0,2.0] Choices: !! ++ drop head tail map sum product length reverse sqrt take Expression: _____ (map _____ [4,9,100])

reverse, sqrt

Fill in the blank (A) with a correct word (with correct spelling): A method declared (A) belongs to the class and is not associated with a particular instance of the class. _________

static

Fill in each of the two blanks in the expression below with a function name or a number so that the completed expression will produce the result when evaluated by the Haskell system. Choices for the function names are given below. Result: 'd' Choices: !! ++ drop head tail map sum product length reverse sqrt take Expression: "Howdy" _____ ______

!!, 3

The following list comprehension [ HERE | x <- [5,4,3], y <- [1,2] ] resulted [(1,5),(2,5),(1,4),(2,4),(1,3),(2,3)] What should replace HERE in the above list comprehension to get the above result? [y,x] [x,y] (y,x) (x,y)

(y,x)

Which one of the following cannot go into the blank space in the given list expression? [ 1, _____ ] If all of the five Haskell expressions can go into the blank space, then choose "All of the other five choices can go into the blank space." 0::Float 0::Int 0::Integer 0::Double All of the other five choices can go into the blank space 0::Bool

0::Bool

Consider the following code snippet: public class BankAccount {public static void main( String[] args ) {BankAccount a = new BankAccount();. . .}} Suppose it is compiled and run as > java BankAccount Below are some of the steps (at the beginning) taken by the JVM to execute the code. They may not be in a correct order. Put them in the correct order (as the JVM executes). ClassLoader loads BankAccount to the method area. A stack frame for the BankAccount constructor is pushed onto the Java stack A stack frame for main() is pushed onto the Java stack. A pointer to the BankAccount class data for the object is created in the heap.

1 ClassLoader loads BankAccount to the method area. 2 A stack frame for main() is pushed onto the Java stack. 3 A stack frame for the BankAccount constructor is pushed onto the Java stack 4 A pointer to the BankAccount class data for the object is created in the heap.

Typical programming language implementation involves the following unordered major steps. Correctly order the steps, in which a language system generates an executable code from given source code. Optimization Lexical analysis Machine code generation Type checking Parsing

1 - Lexical analysis 2 - Parsing 3 - Type checking 4 - Optimization 5 - Machine code generation

Express the list [2] using (1) the cons (:) operator (2) the append (++) operator Please do not put any blank spaces at the beginning, at the end, or in between the characters, for example, use [] instead of [ ] for the empty list.

2:[] [2]++[]

Cindy wrote a function named h, whose type came up as follows. h :: (t -> b) -> t -> t -> (b, b) How many arguments does h have? (answer choices: numbers between 0 and 42) How many function arguments does h have? (answer choices: numbers between 0 and

3, 1

Which steps are involved in the evaluation of the following expression (according to the step-by-step recursive definition of foldr)? Choose all that apply. foldr (-) 1 [5, 2, 4] 5-2 2-4 4-1 1-5 2-3

4-1 2-3

Typical programming language implementation involves the following (unordered) major steps. Which step produced a compile error if the following line of Haskell code caused the error? f C = if C > 0 then [True] else [False] Lexical analysis Optimization Type checking Parsing Machine code generation

Lexical analysis

Typical programming language implementation involves the following (unordered) major steps. In which step regular grammars would most likely be used? Type checking Lexical analysis Parsing Optimization Lexical analysis Machine code generation

Lexical analysis

Typical programming language implementation involves the following (unordered) major steps. Which step would generate a token table from a stream of characters? Machine code generation Lexical analysis Optimization Type checking Parsing

Lexical analysis

Choose every correct statement. - Lisp was the first functional programming language. - The lambda calculus was first developed in 1980s. - In Haskell, (sqrt 5 + 4) means (sqrt (5 + 4)). - Haskell is the first pure functional programming language. - In Haskell, function names must begin with a lower case letter.

Lisp was the first functional programming language. In Haskell, function names must begin with a lower case letter.

Fill in the blank (A) with a correct word (with correct spelling; it is case sensitive): Class (A) is the root of the class hierarchy in Java. _________

Object

Consider the following code. List<?> aList = new LinkedList<String>();aList.add("Hi"); // compile error Which one of the following is not part of the verbose compile error message? CAP#1 extends Object from capture of ? Object cannot be converted to CAP#1 method List.add(int,CAP#1) is not applicable CAP#1 is a fresh type-variable

Object cannot be converted to CAP#1

Typical programming language implementation involves the following (unordered) major steps. In which step context-free grammars would most likely be used? Type checking Lexical analysis Parsing Optimization Machine code generation

Parsing

Typical programming language implementation involves the following (unordered) major steps. Which step generates a syntax tree, given a stream of tokens as input, by checking whether the program is written according to the grammar? Parsing Machine code generation Optimization Type checking Lexical analysis

Parsing

What function does f3 implement? f3 n xs | n <= 0 = [ ] | null xs = [ ] | True = (head xs) : f3 (n-1) (tail xs) Prelude drop function Prelude take function safetail function (to avoid run-time exception) safehead function (to avoid run-time exception) Prelude length function Prelude zip function

Prelude take function

What is the state of thread t3 right after t3 acquired a monitor lock? TIMED_WAITING BLOCKED NEW RUNNABLE TERMINATED WAITING

RUNNABLE

What is the state of thread t3 right after the start() method is invoked on t3 as t3.start()? TIMED_WAITING BLOCKED NEW RUNNABLE TERMINATED WAITING

RUNNABLE

Cindy wrote a function named h, whose type came up as follows. h :: (t -> b) -> t -> t -> (b, b) In the partial application of h such as > h length "Hi" what specific type is the type parameter t instantiated? - Char - String - Bool - Integer - Int - Double - Float

String

Which of the following is not a wrapper class for a primitive type of Java? String Boolean Byte Character Short Long

String

Some of the methods of the Thread class are deprecated as they are considered too dangerous. Which one of the following is not deprecated? Thread.interrupt() Thread.suspend() Thread.resume() Thread.stop()

Thread.interrupt()

Assume B<T> <: A<T> and MyInt <: Integer <: Number. Is the following true (safe) or false (unsafe) in Java? B<? super Number> <: B<?>

True

True or false: A static nested class is associated with its enclosing class.

True

True or false: In JVM's runtime data areas, separate JVM stack is created per thread.

True

True or false: In Java, program order is only respected within a thread.

True

True or false: Inner classes have access to other members of the enclosing class, including private members.

True

True or false: JVM does not allow byte-level access of memory.

True

True or false: The JVM is a stack-based abstract computing machine.

True

True or false: When you "invoke" the generic type with specific types as the type arguments, the type arguments must be non-primitive type such as class or interface type.

True

Typical programming language implementation involves the following (unordered) major steps. Which step would result in a type-annotated parse tree? Parsing Optimization Type checking Machine code generation Lexical analysis

Type Checking

In the following instantiation of the generic class Pair, Pair<Integer, String> p2 = new Pair<>(2, "two"); what would happen with the empty brackets <> ? It will clear the values of p2. A compile error will happen. Type inference will happen. A runtime exception will occur.

Type inference will happen.

Anna is writing a function f, whose type was given as follows. f :: Ord p => p -> p -> p Choose which one of the following operations or the function min Anna cannot use in her definition of f. If all of the given operations or the function min can be used, choose "All can be used." < == > All can be used. >= min

all can be used.

Consider the following code. Choose the correct answer for each blank in the following statement regarding the overriding method copy() in class Van. class Vehicle { . . . public Vehicle copy() { . . . return new Vehicle(); } } class Van extends Vehicle { . . . @Override public Van copy() { . . . return new Van(); } } "It is a/an [Blank 1 (correct / incorrect)] overriding because [Blank 2 (the reason for your answer for Question 1)]"

correct the return type of the overriding copy() can be covariant.

Which of the following functions returns the third element in a list that contains at least three elements? fun (x:xs) = tail xs fun (_:_:x:_) = x fun xs = head (tail (tail (tail xs))) fun xs = xs !! 3

fun (_:_:x:_) = x

Consider the following function g: g n k = [ k | _ <- [1..n] ] Which of the following is a correct statement about g? g accepts a list of length k as an argument g returns a list of length n g returns an empty list g accepts a function as an argument g returns a list of length n g returns a list [1..k]

g returns a list of length n

What is the following function called? "A function that takes a function as an argument or returns a function as a result." pure function constant function partially defined function recursive function lambda function lazy function higher-order function

higher-order function

When overriding methods in Java, the parameter types must be contravariant covariant invariant

invariant

What can go into the underlined space? Haskell's _____ is what helps avoid unnecessary computation and ensuring that programs terminate whenever possible. - pureness - laziness - being a functional programming language - reference type - static type system - side effectful evaluation strategy

laziness

Suppose Car <: Vehicle, and method m defined in the Vehicle class has the signature: m : Vehicle -> Vehicle The Car class is overriding method m. Which of the following are possible type signatures allowed by Java of the overriding method m of Car? Choose all that apply. m : Vehicle -> Vehicle m : Vehicle -> Car m : Car -> Car m : Car -> Vehicle

m : Vehicle -> Vehicle m : Vehicle -> Car

Fill in each of the two blanks in the expression below with a function name or a number so that the completed expression will produce the result when evaluated by the Haskell system. Choices for the function names are given below. (Hint: "Gm" is the same as ['G','m'].) Result: "Gm" Choices: !! ++ drop head tail map sum product length reverse sqrt take Expression: ____ ____ ["Good", "morning"]

map, head

Fill in each of the two blanks in the expression below with a function name or a number so that the completed expression will produce the result when evaluated by the Haskell system. Choices for the function names are given below. Result: [2,3] Choices: !! ++ drop head tail map sum product length reverse sqrt take Expression: ____ ____ ["Hi", "all"]

map, length

Choose a correct word for the blank from the choices (with correct spelling, just as used in the code). "Anonymous inner classes are defined in the __________ expression itself." Choices: nested, new, inner, static, abstract, final, type

new

Given the following code for withdraw and deposit for a banking software, suppose thread P1 is executing withdraw() and thread P2 executing deposit() almost at the same time. Can the execution of the two threads exhibit race hazard? public void withdraw(double amount) { balanceChangeLock.lock(); try {while (balance < amount) {} balance = balance - amount; } finally { balanceChangeLock.unlock(); } } public void deposit(double amount) { balanceChangeLock.lock(); try { balance = balance + amount; } finally { balanceChangeLock.unlock(); } } yes no

no

Fill in the blank (A) with a correct word (with correct spelling): A field declared (A) can only be accessed by the methods in the current class.

private

Bob wrote a function named g, whose type came up as follows: g :: (Int -> Int) -> Int -> Int The function g takes three arguments one at a time takes a pair of arguments as a tuple takes a function as its argument returns a function as its result returns a pair of results

takes a function as its argument

What is the actual output of the following code snippet? Answer exactly as it is shown. List<Double> l1 = new ArrayList<Double>();ArrayList<String> l2 = new ArrayList<String>();System.out.println(l1.getClass() == l2.getClass()); false 0 true 1

true

What is the following process called? "If function f maps arguments of type A to results of type B, and expression e is of type A, then the application (f e)'s type is automatically deduced to type B." type validation type laziness type inference automatic typing dynamic type process

type inference

Which one of the following is not a basic synchronization action between Java threads? monitor locks Thread.sleep() synchronized methods and blocks volatile variables

volatile variables

Given the following code for withdraw and deposit for a banking software, suppose thread P1 is executing withdraw() and thread P2 executing deposit() almost at the same time. Can the execution of the two threads reach a deadlock? public void withdraw(double amount) { balanceChangeLock.lock(); try {while (balance < amount) {} balance = balance - amount; } finally { balanceChangeLock.unlock(); } } public void deposit(double amount) { balanceChangeLock.lock(); try { balance = balance + amount; } finally { balanceChangeLock.unlock(); } }

yes

Is the following grammar in BNF ambiguous (yes) or not ambiguous (no)? < expr > ::= < expr > + < number > | < expr > - < number > | < number >< number > ::= < digit > | < digit > < number > | < expr > < digit > ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 yes no

yes

Suppose the two functions g1 and g2 have the following types. g1 :: Int -> Char g2 :: [Bool] -> Int What is the type of the (final) return value of the composed function (g1 . g2) ? ([Bool], Int) Char (Int, Char) (Char, [Bool]) [Bool] Int

Char

Consider the following inheritance hierarchy. A <: B <: C Which of the following are true? Choose all that apply. C is a supertype of B C is a supertype of A A is a subtype of B B is a subtype of C A is a subtype of C

All of the above

Which one of the following cannot go into the blank space in the given tuple expression? ( 1, _____ ) If all of the five Haskell expressions can go into the blank space, then choose "All of the other five choices can go into the blank space." - All of the other five choices can go into the blank space. - "Howdy" - True - 3.14::Float - 2 - -1

All of the other five choices can go into the blank space

True or false: The Java memory model guarantees sequential consistency.

False

What is the state of thread t3 if t3 is waiting for a monitor lock? TIMED_WAITING BLOCKED NEW RUNNABLE TERMINATED WAITING

BLOCKED

Consider the following code snippet with generics: List <integer> l = new LinkedList <integer>();l.add(Integer.valueOf(7));String x = l.get(0); // this line In "this line" above, what would happen? It will compile, but at run time, a bad cast exception will occur. It does not cause any problem; it will compile and run just fine. Compile error because Integer cannot be converted to String. Compile error because Object cannot be converted to Integer. Compile error because you need type cast the return value of get() to String.

Compile error because Integer cannot be converted to String.

Consider the following code snippet without generics: List l = new LinkedList();l.add(Integer.valueOf(7));Integer x = (Integer) l.get(0); // this line In "this line" above, what would happen without the type cast? It will compile by auto-boxing/unboxing, but at run time, a bad cast exception will occur. Compile error saying that Object cannot be converted to Integer. Compile error saying that such value does not exist. Compile error saying that the index out of bound. Compile error saying that the get() method does not exist.

Compile error saying that Object cannot be converted to Integer.

Assume B<T> <: A<T> and MyInt <: Integer <: Number. Is the following true (safe) or false (unsafe) in Java? A<? extends Number> <: A<? super Integer>

False

Suppose ArrayList<T> <: List<T> and Integer <: Number. Is the following true (safe) or false (unsafe) in Java? ArrayList<Integer> <: List<Number>

False

True or false: In JVM's runtime data areas, separate method area is created per thread.

False

True or false: JVM translates a class definition into a format (class file) a Java compiler understands.

False

True or false: Local inner classes are members of the enclosing class.

False

True or false: The JVM stack is where objects live.

False

Fill in the blank (A) with a correct word (with correct spelling): Newly created objects are allocated within an area of JVM runtime memory known as the (A).

Heap

Which one of the following is a correct statement regarding type parameter constraints? To be able to use the toString() method, you need to do "extends String". Multiple constraints for a single type parameter are not allowed. If no constraints are given, the constraint "extends Object" is assumed. The Object class cannot be extended because it is an abstract class. The only type parameter constraint Java allows is "extends Object".

If no constraints are given, the constraint "extends Object" is assumed.

Function f has the type f :: (Int, Int) -> [Int] and is defined as f (x, y) = [x, y]. That is, f accepts two Ints in a tuple at the same time and returns the two Ints put into a list in the same order they are given in the tuple. Now consider f_curried, a curried version of f, that takes the two Ints one at a time and once both Ints are provided, returns the two Ints put into a list in the same order they are given. First, give the type of f_curried. And then, give the definition of f_curried. Keep argument names the same as in f. f_curried :: _______________ -- type declaration of f_curried f_curried ____________ -- complete definition of f_curried including its

Int -> Int -> [Int, Int] x y = [x, y]

What does "type safety" mean in relation to generics? It means catching more errors at compile time. It means delaying compile time error checking to run time. It means eliminating parametric polymorphism to avoid type errors. It means reducing compile errors as much as possible.

It means catching more errors at compile time.

Which one of the following is not true about Java? Java provides built-in support for computer networks. Java aims for platform dependence for security. Java allows executing code from remote sources. Java uses both static binding and dynamic binding.

Java aims for platform dependence for security.

True or false: The size of the int type of Java varies depending on the operating system and is either 32-bit or 64-bit.

false

Which one of the following is not guaranteed by the rules of the "event A happens-before event B" relation of the Java memory model? Unlocking of a lock by one thread happens before locking of the same lock by another thread. Write of shared variable x by one thread happens-before subsequent reads of x by other threads. Action A followed by action B in the same thread. Actions before start of a thread happen before actions in the thread.

Write of shared variable x by one thread happens-before subsequent reads of x by other threads.

Choose all that apply. Choose the ones that have the same type as "Hi". - "H" : "i" : [] - ["H"] ++ ["i"] - [ 'H' , 'i' ] - 'H' : 'i' : [] - "H" ++ "i" - 'H' : "i" - ['H'] : ["i"] - "Hi" : []

[ 'H' , 'i' ] 'H' : 'i' : [] "H" ++ "i" 'H' : "i"

Consider the following function h h [] = [] h (x:xs) = h xs ++ HERE The following evaluation h [3..6] resulted [6,5,4,3] What should replace HERE in the above definition of h to get the above result? ( xs : x) h x [ ] h [ x ] x [ x ] ( x : xs )

[ x ]

Which list comprehension correctly expresses map and filter given below? filter p xs filter p . map f $ xs map f xs map f . filter p $ xs [ f x | x <- xs] [ f x | x <- xs, p x ] [ x | x <- xs, p x ] [ f x | x <- xs, p ( f x ) ]

[ x | x <- xs, p x ] [ f x | x <- xs, p ( f x ) ] [ f x | x <- xs] [ f x | x <- xs, p x ]

Match each section with a lambda expression that behaves the same way and performs the same task as the section. (1-) (-1)

\x -> 1-x \x -> x-1

The min function accepts two values of an instance type of Ord and returns the smaller of the two. Choose the correct definition of min using a lambda expression with an if conditional expression. min = \x \y -> if x < y = x else y \x -> \y -> if x < y then x else y \x -> y -> if x < y then x else y \x \y -> if x < y then x else y \x y -> if x < y then x otherwise y

\x -> \y -> if x < y then x else y

Fill in each of the two blanks in the expression below with a function name or a number so that the completed expression will produce the result when evaluated by the Haskell system. Choices for the function names are given below. Result: [6,7,8,9,10] Choices: !! ++ drop head tail map sum product length reverse sqrt take Expression: ____ ____ [1..10]

drop, 5

Consider the function f defined as below. f = foldr ( \_ n -> 1+n ) 0 Which of the following is a correct statement regarding the function f? f accepts a list and a number n as its arguments and inserts n at the end of the list. f accepts a list as its argument and sums the elements in the list. f accepts a list as its argument and counts the number of elements in the list. f accepts a list as its argument and adds one to each element in the list. f accepts a list and a number n as its arguments and adds n to each element in the list.

f accepts a list as its argument and counts the number of elements in the list.

True or false: All method declarations in an interface, including default methods, are implicitly private.

false


Set pelajaran terkait

Simulation Lab 11.1: Module 11 Harden PC with Group Policy Editor

View Set

markets and financial intermediation exam 1

View Set

PostTest, Practice Exam 1 & Practice Exam 2

View Set

Coursera Spanish Vocabulary: Meeting People: Vocab 1

View Set

BIOLOGY FINAL EXAM REVIEW CH.12 QUESTIONS

View Set