Main Exam 9

  • 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 9 as PDF for free.

More details

  • Words: 4,556
  • Pages: 33
Question 1 of 61 Which code fragments will print the last argument given on the command line to the standard output, and exit without any output and exceptions if no arguments are given? 1. public static void main(String args[ ]) { if (args.length != 0) System.out.println(args[args.length-1]); } 2. public static void main(String args[ ]) { try { System.out.println(args[args.length-1]); } catch (ArrayIndexOutOfBoundsException e) { } } 3. public static void main(String args[ ]) { int i = args.length; if (i != 0) System.out.println(args[i-1]); } 4. public static void main(String args[ ]) { int i = args.length-1; if (i > 0) System.out.println(args[i]); } 5. public static void main(String args[ ]) { try { System.out.println(args[args.length-1]); } catch (NullPointerException e) {} }

Select 3 correct options a Code No. 1 b Code No. 2 c Code No. 3 d Code No. 4 e Code No. 5

Question 2 of 61 What will be the result of attempting to compile and run class B? class A { final int fi = 10; } class B extends A { int fi = 15; public static void main(String[] args) { B b = new B(); b.fi = 20; System.out.println(b.fi); System.out.println( ( (A) b ).fi } }

);

Select 1 correct option. a It will not compile. b It will print 10 and then 10 c It will print 20 and then 20 d It will print 10 and then 20 e It will print 20 and then 10

Question 3 of 61 What will the following program print? class LoopTest { public static void main(String args[]) { int counter = 0; outer: for(int i = 0; i < 3; i++) middle: for(int j = 0; j < 3; j++) inner: for(int k = 0; k < 3; k++) { if(k-j>0) break middle; counter++; } System.out.println(counter); } }

Select 1 correct option. a 2 b 3 c 6 d 7 e 9

Question 4 of 61 Given the following program, which statment is true? class SomeClass

{

}

public static void main( String args[ ] ) { if (args.length == 0 ) { System.out.println("no arguments") ; } else { System.out.println( args.length + " arguments.") ; } }

Select 1 correct option. a The program will fail to compile. b The program will throw a NullPointerException when run with zero arguments. c The program will print "no arguments" and "1 arguments" when called with zero and one arguments. d The program will print "no arguments" and "2 arguments" when called with zero and one arguments. e The program will print "no arguments" and "3 arguments" when called with zero and one arguments.

Question 5 of 61 Given the class // Filename: Test.java public class Test { public static void main(String args[]) { for(int i = 0; i< args.length; i++) { System.out.print(" "+args[i]); } } }

Consider the following 3 options for running the program: a: java Test b: java Test param1 c: java Test param1 param2 Which of the following statements are true?

Select 2 correct options a The program will throw java.lang.ArrayIndexOutofBoundsException on option a. b The program will throw java.lang.NullPointerException on option a. c The program will print 'Test param1' on option b. d It will print 'param1 param2' on option c. e It will not print anything on option a.

Question 6 of 61 What will the following program print? class Test { public static void main(String args[]) { int i=0, j=0; X1: for(i = 0; i < 3; i++) { X2: for(j = 3; j > 0; j--) { if(i < j) continue X1; else break X2; } } System.out.println(i+" "+j); } }

Select 1 correct option. a 03 b 02 c 30 d 33 e 22

Question 7 of 61 The following class will compile and print 30 twice. class A { void setColor(byte public static void { A a = new A(); byte b = 30; a.setColor(b); a.setColor(30); } }

Select 1 correct option.

i) { System.out.println(""+i); } main(String[] args)

//1 //2

a b

True False

Question 8 of 61 What will the following program print? class Test { public static void main(String args[]) { int c = 0; boolean flag = true; for(int i = 0; i < 3; i++) { while(flag) { c++; if(i>c || c>5) flag = false; } } System.out.println(c); } }

Select 1 correct option. a 3 b 4 c 5 d 6 e 7

Question 9 of 61 Which of the following is correct regarding a HashSet?

Select 1 correct option. a Elements are stored in a sorted order. b It is immutable. c It only keeps unique elements. d Elements can be accessed using a unique key.

Question 10 of 61 Consider the following classes... The above code will print (ColoredPoint, Point) when compiled and run. class Point { int x, y; } class ColoredPoint extends Point { int color; } class Test { static void test(ColoredPoint p, Point q) { System.out.println("(ColoredPoint, Point)"); } static void test(Point p, ColoredPoint q) { System.out.println("(Point, ColoredPoint)"); } public static void main(String[] args) { ColoredPoint cp = new ColoredPoint(); test(cp, cp); } }

Select 1 correct option. a True b False

Question 11 of 61 Which of these statements about interfaces are true?

Select 3 correct options a Interfaces permit multiple implementation inheritance. b Unlike a class, an interface can extend from multiple interfaces. c Members of an interface are never static. d Members of an interface may be static. e interfaces cannot be final.

Question 12 of 61 Following is not a valid comment: /* this comment /* // /** is not valid */

Select 1 correct option. a True b False

Question 13 of 61 What will the following code print? void crazyLoop() { int c = 0; JACK: while (c < 8) { JILL: System.out.println(c); if (c > 3) break JILL; else c++; } }

Select 1 correct option. a It'll not compile. b It'll throw an exception at runtime. c It'll print numbers from 0 to 8 d It'll print numbers from 0 to 3 e It'll print numbers from 0 to 4

Question 14 of 61 Which of the following comments are valid ?

Select 4 correct options a // /* ..... */ b /* /* .... // */ c /* // ..... // */ d /* // .... */ e /* */ */

Question 15 of 61 Consider the following methods ... public int rightShiftInt(int number, int by) { return number >> by; } And the code snippet : { int i = 1, j = 1; i = rightShiftInt(i , 32); j = rightShiftInt(j , 31); j = rightShiftInt(j , 1); System.out.println(i + " , "+ j); //1 }

What will line //1 print ?

Select 1 correct option. a It will print 1 , 0 b It will print 1 , 1 c It will print 0 , 1 d It will print 0 , 0 e None of the above.

Question 16 of 61 What will the following code print when compiled and run? class Base { void methodA() { System.out.println("base - MethodA"); } } class Sub extends Base { public void methodA() { System.out.println("sub - MethodA"); } public void methodB() { System.out.println("sub - MethodB"); } public static void main(String args[]) { Base b=new Sub(); //1 b.methodA(); //2 b.methodB(); //3 } }

Select 1 correct option. a sub - MethodA , sub - MethodB b base - MethodA , sub - MethodB c Compile time error at //1 d Compile time error at //2 e Compile time error at //3

Question 17 of 61 What will happen when you compile and run the following program using the command line: java TestClass 1 2 public class TestClass { public static void main(String[] args) { int i = Integer.parseInt(args[1]); System.out.println(args[i]); } }

Select 1 correct option. a It'll print 1 b It'll print 2 c It'll print some junk value. d It'll throw ArrayIndexOutOfBoundsException e It'll throw NumberFormatException

Question 18 of 61 Which of the following statements are true?

Select 2 correct options a The garbage collector informs the object when it is about to destroy it. b You can directly free up the memory allocated to an object by calling it's finalize() method. c Garbage Collection feature of Java ensures that the program never runs out of memory. d It is possible for a program to make an object available for Garbage Collection e Garbage collector will use mark and sweep algorithm

Question 19 of 61 What will the following code snippet print? int index = 1; String[] strArr = new String[5]; String myStr = strArr[index]; System.out.println(myStr);

Select 1 correct option. a It will print nothing. b It will print 'null' c It will throw ArrayIndexOutOfBounds at runtime. d It will print some junk value. e None of the above.

Question 20 of 61 What will be the result of compiling and running the following code : public class TestClass { public static void main (String args[ ] ) { int k = 1; int i = ++k + k++ + + k ; System.out.print(i+ " "+ k) ; } }

Select 1 correct option. a Compilation error at : "++k + k++ + + k" expression b The program will compile and will print the value 7 and 3 when run. c The program will compile and will print the value 5 and 2 when run. d The program will compile and will print the value 9 and 3 when run. e The program will compile and will print the value 5 and 3 when run.

Question 21 of 61 Where in a constructor, can you place a call to a super class's constructor ?

Select 1 correct option. a Anywhere in the constructor's body. b As the first statement in the constructor. c Only as the first statement and it can be called just like any other method call. d You can't call super class's constructor in a base class as constructors are not inherited.. e None of the above.

Question 22 of 61 Consider the following line of code : Thread t = new Thread( anObjectOfMyClass );

Which of the following could be a valid declaration of MyClass?

Select 1 correct option. a MyClass extends Thread b MyClass implements Thread c MyClass implements Threadable d MyClass implements Run e None of these.

Question 23 of 61 If a.equals(b) returns true, b instanceof class_of_a should be true.

Select 1 correct option. a True b False

Question 24 of 61 Consider the following program : class Test { public static void main(String[] args) { short s = 10; // 1 char c = s; // 2 s = c; // 3 } }

Select 2 correct options a Line 3 is not valid. b Line 2 is not valid. c It will compile because both short and char can hold 10. d None of the lines 1, 2 and 3 is valid.

Question 25 of 61 Which of the following can be used as a constructor for the class shell given below? public class TestClass { // lots of code ... }

Select 2 correct options a public void TestClass() {...} b public TestClass() {...} c public static TestClass() {...} d public final TestClass() {...} e public TestClass(int x) { ...}

Question 26 of 61 Which of the following statements about this program are correct? class CoolThread extends Thread { String id = ""; public CoolThread(String s){ this.id = s; } public void run() { System.out.println(id+"End"); } public static void main(String args []) { Thread t1 = new CoolThread("AAA"); t1.setPriority(Thread.MIN_PRIORITY); Thread t2 = new CoolThread("BBB"); t2.setPriority(Thread.MAX_PRIORITY); t1.start(); t2.start(); } }

Select 1 correct option. a "AAA End" will always be printed before "BBB End". b "BBB End" will always be printed before "AAA End". c The order of "AAA End" and "BBB End" cannot be determined. d The program will not compile. e THe program will throw an exception at runtime.

Question 27 of 61 Which of the statements regarding the given code are correct? public class Test extends Thread { static Object obj1 = new Object(); static Object obj2 = new Object(); public void m1() { synchronized(obj1) { System.out.print("1 "); synchronized(obj2) { System.out.println("2"); } } } public void m2() { synchronized(obj2) { System.out.print("2 "); synchronized(obj1) { System.out.println("1"); } } } public void run() { m1(); m2(); } public static void main(String[] args) { new Test().start(); new Test().start(); } }

Select 2 correct options a It may result in a deadlock and the program might get stuck. b There is no potential for deadlock. c Deadlock may occur but the program will not get stuck as the JVM will resolve the deadlock. d The program will always print 1 2, 2 1, 1 2 and 21 e Nothing can be said for sure.

Question 28 of 61 Which variables of the encapsulationg class, can an inner class access if the inner class is defined in an instance method of the encapsulating class?

Select 4 correct options a All static variables b All final instance variables c All instance variables d All automatic variables. e All final automatic variables

Question 29 of 61 What is wrong with the following code written in a single file named TestClass.java? class SomeThrowable extends Throwable { } class MyThrowable extends SomeThrowable { } public class TestClass { public static void main(String args[]) throws SomeThrowable { try{ m1(); }catch(SomeThrowable e){ throw e; }finally{ System.out.println("Done"); } } public static void m1() throws MyThrowable { throw new MyThrowable(); } }

Select 2 correct options a The main declares that it throws SomeThrowable but throws MyThrowable. b You cannot have more than 2 classes in one file. c The catch block in the main method must declare that it catches MyThrowable rather than SomeThrowable. d There is nothing wrong with the code. e 'Done' will be printed.

Question 30 of 61 What will the following code print? class Test { public static void main(String args[]) { int c = 0; A: for(int i = 0; i < 2; i++) { B: for(int j = 0; j < 2; j++) { C: for(int k = 0; k < 3; k++) { c++; if(k>j) break; } } } System.out.println(c); } }

Select 1 correct option. a 7 b 8 c 9 d 10 e 11

Question 31 of 61 What does the method round(double) of the Math class return?

Select 1 correct option. a int b float c double d long e java.lang.Long

Question 32 of 61 Consider the following subclass definition: public class SubClass extends SuperClass { int i, j, k; public SubClass( int m, int n ) { i = m ; j = m ; public SubClass( int m ) { super(m ); } //2 }

} //1

Which of the following constructors MUST exist in SuperClass for SubClass to compile correctly?

Select 2 correct options a It's ok even if no explict constructor is defined in SuperClass b public SuperClass(int a, int b) c public SuperClass(int a) d public SuperClass() e only 'public SuperClass(int a)' is required.

Question 33 of 61 You are designing a class that will cache objects. This class should work by tracking the "last accessed times" of the objects. Which collection class would you use to store the objects?

Select 1 correct option. a HashSet b ArrayList c LinkedHashMap d LinkedList e TreeMap

Question 34 of 61 What will the following code print ? class Test { public static void main(String[] args) { int k = 1; int[] a = { 1 }; k += (k = 4) * (k + 2); a[0] += (a[0] = 4) * (a[0] + 2); System.out.println( k + " , " + a[0]); } }

Select 1 correct option. a It will not compile. b 4,4 c 25 , 25 d 13 , 13 e None of the above.

Question 35 of 61 What will the following program print when run? import java.util .*; public class TestClass { public static void main(String args[]) { Stack s1 = new Stack (); Stack s2 = new Stack (); processStacks (s1,s2); System.out.println (s1 + " "+ s2); } public static void processStacks(Stack x1, Stack x2) { x1.push (new Integer ("100")); x2 = x1; } }

Select 1 correct option.

a b c d e

[100] [100] [100] [] [] [100] [] [] None of the above.

Question 36 of 61 Which of these are keywords in Java?

Select 3 correct options a default b NULL c String d throws e long

Question 37 of 61 A method is ..

Select 1 correct option. a an implememtation of an abstraction. b an attribute defining the property of a particular abstraction. c a category of objects. d an operation defining the behavior for a particular abstraction. e a blueprint for making operations.

Question 38 of 61 What will be the result of attempting to compile and run the following class? public class InitTest { static String s1 = sM1("a"); { s1 = sM1("b"); } static { s1 = sM1("c"); } 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 fail to compile. b The program will compile without error and will print a, c and b in that order when run. c The program will compile without error and will print a, b and c in that order when run. d The program will compile without error and will print c, a and b in that order when run. e The program will compile without error and will print b, c and a in that order when run.

Question 39 of 61 Which of these objects are immutable ?

Select 4 correct options

a b c d e

Character Byte Short Boolean StringBuffer

Question 40 of 61 Consider the following class: public class PortConnector { public PortConnector(int port) throws IOException { ...lot of valid code. } ...other valid code. }

You want to write another class CleanConnector that extends from PortConnector. Which of the following statements should hold true for CleanConector class?

Select 1 correct option. a It is not possible to define CleanConnector that does not throw IOException at instanciation. b PortConnector class itself is not valid as you cannot throw exception from a constructor. c CleanConnector's constructor cannot throw any exception other than IOException. d CleanConnector's constructor cannot throw any exception other than subclass of IOException. e CleanConnector's constructor cannot throw any exception other than superclass of IOException.

Question 41 of 61 What will the following program print? public class TestClass { public static int leftShift(int a, int b) { return a <<= b; } public static void main(String[] args) { System.out.println( leftShift(4, 2) ); } }

Select 1 correct option. a 4 b 8 c 16 d 32 e 2

Question 42 of 61 In Java, Objects pass messages ...

Select 1 correct option. a by modifying each other's member variables. b by modifying the static member variables of each other's classes. c by calling each other's member methods. d by posting messages to a common message broker. e None of the above.

Question 43 of 61 An abstract method cannot be overridden.

Select 1 correct option. a True b False

Question 44 of 61 What is the return type of method round(float) from the Math class?

Select 1 correct option. a int b float c double d long e Double

Question 45 of 61 Which of the following statements are true?

Select 1 correct option. a Non-static inner classes must have either default or public accessibility. b Non-static inner classes cannot contain static members. c Methods in all nested classes can be declared static. d All nested classes can be declared static. e A static inner class can declare contain a non - static inner class.

Question 46 of 61 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 47 of 61 Which of these statements are true?

Select 1 correct option. a Objects can explicitly be destroyed using the keyword delete. b An object will be garbage collected immediately after the last reference to the object is removed. c If object obj1 is accessible from obj2 and obj2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection. d Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed. e An object can be finalized only once.

Question 48 of 61 You want to run Main.class from the command line. It uses two packages good.* and bad.*. You want to enable assertions for all classes of bad.* and at the same time want to disable them for the package good.*. Which of the following command lines will achieve the above?

Select 1 correct option. a java -ea bad.* Main b java -ea:bad... Main c java -ea:bad... -da:good... Main d java -ea bad.* -da good.* Main e None of these.

Question 49 of 61 What what will : "

hello java guru

return ?

Select 1 correct option.

".trim();

a b c d e

The line of code will not Compile. "hellojavaguru" "hello java guru" "hello java guru " None of the above.

Question 50 of 61 Consider the following code snippet: void m1() throws Exception { try { // line1 } catch (IOException e) { throw new SQLException(); } catch(SQLException e) { throw new InstantiationException(); } finally { throw new CloneNotSupportedException() RuntimeException. } }

// this is not a

Which of the following statements are true?

Select 2 correct options a If IOException gets thrown at line1, then the whole method will end up throwing SQLException. b If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException. c If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException() d If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException. e If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException()

Question 51 of 61 Which of the following are not legal Java identifiers?

Select 1 correct option. a goto b unsigned c String d _xyz e $_abc

Question 52 of 61 Which of these statements are true?

Select 2 correct options a A super( ) or this( ) call must always be provided explicitly as the first statement in the body of the constructor. b If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ). c If neither super( ) or this( ) is declared as the first statement of the body of a constructor, then this( ) will implicitly be inserted as the first statement. d super(...) can only be called in the first line of the constructor but this(...) can be called from anywhere. e You can either call super(...) or this(...) but not both.

Question 53 of 61 Assume that Thread 1 currently holds the lock for an object (obj) for which 4 other threads, Thread 2 to 5, are waiting. Now, Thread 1 want to release the lock but as the same time, it want Thread 3 to get the lock. How will you accomplish this?

Select 1 correct option. a Call t3.resume() after releasing the lock. b Call t3.release() after releasing the lock. c Instead of releasing the lock, call t3.accuire(obj); d Instead of releasing the lock, call t3.notify(obj); e None of these.

Question 54 of 61 Which of the following are correct?

Select 3 correct options a 128 >> 1 gives 64 b 128 >>> 1 gives 64 c 128 >> 1 gives -64 d 128 >>> 1 gives -64 e 128 << 1 gives 256

Question 55 of 61 A method with no access modifier can be overriden by a method marked protected.

Select 1 correct option. a True b False

Question 56 of 61 Which statments regarding the following program are correct? class A extends Thread { static protected int i = 0; public void run() { for(; i<5; i++) System.out.println("Hello"); } } public class TestClass extends A { public void run() { for(; i<5; i++) System.out.println("World"); } public static void main(String args []) { Thread t1 = new A(); Thread t2 = new TestClass(); t2.start(); t1.start(); } }

Select 1 correct option. a It'll not compile as run method cannot be overridden. b It'll print both "Hello" and "World" 5 times each. c It'll print both "Hello" and "World" 5 times each but they may be interspersed. d Total 5 words will be printed. e Either 5 "Hello" or 5 "world" will be printed.

Question 57 of 61 Consider the following code: class A { A() { print(); } private void print() { System.out.println("A"); } } class B extends A { int i = Math.round(3.5f); public static void main(String[] args) { A a = new B(); a.print(); } void print() { System.out.println(i); } }

What will be the output when class B is run ?

Select 1 correct option. a It will print A, 4. b It will print A, A c It will print 0, 4 d It will print 4, 4 e None of the above.

Question 58 of 61 You want to run Main.class [Assume that it belongs to the default package.] from the command line. It uses two packages good.* and bad.*. You want to disable assersions for all classes of bad.* as well as good.* but at the same time want to enable them for Main.class. Which of the following command lines will achieve the above?

Select 1 correct option. a java -ea Main b java -ea:* -da:good.* -da:bad.* Main c java -da:good... -da:bad... Main d java -ea:... -da:good... -da:bad... Main e None of these.

Question 59 of 61 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 60 of 61 Which of the following statements about this program are correct? class CoolThread extends Thread { String id = ""; public CoolThread(String s){ this.id = s; } public void run() { if(id.equals("AAA")) { yield(); } System.out.println(id+"End"); } public static void main(String args []) { Thread t1 = new CoolThread("AAA"); t1.setPriority(Thread.MAX_PRIORITY); Thread t2 = new CoolThread("BBB"); t2.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); } }

Select 1 correct option.

a b c d e

"AAA End" will always be printed before "BBB End". "BBB End" will always be printed before "AAA End". The order of "AAA End" and "BBB End" cannot be determined. The program will not compile. THe program will throw an exception at runtime.

Question 61 of 61 Which of the following is illegal ?

Select 1 correct option. a char c = 320; b float f = 320; c double d = 320; d byte b = 320; e None of the above is illegal.

Related Documents

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