Quiz 1

Report 5 Downloads 269 Views
TODO COMP 101: Introduction to Computing Spring 2018

Quiz 1 Name ____________________________________________________________

PID

____________________________________________________________

HONOR CODE Pledge: I have neither given nor received aid on this exam.

Signed ___________________________________________________________

1

1 True/False ____ (a) The very first assignment of a variable is known as initialization. ____ (b) let n: string; is an example of declaration. ____ (c) The purpose of the statement let m: string = await promptString(“hi”); is to assign the variable m with value “hi”. ____ (d) Once a variable is declared, its type can never change again but its value can. ____ (e) The bit is the fundamental unit of the computer. ____ (f) One byte is ten bits. ____ (g) Adding two numbers together is called concatenation. 2. The following program contains variable declaration and assignment statements. As each line in the program runs, fill in the value of each variable in the columns to the right, after the line completes. If a variable has not been declared, write “DNE”. If a variable has been declared, but not assigned an initial value, write a question mark “?” a b c Line# 1 let c: string; 2 let a: string = “a”; 3 /* c = “c”; */ 4 c = a + a; 5 a = c; 6 // let b: string; 7 let b: string = a; 8 b = a; 9 c = b; 3. For each of the following code pieces, write down in the right box (a) whether the code is correct (contains no compile error) and (b) if the code is incorrect, explain why it is wrong. Assume the code in each box is independent of others. let a: number; let b: number = 8; let c: number = “4”; let d: string = “12”; let e: string = await promptNumber(“What year are you”); let f: number = 10.3; let g: number = f; g = g + g; print(How are you); let h: number = 3.1; print(h); print(“h”);

2

4. When the code on the right runs, what does the user have to input sequentially for it to print out “Hi comp101ers”?

let tar: string = “heel”; let comp: string = “101”; print(“tar” + “!”); print(tar + “!”); print(comp + “101”); print(comp + tar); print(“tar” + “heel”);

let a: number let b: number let c: number let d: string let e: string print(“Hi ” +

= = = = = d

await promptNumber(“Input here.”); 1; a + b; await promptString(“Input here.”); await promptString(“Input here.”); + c + e);

5. When the code on the left runs, write down what will be printed out to the screen.

6. Given the following program on the left, write down the output, one line per box on the right. You may not have to fill all the boxes. let main = async () => { let a: number = 10; let b: number = 2; let c: number = 4; let d: number = a - a * b / c; print(d); a = a * a – a; print(a); let e: number = ((b + c) * (c – b) / (c / b)) / b; print(e); let f: number = a * 0 - a; print(“f”); } main(); 7. Given the following program on the left, write down the output, one line per box on the right. You may not have to fill all the boxes. let main = async () => { let a: number = 10; let b: number = 2; let c: number = 4; c = a; a = b; b = a; print(b); b = c; print(a); print(b); print(c); } main();

3

8. Implement the following main function according to the instructions in the comments. let main = async () => { // (a) Declare a variable named str of appropriate type and assign it with // value “comp”. // (b) Using string concatenation, make it so that str now holds “comp110”. // (c) Declare a number type variable and give it a name you like.

// (d) Assign 123 to the variable in (c).

// (e) Print out the two variables onto the screen.

} 9. Given an incomplete main function below and the candidates for the blanks on the right, fill in each blank by writing down the letter(s) of all valid assignments from the right. A. “High ” + 5 let main = async () => { B. “10 - 1” C. abcd let s: string = ____________________; // (a) D. “1.1” E. 12 let n: number = ____________________; // (b) F. await promptString(“foo”) G. 11 * 10 / 5 s = ________________________________; // (c) H. await promptNumber(“bar”) } I. s + s 10. Below is an incomplete program. let main = async () => { let a: string = await promptString(___________________________); // (a) print(a + “!”); } main(); (a) Given the screenshot of running this program with a user input on the right (before the Ok button is pressed), complete the program by filling in the blank above. (b) What will be printed out after the user in (a) presses Ok?

4