main exam 2

32
Question 1 of 61 What is the result of executing the following fragment of code: boolean b1 = false; boolean b2 = false; if (b2 != b1 = !b2) { System.out.println("true"); } else { System.out.println("false"); } Select 1 correct option. a Compile time error. b It will print true. c It will print false. d Runtime error. e It will print nothing. Question 2 of 61 Consider the following classes... (See Exhibit) Which of the following method declarations would be valid at line //1 ? class Teacher { void teach(String student) { /* lots of code */ } } class Prof extends Teacher { //1 } Select 4 correct options

Upload: api-3752405

Post on 10-Apr-2015

80 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Main Exam 2

Question 1 of 61

What is the result of executing the following fragment of code:

boolean b1 = false;boolean b2 = false;if (b2 != b1 = !b2){ System.out.println("true");}else{ System.out.println("false");}

Select 1 correct option.a Compile time error.  

b It will print true.  

c It will print false.  

d Runtime error.  

e It will print nothing.

Question 2 of 61

Consider the following classes... (See Exhibit) Which of the following method declarations would be valid at line   //1 ? class Teacher{ void teach(String student) { /* lots of code */ }}class Prof extends Teacher{ //1}

Select 4 correct optionsa public void teach() throws Exception  

b private void teach(int i) throws Exception  

c protected void teach(String s)  

d public final void teach(String s)  

e public abstract void teach(String s)

Page 2: Main Exam 2

Question 3 of 61

Which one of these is a proper definition of a class TestClass that cannot be sub-classed?

Select 1 correct option.a final class TestClass {  }  

b abstract class TestClass {  }  

c native class TestClass {  }  

d static class TestClass {  }  

e private class TestClass {  }

Question 4 of 61

The following class will print '2' when compiled and run.

class Test{ public static int[ ] getArray() { return null; } public static void main(String[] args) { int index = 1; try { getArray()[index=2]++; } catch (Exception e){ } //empty catch System.out.println("index = " + index); }}

Select 1 correct option.a True  

b False

Question 5 of 61

Consider the following code... What should replace XXXX? public class TestClass{ class MyException extends Exception {} public void myMethod() throws XXXX { throw new MyException(); }}

Page 3: Main Exam 2

Select 3 correct optionsa MyException  

b Exception  

c No throws clause is necessary  

d Throwable  

e RuntimeException

Question 6 of 61

What letters, and in what order, will be printed when the following program is compiled and run? (See Exhibit)  public class FinallyTest{ public static void main(String args[]) throws Exception { try { m1(); System.out.println("A"); } finally { System.out.println("B"); } System.out.println("C"); } public static void m1() throws Exception { throw new Exception(); }}

Select 1 correct option.a It will print C and B, in that order.  

b It will print A and B, in that order.  

c It will print B and throw Exception.  

d It will print A, B and C  in that order.  

e Compile time error.

Question 7 of 61

What will be the output of the following class...

class Test{ public static void main(String[] args) { int j = 1;

Page 4: Main Exam 2

try { int i = doIt() / (j = 2); } catch (Exception e) { System.out.println(" j = " + j); } } public static int doIt() throws Exception { throw new Exception("FORGET IT"); }}

Select 1 correct option.a It will print j = 1;  

b It will print j = 2;  

c The value of j cannot be determined.  

d It will not compile.  

e None of the above.

Question 8 of 61

Consider the following two methods ...

public int leftShift(int number, int by){ return number << by;}public int rightShift(int number, int by){ return number >> by;}And the code snippet :{ int i = 1; i = leftShift(i, 31); i = rightShift(i, 31); System.out.println(i); //1}

What will line 1 print ?

Select 1 correct option.a Some +ive number greater than 1.  

b Some big -ive number less than 1.  

c It will print 0.  

d It will print 1  

e It will print -1

Page 5: Main Exam 2

Question 9 of 61

Which of these statements are true?

Select 3 correct optionsa The compiler will fail to compile code that explicitly tries to call the finalize

method. 

b The finalize method must be declared with protected accessibility.  

c If an object is being collected by the GC then it's finalize() method is guaranteed to be called.

 

d finalize method can be overloaded.  

e You can give any exception in throws clause of finalize() method.

Question 10 of 61

What will the following program print ?(See Exhibit)  class Test{ public static void main(String[] args) { int i = 4; int ia[][][] = new int[i][i = 3][i]; System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length); }}

Select 1 correct option.a It will not compile.  

b 3, 4, 3  

c 3, 3, 3  

d 4, 3, 4  

e 4, 3, 3

Question 11 of 61

Consider the following class:

public class GoodOne{

int theval;public int hashCode(){

return theval%3;

Page 6: Main Exam 2

}public boolean equals(Object obj){

// 1 insert code here.}

}

Which of the following options may be insterted at //1?

Select 1 correct option.a return true;  

b return theval%3 == 0? true :false;  

c return theval%2 == 0? true :false;  

d return ( (int)Math.random())*10%3 == 0? true :false;  

e return false;

Question 12 of 61

Consider that 'str' is a variable of class java.lang.String. Which of the following lines of code may throw a NullPointerException in certain situations? Or a tougher version of the question could be : Which of the following lines of code are not an example of roboust design ?

Select 3 correct optionsa if ( (str != null) | ( i == str.length() )  

b if ( (str == null) | ( i == str.length() )  

c if ( (str != null) || (i == str.length() )  

d if ( (str == null) || (i == str.length() )

Question 13 of 61

Which of the following statements are true?

Select 2 correct optionsa An inner class may be declared static  

b Anonymous inner class may be declared public.  

c Anonymous inner class may be declared private.  

d Anonymous inner class may be declared protected.  

e Anonymous inner class may extend an abstract class.

Question 14 of 61

Page 7: Main Exam 2

In the following code, after which statement (earliest), the object originally held in s, may be garbage collected ? Assume that there is a Student class with appropriate methods. (See Exhibit)  1. public class TestClass {2. public static void main (String args[]) {3. Student s = new Student("Vaishali", "930012");4. s.grade();5. System.out.println(s.getName());6. s = null;7. s = new Student("Vaishali", "930012");8. s.grade();9. System.out.println(s.getName());10 s = null; } }

Select 1 correct option.a It will not be Garbage Collected till the end of the program.  

b Line 5  

c Line 6  

d Line 7  

e Line 10

Question 15 of 61

Which of the following statements are true?

Select 2 correct optionsa The modulus operator % can only be used with integer operands.  

b & can have integral as well as boolean operands.  

c The arithmetic operators *, / and % have the same level of precedence.  

d && can have integer as well as boolean operands.  

e ~ can have integer as well as boolean operands.

Question 16 of 61

Consider the following  class...

class MyString extends String{ MyString(){ super(); }

Page 8: Main Exam 2

}

The above code will not compile.

Select 1 correct option.a True  

b False

Question 17 of 61

Which operators will always evaluate all the operands?

Select 2 correct optionsa &&  

b |  

c ||  

d ? :  

e %

Question 18 of 61

Consider the following class... (See Exhibit) What will happen when you attempt to compile and run the program? import java.awt.*;import java.awt.event.*;class TestFrame extends Frame{ String s="Message"; public static void main(String args[]) { TestFrame t = new TestFrame(); Button b = new Button("press me"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Message is " +s); } } ); t.add(b); }}

Select 1 correct option.

Page 9: Main Exam 2

a It will not compile.  

b It will compile but not show anything when run.  

c It will compile and show  a very small Frame.  

d It will compile and show a frame big enough to display the button.  

e None of the above.

Question 19 of 61

Consider the following classes...

class Car{ public int gearRatio = 8; public String accelerate() { return "Accelerate : Car"; }}class SportsCar extends Car{ public int gearRatio = 9; public String accelerate() { return "Accelerate : SportsCar"; } public static void main(String[] args) { Car c = new SportsCar(); System.out.println( c.gearRatio+" "+c.accelerate() ); }}

What will be printed when SportsCar is run?

Select 1 correct option.a 8   Accelerate : Car  

b 9  Accelerate : Car  

c 8  Accelerate : SportsCar  

d 9  Accelerate : SportsCar  

e None of the above.

Question 20 of 61

You are modeling a class hierarchy for living things. You have a class LivingThing which has an abstract method reproduce(). Now, you want to have 2 subclasses of LivingThing, Plant and Animal. Obviously, both do reproduce but the mechanisms are different. What would you do?

Select 1 correct option.a Overload the reproduce method in Plant and Animal Clases  

Page 10: Main Exam 2

b Overload the reproduce method in LivingThing Class.  

c Override the reproduce method in Plant and Animal Clases  

d Either overload or override, it depends on the taste of the designer.

Question 21 of 61

In which of the following cases a thread will definitely be alive but not be running?

Select 3 correct optionsa The thread has issued a call to wait( ).  

b The thread is trying to enter a synchronized block and the monitor is not free.  

c A higher priority thread comes in ready to run state.  

d The thread is sleeping as a result of a call to the sleep( ) method.  

e The thread calls yield().

Question 22 of 61

What will be the result of attempting to compile and run the following class?

public class TestClass{ public static void main(String args[ ] ) { int i = 1; int[] iArr = {1}; incr(i) ; incr(iArr) ; System.out.println( "i = " + i + " iArr[0] = " + iArr [ 0 ] ) ; } public static void incr(int n ) { n++ ; } public static void incr(int[ ] n ) { n [ 0 ]++ ; }}

Select 1 correct option.a The code will print i = 1 iArr[0] = 1;  

b The code will print i = 1 iArr[0] = 2;  

c The code will print i = 2 iArr[0] = 1;  

d The code will print i = 2 iArr[0] = 2;  

e The code will not compile.

Question 23 of 61

Page 11: Main Exam 2

Which of the following calls will not stop a thread from executing ? Assume that the thread is already started.

Select 3 correct optionsa start()  

b notify()  

c wait()  

d interrupt()  

e t.join(); //here t is some other thread.

Question 24 of 61

Consider the following program... public class TestClass implements Runnable {   int x = 5;   public void run()   {     this.x = 10;   }   public static void main(String[] args)   {      TestClass tc = new TestClass();      new Thread(tc).start(); // 1      System.out.println(tc.x);   } } What will it print when run?

Select 1 correct option.a 5  

b 10  

c It will not compile.  

d Exception at Runtime.  

e The output cannot be determined.

Question 25 of 61

Which of the following methods are available in the java.lang.Math class?

Select 4 correct options

Page 12: Main Exam 2

a double IEEEremainder(double, double)  

b double log(double a)  

c double log10(double a)  

d double rint(double a)  

e float max(float, float)

Question 26 of 61

Which of the following are valid declarations of the standard main() method?

Select 2 correct optionsa static void main(String args[ ]) { }  

b public static int main(String args[ ]) {}  

c public static void main (String args) {  }  

d final static public void main (String[ ] arguments ) { }  

e public static void main (String[ ] args) {  }

Question 27 of 61

Your application needs to load a set of key value pairs from a database table which never changes. Multiple threads need to access this information but none of them changes it. Which class would be the most appropriate to store such data?

Select 1 correct option.a Hashtable  

b HashMap  

c Set  

d TreeMap  

e List

Question 28 of 61

What is guaranteed by the JVM about the method yield()?

Select 1 correct option.a All lower priority threads will be granted CPU time.  

b The current thread will sleep for some time giving a chance to other threads to run.

 

c Atleast one thread having lower priority will get CPU time.  

Page 13: Main Exam 2

d The thread will sleep until it is notified.  

e Nothing is guaranteed.

Question 29 of 61

Given the following class definitions, the expression  (obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D) correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?   class A {}   class B extends A {}   class C extends B {}   class D extends C {}

Select 1 correct option.a True  

b False

Question 30 of 61

Consider the following code:

interface I{ int i = 1, ii = Test.out("ii", 2);}interface J extends I{ int j = Test.out("j", 3), jj = Test.out("jj", 4);}class Test{ public static void main(String[] args) { System.out.println(J.i); } public static int out(String s, int i) { System.out.println(s + "=" + i); return i; }}

What will be the output when class Test is run ?

Select 1 correct option.a It will print 1.  

b It will print ii=2, j = 3, jj=4 and then 1.  

c It will print ii=2 and then 1.  

Page 14: Main Exam 2

d It will not even compile.  

e None of the above.

Question 31 of 61

The following is a valid member variable declaration ...

private static final transient int i = 20;

Select 1 correct option.a True  

b False

Question 32 of 61

Which of these statements are true?

Select 2 correct optionsa Calling the method sleep( ) does not kill a thread.  

b A thread dies when the start( ) method ends.  

c A thread dies when the thread's constructor ends.  

d A thread dies when the run( ) method ends.  

e Calling the method kill( ) stops and kills a thread.

Question 33 of 61

Given the following code, which of these statements are true?

class TestClass{ public static void main(String args[]) { int k = 0; int m = 0; for ( int i = 0; i <= 3; i++) { k++; if ( i == 2) { // line 1 } m++; } System.out.println( k + ", " + m ); }}

Page 15: Main Exam 2

 

Select 3 correct optionsa It will print 3, 2  when line 1 is replaced by  break;  

b It will print 3, 2 when line 1 is replaced by continue.  

c It will print 4, 3 when line 1 is replaced by  continue.  

d It will print 4, 4 when line 1 is replaced by  i = m++;  

e It will print 3, 3 when line 1 is replaced by  i = 4;

Question 34 of 61

What is the correct declaration of an abstract public method 'add' accessible to everybody, takes no arguments and returns nothing? (Use only one space between words)

 

Question 35 of 61

Which modifiers would be valid in the declaration of a main() method so that the class can be run from command line?

Select 3 correct optionsa native  

b protected  

c public  

d final  

e abstract

Question 36 of 61

Which statements concerning the methods notify() and notifyAll() are true?

Select 1 correct option.a Only instances of class Thread have a method called notify().  

b A call to the method notify() will wake the thread that currently owns the monitor of the object.

 

c The method notify() is synchronized.  

d The method notifyAll() is defined in class Thread.  

Page 16: Main Exam 2

e When there is more than one thread waiting to obtain the monitor of an object, there is no way to be sure which thread will be notified by the notify() method..

Question 37 of 61

What will be the result of  attempting to compile this class?

package test;public class TopClass{ public Inner inner1;}class Inner{ int value;}

Select 1 correct option.a The class will fail to compile, since the class Inner has not yet been declared

when referenced in class TopClass. 

b The class will fail to compile, since inner is a keyword.  

c The class will fail to compile, since you cannot write 2 classes in the same file.  

d The class will fail to compile, since the class Inner must be defined in a file called Inner.java

 

e The classes will compile.

Question 38 of 61

Expression (  s instanceof java.awt.Point  )    will return false if 's' was declared as a variable of class java.lang.String.

Select 1 correct option.a True  

b False

Question 39 of 61

An overriding method can declare ArithmaticException in it's throws clause even if the overridden method does not have any throws clause.

Select 1 correct option.a True  

b False

Page 17: Main Exam 2

Question 40 of 61

Which of the following statements about NaNs are true ?

Select 1 correct option.a Float.NaN == Float.NaN  

b Float.NaN == Double.NaN  

c Float.NaN >= 0  

d Float.NaN < 0  

e None of the above.

Question 41 of 61

Consider the following program...

public class TestClass implements Runnable{ int x = 0, y = 0; public void run() { while(true) { synchronized(this) { x++; y++; System.out.println(" x = "+x+" , y = "+y); } } } public static void main(String[] args) { TestClass tc = new TestClass(); new Thread(tc).start(); new Thread(tc).start(); }}

Select 1 correct option.a It will throw exception at run time as two thread cannot be created from the

same object. 

b It will keep on printing values which show x and y always as equal and increasing by 1 at each line.

 

c It will keep on printing values which show x and y always as different.  

d Nothing can be said about the sequence of values.  

e It will keep on printing values which show x and y always as equal but may increase more than 1 at each line.

Page 18: Main Exam 2

Question 42 of 61

Consider the following class :

class Test{ public static void main(String[] args) { if (args[0].equals("open")) if (args[1].equals("someone")) System.out.println("Hello!"); else System.out.println("Go away "+ args[1]); }}

Which of the following statements are true if the above program is run with the command line : java Test closed

Select 1 correct option.a It will throw ArrayIndexOutOfBoundsException at runtime.  

b It will end without exceptions and will print nothing.  

c It will print "Go away".  

d It will print "Go away" and then will throw ArrayIndexOutOfBoundsException.  

e None of the above.

Question 43 of 61

Which of these classes have a comparator() method?

Select 2 correct optionsa TreeSet  

b HashMap  

c TreeMap  

d HashSet  

e ArrayList

Question 44 of 61

What will be the output of the following program (excluding the quotes, of course)?

public class SubstringTest{ public static void main(String args[]) { String String = "string isa string";

Page 19: Main Exam 2

System.out.println(String.substring(3, 6)); }}

Select 1 correct option.a It will not compile.  

b "ing is"  

c "ing isa"  

d "ing " (There is a space after g)  

e None of the above.

Question 45 of 61

Which of the following code fragments show syntactically correct usage of assertions?

a.)public void assertTest(){

...assert value == 10 "Value is not 10";

}b.)

public void assertTest(){

...assert value == 10 : "Value is not 10";

}c.)

public void assertTest(){

...assert value == 10 , "Value is not 10";

}d.)

public void assertTest(){

...assert value == 10 ? "Value is not 10";

}e.)

public void assertTest(){

...assert value == 10 | "Value is not 10";

}

Select 1 correct option.a a  

b b  

Page 20: Main Exam 2

c c  

d d  

e e

Question 46 of 61

Consider the following program...

class Super { }class Sub extends Super { }public class TestClass{ public static void main(String[] args) { Super s1 = new Super(); //1 Sub s2 = new Sub(); //2 s1 = (Super) s2; //3 }}

Which of the followong statements are correct?

Select 1 correct option.a It will compile and run without any problems.  

b It will compile but WILL throw ClassCastException at runtime.  

c It will compile but MAY throw ClassCastException at runtime.  

d It will not compile.  

e None of the above.

Question 47 of 61

What will be the result of attempting to compile and run the following program?

class TestClass{ public static void main(String args[]) { int i = 0; for (i=1 ; i<5 ; i++) continue; //(1) for (i=0 ; ; i++) break; //(2) for ( ; i<5?false:true ; ); //(3) }}

Select 1 correct option.a The code will compile without error and will terminate without problems when

run. 

Page 21: Main Exam 2

b The code will fail to compile, since the 'continue' can't be used this way.  

c The code will fail to compile, since the 'break' can't be used this way  

d The code will fail to compile, Since the for statement in the line 2 is invalid.  

e The code will compile without error but will never terminate.

Question 48 of 61

class java.util.BitSet implements java.util.Collection.

Select 1 correct option.a True  

b False

Question 49 of 61

What would be printed during execution of the following program?

public class TestClass{ public static void main(String args[ ] ) { shiftTest(1, "A" ) ; shiftTest(1<<31, "B") ; shiftTest(1<<30, "C") ; shiftTest(-1, "D" ) ; } public static void shiftTest(int i , String str) { if (( i >> 1) != (i >>> 1)) System.out.println(str) ; }}

  

Select 2 correct optionsa A  

b B  

c C  

d D  

e None of these.

Question 50 of 61

Which statement regarding the following code is true?

Page 22: Main Exam 2

int a, b;b = 5;

Select 1 correct option.a Variable 'a' is not declared  

b Variable 'b' is not declared  

c Variable 'a' is declared but not initialized  

d Variable 'b' is declared but not initialized.  

e Variable 'b' is initialized but not declared.

Question 51 of 61

Which of these statements concerning the charAt() method of the String class are true?

Select 2 correct optionsa The charAt( ) method can take a char value as an argument.  

b The charAt( ) method returns a Character object.  

c The expression char ch =  "12345".charAt(3) will assign 3 to ch.  

d The expression char ch =  str.charAt(str.length()) where str is "12345",  will assign 3 to ch.

 

e The index of the first character is 0.

Question 52 of 61

Which of these statements concerning interfaces are true?  Select 2 correct optionsa An interface extends an interface.  

b An interface extends a class and implements an interface.  

c A class implements an interface and extends a class.  

d A class extends an interface and implements a class.  

e An interface can only be implemented and cannot be extended.

Question 53 of 61

Which of these are not legal declarations within a class?

Select 1 correct option.

Page 23: Main Exam 2

a static volatile int sa ;  

b final Object[ ] objArr = { null } ;  

c abstract int t ;  

d native void format( ) ;  

e final transient static private double PI = 3.14159265358979323846 ;

Question 54 of 61

An object is

Select 1 correct option.a what classes are instantiated from.  

b an instance of a class.  

c a blueprint for creating concrete realization of abstractions.  

d a reference to an attribute.  

e a variable.

Question 55 of 61

What will the following program print when run?

// Filename: TestClass.javapublic class TestClass{ public static void main(String args[] ){ A b = new B("good bye"); }}class A{ A() { this("hello", " world"); } A(String s) { System.out.println(s); } A(String s1, String s2){ this(s1 + s2); }}class B extends A{ B(){ super("good bye"); }; B(String s){ super(s, " world "); } B(String s1, String s2){ this(s1 + s2 + " ! "); }}

Select 1 correct option.a It will print "good bye".  

b It will print "hello world".  

c It will print "good bye world".  

Page 24: Main Exam 2

d It will print "good bye" followed by "hello world".  

e It will print  "hello world" followed by "good bye".

Question 56 of 61

What will the following program print? (See Exhibit)  public class InitTest{ public InitTest() { s1 = sM1("1"); } static String s1 = sM1("a"); String s3 = sM1("2"); { s1 = sM1("3"); } static { s1 = sM1("b"); } static String s2 = sM1("c"); String s4 = sM1("4"); public static void main(String args[]) { InitTest it = new InitTest(); } private static String sM1(String s) { System.out.println(s); return s; }}

Select 1 correct option.a The program will not compile.  

b It will print :  a b c 2 3 4 1  

c It will print :  2 3 4 1 a b c  

d It will print :  1 a 2 3 b c 4  

e It will print :  1 a b c 2 3 4

Question 57 of 61

Consider the following code snippet:

XXXX m ; switch( m ) { case 32 : System.out.println("32"); break;

Page 25: Main Exam 2

case 64 : System.out.println("64"); break; case 128 : System.out.println("128"); break; }

What type can 'm' be of so that the above code compiles and runs as expected ?

Select 3 correct optionsa int m;  

b long m;  

c char m;  

d byte m;  

e short m;

Question 58 of 61

What will be the result of attempting to compile and run the following program?

class TestClass{ public static void main(String args[]) { boolean b = false; int i = 1; do { i++ ; } while (b = !b); System.out.println( i ); }}

      

Select 1 correct option.a The code will fail to compile, 'while' has an invalid condition expression.  

b It will compile but will throw runtime exception.  

c It will print 3.  

d It will go in an infinite loop.  

e It will print 1.

Question 59 of 61

Given the following variable declaration within the definition of an interface, which of these declarations is equivalent to it?   int i = 10;

Select 1 correct option.

Page 26: Main Exam 2

a public static int i = 10;  

b public final int i = 10;  

c public static final int i = 10;  

d public int i = 10;  

e final int i = 10;

Question 60 of 61

Which of the following code fragments show syntactically correct usage of assertions?

a.)public void assertTest(){

String version = System.getProperty("java.version");assert version == true;

}b.)

public void assertTest(){

String version = System.getProperty("java.version");assert version != null;

}c.)

public void assertTest(){

String version = System.getProperty("java.version");assert version.equals("1.4.0_01");

}d.)

public void assertTest(){

String version = System.getProperty("java.version");assert version;

}e.)

public void assertTest(){

assert version = System.getProperty("java.version");}

Select 2 correct optionsa a  

b b  

c c  

d d  

e e

Page 27: Main Exam 2

Question 61 of 61

Which of the following statements is correct?

Select 1 correct option.a new, delete and goto are keywords in the Java language  

b try, catch and thrown are keywords in the Java language  

c static, unsigned and long are keywords in the Java language  

d exit, class and while are keywords in the Java language  

e return, goto and default are keywords in the Java language