CSCI 461 - Quiz 2
Consider the following code line datatype 'data tree = Empty | Node of 'data tree * 'data * 'data tree; fun sumall Empty = 0 | sumall (Node(x,y,z)) = sumall x + y + sumall z; val tree123 = Node(Node(Empty,1,Empty),2,Node(Empty,3,Empty)); sumall tree123; Which of the following is true? There is an error in code val it = 2 : int val it = 3 : int val it = 6 : int
val it = 6 : int
What is the output of the following? ord #"A"; val it = 67 : int val it = 95 : int val it = 65 : int val it = 97 : int
val it = 65 : int
Consider the following code lines: datatype 'element mylist = NIL | CONS of 'element * 'element mylist; CONS(1.0, CONS(2.0, CONS(3.0, NIL))); Which of the following is true? val it = CONS (1.0,CONS (2.0, CONS(3.0, NIL))) : real mylist val it = [1.0, 2.0, 3.0] : real mylist val it = CONS (1.0,CONS (2.0, CONS(3.0, NIL))) : mylist val it = (1.0, 2.0, 3.0) : real mylist
val it = CONS (1.0,CONS (2.0, CONS(3.0, NIL))) : real mylist
datatype 'element mylist = NIL | CONS of 'element * 'element mylist; CONS(1.0, CONS(2.0, NIL)); Which of the following is true? Error val it = CONS (1.0,CONS (2.0,NIL)) : mylist val it = (1.0, 2.0) : int mylist val it = CONS (1.0,CONS (2.0,NIL)) : real mylist
val it = CONS (1.0,CONS (2.0,NIL)) : real mylist
Consider the following code line datatype 'data tree = Empty | Node of 'data tree * 'data * 'data tree; val tree123 = Node(Node(Empty,1,Empty),2,Node(Empty,3,Empty)); fun incall Empty = Empty | incall (Node(x,y,z)) = Node(incall x, y+1, incall z); incall tree123; Which of the following is true? There is an error in code val it = Node (Node (Empty,2,Empty),3,Node (Empty,4,Empty)) : int tree val it = Node(Node(Empty,1,Empty),2,Node(Empty,3,Empty)) : int tree val it = Node(2, 3, 4): int tree
val it = Node (Node (Empty,2,Empty),3,Node (Empty,4,Empty)) : int tree
option is a predefined type constructor in ML. Consider the following code lines: fun optdiv a b = if b = 0 then NONE else SOME (a div b); optdiv 7 2; what is the result? val it = SOME 1 : int option val it = SOME 3 : int option val it = 3 : int val it = SOME 3 : option
val it = SOME 3 : int option
Consider the following function fun merge (nil, ys) = ys | merge (xs, nil) = xs | merge (x :: xs, y :: ys) = if (x < y) then x :: merge (xs, y :: ys) else y :: merge(x :: xs, ys); What is the result for the following statement: merge ([1, 3, 5], [2, 4, 6]); val it = [1, 2, 3, 4, 5, 6] : list val it = [1, 2, 3, 4, 5, 6] : int list val it = [1, 3, 5, 2, 4, 6] : int list val it = [1, 4, 2, 5, 3, 6 ] : int list
val it = [1, 2, 3, 4, 5, 6] : int list
Consider the following function fun merge (nil, ys) = ys | merge (xs, nil) = xs | merge (x :: xs, y :: ys) = if (x < y) then x :: merge (xs, y :: ys) else y :: merge(x :: xs, ys); What is the result for the following statement: merge ([1,3], [2]); val it = (1, 2, 3) : int list val it = ([2, 1, 3]) : int list val it = ([1, 2, 3]) : int list val it = [1, 2, 3] : int list
val it = [1, 2, 3] : int list
what is the output of following map (op +) [(1,2),(3,4),(5,6)]; val it = [9, 12] : int list val it = [3,7,11] : int list error val it = [3,7,11] : int * int * int
val it = [3,7,11] : int list
What is the output of the following? map (fn x => x mod 2 = 1) [1,2,3,4]; val it = [false,true,false,true] : bool list val it = [0, 1,0,1] : int list val it = [1,0,1,0] : int list val it = [true,false,true,false] : bool list
val it = [true,false,true,false] : bool list
What is the output of the following? map ~ [1,2,3,4]; error val it = [1,2,3,4] : int list val it = [~1,~2,~3,~4] : int list val it = [-1,-2,-3,-4] : int list
val it = [~1,~2,~3,~4] : int list
Consider the following code line datatype 'data tree = Empty | Node of 'data tree * 'data * 'data tree; val tree123 = Node(Node(Empty,1,Empty),2,Node(Empty,3,Empty)); fun isintree x Empty = false | isintree x (Node(left,y,right)) = x=y orelse isintree x left orelse isintree x right; isintree 4 tree123; Which of the following is true? There is an error in code val it = false : bool val it = [1,2,3] : int list val it = true : bool
val it = false : bool
Given fun g a = fn b => a-b; what is the result of g 2 3; val it = 1 : int val it = ~1 : int val it = -1 : int error
val it = ~1 : int
What is the output for following two lines of code in ML? val x = ~; x (3 + 5); val it = 8 : int val it = ~8 : int val it = ~2 : int val it = 2 : int
val it = ~8 : int
Based on code line datatype 'a option = NONE | SOME of 'a; val x = SOME 4; Which of the following is true? val x = SOME 4 : int val x = SOME 4 : int option val x = 4 : int option val x = SOME 4 : option
val x = SOME 4 : int option
Which of the following is not a ML pattern? _ .... A constant A cons of patterns
....
What is the result of following? (fn x => x + 2) 1; 1 3 2 error
3
What is the result of following? (fn x => x + 2) 1; 3 1 error 2
3
What order the following function has? int -> bool -> real -> string 1 3 4 2
3
let val n = 1 in let val n = 3 in n end end; what is the output value? 1 3 4 2
3
What is the value of g 5 when classic rule of scoping is used in ML fun g x = let val inc = 1; fun f y = y + inc; fun h z = let val inc = 2; in f z end; in h x end; 8 7 5 6
6
In ML, the case-expression is defined as following: <case-expr> ::= case <expression> of __________ <match> <expression> <statement> <rule>
<match>
option is a predefined type constructor in ML. Consider the following code lines: fun optdiv (a, b) = if b = 0 then NONE else SOME (a mod b); optdiv 7 0; what is the result? val it = 3 : int val it = NONE : int option val it = SOME 3 : int option Error
Error
What is the result of the following: let val x = 1 val y = 2 in x + y; val it = 2 : int Error. Will not run val it = 3 : int val it = 1 : int
Error. Will not run
A function that does not take any functions as parameters, and does not return a function value, has order 0 True False
False
Coercion uses the types to choose the definition True False
False
The following function will work on any list: fun f (x :: xs) = x; True False
False
The following function will work on any list: fun f [a, _] = a; True False
False
to avoid ambiguous, there is only one definition for a given name true false
False
A function that takes a function as a parameter or returns a function value has order n+1, where n is the order of its highest-order parameter or returned value True False
True
A match in ML is an expression True False
True
According to your book, a definition is anything that establishes a possible binding for a name. True False
True
An occurrence of a name is in the scope of a given definition of that name whenever that definition governs the binding for that occurrence True False
True
C++ allows virtually all operators to be overloaded True False
True
C++, permit the programmer to overload function names True False
True
Given datatype day = Mon | Tue | Wed | Thu | Fri | Sat | Sun; day is new type constructor and Mon, Tue, ..., Sun are new data constructors True False
True
In C++, the namespace specifies the visibility of its components True False
True
In Java and other C-like languages, you can combine statements into one compound statement using { and }. A compound statement also serves as a block: True False
True
ML pattern introduces new variables True False
True
ML's syntax keeps types and expressions separated True False
True
There may be more than one definition for a given name. Each occurrence of the name (other than a definition) has to be bound according to one of its definitions True False
True
Type constructor parameter comes before the type constructor name True False
True
Type constuctor parameter comes before the type constructor name True False
True
bool type in ML is really just an enumeration True False
True
if exp1 then exp2 else exp3 and the following are equivalent. case exp1 of true => exp2 | false => exp3 True False
True
Which of the following is a data constructor that takes a parameter? datatype exint = Value of int | PlusInf | MinusInf; exint PlusInf MinusInf Value
Value
A ___________ is any language construct that contains definitions, and also contains the region of the program where those definitions apply sentence variable block program
block
Which of the following is ML keyword to define a new data type? struct typedef datatype class
datatype
Which of the following is a legal type conversion in java? double x; x = (double) 2; int x; x = 2.0; double x; x = 2;
double x; x = (double) 2;
Which of the following is a legal coercion in java? int x; x = 2.0; double x; x = 2; double x; x = (double) 2;
double x; x = 2;
Which of the following is NOT a data constructor? datatype exint = Value of int | PlusInf | MinusInf; Value MinusInf PlusInf exint
exint
Which of the following is a type constructor? datatype exint = Value of int | PlusInf | MinusInf; Value MinusInf PlusInf exint
exint
Which of the following will produce a warning message? fun f 0 = "yes"; fun f _ = "yes"; fun f x = "yes"; none of the above
fun f 0 = "yes";
Rewrite the following function without using any variables: fun f x = "YES";
fun f _ = "YES";
Which of the following is equivalent to fun f (a,b) = a + b; fun g a = fn b = a+b; fun g a = fn b => a+b; fun g a => fn b = a+b; fun g a => fn b => a+b;
fun g a = fn b => a+b;
Given fun g a = fn b => fn c => a+b+c; What is the type of g? int * int * int -> int int -> int -> int -> int int * int -> int -> int (int -> int) -> (int -> int)
int -> int -> int -> int
Consider the following function fun merge (nil, ys) = ys | merge (xs, nil) = xs | merge (x :: xs, y :: ys) = if (x < y) then x :: merge (xs, y :: ys) else y :: merge(x :: xs, ys); What is the type of this function? cannot be determined int list * int list -> int list list * list -> list
int list * int list -> int list
In C++, which square gets called for square('a') ? int square(int x) { return x*x; } double square(double x) { return x*x; } neither
int square(int x) { return x*x; }
A ______________ is any language construct that contains definitions and a region of the program where those definitions apply, and also has a name that can be used to access those definitions from outside the construct variable statement block labeled namespace
labeled namespace
In ML, you can add a parameter of any type to a data constructor, using the keyword ______ of struct typedef def
of
Which of the following exhibits ad hoc polymorphism, i.e. it has at least two but only finitely many possible types oveloading parameter coercion parametric polymorphism subtype polymorphism
oveloading parameter coercion
A function exhibits ____________ polymorphism if it has a type that contains one or more type variables assignment subtype coercion parametric
parametric
When a language supports coercion of parameters on a function call (or of operands when an operator is applied), the resulting function (or operator) is ____________ over whelmed polymorphic overridden overloaded
polymorphic
a type with type variables is a __________ unitype polytype multitype monotye
polytype
What will happen if run the following line in ML? val int = 3; result: val it = 3 : int result: val int = 3 : int Error. Keyword int cannot be variable name
result: val int = 3 : int
What will happen if run the following line in ML? val real = 3; result: val it = 3 : int result: val real = 3 : real Error. Keyword real cannot be variable name result: val real = 3 : int
result: val real = 3 : int
The question: whether a given occurrence of a name is in the scope of a given definition? If the questios is answered at compile time, it is called __________ scoping static systematic dynamic traditional
static
Given datatype day = Mon | Tue | Wed | Thu | Fri | Sat | Sun; and fun isWeekDay Sat = false | isWeekDay Sun = false | isWeekDay _ = true; What is the value of isWeekDay Mon; true Error false
true
Consider ML structure structure Fred = struct val a = 1; fun f x = x + a; end; How can outsider access a? use structure name and period as: Fred.a ML has no structure There is no way to access a from outside directly use name a
use structure name and period as: Fred.a
Which of the following have the same effect as: fun f x = x * x; val f => fn x = x* x; val f = x => x * x; val fn x => x* x; val f = fn x => x * x;
val f = fn x => x * x;
what is the output of the following foldr (op ^) "" ["abc","def","ghi"]; val it = "ghidefabc" : string val it = "abcdefghi" : string val it = "defabcghi" : string val it = "defghiabc" : string
val it = "abcdefghi" : string
Consider the following halve function: fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a :: b :: cs) = let val (x, y) = halve cs in (a :: x, b :: y) end; What is the result of the following statement? halve [1, 2, 3, 4]; val it = ([1, 4], [2, 3]) : int list * int list val it = ([1, 2], [3, 4]) : int list * int list val it = ([1, 3], [2, 4]) : int list * int list val it = ([1, 4], [2, 3]) : list * list
val it = ([1, 3], [2, 4]) : int list * int list
Consider the following halve function: fun halve nil = (nil, nil) | halve [a] = ([a], nil) | halve (a :: b :: cs) = let val (x, y) = halve cs in (a :: x, b :: y) end; What is the result of the following statement? halve [1]; val it = ([1], []) : int list * int list val it = ([1], []) : int * int list val it = ([], [1]) : int list * int val it = ([], [1]) : int list * int list
val it = ([1], []) : int list * int list
What is the output for following two lines of code in ML? val x = ~; x 3 + 5; val it = 8 : int val it = ~8 : int val it = ~2 : int val it = 2 : int
val it = 2 : int
What is the output? foldl (op -) 0 [1,2,3,4]; val it = ~2 : int val it = [1,2,3,4] : int list error val it = 2 : int
val it = 2 : int
What is the output? ~ 3 + 5; val it = ~8 : int val it = 8 : int val it = 2 : int val it = ~2 : int
val it = 2 : int
What is the output? foldr ( op * ) 1 [1,2,3,4]; error val it = 0 : int val it = [1,2,3,4] : int list val it = 24 : int
val it = 24 : int
What is the result of the following: let val x = 1 val y = 2 in x + y end; val it = 2 : int val it = 3 : int val it = 1 : int val it = 4 : int
val it = 3 : int
What is the result of the following: let val x = 1 val y = 3 in x + y end; val it = 3 : int val it = 2 : int val it = 4 : int val it = 1 : int
val it = 4 : int
