midterm 1 review questionss3.amazonaws.com/110-2016-fall/review-slides/21... · announcements •...

21
Midterm 1 Review Questions

Upload: others

Post on 03-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Midterm1ReviewQuestions

Page 2: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Announcements

• ARSandExcusedAbsenceFormsonWebsite

• StudyGuidePosted

• GraderforReviewProblemSetwillgoLivebyTomorrowMorning• TryingforEC?Comingupwithyourownwaystotestrequirementsisgreat practice.

• ReviewSessionsSundayAfternoon/Evening– TBD

• Format– Gradescope workedsowelltheformatwillberoughlysimilarbutwewon’tuseScantrons.

Page 3: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

public interface Reducer {public double apply(double item, double memo);

}

public class Intrigue implements Reducer {public double apply(double item, double memo) {

return memo + item;}

}

public static void main(String[] args) {Reducer r = ____________;System.out.println(r.apply(2.0, 3.0));

}

Question0. FillintheBlank

Answer:newIntrigue();

Page 4: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

public interface Reducer {public double apply(double item, double memo);

}

public class Intrigue implements Reducer {public double apply(double item, double memo) {

return memo + item;}

}

public class Runner {public static void main(String[] args) {

Reducer r = ____________;System.out.println(r.apply(2.0, 3.0));

}}

Question1. Whatistheoutput?

Answer:5

Page 5: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

public interface Reducer {public double apply(double item, double memo);

}

public class Intrigue implements Reducer {public double apply(double item, double memo) {

return memo + item;}

}

public class Q2 {public static void main(String[] args) {

double[] a = new double[] { 2.0, 3.0, 5.0, 4.0 };Reducer r = new Intrigue();System.out.println(Q2.reduce(a, r, 0.0));

}

public static double reduce(double[] a, Reducer r, double memo) {double result = memo;for (double x : a) {result = r.apply(x, result);

}return result;

} }

Question2. Whatistheoutput?

Answer:14

Page 6: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

public class Q3 {public static void main(String[] args) {

double[] a = new double[] { 2.0, 3.0, 5.0, 4.0 };Reducer r = new Mystery();System.out.println(Q3.reduce(a, r, 0.0));

}

public static double reduce(double[] a, Reducer r, double memo) {double result = memo;for (double x : a) {result = r.apply(x, result);

}return result;

}}

Question3. Whatistheoutput?public class Mystery implements Reducer {

public double apply(double item, double memo) {if (item > memo) {return item;

} else {return memo;

}}

}

Answer:5

Page 7: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question4. FillintheBlank

for (int i = 3; i >= 0; ______) {System.out.println(i);

}

Answer: i-- (or anything that decremented i)

Page 8: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question5.Whatistheoutput?

for ( int i=0 ; i < 4 ; i=i*2 ) {System.out.println(i);

}

Answer:0, 0, 0, ... infinitely

Page 9: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question6. Rankorderthekindofloopyoushouldchooseifyouwantedtoprint eachelementina.

int[] a = new int[]{ 3, 2, 1, /* … and more … */ };

Canyouwriteit?

For-Each, For, While

Page 10: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question7. Rankorderthekindofloopyoushouldchooseifyouwantedtoprint thelastfiveelementsofa.

int[] a = new int[]{ 3, 2, 1, /* … and more … */ };

Canyouwriteit?

1. for, 2. while, 3. for-each (challenging and inefficient to do this with for-each)

Page 11: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question8. Rankorderthekindofloopyoushouldchooseifyouwantedtoprint thekeysofthefollowingMap?

Map<String,Integer> m = new HashMap<String,Integer>();m.put(“foo”, “bar”);m.put(“fiz”, “buz”);

Canyouwriteit?

1. for-each2. while (if you used iterator)3. for (if you used iterator)

Page 12: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question9. Selectthekindofloopyoushouldyouchooseifyou’recountinghowmanycallstotheMathclass’staticrandom methodbeforeitreturnsavaluegreaterthan0.90.

Canyouwriteit?

1. while

Page 13: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question10-12.Whatistheoutput?

public class Q {

public static void main(String[] args) { System.out.println(Q.fun(8)); // 10

System.out.println(Q.fun(9)); // 11 System.out.println(Q.fun(7)); // 12

}

public static int fun(int n) { for (int i = n / 2; i > 1; i--) { if (n % i == 0) { return i; } } return n; }

}

Page 14: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question10.Whatistheoutput?

public class Q10 {

public static void main(String[] args) { System.out.println(Q10.fun(8)); }

public static int fun(int n) { for (int i = n / 2; i > 1; i--) { if (n % i == 0) { return i; } } return n; }

}

Answer: 4

Page 15: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question11.Whatistheoutput?

public class Q11 {

public static void main(String[] args) { System.out.println(Q11.fun(9)); }

public static int fun(int n) { for (int i = n / 2; i > 1; i--) { if (n % i == 0) { return i; } } return n; }

}

Answer: 3

Page 16: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question12.Whatistheoutput?

public class Q12 {

public static void main(String[] args) { System.out.println(Q12.fun(7)); }

public static int fun(int n) { for (int i = n / 2; i > 1; i--) { if (n % i == 0) { return i; } } return n; }

}

Answer: 7

Page 17: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question13. Fillintheblank.

public ____ Map<String,String> mapFactory(String[] k,String[] v){

// Ignore the method body here...

}}

public class Q13 {

public static void main(String[] args) {

String[] keys = new String[] { "a", "b", "c" };

String[] values = new String[] { "apple", "banana", "cherry”

};

Map<String, String> m = Q13.mapFactory(keys, values);

}

Answer: static

Page 18: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question14. Implementthismethod.

static Map<String, String> mapFactory(String[] k, String[] v) {

}

Writeanimplementationof thefollowingmethod.Itshould returnaMapwherekey-valuepairsaremadebymatchingindicesofthekandvarray.

Forexample:Inputwherek={“a”,“b”,“c”}v={“apple”,“banana”,“cherry”}ReturnsaMaplike{a=apple,b=banana,c=cherry}

Map<String, String> m = new HashMap<String, String>();

for (int i = 0; i < k.length; i++) {m.put(k[i], v[i]);

}

return m;

Page 19: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question15. Giventhefollowingimplementation,whatargumentscouldyoucallmapFactory withtocauseanArrayIndexOutOfBoundsException error?

static Map<String,String> mapFactory(String[] k, String[] v) {Map<String, String> m = new HashMap<String, String>();

for (int i = 0; i < k.length; i++) { m.put(k[i], v[i]); } return m; }

Answer: mapFactory(new String[]{”A”}, new String[]{“”});

Page 20: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question16. FillintheblanksAandB.

public static __A___ mapLengths(String[] input) {

___A___ output = new int[____B___];

// We’ll implement this next in Q17.

return output;

}

Thefollowingmethodshouldreturnanarraywhereeachelement

containsthelengthofthecorrespondingelementintheinputarray.

WhatwouldyoureplaceblanksAandBwith?

A: int[] – b: input.length

Page 21: Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements • ARS and Excused Absence Forms on Website • Study Guide Posted • Grader for Review

Question17. Implementthismethod.

public static __A___ mapLengths(String[] input) {___A___ output = new int[____B___];

return output;}

Implementthefollowingmethod.

ItshouldreturnanarraywhereeachelementcontainsthelengthofthecorrespondingStringintheinputarray.

Reminder:Stringhasamethodnamedlength()thatwillreturnitslength.Arrayhasapropertynamed.lengththatwillreturnitslength.

for(int i = 0; i < input.length; i++) {output[i] = input[i].length();

}