midterm by keralaguest

VIEWS: 17 PAGES: 14

									                                                                                     Page 1 of 14



Q.1. For multiple-choice questions, encircle your choice(s) e.g. A                  (28 points)

i) Which of the following declaration(s) is/are legal?

A. friendly String s;
B. public final static int w( );
C. abstract double d;
D. abstract final double hyperbolicCosine( ) ;



ii) Consider the following line of code:
       int x[] = new int[25];
After execution, which statement or statements are true?

A. x[24] is 0.
B. x[24] is undefined.
C. x[25] is 0.
D. x[0] is null.
E. x.length is 25.


iii) How can you force garbage collection of an object? (Choose one or more.)

A. Call System.gc( ).
B. Call System.gc( ), passing in a reference to the object to be garbage-collected.
C. Call Runtime.gc( ).
D. Set all references to the object to null
E. None of the above.


iv) What is the range of values that can be assigned to a variable of type short?

A. It depends on the underlying hardware.
B. 0 through 216 – 1
C. 0 through 232 – 1
D. -215 through 215 – 1
E. -216 through 216 – 1
                                                                                   Page 2 of 14



v) If all three top-level elements occur in a source file, they must appear in which order?

A.      Imports, package declaration, classes.
B.     Classes, imports, package declarations.
C.     Package declaration must come first; order for imports and class definitions
       is not significant.
D.     Package declaration, imports, classes.
E.     Imports must come first; order for package declaration and class definitions
       is not significant.



vi) What is the output of the following program?

class Integer
{
      public static void main(String args[])
      {
            int i;
            System.out.println(i);
      }
}

A.     A value of 0 will be printed out.
B.     Nothing will be printed out.
C.     A compile time error.
D.     None of the above.



vii) The next two parts refer to the following code:

1. package abcde;
2.
3. public class Bird{
4.     protected static int referenceCount = 0;
5.     public Bird(){referenceCount++;}
6.     protected void fly (){/*flap wings etc.*/}
7.     static int getRefCount(){return referenceCount;}
8.     }




a) Which statement(s) is/are true about class Bird above and class Parrot below?
                                                                                   Page 3 of 14



1. class Parrot extends abcde.Bird{
2.     public void fly(){/*parrot flight code */}
3.     public int getRefCount(){return referenceCount;}
4. }

A. There is a compilation error in Parrot.java at line 2, because method fly( ) is protected
   in the superclass and classes Bird and Parrot are in the same package.
B. There is a compilation error in Parrot.java at line 2, because method fly( ) is protected
    in the superclass and public in the subclass and methods may not be overridden to be
    more public.
C. There is a compilation error in Parrot.java at line 3, because method getRefCount( ) is
    static in the superclass and static methods may not be overridden to be non-static.
D. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method fly()
   is ever called on an instance of class Parrot.
E. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method
    getRefCount( ) is ever called on an instance of class Parrot.

b) Which statement(s) is/are true about class Bird above and class Nightingale below?
1. package singers;
2.
3. class Nightingale extends abcde.Bird{
4.     Nightingale(){referenceCount++;}
5.
6.     public static void main(String args[]){
7.            System.out.print("BEFORE: " + referenceCount);
8.            Nightingale florence = new Nightingale();
9.            System.out.println(" After: " + referenceCount);
10.           florence.fly();
11.    }
12. }

A. The program will compile and execute. The output will be
       Before: 0 After: 2
B. The program will compile and execute. The output will be
       Before: 0 After: 1
C. There is a compilation error in Nightingale.java at line 4, because static members
    cannot be overridden.
D. There is a compilation error in Nightingale.java at line 10, because method fly( ) is
   protected in the superclass.
E. Compilation of Nightingale will succeed, but an exception will be thrown at line 10,
    because method fly( ) is protected in the superclass.
                                                                             Page 4 of 14



viii) Consider the following definition:

  public class X{
      public int a = 1;
      private int b = 2;
      public void method(final int c) {
            int d = 3;
            class Y {
                  private void iMethod(int e){
                         /** Label**/
                  }
            }
      }
   }

Which variables may be referenced correctly at the line marked Label? (Choose one or
more.)

A. a
B. b
C. c
D. d
E. e


ix) Which statement(s) is/are true about the code below?

1. class HasStatic
 2. {
 3.     private static int x = 100;
 4.
 5.     public static void main(String args[])
 6.     {
 7.           HasStatic hs1 = new HasStatic();
 8.           hs1.x++;
 9.           HasStatic hs2 = new HasStatic();
 10.          hs2.x++;
 11.          hs1 = new HasStatic();
 12.          hs1.x++;
 13.          HasStatic.x++;
 14.          System.out.println(x);
 15. }
  16. }
                                                                                       Page 5 of 14



A. Line 8 will not compile, because it is a static reference to a private variable.
B. Line 13 will not compile, because it is a static reference to a private variable.
C. The program compiles, and the output is x = 102.
D. The program compiles, and the output is x = 103.
E. The program compiles, and the output is x = 104.




x) Given that the method dodgy( ) might throw a java.io.IOException,
java.lang.RuntimeException, or java.net.MalformedURLException (which is a subclass
of java.io.IOException), which of the following classes and sets of classes are legal?
(Choose one or more.)


A. public class aClass{
           public void aMethod( ){
                 dodgy( );
           }
       }


B. public class aClass{
           public void aMethod( ) throws java.io.IOException{
                 dodgy( );
           }
   }


C. public class aClass{
           public void aMethod( ) throws java.lang.RuntimeException{
                 dodgy( );
           }
   }



D. public class aClass{
           public void aMethod( ){
                 try{
                       dodgy( );
                 }
                 catch (IOException e){
                       e.printStackTrace( );
                 }
           }
   }
                                                                             Page 6 of 14



E. public class aClass{
       public void aMethod( ) throws java.net.MalformedURLException{
             try{dodgy( );}
             catch (IOException e){/*ignore it*/}
       }
   }

       public class anotherClass extends aClass{
       public void aMethod( ) throws java.io.IOException {
             super.aMethod( );
       }
   }



xi) Encircle the true statement(s).

A. An abstract class may not have any final methods.
B. A final class may not have any abstract methods.
C. You can create references of abstract class type.
D. A class cannot extend multiple superclasses.

xii) Encircle the true statement(s).

A. An interface cannot extend a class.
B. An interface cannot extend multiple superinterfaces.
C. A class cannot partially implement an interface.
D. You can create references of interface type.


xiii) What will happen if you compile/run the following program? (Choose one or more.)
       public class Test
       {
             static String str1 = "Hello";
             static String str2 = "Hi";

               public static void main(int[] args)
               {
                     System.out.println(str2);
               }

               public static void main(String[] args)
               {
                     System.out.println(str1);
               }
       }
                                                                                    Page 7 of 14



A. Duplicate method main( ), compilation error
B. Prints Hello
C. Prints Hi
D. Run-time exception

Q.2. Write a program that asks you to enter your name at the console and then prints
“Hello X” where X is your name that you had entered. Include exception handling.
                                                                                    (4 points)


import java.io.*;

public class Hello{

       public static void main(String args[]) throws IOException // or you can use try catch
       {
               InputStreamReader ir = new InputStreamReader(System.in);
               BufferedReader br = new BufferedReader(ir);
               //or you can use the following line instead of the above two
               //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               System.out.println("Enter your name:");
               String name = br.readLine();
               System.out.println("Hello " + name + " !");
       }

}




Q.3.                                                                                (6 points)
i) Given two objects p and q, write a line of code that tests if the two objects have the
same data.

                                       if(p.equals(q))

//equals is a method in Object class


ii) Given two objects p and q, write a line of code that checks if they contain the same
reference.

                                          if(p= =q)
                                                                                       Page 8 of 14




iii) Is it a syntax error to have the constructor of a class as private? If not, how will that
class be instantiated?


No, it is not a syntax error But then no other class instantiate your class. Your class may
contain public methods (factory methods) and those methods can construct an object and
return it, but no other class can.




Q.4.                                                                        (16 points)
Analyze each of the following segments of code. If a segment of code has one or more
compilation errors/syntax errors indicate and explain the problem(s). Otherwise, write
the output of the segment of code. You should assume that each of these segments of
code is a part of a complete program.


i)      class A {
              static String s ;
              public static void main(String args[]) {
                    System.out.println("s = " + s);

                }
        }

s = null;

(default initialization)




ii)     class A {
              public static void main(String args[]) {
                    String s = new String();
                    System.out.println("s = " + s);
              }
        }


s=

(initialized to empty String object)
                                                                                 Page 9 of 14




iii)
class A{
        public static void m(){
             System.out.println("In A's method");
      }
}

class B extends A{
        static void m(){
             System.out.println("In B's method");
      }

       public static void main(String a[]){
             B objB = new B( );
             A objA = new A( );
             m( );
             A.m( );
       }
}

Compilation error. m() in B overrides m() in A but tries to assign weaker access privilege
i.e. friendly to m() than in its super class.




iv)
class MyClass
{
      MyClass(String message)
      {
            System.out.println("Java rules!");
      }
      void myMethod()
      {
            System.out.println("Microsoft sucks!");
      }

       public static void main(String args[])
       {
             MyClass m = new MyClass();
             m.myMethod();
       }
}

Compile time error. No such MyClass constructor that takes no arguments.
                                                                              Page 10 of 14



v)
public class TestConversions{
      public static void main(String[] args) {
            String t = "Jan 13, 2003";
            String s = t.substring(4, 6);
            int i = Integer.parseInt(s);
            int n = i + 7;
            String w = t.substring(0, 4) + n + t.substring(6);
            System.out.println(t);
            System.out.println(i);
            System.out.println(n);
            System.out.println(w);
      }
}

Jan 13, 2003
13
20
Jan 20, 2003




vi)
class Catch {
  public static void main(String args[]) {
    try {
      int x = 0;

           int e = 11 / x;

           Line of code /*this line of code generates an unpredictable
                          exception*/

         } catch(Exception e) {
           System.out.println("Gotcha.");
         } catch(ArithmeticException e) {
           System.out.println("Caught.");
         }
     }
}
Compile Error.
The highlighted lines are the unreachable code. ArithmeticException already caught.
                                                                  Page 11 of 14



vii)
       class MyClass {
            int d=0;
            int a=40;
            int b;
            void get(){
              b = a/0;
            }
            void test(){
                    try{
                           get( );
                     }catch(ArrayIndexOutOfBoundsException e)
                           {System.out.println("Caught in test: "+ e);}
              }

              public static void main (String args[]){
                    try{
                          MyClass m=new MyClass ();
                          m.test();
                    }catch(ArithmeticException e){
                          System.out.println("Caught in main: "+ e);
                    }
              }
         }




Caught in main()
Java.lang.ArithmeticException: / by zero
                                                                                Page 12 of 14



viii)
class A {
  int x = 100;

    void test() {
      for(int i=0; i<2; i++) {
        int a = 33;
        class B {
          void display() {
            System.out.println( x + " " +          a);
          }
        }
        B b = new B();
        b.display();
      }
    }
}

class Demo {
  public static void main(String args[]) {
    A a = new A();
    a.test();
  }
}

Compile Time error. variable „a‟ is not final and cannot be accessed in Class B‟s
display(). Class B is inner class
                                                                                 Page 13 of 14



Q.5.                                                                             (9 points)
i) Draw the output of the following methods on the grid below, assuming that the top-left
corner of this grid is the top-left corner of the window you are drawing on. Each square
of the grid is a pixel.
                 drawRect(2,3,3,2);
                 fillArc(2,3,3,2,270,-180);




ii) Write a short program that draws two concentric circles (one circle centred within the
other) with the outer circle yellow and the inner circle green. You can place your
concentric circles anywhere on your window. Moreover, you can omit code that takes
care of proper closing of the window.




Solution to this problem will be uploaded tomorrow.
                                                                               Page 14 of 14




Q.6.                                                                           (12 points)
i)What is the name of the region of RAM where objects live?

Heap

ii) What is the name of the region of RAM where object references live?

Stack

iii) What is a heavyweight component?

That delegates the GUI-related task to the platform‟s windowing system through peer
methods in java.awt.peer


iv) What is a lightweight component?
Written completely in Java




v) Why is there an x in javax.swing package name?

Java 1.1 didn‟t allow new package by name java. …



vi) What is meant by the Resumption Model of exception handling?

Control can return to the throw point after an exception occurs and resume from there.




*******************************END************************************

								
To top