Showing posts with label J2SE. Show all posts
Showing posts with label J2SE. Show all posts

Wednesday, December 7, 2011

Important Concepts To Remeber during Written Java Test

Point 1)
The code below is executed by "java -Dmyprop=myprop Test". Which two, placed instead of "//some code goes here", will produce the output "myprop"? (Choose two)
public class Test { 
    public static void main(String args[]) {
        String prop = //some code goes here//
        System.out.print(prop);
    }
}
 A) System.getEnv("myprop");
 B) System.load("myprop");
 C) System.property("myprop");
 D) System.getProperty("myprop");
 E) System.get("myprop");
 F) System.getProperties().getProperty("myprop");
Answers: D, F
System.getProperty functions gets the system property indicated by the specified key.


Point 2)
Always Remember
A Static method can call other Static Methods in same class
A Static method can not call other Non Static Methods directly in same class
A Non Static Method can call static method directly

Point 3)
public class Room {
    public int roomNr;
    private Date beginDtm;
    private Date endDttm;
    
    public void book(int roomNr, Date beginDttm, Date endDttm) {
        this.roomNr = roomNr;
        this.beginDtm = beginDttm;
        this.endDttm = endDttm;
    }
}
The variable roomNr breaks encapsulation.

Point 4)
Object myObj = new String[]{"one", "two", "three"}; is valid declaration and initiation

Point 5)
 public void waitForSomething() {
        SomeClass o = new SomeClass();
        synchronized (o) {
            o.wait();
            o.notify();         
        }}
A) This code may throw an InterruptedException
B) This code may throw an IllegalStateException
C) This code may throw a TimeOutException
D) Reversing the ofrer of o.wait() and o.notify() will cause this method to complete normally.
Answer: A.
Object
API:
public final void wait() throws InterruptedException


Point 6)
String s="abcd";
System.out.println(s.charAt(3));

Will print 3 because chatAt starts from 0

Point 7)
Given the code. What is the result?
import java.io.*;
public class Hotel implements Serializable {
    private Room room = new Room();
    
    public static void main(String[] args) {
        Hotel h = new Hotel();
        try {
            FileOutputStream fos = new FileOutputStream("Hotel.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(h);
            oos.close();
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
class Room {
}
 A) Compilation fails.
 B) An instance of Hotel is serialized.
 C) An instance of Hotel and an instance of Room are both serialized.
 D) An exception is thrown at runtime.
Answer: D.
java.io.NotSerializableException is thrown at runtime. All variables of a class being serialized must be serializable as well.
If Room become transient then its fine private transient Room room = new Room();

Point 8)
      public static void main(String[] args) {

            Integer[] a=new Integer[10];
            printValue(a); // Problem here. Integer can not be passed in Double. Compile time error
      }

      public static void printValue(Double[] d){
            System.out.println(d);}
            public static void main(String[] args) {

            Integer[] a=new Integer[10];
            printValue(a); // Problem here. Integer can not be passed in Long. Compile time error

      }

      public static void printValue(Long[] d){
            System.out.println(d);
      }