Tuesday, November 26, 2013

Question: Variable Decleration and Scope

Given:

public static void main(String[] args) {
   for (int i = 0; i <= 10; i++) {
      if (i > 6) 

           break;
   }
    System.out.println(i);
 }


What is the result?

A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime

Execution:
The variable i have scope within for loop only and it is printed out of scope will give Compile Time Exception. So it says can't find symbol 'i'

Answer:
E. Compilation fails.


Question: Hashmap & HashSet

Given:

public class Person {

   private name;
   public Person(String name) {
     this.name = name;
   }
   

   public int hashCode() {
    return 420;
   }
}

Which statement is true?

A. The time to find the value from HashMap with a Person key depends on the size of the map.
B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
C. Inserting a second Person object into a HashSet will cause the first Person object to be
removed as a duplicate.
D. The time to determine whether a Person object is contained in a HashSet is constant and does
NOT depend on the size of the map.


Execution:

A: The more the size Hashmap will have it will take time  to execute which is correct
B. If we will delete a person key then it will not delete all keys of person type. It is just like deleting String key and all String keys got removed.
C. The previous object will get over write not removed
D. To determine size of collection time is always be taken with respect to size.

Answer:
A. The time to find the value from HashMap with a Person key depends on the size of the map.

Question: Typecasting and Array

Given:

public static void main(String[] args) {
  Object obj = new int[] { 1, 2, 3 };
  int[] someArray = (int[])obj;

  for (int i : someArray) System.out.print(i + " ");
}


What is the result?
A. 1 2 3
B. Compilation fails because of an error in line Object obj .
C. Compilation fails because of an error in line int[] someArray.
D. Compilation fails because of an error in line for (int i.
E. A ClassCastException is thrown at runtime.




































Execution:
Correct declaration and casting
and correct fetching of data
 
Answer: A

Question: Interfaces, abstract class, inheritence and function decleration

Given:

interface Data { public void load(); }
abstract class Info { public abstract void load(); }

Which class correctly uses the Data interface and Info class?

A.
public class Employee extends Info implements Data {
        public void load() { /*do something*/ }
}

B.
public class Employee implements Info extends Data {
public void load() { /*do something*/ }
}

C.
public class Employee extends Info implements Data {
public void load(){ /*do something*/ }
public void Info.load(){ /*do something*/ }
}


D.
public class Employee implements Info extends Data {
public void Data.load(){ /*do something*/ }
public void load(){ /*do something*/ }
}

E. public class Employee implements Info extends Data {
public void load(){ /*do something*/ }
public void Info.load(){ /*do something*/ }
}

F.
public class Employee extends Info implements Data{
public void Data.load() { /*do something*/ }
public void Info.load() { /*do something*/ }
}

Execution:
In option B, Interface only can implement and Class can only extends.
In option C Info.load() is invalid method declaration
D & E are wrong use of inheritance i.e always to implement interface and to extend classes
 F is invalid method declaration.
Answer: A
public class Employee extends Info implements Data {
        public void load() { /*do something*/ }
}

Question: Override, extends and implements

Given:

interface A { public void aMethod(); }

interface B { public void bMethod(); }
 

& interface C extends A,B { public void cMethod(); }
 

class D implements B {
@  public void bMethod(){}
}
 

$ class E extends D implements C {
  public void aMethod(){}
#  public void bMethod(){}
  public void cMethod(){}
}

What is the result?

A. Compilation fails because of an error in line &.
B. Compilation fails because of an error in line $.
C. Compilation fails because of an error in line #.
D. If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line
@.
E. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in
Line @.
F. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in
Line #.

Execution:
There will be no compilation error in any line.
If we create object of some class and reference of it's base class then call some overrided method then it will only execute method of class object(new E()) rather then reference (D e).

Answer:
F. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in
Line 9.

Monday, November 25, 2013

Question: Iterator, For Each and Lists

Given:

public static Iterator reverse(List list) {
     Collections.reverse(list); 
     return list.iterator();
}

public static void main(String[] args) {
   List list = new ArrayList();
   list.add("1");
   list.add("2");
   list.add("3");
   for (Object obj: reverse(list))
           System.out.print(obj + ", ");
}

What is the result?

A. 3, 2, 1,
B. 1, 2, 3,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.

Execution:
For Each is not applicable to expression type. You require an Array or Iterable
but not Iterator. Here the reverse method is returning an iterator  
to correct it should be used as
for (Object obj: list)

Answer:
C. Compilation fails.

Question: Switch / enum

Given:

public class Test {
    public enum Dogs {collie, harrier};
    

    public static void main(String [] args) {
    Dogs myDog = Dogs.collie;
    switch (myDog) {
       case collie:
          System.out.print("collie ");
       case harrier:
          System.out.print("harrier ");
     }
   }

}
What is the result?
A. collie
B. harrier
C. Compilation fails.
D. collie harrier
E. An exception is thrown at runtime.

Execution:
It will first print coolie but as BREAK is not there in case so it will execute next statement too and will print harrier.
so answer will be coolie harrier

Answer:
D. collie harrier