how can you rapidly comput1

Upload: charanleen100

Post on 07-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 How Can You Rapidly Comput1

    1/27

    How can you rapidly compute ~j ?

    Answer: ~j = (-j) - 1;

    What is the output of the following code ?

    Vector ts = new Vector();

    ts.add("abc");

    ts.add("abd");

    Iterator it = ts.iterator();

    ts.remove("abc");

    it.remove();

    while(it.hasNext())

    {

    System.out.println("val = " + it.next());

    }

    The line:

    it.remove();

    throws a java.util.ConcurrentModificationException exception because the vector was modified *after*

    the creation of the iterator it by the line:

    ts.remove("abc");

    In order to avoid this behavior only call the method remove() from the iterator.

    What is the output of the following code ?

  • 8/3/2019 How Can You Rapidly Comput1

    2/27

    public class CDummy extends Vector

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    CDummy dmy = new CDummy();

    dmy.add("Unu");

    dmy.add("Doi");

    System.out.println(dmy);

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    [Unu, Doi]

    Does the name of a thread have to be unique ? Yes/No

    Answer: No.

    Multiple threads with the same name can execute simultaneously.

    What is the output of the following code ?

    public class CDummy

  • 8/3/2019 How Can You Rapidly Comput1

    3/27

    {

    final strictfp public static void main(String args[])

    {

    System.out.println("CDummy.");

    }

    }

    CDummy.

    The class compiles and run without issues. The JLS states that the run function must be public, static,

    return void and take as parameters an array of strings. The above defined function meets the

    requirements.

    Will an empty file compile ? Yes/No

    Yes, there will be no compilation error but the compiler will not create a class file. (at least this is the

    behavior with jdk 1.4)

    What is the output of the following code ?

    Integer ii = new Integer(10);

    System.out.println(ii instanceof (Object));

    The code does not compile. It is not allowed to surround the type with parenthesis. You can only

    surround the object:

    System.out.println((ii) instanceof Object);

    Does the following code compile ?

    Integer arr [] = new Integer[5];

    arr.length = 7;

  • 8/3/2019 How Can You Rapidly Comput1

    4/27

    No. The length attribute is final. This attribute is assigned when the array is created. Any attempt to

    modify it will be forbidden by the compiler.

    What is the output of the following code ?

    class Short

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    java.lang.Short sh = new java.lang.Short((short)34);

    System.out.println(sh);

    }

    }

    CDummy.

    34

    The class name "Short" does not collide with the class java.lang.Short because they are not in the same

    package.

    What is the output of the following code ?

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

  • 8/3/2019 How Can You Rapidly Comput1

    5/27

    StringBuffer sb = new StringBuffer("Some");

    sb.append(" text");

    System.out.println(sb);

    }

    }

    Some text

    The method append from StringBuffer directly modifies the underlying object (StringBuffer is *not*

    immutable).

    What is the output of the following code ?

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    String [][] text = new String [10][];

    System.out.println(text[0][0]);

    }

    }

    The code prints "CDummy." then it will throw a NullPointerException. This is happening because text[0]

    is null and the above code tries to dereference this null value.

    What is the output of the following code ?

  • 8/3/2019 How Can You Rapidly Comput1

    6/27

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    Byte b1 = new Byte((byte)100);

    Byte b2 = new Byte((byte)10);

    System.out.println("" + b1.byteValue() + b2.byteValue());

    }

    }

    CDummy.

    10010

    Pay attention: the above operation concatenates strings.

    What is the output of the following code ?

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    Byte b1 = new Byte((byte)100);

    Byte b2 = new Byte(b1);

  • 8/3/2019 How Can You Rapidly Comput1

    7/27

    System.out.println("" + b1.byteValue() + b2.byteValue());

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile ! There is no Byte constructor that takes as a parameter a Byte. This is true

    for all std wrapper classes: Boolean, Byte, Character, Short, Integer, Long, Float, Double.

    What is the output of the following code ?

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    Byte b1 = new Byte((byte)20);

    Byte b2 = new Byte((byte)30);

    System.out.println(b1.byteValue() + b2.byteValue() + "");

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    CDummy.

    50

    Because the string "" is the last in the printed expression the whole expression is converted to a string

    after the addition of b1 and b2.

    Does the following code compile ?

  • 8/3/2019 How Can You Rapidly Comput1

    8/27

    import java.io.IOException;

    public class SimpleJava

    {

    public static void main(String args[]) throws IOException

    {

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    Yes. If you try to catch a checked exception (like IOException) from an empty try block the code will not

    compile. The compiler will issue the following error:

    exception java.io.IOException is never thrown in body of corresponding try statement

    This is happening because IOException is a checked exception. Unfortunately the same check does not

    apply for a function's "throws" clause that never throws the checked exception that it declares to throw.

    In our case the function main() declares that it throws the checked exception IOException, yet it is clearthat the main() function will never throw that exception.

    What is the output of the following code ?

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

  • 8/3/2019 How Can You Rapidly Comput1

    9/27

    transient int toto = 7;

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

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile. Transient variables are not serialized. Local variables are not serialized

    anyway so it makes no sense to apply this qualifier to a local variable.

    Does the following code throw an exception at runtime ? (Yes/No)

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    Float toto = new Float("10F");

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

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    No. It is legal to feed the Float constructor with a string ending in 'f' or 'F'.

    How do you get the length of a string ?

    \/\/\/\/\/\/\/\/\/\/

    Answer: using the length() method.

  • 8/3/2019 How Can You Rapidly Comput1

    10/27

    How do you compute the intersection of 2 collections ?

    \/\/\/\/\/\/\/\/\/\/

    Using the member function someCollection.retainAll(Collection col). This function retains in the original

    collection someCollection only the elements that are also found in the collection col.

    What is the output of the following code ? (it is contained in one source file)

    public interface IFace {}

    public class CDummy implements IFace

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    }

    }

    Does the following code compile ?

    class Parent{}

    class DerivedOne extends Parent{}

    class DerivedTwo extends Parent{}

  • 8/3/2019 How Can You Rapidly Comput1

    11/27

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    DerivedOne d1 = new DerivedOne();

    Parent p1 = new DerivedOne();

    DerivedTwo some1 = (DerivedTwo) p1;

    DerivedTwo some2 = (DerivedTwo) d1;

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    The line:

    DerivedTwo some2 = (DerivedTwo) d1;

    fails to compile. A reference of type DerivedOne (the object d1 is of type DerivedOne) cannot point to an

    object of type DerivedTwo, so the cast from DerivedOne to DerivedTwo performed in the above line will

    fail.

    The line:

    DerivedTwo some1 = (DerivedTwo) p1;

    passes the compilation but throws a run time exception because the p1 reference really points to an

    object of type DerivedOne.

    You have the following classes defined in 2 separate files. Why the class CDummy2 does not compile ?

    FILE CDummy.java

  • 8/3/2019 How Can You Rapidly Comput1

    12/27

    public class CDummy

    {

    static class SomeStaticNested{

    void printIt()

    {

    System.out.println("Message from static nested: Ola !");

    }

    }

    class SomeNested{

    void printIt()

    {

    System.out.println("Message from nested: Ola !");

    }

    }

    }

    FILE CDummy2.java

    public class CDummy2

    {

    public static void main(String args[])

    {

  • 8/3/2019 How Can You Rapidly Comput1

    13/27

    System.out.println("CDummy.");

    CDummy.SomeStaticNested ssm = new CDummy.SomeStaticNested();

    ssm.printIt();

    CDummy.SomeNested sm = new CDummy().new SomeNested();

    sm.printIt();

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    There is nothing wrong with the class CDummy2. It compile just fine.

    Does the following code compile ?

    new Thread(new Thread());

    \/\/\/\/\/\/\/\/\/\/

    Yes. The class Thread implements the interface Runnable. There is no Thread() constructor that takes a

    Thread as an argument but there are constructors that take a Runnable reference.

    Does the following code compile ?

    public class CDummy implements Runnable

    {

    void run()

    {

    }

    }

    \/\/\/\/\/\/\/\/\/\/

  • 8/3/2019 How Can You Rapidly Comput1

    14/27

    No. The run() method is public in the interface Runnable; you cannot assign a weaker access privilege.

    (in this case you try to assign a package level access to the method run())

    What is the output of the following code ?

    Vector a = new Vector();

    a.add("Teo");

    System.out.println(a[0]);

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile. The Vector cannot be accessed like an array.

    What is the output of the following code ?

    class SimpleJava

    {

    public static void main(String args[])

    {

    System.out.println("SimpleJava ....");

    int arr[] = new int[3] {1,2,3};

    System.out.println("arr[0] = " + arr[0]);

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile. Upon creation the array can be initialized with a list specified between {} but

    in this case it is illegal to specify its dimension. (the dimension is implicitly equal with the number of

    elements specified in the list)

    What is the output of the following code ?

  • 8/3/2019 How Can You Rapidly Comput1

    15/27

    FILE 1

    package name.mihaiu.test1;

    public class CTest1

    {

    static String name = "CTest1";

    }

    FILE 2

    package name.mihaiu.test2;

    public class CTest2 extends name.mihaiu.test1.CTest1

    {

    public static void main(String args[])

    {

    System.out.println("Class name =" + name);

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile. The static variable name has package access level. In order to be accessible

    from another package (in our case "name.mihaiu.test2") it must have public level access.

    Can you provide a constructor for an anonymous inner class ? Yes/No

    \/\/\/\/\/\/\/\/\/\/

    No. A constructor, by definition, has the same name as its class. Since an anonymous class has no name

    you cannot provide a constructor for it.

    Can you have protected member functions in an interface ?

    \/\/\/\/\/\/\/\/\/\/

  • 8/3/2019 How Can You Rapidly Comput1

    16/27

    No. By definition an interface represents the part of the class that is accesible to clients, therefore it

    must be public.

    What kind of primitives can be used in a switch instruction ? (enumerate them)

    \/\/\/\/\/\/\/\/\/\/

    Answer: "byte", "char", "short" and "int".

    What is the return value of assert ?

    \/\/\/\/\/\/\/\/\/\/

    There is no return value; assert is a Java keyword and not a function.

    What is the output of the following code ?

    abstract public class CDummy

    {

    public static void main(String argv[])

    {

    System.out.println("CDummy.");

    }

    abstract void doIt();

    }

    \/\/\/\/\/\/\/\/\/\/

    CDummy.

    Since the abstract class is never instantiated and the abstract method doIt() is never called, the code

    compiles & run without issues.

    What is the output of the following code ?

  • 8/3/2019 How Can You Rapidly Comput1

    17/27

    public class CDummy

    {

    public static void main(String args[])

    {

    try {

    testX();

    }

    catch (Exception ee)

    {

    System.out.println("Exception !");

    }

    }

    public static int testX() throws ArithmeticException

    {

    try {

    throw new ArithmeticException();

    }finally {

    System.out.println("Finally");

    return 4;

    }

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    "Finally". Since there is a return statement in the finally block, the generated exception is scrapped.

  • 8/3/2019 How Can You Rapidly Comput1

    18/27

    Does the following code compile ?

    public class CDummy

    {

    };

    \/\/\/\/\/\/\/\/\/\/

    Yes ! The semicolon at the end of the class definition does not seem to bother the java compiler !

    Does the following code compile ? Yes/No

    import java.io.IOException;

    class CBase

    {

    public static int testX2() throws IOException { return 1;}

    }

    public class CDummy extends CBase

    {

    public static void main(String args[])

    {

    }

    public static char testX2() throws IOException { return 7;}

    }

    \/\/\/\/\/\/\/\/\/\/

  • 8/3/2019 How Can You Rapidly Comput1

    19/27

    No ! The overridden method differs only by the return type !

    Does the following code compile ?

    import java.io.IOException;

    class CBase

    {

    public static int testX2() throws IOException { return 1;}

    }

    public class CDummy extends CBase

    {

    public static void main(String args[])

    {

    }

    protected static int testX2() throws IOException { return 7;}

    }

    \/\/\/\/\/\/\/\/\/\/

    No ! The overridden method attempts to restrict the access level (from public to protected). This is

    illegal in Java.

    Does the following code compile ?

    import java.io.IOException;

    class CBase

  • 8/3/2019 How Can You Rapidly Comput1

    20/27

    {

    private static int testX2() throws IOException { return 1;}

    }

    public class CDummy extends CBase

    {

    public static void main(String args[])

    {

    }

    protected static char testX2() throws Exception { return 7;}

    }

    \/\/\/\/\/\/\/\/\/\/

    Yes ! Because the method testX2() in CBase is private the method is not visible at all in CBase. As a result

    the method testX2() from CDummy is not in the same namespace as the corresponding method from

    CBase. If the method textX2() from CBase would be protected then the code will fail to compile for 2

    reasons:

    the overriding function testX2() from the class CDummy differs only by the return type from the

    overridden function textX2() from the class CBase;

    the overriding function throws Exception which is not derived from IOException (in fact it is the other

    way around);

    You cannot override final or private methods: True/False.

    \/\/\/\/\/\/\/\/\/\/

    True. Final methods cannot be overridden by definition while private methods cannot be overridden

    because they are not visible outside the class itself. (however the method name can be reused)

    Can you override a variable ?

    \/\/\/\/\/\/\/\/\/\/

    Yes. In this case the variable is said to shadow the variable from the superclass.

  • 8/3/2019 How Can You Rapidly Comput1

    21/27

    Observation:

    The polymorphism does not work for variables. The following code will print 7.

    class BookElement {int someVar = 7;}

    class Chapter extends BookElement {int someVar = 9;}

    public class CDummy

    {

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    Chapter ch = new Chapter();

    BookElement bk = ch;

    System.out.println(bk.someVar); // it prints 7

    }

    }

    The references for variables are computed at compile-time not run-time.

    Is method synchronization inherited ? True/False.

    \/\/\/\/\/\/\/\/\/\/

    Answer: False.

    What is the output of the following program ?

  • 8/3/2019 How Can You Rapidly Comput1

    22/27

    public class CDummy

    {

    static int unu = 1;

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    CDummy cd = new CDummy();

    System.out.println(cd.unu);

    System.out.println(unu);

    System.out.println(CDummy.unu);

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    Dummy.

    1

    1

    1

    What is wrong with the following code ? (there is a logical not syntactical error)

    public void coolFunction()

    {

  • 8/3/2019 How Can You Rapidly Comput1

    23/27

    String tmp;

    synchronized(tmp)

    {

    doSyncWork();

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    The synchronization takes place on a local variable. This is useless because all threads have their local

    copy of a local variable. As it is the code is not synchronized at all.

    What is the output of the following code ?

    try {

    System.out.println("Test !");

    }

    catch(InterruptedException ee) {

    System.out.println("The main thread was interrupted !");

    }

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile because the exception InterruptedException is never thrown.

    (InterruptedException is checked)

    What is the output of the following code ?

    try {

    System.out.println("Test !");

    }

  • 8/3/2019 How Can You Rapidly Comput1

    24/27

    catch(IndexOutOfBoundsException ee) {

    System.out.println("The main thread was interrupted !");

    }

    \/\/\/\/\/\/\/\/\/\/

    Test !

    Attention: IndexOutOfBoundsException is not checked (for this reason the code compiles)

    Can you attach many threads to a class extending Thread ? True/False.

    \/\/\/\/\/\/\/\/\/\/

    Answer: True.

    This is a tricky question. Bear in mind that the class Thread implements the Runnable interface. As a

    result you could always write something like this:

    class MyThread extends Thread{}

    MyThread th = new MyThread();

    new Thread(th).start();

    new Thread(th).start();

    new Thread(th).start();

    Thus to the object "th" you can attach many threads.

    OTOH it is true that you cannot call many times the start() method on the same object:

    th.start();

    th.start();

    because the code will fail at runtime.

    Can you attach many threads to a class implementing Runnable ? True/False.

  • 8/3/2019 How Can You Rapidly Comput1

    25/27

    \/\/\/\/\/\/\/\/\/\/

    Answer: True

    Will the following code compile ? Yes/No

    class CDummy

    {

    int someInt;

    void printData(String input) {

    synchronized (someInt) {

    System.out.println(input);

    }

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    No ! Synchronization can only be done on Object(s) not primitives.

    When an IllegalMonitorStateException is thrown ?

    \/\/\/\/\/\/\/\/\/\/

    When the wait() or notify() functions are called on an Object on which the current thread does not have

    a lock.

    Does the following code compile ? Yes/No

    public class CDummy

    {

    static int someVar1 = 9;

    int someVar2 = 11;

  • 8/3/2019 How Can You Rapidly Comput1

    26/27

    public static void main(String args[])

    {

    System.out.println("CDummy.");

    NestedTwo ns2 = new CDummy().new NestedTwo();

    System.out.println(ns2.getOuterVariable2());

    }

    class NestedTwo

    {

    static int getOuterVariable1() {return someVar1;}

    int getOuterVariable2() {return someVar2;}

    }

    }

    \/\/\/\/\/\/\/\/\/\/

    No. Inner classes cannot have static attributes or members. The function getOuterVariable1() is static.

    What are nested classes ?

    \/\/\/\/\/\/\/\/\/\/

    Any class defined inside another class.

    What are inner classes ?

    \/\/\/\/\/\/\/\/\/\/

    Non-static nested classes.

    How many types of nested classes there are ?

  • 8/3/2019 How Can You Rapidly Comput1

    27/27

    \/\/\/\/\/\/\/\/\/\/

    Static nested classes and inner classes.

    How can you define a static inner class ?

    \/\/\/\/\/\/\/\/\/\/

    Inner classes are not static by definition, nor do they allow any static member functions or attributes.