MT1 Review - Amazon S3s3.amazonaws.com/.../slides/22-MT1-Review-Key.pdfMT1 Review Lecture 22 Go...
Transcript of MT1 Review - Amazon S3s3.amazonaws.com/.../slides/22-MT1-Review-Key.pdfMT1 Review Lecture 22 Go...
MT1 ReviewLecture 22
Go ahead and get out pencil/paper and have PollEv open!
Review Sessions
• Regular:• Small Group Walk-in Th/Fr from 2-6pm in FB008
• In-class:• Today: Question-based Review
• Midterm Review:• Monday from 5pm – 7pm in Hamilton 100
• First 60 minutes lecture, Q&A after
1. Given the following function:
function foo(n: number): boolean {return n % 3 === 0;
}
And array…
let a: number[] = [1, 2, 3, 4];
What are the return types of the following?
a.filter(foo)a.map(foo)
Done? What are the values returned?
1. 2.
1. Given the following function:
function foo(n: number): boolean {return n % 3 === 0;
}
And array…
let a: number[] = [1, 2, 3, 4];
What are the return types of the following?
a.filter(foo)a.map(foo)
Done? What are the values returned?
1. 2.
map: boolean[], filter: number[]
map: [false, false, true, false], filter: [ 3 ]
2. Given the following description of array's concat method, and the Reducer<number, number[]> function baz…
function baz(memo: number[], item: number): number[] {let start: number[] = [item];return start.concat(memo);
}
a.concat(b) will return a new, concatenated array starting with a's elements and ending with b's elements.
What are result's elements after the following code runs:
let input: number[] = [1, 2, 3];let result: number[] = input.reduce(baz, []);
2. Given the following description of array's concat method, and the Reducer<number, number[]> function baz…
function baz(memo: number[], item: number): number[] {let start: number[] = [item];return start.concat(memo);
}
a.concat(b) will return a new, concatenated array starting with a's elements and ending with b's elements.
What are result's elements after the following code runs:
let input: number[] = [1, 2, 3];let result: number[] = input.reduce(baz, []);
[3, 2, 1]
3. What is printed when the following statement runs?function zazzle(a: number[], test: Predicate<number>): boolean {
for (let i: number = 0; i < a.length; i++) {if (test(a[i])) {
return true;}
}return false;
}
function isOdd(n: number): boolean {return n % 2 === 1;
}
print(zazzle([2, 3, 4], isOdd));
Done? Try to think of what array you could call zazzle with to cause it to return something else.
3. What is printed when the following statement runs?function zazzle(a: number[], test: Predicate<number>): boolean {
for (let i: number = 0; i < a.length; i++) {if (test(a[i])) {
return true;}
}return false;
}
function isOdd(n: number): boolean {return n % 2 === 1;
}
print(zazzle([2, 3, 4], isOdd));
Done? Try to think of what array you could call zazzle with to cause it to return something else.
true
4. What is printed when the main function below runs?
class Dog {name: string;
constructor(name: string) {print("A");this.name = name;
}
translate(word: string): string {print("B");if (word !== "hello") {
return "C";} else {
return "D";}
}
speak(word: string): string {print("E");return this.translate(word);
}}
function main(): void {print("F");let krackle: Dog;krackle = new Dog("Krackle");
let hello: string;hello = krackle.speak("hello");
print(krackle.speak("world"));print(hello);
}
4. What is printed when the main function below runs?
class Dog {name: string;
constructor(name: string) {print("A");this.name = name;
}
translate(word: string): string {print("B");if (word !== "hello") {
return "C";} else {
return "D";}
}
speak(word: string): string {print("E");return this.translate(word);
}}
function main(): void {print("F");let krackle: Dog;krackle = new Dog("Krackle");
let hello: string;hello = krackle.speak("hello");
print(krackle.speak("world"));print(hello);
}
F, A, E, B, E, B, C, D
5. Diagram the TrainCarobjects in memory using the convention to the right:class TrainCar {
car: number;next: TrainCar | null;
constructor(car: number, next?: TrainCar) {this.car = car;if (next === undefined) {
this.next = null;} else {
this.next = next;}
}}
car# car#
variable variable
null
let kaboose: TrainCar = new TrainCar(110);let sleeper: TrainCar = new TrainCar(32, kaboose);let coach: TrainCar = new TrainCar(45, sleeper);let motor: TrainCar = new TrainCar(24, sleeper);
5. Diagram the TrainCarobjects in memory using the convention to the right:
class TrainCar {car: number;next: TrainCar | null;
constructor(car: number, next?: TrainCar) {this.car = car;if (next === undefined) {
this.next = null;} else {
this.next = next;}
}} let kaboose: TrainCar = new TrainCar(110);
let sleeper: TrainCar = new TrainCar(32, kaboose);let coach: TrainCar = new TrainCar(45, sleeper);let motor: TrainCar = new TrainCar(24, sleeper);
110
kaboose
null
32
sleeper
45
coach
24
motor
6. Diagram the TrainCarobjects in memory using the convention to the right: car# car#
variable variable
null
let a: TrainCar = new TrainCar(110);let b: TrainCar = new TrainCar(32, a);let c: TrainCar = new TrainCar(45, b);let d: TrainCar = new TrainCar(24, c);if (c.next !== null) {
c.next.next = d;}
6. Diagram the TrainCarobjects in memory using the convention to the right:
let a: TrainCar = new TrainCar(110);let b: TrainCar = new TrainCar(32, a);let c: TrainCar = new TrainCar(45, b);let d: TrainCar = new TrainCar(24, c);if (c.next !== null) {
c.next.next = d;}
110
a
null
32
b
45
c
24
d
7. What is printed when the fun function below is called with an argument of 4?
function fun(n: number): number {if (n <= 1) {
return 1;} else {
return fun(n - 1) + fun(n - 1);}
}
print(fun(4));
7. What is printed when the fun function below is called with an argument of 4?
function fun(n: number): number {if (n <= 1) {
return 1;} else {
return fun(n - 1) + fun(n - 1);}
}
print(fun(4)); 8
8. What is the output?
let count: number = 0;
for (let i: number = 0; i < 6; i = i + 2) {if (count === 0) {
print(i);}count++;
}
print(count);
8. What is the output?
let count: number = 0;
for (let i: number = 0; i < 6; i = i + 2) {if (count === 0) {
print(i);}count++;
}
print(count);03
9. Given the Iterator class, what is printed when the program runs?
class Iterator {a: number[];i: number;
constructor(a: number[]) {this.a = a;this.i = a.length;
}
hasNext(): boolean {return this.i > 0;
}
next(): number {this.i--;return this.a[this.i];
}}
let numbers: number[] = [1, 2, 3];
let itr: Iterator = new Iterator(numbers);numbers[numbers.length] = 4;numbers[1] = 5;
while (itr.hasNext()) {print(itr.next());
}
9. Given the Iterator class, what is printed when the program runs?
class Iterator {a: number[];i: number;
constructor(a: number[]) {this.a = a;this.i = a.length;
}
hasNext(): boolean {return this.i > 0;
}
next(): number {this.i--;return this.a[this.i];
}}
let numbers: number[] = [1, 2, 3];
let itr: Iterator = new Iterator(numbers);numbers[numbers.length] = 4;numbers[1] = 5;
while (itr.hasNext()) {print(itr.next());
}
3, 5, 1