CSE114FinalPracticeSpring2024Solutions

.pdf

School

Stony Brook University *

*We aren’t endorsed by this school

Course

114

Subject

Computer Science

Date

May 7, 2024

Type

pdf

Pages

14

Uploaded by SargentMongoose3260 on coursehero.com

1 CSE 114 Final Exam Spring 2024: Practice Solutions Question Value Score 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 10 22 10 23 10 24 10 Total 100
2 1. (2 points) Java class inheritance supports multiple inheritance. TRUE FALSE 2. (2 points) ArrayList provides the constant time access and update like an array data structure TRUE FALSE 3. (2 points) Constructors of the parent class are inherited in the child class TRUE FALSE 4. (2 points) An abstract class can have a non-abstract method TRUE FALSE 5. (2 points) An abstract class can be used as a reference type in polymorphism. TRUE FALSE 6. ArithmeticException is an example of a checked exception. TRUE FALSE 7. (2 points) IOException is an example of a checked exception. TRUE FALSE 8. (2 points) An interface can be used as a reference type in polymorphism. TRUE FALSE 9. (2 points) When a class overrides a method, what object reference is used to call the method inherited from the super class? inherited super class methodName 10. (2 points) An interface can have a non-abstract method. TRUE FALSE
3 11. (4 points) Explain why the following program displays [1, 3] rather than [2, 3] import java.util.ArrayList; public class Problem { public static void main(String[] args ) { ArrayList<Integer> list = new ArrayList<>(); list .add(1); list .add(2); list .add(3); list .remove(1); System. out .println( list ); } } How do you remove integer 3 from the list, when the list has elements 1, 2 and 3 in this order (without using remove with index)? Solution: because list.remove(1) deletes the second element of the list which is 2. We can remove integer 3 from the list by using the statement list.remove(new Integer(3)); 12. (4 points) The for loop in the following program is converted into the while loop below. What is wrong? Correct it. public class Test { public static void main(String[] args ) { int sum = 0; for ( int i = 0; i < 4; i ++) { if ( i % 3 == 0) continue ; sum += i ; } System. out .println( sum ); } } public class Test { public static void main(String[] args ) { int sum = 0, i = 0; while ( i < 4) { if ( i % 3 == 0) continue ; sum += i ; i ++; }
4 System. out .println( sum ); } Solution: infinite loop. To fix the problem increment i inside the if- condition. public class Test { public static void main(String[] args ) { int sum = 0, i = 0; while ( i < 4) { if ( i % 3 == 0) { i ++; continue ; } sum += i ; i ++; } System. out .println( sum ); } } 13. (4 points) What is the difference between the method overloading and method overriding in Java? Solution: Method overloading : It happens when two methods in the class have same name but different signature. Method overriding : It happens in inheritance when the non-static method in the child class has the same method signature as the corresponding non-static method in the parent class. Here the non-static method in the child class is said to override the corresponding method in the parent class. 14. (4 points) You coded the following in the Test.java class in order to output the smallest element in the array a: public class Test { public static void main(String[] args ) { int [][] a = {{9, 8, 7, 6},{10, 20, 30, 40}}; int min = a [0][0]; for ( int i = 1; i < a . length ; i ++) {
5 for ( int j = 0; j < a [ i ]. length ; j ++) { if ( a [ i ][ j ] < min ) min = a [ i ][ j ]; } } System. out .println( "The minimum is:-->" + min ); } } The program compiles properly, but when you run, you get the following output: The minimum is 9. You expect the value of min to be 6. Explain what the problem is and how to fix it. Solution: The outer for loop should start from i = 0; currently we are not considering the first row except the first element which is 9. 15. Complete the following program. public class Quiz { public static void main(String[] args ) { B b = new B(); b .m1(); } } interface A{ public void m1(); } // complete the following code, where B implements interface A class B …………………………………………………… { } Solution: public class Quiz { public static void main(String[] args) { B b = new B(); b.m1(); } } interface A{ public void m1(); }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help