Solution to Sample Midterm 1 - Computability and Languages | CSE 460, Exams of Computer Science

Material Type: Exam; Professor: Torng; Class: Computability and Languages; Subject: Computer Science & Engineering; University: Michigan State University; Term: Fall 2001;

Typology: Exams

Pre 2010

Uploaded on 07/23/2009

koofers-user-yw4
koofers-user-yw4 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 460 - Solutions to Sample Midterm 1
Exam
Fall Semester, 2001
The total points are as follows:
The basic points on this exam sum to 46.
The advanced points on this exam sum to 12.
There are also 3 extra credit advanced points on the exam.
1. Consider the following pseudocode program P.
bool main(int n) {
int k=0;
if ( n < 0 ) return(no);
while ( k*k != n ) k++;
return(yes);
}
(a) What is Y(P)? [2, basic]
The set of integers that are perfect squares.
(b) What is N(P)? [2, basic]
The set of negative integers.
(c) What is I(P)? [2, basic]
The set of positive integers that are not perfect squares.
(d) What is C(P)? [2, basic]
The empty set.
(e) Does Psolve Y(P)? Explain why or why not. [2, basic]
No it does not since it loops on some inputs. In order to solve a problem Pmust halt
on all legal inputs.
(f) Is Y(P) solvable? Explain why or why not. [2, basic]
Yes it is. Even though Pdoes not solve the set of integers that are perfect squares, we
can easily create a different program that does solve Y(P). For example
bool main(int n) {
int k=0;
while ( k*k < n ) k++;
if ( k*k == n ) return (yes);
else return(no);
}
1
pf3
pf4
pf5

Partial preview of the text

Download Solution to Sample Midterm 1 - Computability and Languages | CSE 460 and more Exams Computer Science in PDF only on Docsity!

CSE 460 - Solutions to Sample Midterm 1

Exam

Fall Semester, 2001

The total points are as follows: The basic points on this exam sum to 46. The advanced points on this exam sum to 12. There are also 3 extra credit advanced points on the exam.

  1. Consider the following pseudocode program P.

bool main(int n) { int k=0; if ( n < 0 ) return(no); while ( k*k != n ) k++; return(yes); }

(a) What is Y (P )? [2, basic] The set of integers that are perfect squares. (b) What is N (P )? [2, basic] The set of negative integers. (c) What is I(P )? [2, basic] The set of positive integers that are not perfect squares. (d) What is C(P )? [2, basic] The empty set. (e) Does P solve Y (P )? Explain why or why not. [2, basic] No it does not since it loops on some inputs. In order to solve a problem P must halt on all legal inputs. (f) Is Y (P ) solvable? Explain why or why not. [2, basic] Yes it is. Even though P does not solve the set of integers that are perfect squares, we can easily create a different program that does solve Y (P ). For example bool main(int n) { int k=0; while ( kk < n ) k++; if ( kk == n ) return (yes); else return(no); }

  1. Consider the Failure Problem F AIL that is defined as follows:
    • Input: Program P , nonnegative integer x (an input to program P ).
    • Yes/No Question: Does P loop or crash on x?

Assume PF is a program that solves F AIL. Consider the following table that describes the behavior of some programs on some inputs.

0 1 2 3

P0 Crash Accept Loop Crash P1 Reject Reject Reject Reject P2 Loop Accept Loop Loop P3 Accept Reject Accept Accept

(a) Based on the above table, fill in the following table with the value that PF would give to the corresponding input. For example, the upper left entry of your answer should reflect what PF would return on the input (P0,0). [3, basic]

P0 YES NO YES YES

P1 NO NO NO NO

P2 YES NO YES YES

P3 NO NO NO NO

(b) Consider the following incomplete program D. bool main(unsigned int y) { program P = generate(y); if (PF (P, y)) Action A; else Action B; } program generate(unsigned int y); bool PF (program P, unsigned int y); In order to complete program D, what actions would be appropriate? Circle all that apply. [2, advanced] Action A: Accept, Reject, Loop, Crash Correct choices are Accept, Reject Action B: Accept, Reject, Loop, Crash Correct choices are Loop, Crash

(c) Explain your answer to part (b). [3, advanced] If we end up at Action A, this means that PF (P, y) evaluated to true. That means that P fails on y which means that P either loops or crashes on y. In order to guarantee that D behaves differently on y, we can make D accept or reject y. Thus, these are the only

program main(program P, unsigned int y) Note the return type program should be one that takes as input a string whereas the input parameter program type should be one that takes as input an unsigned int. If you specify that difference, that is good, but not required.

  1. For programs Q 2 and Q 3 , the string alphabet is {a, b}. Consider the program P 3 that has input and output as follows. - Input: A program Q 1 and a non-negative integer inputx - Output: A program Q 3 as follows: bool main(string z) { unsigned int x = inputx; /* inputx is one of two inputs to P 3 / Q 1 (x); return(Q 2 (z)); } bool Q 1 (unsigned int y); / Details unknown */ bool Q 2 (string z) { if ((z == “a”) || (z == “ab”)) return (yes); else return(no); }

(a) Specify Y (Q 3 ) as precisely as you can. [2, basic] If Q 1 halts on inputx, then Y (Q 3 ) = {a, ab}. Otherwise, if Q 1 loops on inputx, then Y (Q 3 ) = {}. (b) This program P 3 computes an answer-preserving input transformation from the halting problem to some of the following problems. Identify all such problems, and for those which this does not apply, state whether or not there is a Yes→No violation or a No→Yes violation. [4, advanced] i. Input: A program P that takes as input a string over {a, b} Yes/No Question: Is Y (P ) infinite? This does not work. There is a Yes→No violation.

ii. Input: A program P that takes as input a string over {a, b} Yes/No Question: Is Y (P ) finite? This does not work. There is a No→Yes violation.

iii. Input: A program P that takes as input a string over {a, b} Yes/No Question: Is ab ∈ Y (P )? This does work. iv. Input: A program P that takes as input a string over {a, b} Yes/No Question: Is Y (P ) = {a}? This does not work. There is a Yes→No violation.

  1. Consider the following diagrams.

Set of problems/languages over {a,b} Set of strings over {a,b}

(a) Place the following items into both diagrams if appropriate. Note, some items may only fit in one diagram. If so, state this next to the item below. [5, basic]

i. RE: only in left diagram ii. REC: only in left diagram iii. aba: only in right diagram iv. {a}∗ v. S = the set of all even length strings over alphabet {a, b}. (b) Place H and Hc^ into the left diagram where H denotes the Halting problem. [2,basic] See corresponding powerpoint presentation for diagrams with all items above in proper relation to each other.

  1. Let S be the set of strings with exactly one a over the alphabet {a, b}.

(a) Give the 3 SHORTEST strings that are in S. [1,basic] a, ab, ba

(b) Give 3 strings that are not in S. [1,basic] λ, b, bb, aa, bbb, aaa, aba is a list of more than 3

(c) Suppose we list the strings in S in alphabetical order. Which of the following strings appear in a finite position on this list? Circle all that apply. [2, basic] a, ba, abbb, bbba, bbabb, bbbbabbbb The only correct choices are those that begin with a. In this list, that would be a and abbb. (d) Above, we have observed that when we list all the strings in S in alphabetical order, some strings do not appear in a finite position on this list. Does this prove that S is uncountably infinite? Why or why not? [2, basic] No it does not. It merely shows that this ordering does not prove that S is countably infinite. However, we only need one ordering to work in order for S to be countably infinite. An ordering that does work is to list the elements of S by order of their length. For strings of the same length, order them alphabetically (i.e. by the position of the one

Answer 2: You could also prove this by representing the majority operation as a combination of other set operations such as set union and set intersection. Since RE is closed under both of these operations, this would imply RE is also closed under the majority operation.

  1. Suppose I want to verify the correctness of program P.

(a) If I implement a testing approach, do I need the source code for P or will an executable version of P be sufficient? Briefly explain your answer. [2, basic] In this case, I do not need the source code as all I will do is run P on some inputs and see what P produces as output. I do not examine how P performs its job or do any analysis of its operation or logic.

(b) If I implement an analysis approach, do I need the source code for P or will an executable version of P be sufficient? Briefly explain your answer. [2, basic] In this case, I do need the source code of P as I will be performing an analysis of P to derive some overall assessment of the program. I will probably be analyzing how it works and the logic behind the program, so an executable version is not sufficient.

  1. Prove that REC is closed under the set concatenation operation. [extra credit 3, advanced]
    • Let L 1 and L 2 be arbitrary solvable languages.
    • Let P 1 and P 2 be programs that solve L 1 and L 2 , respectively.
    • Construct program P ′^ from programs P 1 and P 2.
    • Argue that P ′^ solves L 1 L 2.
    • Therefore, L 1 L 2 is in REC.
    • Therefore, REC is closed under set concatenation.

What does it mean for a string x to be in L 1 L 2? Contrary to many suggested answers, this does not necessarily mean that x ∈ L 1 or x ∈ L 2. Rather, it means that some prefix of x is in L 1 and the corresponding suffix of x is in L 2.

Where does this prefix begin and end? We don’t know, so we have to test all possible starting and ending locations. Specifically, suppose x has length n. Then for 0 ≤ i ≤ n, P ′^ will test the prefix consisting of the first i characters of x by running P 1 on that prefix and the corresponding suffix consisting of the remaining n − i characters by running P 2 on that suffix. If both say yes for any value of i, then P ′^ returns yes. Otherwise, if at least one fails for all values of i, then P ′^ returns no.