0% found this document useful (0 votes)
6 views

Final Exam Practice Questions

The document outlines tasks related to exception handling, inheritance, lambda expressions, and streams in Java. It includes code snippets for tracing exceptions, greeting methods in an inheritance hierarchy, filtering and sorting student data using predicates, and converting traditional loops to stream operations. Each section requires the reader to analyze code behavior and implement specific functionalities.

Uploaded by

Papa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Final Exam Practice Questions

The document outlines tasks related to exception handling, inheritance, lambda expressions, and streams in Java. It includes code snippets for tracing exceptions, greeting methods in an inheritance hierarchy, filtering and sorting student data using predicates, and converting traditional loops to stream operations. Each section requires the reader to analyze code behavior and implement specific functionalities.

Uploaded by

Papa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSC 213- Final Handout

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 {

public void greet (M_Animal a){


System.out.println("Sniff");
}
}
class Person extends M_Animal {
public void greet (M_Animal a){
System.out.println("Grrf");
}
public void greet (Person a){
System.out.println("hi");
}
}
class Professor extends Person {
public void greet (Professor a){
System.out.println("Good Day");
}
public void greet (Person a){
System.out.println("hello");
}
public void greet (Student a){
System.out.println("How can I help you?");
}
}
class Student extends Person {
public void greet (Professor a){
System.out.println("What's the answer?");
}
public void greet (Person a){
System.out.println("hey");
}
public void greet (Student a){
System.out.println("yo");
}
}
class Driver {
public static void main(String[] args) {
M_Animal rex = new M_Animal();
M_Animal bob = new Professor();
Professor sara = new Professor();
Person jane = new Student();
rex.greet(bob);
//1)What will print

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

}
}

3) Lambda Expression/ Sort


For List of students use predicate to filter students with gpa>3.
ArrayList<Student> filtered;
//a) use Predicate and lambda expression to filter students
with gpa>3.0
Predicate< Student> pred =

System.out.println("Print the Filtered Students:");

//b) use Predicate and lambda expression to filter based on age


Predicate< Student> pred2 =

System.out.println("Print the Filter Students with age>22");

//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;
}
}

//Convert the code above to use the stream


System.out.println("Stream: "+
numbers.stream()

);

You might also like