Main Exam 1

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Main Exam 1 as PDF for free.

More details

  • Words: 5,072
  • Pages: 31
Question 1 of 61 In the following code what will be the output if 0 (integer value zero) is passed to loopTest()? (See Exhibit) public class TestClass { public void loopTest(int x) { loop: for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) { System.out.println(i); if (x == 0) { continue loop; System.out.println(j); } } } }

}

Select 1 correct option. a The program will not compile. b It will print 1 2 3 4. c It will print 1 1 2 3 4 d It will print 1 1 2 2 3 3 4 4 e Produces no output

Question 2 of 61 Which line contains a valid constructor in the following class definition? public class TestClass { int i, j; public TestClass getInstance() { return new TestClass(); } public void TestClass(int x, int y) { i = x; j = y; } public TestClass TestClass() { return new TestClass(); } public ~TestClass() { i = x; j = y; } }

Select 1 correct option. a Line 1 b Line 2 c Line 3 d Line 4 e None of the above.

//1 //2 //3 //4

Question 3 of 61 Which of the following options can be a part of a correct inner class declaration or a combined declaration and instance initialization ? Select 2 correct options a private class C b new SimpleInterface() { ... } c new ComplexInterface(x) { ... } d private final abstract class C e new ComplexClass() implements SimpleInterface

Question 4 of 61 Consider the following program... class ArrayTest { public static void main(String[] args) { int ia[][] = { {1, 2}, null }; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) System.out.println(ia[i][j]); } }

Which of the following statements are true? Select 1 correct option. a It will not compile. b It will throw an ArrayIndexOutOfBoundsException at Runtime. c It will throw a NullPointerException at Runtime. d It will compile and run without throwing any exceptions. e None of the above.

Question 5 of 61 Consider the following class and interface definitions: public class Sample implements IInt { public static void main(String[] { Sample s = new Sample(); int j = s.thevalue; int k = IInt.thevalue; int l = thevalue; } } public interface IInt { int thevalue = 0; }

args) //1 //2 //3 //4

What will happen when the above code is compiled and run? Select 1 correct option. a It will give an error at compile time at line //1. b It will give an error at compile time at line //2. c It will give an error at compile time at line //3 d It will give an error at compile time at line //4. e It will compile and run without any problem.

Question 6 of 61 What will be result of attempting to compile this class? import java.util.*; package test; public class TestClass { public OtherClass oc = new OtherClass(); } class OtherClass { int value; }

Select 1 correct option.

a b c d e

The class will fail to compile, since the class OtherClass is used before it is defined. There is no problem with the code. The class will fail to compile, since the class OtherClass must be defined in a file called OtherClass.java The class will fail to compile . None of the above.

Question 7 of 61 Class finalization can be done by implementing the following method: static void classFinalize() throws Throwable ;

Select 1 correct option. a True b False

Question 8 of 61 Which of the following code segments will correctly double the value of an integer i ? Select 4 correct options a i << 1 ; b i=i*2; c i *= 2 ; d i += i ; e i <<= 1 ;

Question 9 of 61 What will the following program print? public class TestClass { public static void main(String[] args) { int x = 1; int y = 0; if( x/y ) System.out.println("Good"); else System.out.println("Bad"); } }

Select 1 correct option.

a b c d e

Good Bad Exception at runtime saying division by Zero. It will not compile. None of the above.

Consider the following class hierarchy class A {

public void m1() { } class B extends A { public void m1() { } class C extends B { public void m1() { /* //1 ... lot of code. */ } }

}

}

Select 2 correct options a You cannot access class A's m1() from class C for the same object ( ie. 'this'). b You can access class B's m1() using super.m1() from class C. c You can access class A's m1() using ( (A) this ).m1() from class C. d You can access class A's m1() using super.super.m1() from class C.

Question 10 of 61 Consider the following class hierarchy class A

{

public void m1() { } class B extends A { public void m1() { } class C extends B { public void m1() { /* //1 ... lot of code. */ } }

}

}

Select 2 correct options a You cannot access class A's m1() from class C for the same object ( ie. 'this'). b You can access class B's m1() using super.m1() from class C. c You can access class A's m1() using ( (A) this ).m1() from class C. d You can access class A's m1() using super.super.m1() from class C.

Question 11 of 61 Consider the following array definitions: int[] array1, array2[]; int[][] array3; int[] array4[], array5[]; Which of the follwing are valid statements? Select 3 correct options a array2 = array3; b array2 = array4; c array1 = array2; d array4 = array1; e array5 = array3

Question 12 of 61 Given the following class, which statements can be inserted at line 1 without causing the code to fail compilation? public class TestClass { int a; int b = 0; static int c; public void m() { int d; int e = 0; // Line 1 } }

Select 4 correct options a a++; b b++; c c++; d d++; e e++;

Question 13 of 61 Which lines contain a valid constructor in the following code? public class TestClass { public TestClass(int a, int b) { } public void TestClass(int a) { } public TestClass(String s); // 3 private TestClass(String s, int a) public TestClass(String s1, String }

Select 3 correct options a Line // 1 b Line // 2 c Line // 3 d Line // 4 e Line // 5

// 1 // 2 { } //4 s2) { }; //5

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; loop : // 1 { System.out.println("Loop Lable line"); try { for ( ; true ; i++ ) { if( i >5) break loop; // 2 } } catch(Exception e) { System.out.println("Exception in loop."); } finally { System.out.println("In Finally"); // 3 } } } }

Select 1 correct option. a Compilation error at line 1 as this is an invalid syntax for defining a label. b Compilation error at line 2 as 'loop' is not visible here. c No compilation error and line 3 will be executed. d No compilation error and line 3 will NOT be executed. e Only ' Loop Lable line' will be printed.

Question 15 of 61 What will be the result of attempting to compile and run the following program? public class TestClass { public static void main(String args[]) { try { RuntimeException re = null; throw re; } catch(Exception e) { System.out.println(e); } } }

Select 1 correct option. a The code will fail to compile, since RuntimeException cannot be caught by catching an Exception. b The program will fail to compile, since re is null. c The program will compile without error and will print java.lang.RuntimeException when run. d The program will compile without error and will print java.lang.NullPointerException when run. e The program will compile without error and will run and print 'null'.

Question 16 of 61 Which of the following statements regarding java.util.HastSet is correct? Select 1 correct option. a It keeps the elements in a sorted order. b It allows duplicate elements because it is based on HashMap. c It stores name-value pairs. d The order of elements while iteration remains the same always. e It allows null value to be stored.

Question 17 of 61 Which of these statements are true? (In option 1, Assume that finalize() is called as a part of garbage collection of an object.)

Select 2 correct options a If an exception is thrown during execution of the finalize method of an object, then the exception is ignored and the object is destroyed. b All objects have a finalize method. c To destroy an object explicitly, you must implement the finalize method. d To make sure that objects of a class are never destroyed by GC, you can implement finalize method with private accessibility. e If a class implements the finalize method, then the call to super class's finalize method is automatically inserted.

Question 18 of 61 In which of these variable declarations, will the variable remain uninitialized unless explicitly initialized? Select 1 correct option. a Declaration of an instance variable of type int. b Declaration of a static class variable of type float. c Declaration of a local variable of type float. d Declaration of a static class variable of class Object e Declaration of an instance variable of class Object.

Question 19 of 61 Which is the earliest line in the following code after which the object created on the //1 can be garbage collected, assuming no compiler optimizations are done? public class NewClass

{

}

static String enterTheDragon() { String a = new String("hello"); String b = new String("world !"); // 1 String c = new String(a + b + ""); // 2 String d = b; // 3 b = a; // 4 d = a; // 5 return c; // 6 } public static void main(String args[]) { String s = enterTheDragon(); // 7 System.out.println(s); // 8 }

Select 1 correct option.

a b c d e

Line 1 Line 2 Line 3 Line 4 Line 5

Question 20 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 21 of 61 Compared to public, protected and private accessibility, default accessibility is.... Select 1 correct option. a Less restrictive than public b More restrictive than public, but less restrictive than protected. c More restrictive than protected, but less restrictive than private. d More restrictive than private. e Less restrictive than protected from within a package, and more restrictive than protected from outside a package.

Question 22 of 61 Which of the following are valid declarations: Select 3 correct options

a b c d e

int a = b = c = 100; int a, b, c; a = b = c = 100; int a, b, c=100; int a=100, b, c; int a= 100 = b = c;

Question 23 of 61 What will the following class print when run? public class Sample

{

public static void main(String[] args) { String s1 = new String("java"); StringBuffer s2 = new StringBuffer("java"); replaceString(s1); replaceStringBuffer(s2); System.out.println(s1 + s2); } static void replaceString(String s) { s = s.replace('j', 'l'); } static void replaceStringBuffer(StringBuffer s) { s.append("c"); } }

Select 1 correct option. a javajava b lavajava c javajavac d lavajavac e None of these.

Question 24 of 61 Consider the following class: Which of the following statements are correct regarding the above class? import java.util.*; public class Info { String s1, s2, s3; public Info(String a, String b, String c) { s1=a; s2=b; s3=c; } public boolean equals(Object obj) { if(! (obj instanceof Info) ) return false; Info i = (Info) obj; return (s1+s2+s3).equals(i.s1+i.s2+i.s3); } public int hashCode() { return s1.hashCode(); } public static void main(String[] args) { HashMap map = new HashMap(); Info i1 = new Info("aaa", "aaa", "aaa"); Info i2 = new Info("aaa", "bbb", "ccc"); map.put(i1, "hello"); //1 map.put(i2, "world"); //2 } }

Select 1 correct option. a Question of invalid 61 implementation of hashCode() method with respect to the This25 is an given equals() method. b What will beone the of output of compiling andberunning thethe following program? Only the Info objects will stored in HashMap. c Both the objects will be stored in the HashMap. class CloneTest d An exception will be thrown at run time at line //2. { public 26 static Question of 61 void {

main(String[] args)

int ia[ ][ ] = { { 1 , 2}, null };

Which of these concerning collection interfaces are true? int ja[statements ][ ] = (int[ ] [ the ])ia.clone(); System.out.print((ia == ja) + " "); == ja[0] && ia[1] == ja[1]); 3System.out.println(ia[0] correct options

Select a } Set extends Collection. } b List extends Collection. c methods defined in Collection are also defined in List. Select 1All correct option. d HashMap SortedMap. a It will not implements compile because Arrays cannot be cloned like this as clone() is not e HashMap extends Hashtable public. b It will not compile because either clone() should be in try-catch block or main should have throws clause. c It will print 'false false' when run. d It will print 'false true' when run. e It will print 'true true' when run.

Question 27 of 61 Consider the following program: Which of the following is correct regarding the execution of the given program? public class TestClass extends Thread { private static int threadcounter = 0; public void run() { threadcounter++; System.out.println(threadcounter); } public static void main(String[] args) throws Exception { for(int i=0; i<10; i++) { synchronized(TestClass.class) { new TestClass().start(); } } } }

Select 2 correct options a Numbers 1 to 10 will be printed (none repeated or missed) in a serial order. b Numbers 1 to 10 will be printed (none repeated or missed) in any order. c Total of 10 numbers will be printed d The final value of threadcounter just before the program terminates will be 10. e The final value of threadcounter just before the program terminates can be any thing from 1 and 10.

Question 28 of 61 What will the following class print when executed? class Test { static static static public {

boolean a; boolean b; boolean c; static void main (String[] args) boolean bool = (a = true) || (b = true) && (c = true); System.out.print(a + ", " + b + ", " + c);

}

}

Select 1 correct option. a true, false, true b true, true, false c true, false, false d true, true, true

Question 29 of 61 Consider the following method... public int setVar(int a, int b, float c) { ...} Which of the following methods correctly overload the above method ? 1. public int setVar(int a, float b, int c) { return setVar(a, c, b); } 2. public int setVar(int a, float b, int c) { return this(a, c, b); } 3. public int setVar(int x, int y, float z) { return x+y; } 4. public float setVar(int a, int b, float c) { return c*a; } 5. public float setVar(int a) { return a; }

Select 2 correct options

a b c d e

1 2 3 4 5

Question 30 of 61 Which of the following access control keywords can be used to enable all the subclasses to access a method defined in the base class? Select 2 correct options a public b private c protected d No keyword is needed.

Question 31 of 61 What sequence of digits will the following program print? import java.util.* ; public class ListTest { public static void main(String args[]) { List s1 = new ArrayList( ); s1.add("a"); s1.add("b"); s1.add(1, "c"); List s2 = new LinkedList( s1.subList(1, 1) ); s1.addAll(s2); System.out.println(s1); } }

Select 1 correct option. a The sequence a, b, c is printed. b The sequence a, b, c, b is printed. c The sequence a, c, b, c is printed. d The sequence a, c, b is printed. e None of the above.

Question 32 of 61 Which of the following will not give any errors at compile and run time? Select 4 correct options a if (8 == 81) {} b if (x = 3) {} // assume that x is an int c if (true) {} d if (bool = false) {} //assume that bool is declared as a boolean e if (x == 10 ? true:false) { } // assume that x is an int

Question 33 of 61 Which statements concerning the value of a member variable are true, when it is not initialized explicitly? Select 1 correct option. a The value of a String variable is "" (empty string). b The value of all numeric types is zero and of char is '0'. c boolean variable will remain uninitialized. d boolean variable will be initialized to true. e The value of all object variables is null.

Question 34 of 61 How can you declare 'i' so that it is not visible outside the package 'test'. package test; class Test { XXX int i; /* lot of code */ }

Select 2 correct options a private b public c protected d No access modifier e friend

Question 35 of 61 What will be the result of attempting to compile and run the following program? public class Test extends Thread { String msg = "default" ; public Test(String s) { msg = s; } public void run() { System.out.println(msg); } public static void main(String args[]) { new Test("String1").start(); new Test("String2").start(); System.out.println("end"); } }

Select 1 correct option. a The program will fail to compile. b It will always print 'String1' 'String2' and 'end', in that order. c It will always print 'String1' 'String2' in random order followed by 'end'. d It will always print 'end' first. e No order is guaranteed.

Question 36 of 61 Consider following two classes: What will be the output of compiling and running class B ? //in file A.java package p1; public class A { protected int i = 10; public int getI() { return i; } } //in file B.java package p2; import p1.*; public class B extends p1.A { public void process(A a) { a.i = a.i*2; } public static void main(String[] args) { A a = new B(); B b = new B(); b.process(a); System.out.println( a.getI() ); } }

Select 1 correct option. a It will print 10. b It will print 20. c It will not compile. d It will throw an exception at Run time. e None of the above.

Question 37 of 61 Which digits, and in which order, will be printed when the following program is run? (See Exhibit) public class TestClass

{

}

public static void main(String args[]) { int k = 0; try{ int i = 5/k; } catch (ArithmeticException e){ System.out.println("1"); } catch (RuntimeException e){ System.out.println("2"); return ; } catch (Exception e){ System.out.println("3"); } finally{ System.out.println("4"); } System.out.println("5"); }

Select 1 correct option. a The program will print 5. b The program will print 1 and 4, in that order. c The program will print 1, 2 and 4, in that order. d The program will print 1, 4 and 5, in that order. e The program will print 1,2, 4 and 5, in that order.

Question 38 of 61 Consider the following program: Which of the following options can inserted at //2 so that "Waiting over" is printed assuming that gatekeeper.wait() is executed by the first thread before the second thread is started. public class TestClass extends Thread

{

}

private static Object gatekeeper = new Object(); private boolean isReader = false; public TestClass(boolean isReader) { this.isReader = isReader; } public void run() { synchronized(gatekeeper) { try { if(isReader) { gatekeeper.wait(); System.out.println("Waiting over"); //1 } else { System.out.println("Never waited"); // 2 insert statement here } } catch(Exception e){ } } } public static void main(String[] args) throws Exception { new TestClass(true).start(); new TestClass(false).start(); }

Select 1 correct option. a gatekeeper.notifyAll(); b gatekeeper.release(); c gatekeeper.wait(); d getCurrentThread().interrupt(); e Nothing can be done because the second thread will not be able to enter the Question 39 of 61 block. synchronized Which of these statements concerning the use of standard collection interfaces are true? Select 1 correct option. a None of the standard collection classes are thread safe. b class HashSet implements SortedSet. c Collection classes implementing List cannot have duplicate elements. d ArrayList can only accommodate a fixed number of elements. e Some operations may throw an UnsupportedOperationException.

Question 40 of 61 Which of the following statements concerning the switch construct are true? Select 3 correct options a A character literal can be used as a value for a case label. b A 'long' cannot be used as a switch variable. c An empty switch block is a valid construct. d A switch block must have a default label. e If present, the default label must be the last of all the labels.

Question 41 of 61 Consider : class A {} class B extends A {} class C extends B {} Which of these boolean expressions correctly identifies when an object 'o' acutally refers to an object of class B and not of C? Select 2 correct options a (o instanceof B) && (!(o instanceof A)) b !((o instanceof A) || (o instanceof B)) c (o instanceof B) && (!(o instanceof C)) d ! ( !(o instanceof B) || (o instanceof C)) e (o instanceof B) && !((o instanceof A) || (o instanceof C))

Question 42 of 61 What interface can a class implement so that it can execute in a seperate thread? Select 1 correct option. a Runnable b Threadable c Executable d Thread e Run

Question 43 of 61 Write a line of code that, when inserted at line 1, will make the overriding method in SubClass invoke the overridden method in BaseClass on the current object with the same parameter.

class BaseClass { public void print(String s) { System.out.println("BaseClass :"+s); } } class SubClass extends BaseClass { public void print(String s) { System.out.println("SubClass :"+s); // Line 1 } public static void main(String args[]) { SubClass sc = new SubClass(); sc.print("location"); } } ---------------

Question 44 of 61 Consider the following situation: Thread T1 holds the lock for an object Obj. Thread T2 is has called obj.wait() and is blocked. What will allow the thread T2 to become runnable? Select 1 correct option. a Thread T1 calls Thread.sleep(100). b Thread T2's wait() times out. c Thread T1 is interrupted. d Thread T1 releases the lock on Obj and calls the notify() method on T2. e Thread T1 releases the lock on Obj and calls the notify() method on Obj.

Question 45 of 61 public class TestClass { public static void main(String[] args) { String tom = args[0]; String dick = args[1]; String harry = args[2]; } }

What will the value of 'harry' if the program is run from the command line: java TestClass 111 222 333 Select 1 correct option.

a b c d e

111 222 333 It will throw an ArrayIndexOutOfBoundsException None of the above.

Question 46 of 61 What will be the output of compiling and running the following program: class TestClass implements I1, I2 {

public void m1() { System.out.println("Hello"); } public static void main(String[] args) { TestClass tc = new TestClass(); ( (I1) tc).m1(); }

} interface I1 { int VALUE = 1; void m1(); } interface I2 { int VALUE = 2; void m1(); }

Select 1 correct option. a It will print 'Hello'. b There is no way to access any 'VALUE' in TestClass. c The code will work fine only if 'VALUE' is removed from one of the interfaces. d It will not compile. e None of the above.

Question 47 of 61 What will be the output when the following program is compiled and run? (See Exhibit) public class TestClass extends Thread { String name = ""; public TestClass(String str) { name = str; } public void run() { try { Thread.sleep( (int) (Math.random()*1000) ); System.out.println(name); } catch(Exception e) { } } public static void main(String[] str) throws Exception { Thread t1 = new TestClass("tom"); Thread t2 = new TestClass("dick"); t1.start();t2.start(); t1.join(); t2.join(); System.out.println("harry"); } }

Select 1 correct option. a It'll always print tom, dick and harry, in that order. b It'll always print harry in the end. c It may print tom, dick and harry, in any order. d tom will always be printed before dick. e tom will always be printed first.

Question 48 of 61 Consider the following code for the min() method: public static void main(String[] args) throws Exception { int i = 1, j = 10; do { if (i++ > --j) continue; } while (i < 5); System.out.println("i=" + i + " j=" + j); }

What will be the output when the above code is executed? Select 1 correct option. a i=6 j=6 b i=5 j=6 c i=5 j=5 d i=6 j=5 e None of these.

Question 49 of 61 The following code snippet will not compile... int i = 10; System.out.println( i<20 ? out1() : out2() );

Assume that out1 and out2 have method signature: public void out1(); and public void out2(); Select 1 correct option. a True b False

Question 50 of 61 What will the following program print? public class TestClass { public static void main(String[] args) { Object obj1 = new Object(); Object obj2 = obj1; if( obj1.equals(obj2) ) System.out.println("true"); else System.out.println("false"); } }

Select 1 correct option. a true b false c It will not compile. d It will compile but throw an exception at run time. e None of the above.

Question 51 of 61 Consider the following class: public class TestClass { public static boolean isGoodnumber(int n) { assert n>5; return true; } public static void main(String[] args) { int i = Integer.parseInt(args[1]); int j = Integer.parseInt(args[2]); do { i--; if( i < 5) break; }while( isGoodnumber(j--) ); System.out.println(i); } }

Assuming that it is run using the following command line, which of the given options are correct? java -ea TestClass 8 7 8 Select 1 correct option. a 5 b 4 c It will throw an AssertionError d It will not compile because AssersionError is not declared in the throws clause of isG0odNumber() and main() methods.

Question 52 of 61 Which of the following is not a primitive data value in Java? Select 2 correct options

a b c d e

"x" 'x' 10.2F Object false

Question 53 of 61 Which of the following statements can be inserted successfully at // 1? public class InitTest { static int si = 10; int i; final boolean bool; // 1 }

Select 1 correct option. a instance { bool = true; } b InitTest() { si += 10; } c { si = 5; i = bool ? 1000 : 2000;} d { i = 1000; } e { bool = (si > 5); i = 1000; }

Question 54 of 61 Consider: o1 and o2 denote two different object references to different objects of same class. Which of these statements are true? Select 2 correct options a o1.equals(o2) is always false. b o1.hashCode( ) == o2.hashCode( ) is always false. c o1 == o2 is always false. d Nothing can be said about o1.equals(o2) . e Nothing can be said about o1 == o2.

Question 55 of 61 Which statements concerning the relation between the non static inner classes and their outer class instances are true? Select 4 correct options

a b c d e

Member variables of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers. Member variables of the outer instance can be referred to using only the variable name within the inner instance. More than one inner instance can be associated with the same outer instance. An inner class can extend it's outer class. A final outer class cannot have any inner classes.

Question 56 of 61 Consider the following classes: class A { public void getItDone(int counter) { assert counter >= 0 : "Less than zero"; for(int i=0; i
} class B extends A { public void getItDone(int counter) { assert counter < 100 : "Greater than 100"; for(int i=counter; i>0; i--){ } } public static void main(String args[]) { A a = new B(); a.getItDone(-4); } }

What will happen when the above classes are compiled and class B is run? Select 1 correct option. a The classes will not compile because getItDone() methods do not declare appropriate exceptions. b The program will throw an exception having the message "Less than zero". c The classes will compile and execute without any problem. d The program will never terminate

Question 57 of 61 Which of the following declarations are valid? float f = 1.0; float f = 43e1; float f = -1; float f = 0x0123; float f = 4; Select 3 correct options a float f1 = 1.0; b float f = 43e1; c float f = -1; d float f = 0x0123; e float f = 4;

Question 58 of 61 Only objects of class Thread or it's subclass can be used as monitors. Select 1 correct option. a True b False

Question 59 of 61 Which of the following methods can be called on a String object? Select 4 correct options a substring(int i) b substring(int i, int j) c substring(int i, int j, int k) d equals(Object o) e equals(String s)

Question 60 of 61 Which of the following statements are acceptable? Select 3 correct options

a b c d e

Object o = new Menu(); Boolean bool = false; char ch = 10; Thread t = new Runnable(); Runnable r = new Thread();

Question 61 of 61 What will the following lines of code print? String s = "java"; s.replace('j', 'l'); s.substring(0, 2); System.out.println(s);

Select 1 correct option. a java b lava c la d ja e lav

Related Documents

Main Exam 1
November 2019 16
Main Exam 1 Answers
November 2019 16
Main Exam 3
November 2019 16
Main Exam 6
November 2019 16
Main Exam 3 Answers
November 2019 26
Main Exam 9 Answers
November 2019 31