Java Exam Preparation Practice Sheet Group – A (Classes, Objects, Methods & Fields) – Full Marks: 45 – Time: 1 Hour 20 Minutes 1. The following complete program prints four lines when executed. Show the four lines that are printed in the order in which they are printed. [E 6.1] 4 public class ArrayTest { public static void main(String[] args) { int[] test = new int[2]; test[0] = test[1] = 5; System.out.println(test[0] + "," + test[1]); fiddle(test, test[1]); System.out.println(test[0] + "," + test[1]); } static void fiddle(int[] test, int element) { test[0] = 10; test[1] = 11; element = 12; System.out.println(test[0] + "," + test[1] + "," + element); test = new int[2]; test[0] = 20; test[1] = 21; System.out.println(test[0] + "," + test[1]); } } 2. Identify errors in the following program and state the reasons: [E 6.3] 6 class QW1 { private int a; private int b; public QW1(int i, int j) {a = i; b = j;} public QW1(int i) {a = i; b = i;} public void show() { System.out.println(a); System.out.println(b); } public static final int int a[] = int b[] = void main() { ARRAY_SIZE; new int[ARRAY_SIZE]; {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for (int i = 0; x <= b.length; i++) { System.out.println(b[i]); } b += 10; show(); } } 3. Generate the output of the following program: 4 class One { int val; } public class Two { public static void main(String args[]) { int i1 = 5; int i2 = i1; i2 = 6; System.out.println(i1); System.out.println(i2); One ob1 = new One(); 1 ob1.val = 5; One ob2 = new One(); ob2 = ob1; ob2.val = 6; System.out.println(ob1.val); System.out.println(ob2.val); } } 4. Identify errors and state the reasons: [E 6.4] 3 class test { int x = 25; private int y = 45; class in { private int p = 90; void set_p() { p = 25; set_val(100, 200); } } private x = y = p = } void set_val(int a, int b) { a; b; 34; void temp() { in t = new in(); t.set_p(); t.p = 23; } } class test_in { public static void main(String args[]) { test ob = new test(); temp(); in ob1 = new in(); in.set_p(); } } 5. Design a class named Student that has two private data – student id and score. The class should contain a parameterized constructor to initialize its data members and one method to display the information. Now write a Java program that will use an array of Student objects to represent information about n numbers of students. Your program should take input from the keyboard and display the information of the students. [E 6.2 (similar)] 3+4=7 6. Why you should or should not use the finalize() method? [T 6.19 (Similar)] 3 7. Consider the following piece of code: Employee E1 = new Employee("Karim", 5001); Employee E2 = new Employee(E1); What are the values of the expressions E1.equals(E2) and E1 == E2? Why? [T 6.14] 3 8. What problem will arise in the following constructor? How can you solve it? [T 6.11] 3 class Student { int roll; int marks; Student(int roll, int marks) {.......} 2 } 9. Explain why you must be careful when passing objects to a method or returning objects from a method. [T 6.12] 3 10. Specify which overloaded method is called by f1(100)? Or is there any error in the following program fragment? State reason behind your answer. 2 class A { int f1(int a) {.......} double f1(int a) {.......} void meth() {System.out.println(f1(10));} } 11. Is it mandatory to use constructor in a class? State reason on behalf of your opinion. 2 12. Write a Java program having two classes named t1 and t2. Class t1 contains as integer number as public data and a constructor to initialize the data. Class t2 contains an array of objects of type t1 and a member method that will recursively print all elements of the array at even indices in reverse order. 5 Group – B (Inheritance, Abstract Classes and Interfaces) – Full Marks: 60 – Time: 1 Hour 45 Minutes 1. Generate the output of the following program: [E 8.1] 6 class Add { protected int i; Add(int a) {i = a;} protected void addIt(int amount) {i += amount;} protected int getIt() {return i;} } class DAdd extends Add { private int i; DAdd(int a, int b) { super(a); i = b; } protected void addIt(int amount) {i = i + super.i + amount;} protected int getIt() {return i;} protected void doubleIt(int amount) {addIt(2 * amount);} } public class TestAdder { public static void main(String args[]) { Add A = new Add(3); DAdd DA = new DAdd(1, 5); A.addIt(20); System.out.println(A.getIt()); A = DA; A.addIt(20); System.out.println(A.getIt()); DA.doubleIt(20); System.out.println(A.getIt()); } } 2. Identify errors in the following program and state the reasons. [E 8.2] import java.io.*; class A { int p = 120; public void print() { System.out.println("p:" + p); } } 3 7 abstract class B extends A { int d = 525; public void print() { System.out.println("d:" + d); } public void print(int k) { System.out.println("In B"); } abstract int add(); } class C extends B { int m = 424; public void print() { System.out.println("m:" + m); } } class test { public static A a = new B b = new C c = new void main(String[] args) { A(); B(); C(); c = a; c.print(); a = b; a.d = 230; a.print(); a.print(120); b = c; b.add(); } } 3. What would be the output of the following statements? [E 8.3] class A { A() { System.out.println("Inside } class B extends A { B() { System.out.println("Inside } class C extends A { C() { System.out.println("Inside } class Example { public static void main(String[] C obj = new C(); } } A"); } B"); } C"); } args) { 4. Consider the following code segment: interface Face1 { public void bar(); } class Class1 { private int size = 0; private String name; public Class1(String name) { this.name = name; } public String getName() { return (name); } public void foo() { System.out.println("Class1.foo()"); } } class Class2 extends Class1 implements Face1 { int size = 0; int x; 4 3 public Class2(String name) { super(name); } public void foo() { System.out.println("Class2.foo()"); } public void bar() { System.out.println("Class1.bar()"); } } What will be the output after the following code is executed? [E 8.4] 3 Face1 c = new Class2("ME"); c.bar(); 5. X is a subclass of Y. Does the last two assignments below produce a compile-time error? [T 8.14] X Y y x x y = = 2 = new X(); = new Y(); x; y; 6. Find errors in the following program and state the reasons. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 class A { int a; A(int x) { a = x; } void A() { return a; } } class B extends A { int a; int b; B(int x) { b = x; } } interface C extends A { int c; C(int x) { c = x; } abstract void mysteryMethod1(); final int mysteryMethod2(); boolean mysteryMethod3(boolean val); } abstract class D extends B implements C { int a; int b; int d; abstract D() { super.a = 1; super.super.a = 2; super(3); } abstract void show() { System.out.println(super.A()); } abstract final void mysteryMethod4(); 5 15 50 } 51 52 public class Test1 extends D { 53 public static void main(String[] args) { 54 System.out.println("What an erroneous program this is!"); 55 } 56 57 void mysteryMethod1() { 58 System.out.println("Mysterious method, indeed!"); 59 } 60 61 void test() { 62 D objD; 63 C objC; 64 objD = new D(); 65 objC = new C(10); 66 } 67 } 7. What is the difference between method overriding and method overloading? Explain with example. [T 8.3] 4 8. Why should you use the keyword super in your Java program? Explain with example. [T 8.2] 3 9. How can an interface be partially implemented? [T 8.12] 3 10. You have to design a class hierarchy as shown below: Account Savings Account Current Account The parent class contains the general information about an account and an abstract method to calculate the yearly interest. For savings account, the interest rate is 10% and for current account the interest rate is 6%. All the data members of the Account class are initialized through a parameterized constructor. Your program should be able to deposit and withdraw money from a saving account. Perform the same operation on a current account. [E 8.5] 4+3=7 11. Write a Java program to implement multiple inheritance. [E 8.13] 4 12. State the advantages of using interfaces. [T 8.9] 3 Group – C (Packages) – Full Marks: 10 – Time: 20 Minutes 1. Consider the following Java program fragments and answer the questions below: [E 9.1] i. Which instance variables are accessed using the object b inside main() method? ii. Which instance variables are accessed using the object c inside main() method? iii. Which instance variables are accessed using the object k inside mCOne() method? 6 4 A.java B.java C.java package pA; package pA; package pA; public class A { private int fAOne; protected int fATwo; int fAThree; public void mAOne(){} } public class B extends A{ public int fBOne; public void mBOne(){} } public class C { public int fCOne; public void mCOne(){ B k = new B(); } } D.java import pA.*; class D { public static void main(String[] args) { B b = new B(); C c = new C(); } } 2. Write the line numbers of the following programs which will generate compile errors: P1.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 P2.java package p1; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class P1 { public int p1_pub; private int p1_pri; protected int p1_pro; int p1_def; } class P1_Sub extends P1_Sub() { super.p1_pub super.p1_pro super.p1_def super.p1_pri } } class P1_Test { P1_Test() { P1 p1 = new p1.p1_pub = p1.p1_pro = p1.p1_def = p1.p1_pri = } } 6 P1 { = = = = 5; 5; 5; 5; P1(); 5; 5; 5; 5; 7 package p2; import p1.*; public class P2 { P2() { P1 p1 = new p1.p1_pub = p1.p1_pro = p1.p1_def = p1.p1_pri = } } P1(); 5; 5; 5; 5; class P2_SubP1 extends P2_SubP1() { super.p1_pub = super.p1_pro = super.p1_def = super.p1_pri = } } class P2_SubP2 P2_SubP2() p1_pub p1_pro p1_def p1_pri } } P1 { 5; 5; 5; 5; extends P2_SubP1 { { = 5; = 5; = 5; = 5; Group – D (Exception Handling) – Full Marks: 15 – Time: 45 Minutes 1. Generate the output of the following program: 5 import java.io.*; class TestException { public static void main(String[] args) throws IllegalAccessException { TestException ob = new TestException(); try { System.out.println("return value: " + ob.m()); } catch (IllegalAccessException e) { System.out.println("Exception caught in main"); } finally { System.out.println("finally--main"); } System.out.println("End of main"); } int m() throws IllegalAccessException { try { return method(); } catch (ArithmeticException e) { return 2; } finally { System.out.println("finally--m."); } } int method() throws IllegalAccessException { try { int x = 5; if (x == 5) { throw new IllegalAccessException("test"); } return x; } catch (IllegalAccessException e) { try { throw new IllegalAccessException("test"); } catch (ArithmeticException e1) { return 2; } finally { System.out.println("nested finally: " + e); } }catch (SecurityException e) { return 9; } finally { System.out.println("finally---method"); } } } 2. Determine errors in the following program. Correct them and generate the output. [E 10.3] class TestException { public static void main(String args[]) { try { method(); System.out.println("After method()"); } catch(RuntimeException ex) { System.out.println("Exception in main"); } System.out.println("End of main"); } 8 3 static void method() throws Exception { try { final int zero=0; int y=2/zero; System.out.println("Recovered from error");} catch(RuntimeException ex) { System.out.println("Runtime Exception in method"); throw ex; } finally { System.out.println("Finally in method"); } System.out.println("End of method"); } } 3. Write a program that will take some positive integers from the keyboard and find their summation. If any negative number is input, then your program should handle it with a user-defined exception. [Complete Concepts Program – How to Create a User-Defined Exception] 7 Group – E (File I/O) – Full Marks: 25 – Time: 45 Minutes 1. Write a Java program that will write a list of float numbers into a file. Your program will then read the content of the file and find the multiplication of the numbers. [E 19.1 (Similar)] 7 2. Write a Java program to copy a binary file and a text file. [Complete Concepts Program] 4 + 4 =8 3. Write a Java program to append some lines of text (which will be input from keyboard) into a text file. [Complete Concepts Program] 5 4. Write a Java program to display the contents of a directory. Your program should display the following properties of each content: [Complete Concepts Program] 5 i. ii. iii. iv. Name of the file or folder Path of it Whether it is a file or folder Size of the file or folder 9 Solutions Group A – 3: 5 6 6 6 Group B – 6: 1. Line 8: Method declaration should be: int A() {...}. 2. Class A’s constructor must be called in Class B. See point 8.3 3. Line 22: Interfaces can extend only interfaces. See point 8.16 4. Line 23: Variable c must be initialized. See point 8.14 5. Line 25: Method C cannot have a body. 6. Line 30: Method mysteryMethod2()cannot be final. 7. Line 39: Constructor D() cannot be abstract. See point 8.11 8. Line 41: Variable a in class A cannot be accessed in this way. 9. Line 42: Call to super() must be the first statement inside a constructor. See point 8.4 10. Line 45: Abstract methods cannot have body. 11. Line 49: Abstract method mysteryMethod4() cannot be final. 12. Line 57: mysteryMethod1() must be implemented as public. See point 8.14 13. Line 64: class D is abstract, cannot be instantiated. See point 8.10 14. Line 65: interface C is abstract, cannot be instantiated. See point 8.17 15. Class Test1 is not abstract and does not override abstract method mysteryMethod4() in D or implement mysteryMethod2() and mysteryMethod3() in C. See point 8.12 & 8.18 Group C – 2: package p1: 15, 25. package p2: 8, 9, 10, 18, 19, 27, 28. Group D – 1: nested finally: java.lang.IllegalAccessException: test finally---method finally--m. Exception caught in main finally—main End of main 10