DECEMBER 2006

 

Code: DC-12                                                                                     Subject: OOPS THRU JAVA

Time: 3 Hours                                                                                                     Max. Marks: 100

NOTE: There are 9 Questions in all.

·      Question 1 is compulsory and carries 20 marks.

·      Out of the remaining EIGHT Questions answer any FIVE Questions. Each question carries 16 marks.

·      Any required data not explicitly given, may be suitably assumed and stated.                        

Q.1            State which of the statement is true and which are false.                                  (2x10)

 

a.       The expression (a < b || x = = y) is true if both a < b and x = = y are true.    

 

b.      If present, then package must be the first non-comment statement in a file.

                                                                      

c.       It is legal to access any instance variable inside a static method.

                                                                                   

d.      Subclass and super class cannot have methods with same signatures.

        

e.       A String object cannot be modified after it is created.

                            

f.        All the methods in an abstract class must be defined as abstract.

       

g.       There is no need to explicitly destroy the objects created using new.

 

             h.  A final class cannot be extended.

 

             i.   The Reader class is at the top of the byte stream hierarchy.                                   

 

j.        Java always provides a default constructor to a class.

 

 

Answer any FIVE Questions out of EIGHT Questions.

Each question carries 16 marks.

  Q.2     a.   Write a program to define a class of employees. Whenever a new employee is created, program should display number of already existing employees.  (6)

 

             b.   What is the output of the following code segments?                                           (2x5)

               i.    public static void main(String args[]) {

                           int i = 0, k = 0;

                           while (i < 15) {

                                    if ( i % 2 == 1)             { i++; continue; }

                                    if ( i % 2 == 1) break;

                                    k++; i++;

                           }

                           System.out.println(" k = " + k);

                     }

               ii.   class test{

                           public static void main(String args[]) {

                                 int   x = 10, y = 20;

                                 int   p = dummy(x, y);

                                 int   q = dummy(p, dummy(x, 2));

                                 System.out.println("p = " + p + " and q = " + q);

                           }

 

                           static int dummy(int a, int b) {

                                 return (a * b);

                           }

                     }

               iii.  public static void main(String args[]) {

                           int   x = 5, y = 50, n = 0;

                           while ( x <= y) {

                                 x = y /x;

                                 n++;

                           }

                           System.out.println(" n = " + n);

                     }

               iv.  public static void main(String args[]) {

                           for (int j = 1; j < 3; j++)

                                 for (int k = 1; k < 3; k++)

                                       if (j = = k)

                                             System.out.print("1  ");

                                       else

                                             System.out.print("0  ");

                     }

v.      public static void main(String args[]) {

int no_of_apples = 4, no_of_oranges = 5;

System.out.println("A total of " + no_of_apples + no_of_oranges +

      " fruits are available.");

             }

 

  Q.3     a.   Can a final class have abstract method? Justify your answer.                             (3)

                                                                             

             b.   Of the various available access specifiers, which are legal for a class and which are illegal? How do legal access specifiers change the visibility of the class?                                                               (5)

 

             c.   Write a program to compute ab using recursion. Assume both a and b are positive integers.             (8)

 

   Q.4    a.   Briefly explain the significance of CLASSPATH.                                                (3)

 

             b.   What is a stream in Java? Describe the two types of streams available in Java.                    (5)

       

             c.   Write a Java program to count the number of characters in a file and then display the line “There are x characters in the file”, where x is the total number of characters in the file.                           (8)       

                  

  Q.5           Create a class time. Also create its two-sub classes hr12 and hr24 to represent two formats for time of the day. Define the methods in hr12 and hr24 to:                                                          

 

               (i)   initialize time when the object is created.

               (ii)  add x hours to the time stored in the object, where x is passed as argument to the method.

               (iii) display time in appropriate format.

                  

               Define appropriate methods in class time to show dynamic method dispatch.         (16)

 

  Q.6     a.    What are nested classes? How do they differ from the sub classes?                     (6)

 

             b.   How does Java handle multiple catch clauses with a try block? Is it possible to write multiple catch statements in any order? Justify.           (6)

 

             c.   How do static methods differ from final methods?                                             (4)                      

 

  Q.7     a.   In a class hierarchy, in what order are the constructors for the classes that make up the hierarchy called? How can a specific parameterized constructor of the super class be called by the sub class?            (4)

            

             b.   Differentiate between the following

                   (i)  java applets and java applications.

                   (ii) multiple and multilevel inheritance.   (3x2=6)

 

c.       Identify and correct the errors in the following code segments.                                

                   (i)     Byte a = 2; Byte b = 2 * a;

                   (ii)    final int a;                                                                                               

                   (iii)   for (int k = 0, n = 0; k < 10; k++)

                            if (k % 2 = = 0)

                                 n++;

                                                                           System.out.println(" No. of evens between 0 and 9 is " + n);                                                                 (2x3=6)

 

  Q.8     a.   Write a program to read in n elements (n is passed through the command line arguments) into an array. Find the position and value of the second largest number in the array.                                         (10)

 

             b. Which feature of exception handling mechanism makes it possible to execute some code prior to exit from the try/catch block? Briefly explain that feature.                                                                      (6)

 

  Q.9           Write short notes on the following:

                   (i) Interfaces                                                                                                   

                   (ii) Garbage Collection

                (iii) Advantages of Bytecode

                   (iv) Constructors                                                                                 (4*4 = 16)