Review PDF

Title Review
Author Marc Somez
Course Intermediate Programming
Institution Florida International University
Pages 6
File Size 152.9 KB
File Type PDF
Total Downloads 100
Total Views 132

Summary

Mid term review...


Description

Review 1 Q1. Given the following piece of code: public class A { public A(){} } public class B extends A { public B(){} } public class driver { public static void main(String[] arg) { A a = new B(); B b = new A(); } } What will happen when you try to compile it? Explain. The code will not compile. The line B b = new A(); Causes syntax error. The reference variable of the subclass cannot be used to create object of the superclass.

Q2. Given the following piece of code: abstract class A { A(){ } abstract int method1(); abstract void method2(); } abstract class B extends A { super(); int method2() { return 0; } } What will happen when you try to compile it? Explain. The code will not compile. The usage of super as a call to a constructor of a superclass, must be called from within a constructor of a subclass; and must be the first executable statement in the constructor that is doing the calling.

1

Q3. Indicate true or false for the following statements. (i) A final class can have instances.(True) (ii) A final class can be extended. (False) (iii) A final class can be implemented. (False) (iv) An abstract class can be extended (True) (v) An abstract class cannot be inherited.(False) (vi) A concrete class does not have to define all of the abstract methods that it inherits from an abstract class. (False)

Q4. What output is generated from the following program? class A { void print() { System.out.println("a is not an instance of B"); } } class B extends A { void print() { System.out.println("b is an instance of A"); } } class Tester { public static void main(String[] arg) { A a = new A(); B b = new B(); if (!(a instanceof B)) a.print(); if (b instanceof A) b.print(); } } Output in order is not an instance of B b is an instance of A

Q5. Which of the following is a correct interface? (i) interface A (ii) interface A { { void print() { } print(); } } (iii) interface A { abstract void print(){ } }

(iv) interface A { void print(); }

Answer (iv)

2

Q6. The following program has errors. Find the errors and re-write the program. abstract class A { abstract void print(); abstract int query(); } class B extends A ----→ → [Correction here: abstract class B extends A or define the query method] { void print() { System.out.println("Inheritance and abstract class"); } } class C extends A -→[Correction here: same issue as class B] { int query() { return 10; } } class D extends A { void print() { } int query() { return 20; } abstract void display(); }



Class A has no error. Class B has error. It does not define the abstract method, query, that it inherits. The correction is either to define the method, OR, the class be declared an abstract class.



Class C has similar problem. It does not define the abstract method print.



Class D defines both method, but it includes an abstract method, display, which makes the class an abstract class. By not declaring it abstract class cause syntax error.

One possibility fix abstract class A { abstract void print(); abstract int query(); }

3

class B extends A { void print() { System.out.println("Inheritance and abstract class"); } int query(){ return 2; } } class C extends A { int query() { return 10; } void print() { System.out.println("Inheritance and abstract class"); } } class D extends A { void print() { } int query() { return 20; } void display(){}; }

Q7. Consider the following Java code. abstract class A { A() { } } class B extends A { B() { } } Assume that there is a test class; select those statements that will compile. (a) B b = new B();

4

(b) A a = new B(); (c) B b = new A(); (d) A a = new A(); (e) B b = new (B);

Q8. Exceptions identify errors that arise in your program. Name the two categories of exceptions that are derived directly from Throwable and differentiate between them. The two categories of exceptions that are derived directly from the class Throwable are Error and Exception. The class Error represents catastrophic events that cannot be caught and be corrected. The category Exception represents errors that can be caught be caught and dealt with in such a way that the program does not terminate abnormally.

Q9 Write down the output as it would appear on the screen. class Exceptions { public static void main(String[] args) int x[] = {2, 4, 6}; method(0, x); method(x[2], x); method(2, x); }

{

Programming II The first value is 2 The second value is 0 This line will be printed Yes its true Programming II ArrayIndexOutOfBoundsException caught Yes its true

Programming II static void method(int i, int arr[]) The first value is 6 ArithemticException caught { Yes its true String str = "Yes its true"; try { System.out.println("Programming II"); System.out.println("This first value is " + arr[i]); System.out.println("This second value is " + arr[i]/(arr[i] - 6)); System.out.println("This line will be printed"); } catch(ArithmeticException e) { System.out.println("ArithemticException caught"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException caught"); } finally { System.out.println(str); } }

5

} [Q10 nor Q11 will be on the midterm] Q10 Given the following incomplete class definition, write appropriate Java code that will use the string str to create a file object. [1 mark] class MyFile { File f; MyFile(String str) { f = new File(str); } }

Q11 Given the following method declaration, write appropriate code that will retrieve the number of bytes in the file. int getBytes(File f) { return (int) f.length(); }

6...


Similar Free PDFs