Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements •...
Transcript of Midterm 1 Review Questionss3.amazonaws.com/110-2016-fall/Review-Slides/21... · Announcements •...
Midterm1ReviewQuestions
Announcements
• ARSandExcusedAbsenceFormsonWebsite
• StudyGuidePosted
• GraderforReviewProblemSetwillgoLivebyTomorrowMorning• TryingforEC?Comingupwithyourownwaystotestrequirementsisgreat practice.
• ReviewSessionsSundayAfternoon/Evening– TBD
• Format– Gradescope workedsowelltheformatwillberoughlysimilarbutwewon’tuseScantrons.
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();
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
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
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
Question4. FillintheBlank
for (int i = 3; i >= 0; ______) {System.out.println(i);
}
Answer: i-- (or anything that decremented i)
Question5.Whatistheoutput?
for ( int i=0 ; i < 4 ; i=i*2 ) {System.out.println(i);
}
Answer:0, 0, 0, ... infinitely
Question6. Rankorderthekindofloopyoushouldchooseifyouwantedtoprint eachelementina.
int[] a = new int[]{ 3, 2, 1, /* … and more … */ };
Canyouwriteit?
For-Each, For, While
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)
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)
Question9. Selectthekindofloopyoushouldyouchooseifyou’recountinghowmanycallstotheMathclass’staticrandom methodbeforeitreturnsavaluegreaterthan0.90.
Canyouwriteit?
1. while
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; }
}
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
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
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
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
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;
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[]{“”});
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
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();
}