30quasimp

Upload: bharathsajja

Post on 03-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 30quasimp

    1/29

    30 imp questions!!

    What will happen when you attempt to compile and run the following code? (Assume that thecode is compiled and run with assertions enabled.)public class AssertTest{public void methodA(int i){assert i >=0 : methodB();System.out.println(i);}public void methodB(){System.out.println("The value must not be negative");}

    public static void main(String args[]){AssertTest test =new AssertTest();test.methodA(-10);}}It will print -10It will result in Assertion Error showing the message -"The value must not be negative".The code will not compile.None of these

    Question 2:-What will happen when you attempt to compile and run the following code?public class Static{static{int x =5;}static int x,y;public static void main(String args[]){

    x--; myMethod();System.out.println(x +y +++x);}

    public static void myMethod(){y =x+++++x;}

  • 7/28/2019 30quasimp

    2/29

    }Compile time error prints : 1prints : 2 prints : 3prints : 7 prints : 8

    Question 3Given the following code, what will be the output?class Value{public int i =15;}public class Test{public static void main(String argv[]){Test t =new Test();

    t.first();}public void first(){int i =5;Value v =new Value();v.i =25;second(v, i);System.out.println(v.i);}public void second(Value v, int i){i =0;v.i =20;Value val =new Value();v =val;System.out.println(v.i +" " +i);}}15 0 20 015 0 1520 20

    Question 4What will happen when you attempt to compile and run the following code?class MyParent{int x, y;MyParent(int x, int y)

  • 7/28/2019 30quasimp

    3/29

    {this.x =x;this.y =y;}public int addMe(int x, int y)

    {return this.x +x +y +this.y;}public int addMe(MyParent myPar){return addMe(myPar.x, myPar.y);}}class MyChild extends MyParent{in z;

    MyChild (int x, int y, int z){super(x,y);this.z =z;}public int addMe(int x, int y, int z){return this.x +x +this.y +y +this.z +z;}public int addMe(MyChild myChi){return addMe(myChi.x, myChi.y, myChi.z);}public int addMe(int x, int y){return this.x +x +this.y +y;}}public class MySomeOne{public static void main(String args[]){MyChild myChi =new MyChild(10, 20, 30);MyParent myPar =new MyParent(10, 20);int x =myChi.addMe(10, 20, 30);int y =myChi.addMe(myChi);int z =myPar.addMe(myPar);System.out.println(x +y +z);}}

  • 7/28/2019 30quasimp

    4/29

    300 240120 180Compilation Error None of the above

    Question 5

    The class AssertionError has "is - a" relationship with these classes:RuntimeExceptionErrorVirtualMachineErrorIllegalAccessExceptionThrowable

    Question 6What will be the result of executing the following code?1. boolean a =true;2. boolean b =false;

    3. boolean c =true;4. if (a ==true)5. if (b ==true)6. if (c ==true) System.out.println("Some things are true in this world");7. else System.out.println("Nothing is true in this world!");8. else if (a && (b =c)) System.out.println("It's too confusing to tell what is true and what isfalse");9. else System.out.println("Hey this won't compile");The code won't compile"Some things are true in this world" will be printed"Hey this won't compile" will be printedNone of these

    Question 7What will happen when you attempt to compile and run the following code?interface MyInterface{}public class MyInstanceTest implements MyInterface{static String s;public static void main(String args[]){MyInstanceTest t =new MyInstanceTest();if(t instanceof MyInterface){System.out.println("I am true interface");}else{

  • 7/28/2019 30quasimp

    5/29

    System.out.println("I am false interface");}if(s instanceof String){System.out.println("I am true String");

    }else{System.out.println("I am false String");}}}Compile time errorRuntime errorPrints : "I am true interface" followed by " I am true String"Prints : "I am false interface" followed by " I am false String"

    Prints : "I am true interface" followed by " I am false String"Prints : "I am false interface" followed by " I am true String"

    Question 8

    What results from attempting to compile and run the following code?

    public class Ternary

    {

    public static void main(String args[])

    {

    int a =5;

    System.out.println("Value is - " +((a

  • 7/28/2019 30quasimp

    6/29

    Question 9

    In the following pieces of code, A and D will compile without any error. True/False?

    A: StringBuffer sb1 ="abcd";

    B: Boolean b =new Boolean("abcd");

    C: byte b =255;

    D: int x =0x1234;

    E: float fl =1.2;

    Question 10

    Considering the following code, which variables may be referenced correctly at line 12?

    public class Outer

    {

    public int a =1;

    private int b =2;

    public void method(final int c)

    {

    int d =3;

    class Inner

    {

    private void iMethod(int e)

  • 7/28/2019 30quasimp

    7/29

    {

    }

    }

    }

    }

    a bc deQuestion 11

    What will be the result of executing the following code?

    public static void main(String args[])

    {

    char digit ='a';

    for (int i =0; i

  • 7/28/2019 30quasimp

    8/29

    System.out.println(j);

    }

    default :

    {

    int j =100;

    System.out.println(j);

    }

    }

    }

    int i =j;

    System.out.println(i);

    }

    100 will be printed 11 times. 100 will be printed 10 times and then there will be a runtimeexception.The code will not compile because the variable i cannot be declared twice within the main()method. The code will not compile because the variable j cannot be declared twice within theswitch statement.None of these.

    Question 12

    Which of the following collection classes from java.util package are Thread safe?

    Vector ArrayListHashMap Hashtable

    Question 13

  • 7/28/2019 30quasimp

    9/29

    What will happen when you attempt to compile and run the following code?

    class MyThread extends Thread

    {

    public void run()

    {

    System.out.println("MyThread: run()");

    }

    public void start()

    {

    System.out.println("MyThread: start()");

    }

    }

    class MyRunnable implements Runnable

    {

    public void run()

    {

    System.out.println("MyRunnable: run()");

    }

    public void start()

    {

    System.out.println("MyRunnable: start()");

  • 7/28/2019 30quasimp

    10/29

    }

    }

    public class MyTest

    {

    public static void main(String args[])

    {

    MyThread myThread =new MyThread();

    MyRunnable myRunnable =new MyRunnable();

    Thread thread =new Thread(myRunnable);

    myThread.start();

    thread.start();

    }

    }

    Prints : MyThread: start() followed by MyRunnable:run() Prints : MyThread: run() followed byMyRunnable:start()Prints : MyThread: start() followed by MyRunnable:start() Prints : MyThread: run() followed byMyRunnable:run()Compile time error None of the above

    Question 14

    What will be the result of executing the following code?

    // Filename; SuperclassX.java

    package packageX;

  • 7/28/2019 30quasimp

    11/29

    public class SuperclassX

    {

    protected void superclassMethodX()

    {

    }

    int superclassVarX;

    }

    // Filename SubclassY.java

    1. package packageX.packageY;

    2.

    3. public class SubclassY extends SuperclassX

    4. {

    5. SuperclassX objX =new SubclassY();

    6. SubclassY objY =new SubclassY();

    7. void subclassMethodY()

    8. {

    9. objY.superclassMethodX();

    10. int i;

    11. i =objY .superclassVarX;

    12. }

    13. }

  • 7/28/2019 30quasimp

    12/29

    Compilation error at line 5 Compilation error at line 9Runtime exception at line 11 None of these

    Question 15

    Consider the class hierarchy shown below: --------------------------------------------------------------------

    class FourWheeler implements DrivingUtilities

    class Car extends FourWheeler

    class Truck extends FourWheeler

    class Bus extends FourWheeler

    class Crane extends FourWheeler

    ----------------------------------------------------------------------

    Consider the following code below:

    1. DrivingUtilities du;

    2. FourWheeler fw;

    3. Truck myTruck =new Truck();

    4. du =(DrivingUtilities)myTruck;

    5. fw =new Crane();

  • 7/28/2019 30quasimp

    13/29

    6. fw =du;

    Which of the statements below are true?

    Line 4 will not compile because an interface cannot refer to an object. The code will compile andrun.The code will not compile without an explicit cast at line 6, because going down the hierarchywithout casting is not allowed. The code at line 4 will compile even without the explicit cast.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.Question 16

    What results from the following code?

    1. class MyClass

    2. {

    3. void myMethod(int i) {System.out.println("int version");}

    4. void myMethod(String s) {System.out.println("String version");}

    5. public static void main(String args[])

    6. {

    7. MyClass obj =new MyClass();

    8. char ch ='c';

    9. obj.myMethod(ch);

    10. }

    11. }

    Line 4 will not compile as void methods can't be overridden.An exception at line 9Line 9 will not compile as there is no version of myMethod, which takes a char as argument.The code compiles and produces output: int version.The code compiles and produces output: String versionQuestion 17

  • 7/28/2019 30quasimp

    14/29

    What is the result when you compile and run the following code?

    public class ThrowsDemo

    {

    static void throwMethod()

    {

    System.out.println("Inside throwMethod.");

    throw new IllegalAccessException("demo");

    }

    public static void main(String args[])

    {

    try

    {

    throwMethod();

    }

    catch (IllegalAccessException e)

    {

    System.out.println("Caught " +e);

    }

    }

    }

  • 7/28/2019 30quasimp

    15/29

    Compilation error Runtime errorCompile successfully, nothing is printed. Inside throwMethod. followed by caught:java.lang.IllegalAccessExcption: demoQuestion 18

    What will be printed when you execute the following code?

    class X

    {

    Y b =new Y();

    X()

    {

    System.out.print("X");

    }

    }

    class Y

    {

    Y()

    {

    System.out.print("Y");

    }

    }

    public class Z extends X

    {

    Y y =new Y();

  • 7/28/2019 30quasimp

    16/29

    Z()

    {

    System.out.print("Z");

    }

    public static void main(String[] args)

    {

    new Z();

    }

    }

    ZYZXYZYXYZ

    Question 19

    What will happen when you attempt to compile and run the following code snippet?

    Boolean b =new Boolean("TRUE");

    if(b.booleanValue())

    {

    System.out.println("Yes : " +b);

    }

    else

    {

  • 7/28/2019 30quasimp

    17/29

    System.out.println("No : " +b);

    }

    The code will not compile.It will print - Yes : trueIt will print - Yes : TRUEIt will print - No : falseIt will print - No : FALSE

    Question 20

    What is the result when you compile and run the following code?

    public class Test

    {

    public void method()

    {

    for(int i =0; i

  • 7/28/2019 30quasimp

    18/29

    Question 21

    What will happen when you attempt to compile and run the following code?

    int Output =10;

    boolean b1 =false;

    if((b1 ==true) && ((Output +=10) ==20))

    {

    System.out.println("We are equal " +Output);

    }

    else

    {

    System.out.println("Not equal! " +Output);

    }

    Compilation error, attempting to perform binary comparison on logical data type.Compilation and output of "We are equal 10".Compilation and output of "Not equal! 20".Compilation and output of "Not equal! 10".Question 22

    What will be the result of executing the following code?

    Given that Test1 is a class.

    1. Test1[] t1 =new Test1[10];

    2. Test1[][] t2 =new Test1[5][];

  • 7/28/2019 30quasimp

    19/29

    3. if (t1[0] ==null)

    4. {

    5. t2[0] =new Test1[10]

    6. t2[1] =new Test1[10]

    7. t2[2] =new Test1[10]

    8. t2[3] =new Test1[10]

    9. t2[4] =new Test1[10]

    10. }

    11. System.out.println(t1[0]);

    12. System.out.println(t2[1][0]);

    The code will not compile because the array t2 is not initialized in an unconditional statementbefore use.The code will compile but a runtime exception will be thrown at line 12.The code will compile but a runtime exception will be thrown at line 11.None of these.Question 23

    What will happen when you attempt to compile and run the following code?

    class Base

    {

    int i =99;

    public void amethod()

    {

    System.out.println("Base.amethod()");

    }

  • 7/28/2019 30quasimp

    20/29

    Base()

    {

    amethod();

    }

    }

    public class Derived extends Base

    {

    int i =-1;

    public static void main(String argv[])

    {

    Base b =new Derived();

    System.out.println(b.i);

    b.amethod();

    }

    public void amethod()

    {

    System.out.println("Derived.amethod()");

    }

    }

  • 7/28/2019 30quasimp

    21/29

    Derived.amethod()

    -1

    Derived.amethod()

    Derived.amethod()

    99

    Derived.amethod()

    99

    Derived.amethod()

    Compile time error

    @

    Question 24

    What will be the output on compiling/running the following code?

    public class MyThread implements Runnable

    {

    String myString ="Yes ";

    public void run()

    {

    this.myString ="No ";

    }

  • 7/28/2019 30quasimp

    22/29

    public static void main(String[] args)

    {

    MyThread t =new MyThread();

    new Thread(t).start();

    for (int i=0; i

  • 7/28/2019 30quasimp

    23/29

    return ++myCount;

    }

    public void getYourNumber()

    {

    yourNumber =nextCount();

    }

    }

    The code will give compilation error.The code will give runtime error.Each thread will get a unique number.The uniqueness of the number among different Threads can't be guaranteed.

    Question 26

    Which of the following lines will print false?

    1. public class MyClass

    2. {

    3. static String s1 ="I am unique!";

    4. public static void main(String args[])

    5. {

    6. String s2 ="I am unique!";

    7. String s3 =new String(s1);

    8. System.out.println(s1 ==s2);

    9. System.out.println(s1.equals(s2));

  • 7/28/2019 30quasimp

    24/29

    10. System.out.println(s3 ==s1);

    11. System.out.println(s3.equals(s1));

    12. System.out.println(TestClass.s4 ==s1);

    13. }

    14. }

    15.

    16. class TestClass

    17. {

    18. static String s4 ="I am unique!";

    19. }

    Lines 10 and 12Line 12 onlyLines 8 and 10None of these

    Question 27

    What is displayed when the following code is compiled and executed?

    String s1 =new String("Test");

    String s2 =new String("Test");

    if (s1==s2)

    System.out.println("Same");

  • 7/28/2019 30quasimp

    25/29

    if (s1.equals(s2))

    System.out.println("Equals");

    SameEqualsThe code compiles, but nothing is displayed upon execution.The code fails to compile.

    Question 28

    What is displayed when the following is executed?

    class Parent

    {

    private void method1()

    {

    System.out.println("Parent's method1()");

    }

    public void method2()

    {

    System.out.println("Parent's method2()");

    method1();

    }

    }

    class Child extends Parent

  • 7/28/2019 30quasimp

    26/29

    {

    public void method1()

    {

    System.out.println("Child's method1()");

    }

    public static void main(String args[])

    {

    Parent p =new Child();

    p.method2();

    }

    }

    Compile time errorRun time errorprints : Parent's method2()Parent's method1()

    prints : Parent's method2()Child's method1()

    Question 29

    What will happen when you attempt to compile and run the following code snippet?

    String str ="Java";

    StringBuffer buffer =new StringBuffer(str);

  • 7/28/2019 30quasimp

    27/29

    if(str.equals(buffer))

    {

    System.out.println("Both are equal");

    }

    else

    {

    System.out.println("Both are not equal");

    }

    It will print - Both are not equalIt will print - Both are equalCompile time error as you can not use equals for objects of different classesRuntime error as you can not use equals for objects of different classesNone of these

    Question 30

    What will happen when you attempt to compile and run the following code?

    public class MyThread extends Thread

    {

    String myName;

    MyThread(String name)

    {

    myName =name;

    }

    public void run()

  • 7/28/2019 30quasimp

    28/29

    {

    for(int i=0; i

  • 7/28/2019 30quasimp

    29/29

    In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times),mt1.join(); can be placed at //XXX position.In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times),mt1.sleep(100); can be placed at //XXX position.In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times),

    mt1.run(); can be placed at //XXX position.In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times),there is no need to write any code.

    AnilReddy

    Founder & Administrator | JavaEra.com | Certified Java Programmer