Final Exam Practice Questions
Final Exam Practice Questions
1) Exception: Trace the program and fill out the table. Show which one produces the Stack Trace.
/*Exception*/
class TestException {
public static void m1() {/*throw new ArithmeticException();/* Code
is not given...*/}
public static void m2() {/*throw new ArithmeticException();/* Code
is not given...*/}
public static void m3() {/*throw new IllegalArgumentException();/*
Code is not given...*/}
public static void main(String[] args) {
System.out.println("A, ");
m1();
try {
System.out.println("B, ");
m2();
System.out.println("C, ");
m3();
System.out.println("D, ");
}
catch (ArithmeticException e) {
System.out.println("E, ");
return;
}
catch (Exception e) {
System.out.println("F, ");
}
finally {
System.out.println("H, ");
}
System.out.println("G. ");
}
}
description output
Call to m1 and m2 complete normally and, if called,
m3 completes normally
Call to m1 is complete, and m2 throws an
ArithmeticException and, if called, m3 complete
normally
Call to m1 and m2 complete normally and, if called,
m3 throws IllegalArgumentException
Call to m1 and m2 complete normally and, if called,
m3 throws an ArithmeticException
Call to m1 throws an ArithmeticException and, if
called, others complete normally
2) Hierarchy/ Inheritance: Trace the code and print the correct message when the greet method is
called.
public class M_Animal {
bob.greet(rex);
//2)What will print
sara.greet(bob);
//3)What will print
jane.greet(sara);
//4)What will print
sara.greet(jane);
//5)What will print
}
}
//c) Sort the students list based on name and for same names
sort based on age
4) Stream
List <Integer> numbers = Arrays.asList(1,2,3,4,5,6);
/* find the double of the first even number that is>3*/
List <Integer> numbers2 =
Arrays.asList(1,2,3,5,4,6,7,8,9,10);
int res=0;
for (int e:numbers2) {
if (e>3 && e%2==0){
res = e*2;
break;
}
}
);