Thursday, June 04, 2009

Java Fundamental-I

Q1 Given the following class, which statements can be inserted at position 1 without causing a compilation error?

public class Q6db8 {

int a;

int b = 0;

static int c;


public void m() {

int d;

int e = 0;

// Position 1

}

}

Select the four correct answers.

a. a++;

b. b++;

c. c++;

d. d++;

e. e++;


Q2 Which statements are true about the effect of the >> and >>> operators? Select the three correct answers.

a. For non-negative values of the left operand, the >> and >>> operators will have the same effect.

b. The result of (-1 >> 1) is 0.

c. The result of (-1 >>> 1) is –1.

d. The value returned by >>> will never be negative if the left operand is non-negative.

e. When using the >> operator, the left-most bit of the resulting value will always have the same bit value as the left-most bit of the left operand.

Q3 What is wrong with the following code?

class MyException extends Exception {}

public class Qb4ab {

public void foo() {

try {

bar();

} finally {

baz();

} catch (MyException e) {}

}

public void bar() throws MyException {

throw new MyException();

}

public void baz() throws RuntimeException {

throw new RuntimeException();

}

}

Select the one correct answer.

a. Since the method foo() does not catch the exception generated by the method baz(), it must declare the RuntimeException in a throws clause.

b. A try block cannot be followed by both a catch and a finally block.

c. An empty catch block is not allowed.

d. A catch block cannot follow a finally block.

e. A finally block must always follow one or more catch blocks.


Q4 What will be written to the standard output when the following program is run?

public class Qd803 {

public static void main(String[] args) {

String word = "restructure";

System.out.println(word.substring(2, 3));

}

}

Select the one correct answer.

a. est

b. es

c. str

d. st

e. s


Q5 Given that a static method doIt() in the class Work represents work to be done, which block of code will succeed in starting a new thread that will do the work?

Select the one correct answer.

a. Runnable r = new Runnable() {

public void run() {

Work.doIt();

}

};


b. Thread t = new Thread(r);

t.start();


c. Thread t = new Thread() {

public void start() {

Work.doIt();

}

};

t.start();


d. Runnable r = new Runnable() {

public void run() {

Work.doIt();

}

};

r.start();


e. Thread t = new Thread(new Work());

t.start();


f. Runnable t = new Runnable() {

public void run() {

Work.doIt();

}

};

t.run();


Q6 What will be printed when the following program is run?

public class Q8929 {

public static void main(String[] args) {

for (int i=12; i>0; i-=3)

System.out.print(i);

System.out.println("");

}

}

Select the one correct answer.

a. 12

b. 129630

c. 12963

d. 36912

e. None of the above.


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

public class Q275d {

static int a;

int b;

public Q275d() {

int c;

c = a;

a++;

b += c;

}

public static void main(String[] args) {

new Q275d();

}

}

Select the one correct answer.

a. The code will fail to compile since the constructor is trying to access static members.

b. The code will fail to compile since the constructor is trying to use static field a before it has been initialized.

c. The code will fail to compile since the constructor is trying to use field b before it has been initialized.

d. The code will fail to compile since the constructor is trying to use local variable c before it has been initialized.

e. The code will compile and run without any problems.


Q8 What will be written to the standard output when the following program is run?

public class Q63e3 {

public static void main(String[] args) {

System.out.println(9 ^ 2);

}

}

Select the one correct answer.

a. 81

b. 7

c. 11

d. 0

e. false


Q9 Which statements is true about the compilation and execution of the following program with assertions enabled?

public class Qf1e3 {

String s1;

String s2 = "hello";

String s3;

Qf1e3() {

s1 = "hello";

}

public static void main(String[] args) {

(new Qf1e3()).f();

}

{

s3 = "hello";

}

void f() {

String s4 = "hello";

String s5 = new String("hello");

assert(s1.equals(s2)); // (1)

assert(s2.equals(s3)); // (2)

assert(s3 == s4); // (3)

assert(s4 == s5); // (4)

}

}

Select the one correct answer.

a. The compilation will fail.

b. The assertion on the line marked (1) will fail.

c. The assertion on the line marked (2) will fail.

d. The assertion on the line marked (3) will fail.

e. The assertion on the line marked (4) will fail.

f. The program will run without any errors.


Q10 Which declarations of the main() method are valid in order to start the execution of an application?

Select the two correct answers.

a. public void main(String args[])

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

c. public static main(String[] argv)

d. final public static void main(String [] array)

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


Q11 Under which circumstance will a thread stop?

Select the one correct answer.

a. The run() method that the thread is executing ends.

b. The call to the start() method of the Thread object returns.

c. The suspend() method is called on the Thread object.

d. The wait() method is called on the Thread object.


Q12 When creating a class that associates a set of keys with a set of values, which of these interfaces is most applicable?

Select the one correct answer.

a. Collection

b. Set

c. SortedSet

d. Map


Q13 What is the result of running the following code with assertions enabled?

public class Q1eec {

static void test(int i) {

int j = i/2;

int k = i >>> 1;

assert j == k : i;

}

public static void main(String[] args) {

test(0);

test(2);

test(-2);

test(1001);

test(-1001);

}

}

Select the one correct answer.

a. The program executes normally and produces no output.

b. An AssertionError with 0 as message is thrown.

c. An AssertionError with 2 as message is thrown.

d. An AssertionError with -2 as message is thrown.

e. An AssertionError with 1001 as message is thrown.

f. An AssertionError with -1001 as message is thrown.


Q14 What will be written to the standard output when the following program is run?

class Base {

int i;

Base() { add(1); }

void add(int v) { i += v; }

void print() { System.out.println(i); }

}

class Extension extends Base {

Extension() { add(2); }

void add(int v) { i += v*2; }

}

public class Qd073 {

public static void main(String[] args) {

bogo(new Extension());

}

static void bogo(Base b) {

b.add(8);

b.print();

}

}

Select the one correct answer.

a. 9

b. 18

c. 20

d. 21

e. 22


Q15 Which declarations of a native method are valid in the declaration of the following class?

public class Qf575 {

// Insert declaration of a native method here

}

Select the two correct answers.

a. native public void setTemperature(int kelvin);

b. private native void setTemperature(int kelvin);

c. protected int native getTemperature();

d. public abstract native void setTemperature(int kelvin);

e. native int setTemperature(int kelvin) {}


Q16 Which collection implementation is suitable for maintaining an ordered sequence of objects, when objects are frequently inserted and removed from the middle of the sequence?

Select the one correct answer.

a. TreeMap

b. HashSet

c. Vector

d. LinkedList

e. ArrayList


Q17 Which statements can be inserted at the indicated position in the following code to make the program print 1 on the standard output when executed?

public class Q4a39 {

int a = 1;

int b = 1;

int c = 1;

class Inner {

int a = 2;

int get() {

int c = 3;

// Insert statement here.

return c;

}

}

Q4a39() {

Inner i = new Inner();

System.out.println(i.get());

}

public static void main(String[] args) {

new Q4a39();

}

}

Select the two correct answers.

a. c = b;

b. c = this.a;

c. c = this.b;

d. c = Q4a39.this.a;

e. c = c;


Q18 Which is the earliest line in the following code after which the object created in the line marked (0) will be a candidate for garbage collection, assuming no compiler optimizations are done?

public class Q76a9 {

static String f() {

String a = "hello";

String b = "bye"; // (0)

String c = b + "!"; // (1)

String d = b; // (2)

b = a; // (3)

d = a; // (4)

return c; // (5)

}

public static void main(String[] args) {

String msg = f();

System.out.println(msg); // (6)

}

}

Select the one correct answer.

a. The line marked (1).

b. The line marked (2).

c. The line marked (3).

d. The line marked (4).

e. The line marked (5).

f. The line marked (6).


Q19 Which method from the String and StringBuffer classes modifies the object on which it is invoked?

Select the one correct answer.

a. The charAt() method of the String class.

b. The toUpperCase() method of the String class.

c. The replace() method of the String class.

d. The reverse() method of the StringBuffer class.

e. The length() method of the StringBuffer class.


Q20 Which statement, when inserted at the indicated position in the following code, will cause a runtime exception?

class A {}

class B extends A {}

class C extends A {}

public class Q3ae4 {

public static void main(String[] args) {

A x = new A();

B y = new B();

C z = new C();

// Insert statement here

}

}

Select the one correct answer.

a. x = y;

b. z = x;

c. y = (B) x;

d. z = (C) y;

e. y = (A) y;


Q21 Which of these are keywords in Java?

Select three correct answers.

a. default

b. NULL

c. String

d. throws

e. long


Q22 A method within a class is only accessible by classes that are defined within the same package as the class of the method. How can such a restriction be enforced?

Select the one correct answer.

a. Declare the method with the keyword public.

b. Declare the method with the keyword protected.

c. Declare the method with the keyword private.

d. Declare the method with the keyword package.

e. Do not declare the method with any accessibility modifiers.


Q23 Which code initializes the two-dimensional array tab so that tab[3][2] is a valid element?

Select the two correct answers.

a. int[][] tab = {

{ 0, 0, 0 },

{ 0, 0, 0 }

};


b. int tab[][] = new int[4][];

for (int i=0; i 0) System.out.println(args[ix]);
}

e. public static void main(String[] args) {
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}

Q32 Which statements are true about the collection interfaces?
Select the three correct answers.
a. Set extends Collection.
b. All methods defined in Set are also defined in Collection.
c. List extends Collection.
d. All methods defined in List are also defined in Collection.
e. Map extends Collection.

Q33 Which is the legal range of values for a short?
Select the one correct answer.
a. –27 to 27–1
b. –28 to 28
c. –215 to 215–1
d. –216 to 216–1
e. 0 to 216–1

Q34 What is the name of the method that threads can use to pause their execution until signalled to continue by another thread?
Fill in the name of the method (do not include a parameter list).

Q35 Given the following class definitions, which expression 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 A {}
Select the one correct answer.
a. obj instanceof B
b. obj instanceof A && !(obj instanceof C)
c. obj instanceof B && !(obj instanceof C)
d. !(obj instanceof C || obj instanceof D)
e. !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)

Q36 What will be written to the standard output when the following program is executed?
public class Q8499 {
public static void main(String[] args) {
double d = -2.9;
int i = (int) d;
i *= (int) Math.ceil(d);
i *= (int) Math.abs(d);
System.out.println(i);
}
}
Select the one correct answer.
a. -12
b. 18
c. 8
d. 12
e. 27

Q37 What will be written to the standard output when the following program is executed?
public class Qcb90 {
int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = { 0 };
g(b, c);
System.out.println(a + " " + b + " " + c[0] + " ");
}
public void g(int b, int[] c) {
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String[] args) {
Qcb90 obj = new Qcb90();
obj.f();
}
}
Select the one correct answer.
a. 0 0 0
b. 0 0 1
c. 0 1 0
d. 1 0 0
e. 1 0 1

Q38 Given the following class, which are correct implementations of the hashCode() method?
class ValuePair {
public int a, b;
public boolean equals(Object other) {
try {
ValuePair o = (ValuePair) other;
return (a == o.a && b == o.b)
|| (a == o.b && b == o.a);
} catch (ClassCastException cce) {
return false;
}
}
public int hashCode() {
// Provide implementation here.
}
}
Select the three correct answers.
a. return 0;
b. return a;
c. return a + b;
d. return a - b;
e. return a ^ b;
f. return (a << 16) | b; Q39 Which statements are true regarding the execution of the following code? public class Q3a0a { public static void main(String[] args) { int j = 5; for (int i = 0; i < j-- : i > 0;
System.out.println(i*j);
}
}
}
Select the two correct answers.
a. An AssertionError will be thrown if assertions are enabled at runtime.
b. The last number printed is 4 if assertions are disabled at runtime.
c. The last number printed is 20 if assertions are disabled at runtime.
d. The last number printed is 4 if assertions are enabled at runtime.
e. The last number printed is 20 if assertions are enabled at runtime.

Q40 Which of the following method names are overloaded?
Select the three correct answers.
a. The method name yield in java.lang.Thread
b. The method name sleep in java.lang.Thread
c. The method name wait in java.lang.Object
d. The method name notify in java.lang.Object

Q41 Which are valid identifiers?
Select the three correct answers.
a. _class
b. $value$
c. zer@
d. ångström
e. 2much

Q42 What will be the result of attempting to compile and run the following program?
public class Q28fd {
public static void main(String[] args) {
int counter = 0;
l1:
for (int i=0; i<10; i++) { l2: int j = 0; while (j++ < 10) { if (j > i) break l2;
if (j == i) {
counter++;
continue l1;
}
}
}
System.out.println(counter);
}
}
Select the one correct answer.
a. The program will fail to compile.
b. The program will not terminate normally.
c. The program will write 10 to the standard output.
d. The program will write 0 to the standard output.
e. The program will write 9 to the standard output.

Q43 Given the following interface definition, which definition is valid?
interface I {
void setValue(int val);
int getValue();
}
Select the one correct answer.

a. class A extends I {
int value;
void setValue(int val) { value = val; }
int getValue() { return value; }
}

b. interface B extends I {
void increment();
}

c. abstract class C implements I {
int getValue() { return 0; }
abstract void increment();
}

d. interface D implements I {
void increment();
}

e. class E implements I {
int value;
public void setValue(int val) { value = val; }
}

Q44 Which statements are true about the methods notify() and notifyAll()?
Select the two correct answers.
a. An instance of the class Thread has a method named notify that can be invoked.
b. A call to the method notify() will wake the thread that currently owns the lock of the object.
c. The method notify() is synchronized.
d. The method notifyAll() is defined in class Thread.
e. When there is more than one thread waiting to obtain the lock of an object, there is no way to be sure which thread will be notified by the notify() method.

Q45 Which statements are true about the correlation between the inner and outer instances of member classes?
Select the two correct answers.
a. Fields of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers.
b. Fields of the outer instance can never be accessed using only the variable name within the inner instance.
c. More than one inner instance can be associated with the same outer instance.
d. All variables from the outer instance that should be accessible in the inner instance must be declared final.
e. A class that is declared final cannot have any member classes.

Q46 What will be the result of attempting to compile and run the following code?
public class Q6b0c {
public static void main(String[] args) {
int i = 4;
float f = 4.3;
double d = 1.8;
int c = 0;
if (i == f) c++;
if (((int) (f + d)) == ((int) f + (int) d)) c += 2;
System.out.println(c);
}
}
Select the one correct answer.
a. The code will fail to compile.
b. The value 0 will be written to the standard output.
c. The value 1 will be written to the standard output.
d. The value 2 will be written to the standard output.
e. The value 3 will be written to the standard output.

Q47 Which operators will always evaluate all the operands?
Select the two correct answers.
a. ||
b. +
c. &&
d. ? :
e. %

Q48 Which statement concerning the switch construct is true?
Select the one correct answer.
a. All switch statements must have a default label.
b. There must be exactly one label for each code segment in a switch statement.
c. The keyword continue can never occur within the body of a switch statement.
d. No case label may follow a default label within a single switch statement.
e. A character literal can be used as a value for a case label.

Q49 Which modifiers and return types would be valid in the declaration of a main() method that starts the execution of a Java standalone application?
Select the two correct answers.
a. private
b. final
c. static
d. int
e. abstract
f. String

Q50 Which of the following expressions are valid?
Select the three correct answers.
a. System.out.hashCode()
b. "".hashCode()
c. 42.hashCode()
d. ("4"+2).equals(42)
e. (new java.util.Vector()).hashCode()

Q51 Which statement regarding the following method definition is true?
boolean e() {
try {
assert false;
} catch (AssertionError ae) {
return true;
}
return false; // (1)
}
Select the one correct answer.
a. The code will fail to compile since catching an AssertionError is illegal.
b. The code will fail to compile since the return statement at (1) is unreachable.
c. The method will return true under all circumstances.
d. The method will return false under all circumstances.
e. The method will return true if and only if assertions are enabled at runtime.

Q52 If str denotes a String object with the string "73", which of these expressions will convert the string to the int value 73?
Select the two correct answers.
a. Integer.intValue(str)
b. ((int) str)
c. (new Integer(str)).intValue()
d. Integer.parseInt(str)
e. Integer.getInt(str)

Q53 Insert a line of code at the indicated location that will call the print() method in the Base class.
class Base {
public void print() {
System.out.println("base");
}
}
class Extension extends Base {
public void print() {
System.out.println("extension");

// Insert a line of code here.
}
}
public class Q294d {
public static void main(String[] args) {
Extension ext = new Extension();
ext.print();
}
}
Fill in a single line of code.

Q54 Given the following code, which statements are true?
public class Vertical {
private int alt;
public synchronized void up() {
++alt;
}
public void down() {
--alt;
}
public synchronized void jump() {
int a = alt;
up();
down();
assert(a == alt);
}
}
Select the two correct answers.
a. The code will fail to compile.
b. Separate threads can execute the up() method concurrently.
c. Separate threads can execute the down() method concurrently.
d. Separate threads can execute both the up() and down() method concurrently.
e. The assertion in the jump() method will not fail under any circumstances.

Q55 What will be written to the standard output when the following program is run?
public class Q03e4 {
public static void main(String[] args) {
String space = " ";
String composite = space + "hello" + space + space;
composite.concat("world");
String trimmed = composite.trim();
System.out.println(trimmed.length());
}
}
Select the one correct answer.
a. 5
b. 6
c. 7
d. 12
e. 13

Q56 Given the following code, which statements are true about the objects referenced through the fields i, j, and k, given that any thread may call the methods a(), b(), and c() at any time?
class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v--; }
}
public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}
public void c() {
k.inc();
System.out.println("c");
k.dec();
}
}
Select the two correct answers.
a. i.v is guaranteed always to be 0 or 1.
b. j.v is guaranteed always to be 0 or 1.
c. k.v is guaranteed always to be 0 or 1
d. j.v will always be greater than or equal to k.v at any give time.
e. k.v will always be greater than or equal to j.v at any give time.

Q57 Which statements are true about casting and conversion?
Select the three correct answers.
a. Conversion from int to long does not need a cast.
b. Conversion from byte to short does not need a cast.
c. Conversion from float to long does not need a cast.
d. Conversion from short to char does not need a cast.
e. Conversion from boolean to int using a cast is not possible.

Q58 Which method declarations, when inserted at the indicated position, will not cause the program to fail during compilation?
public class Qdd1f {
public long sum(long a, long b) { return a + b; }
// Insert new method declarations here.
}
Select the two correct answers.
a. public int sum(int a, int b) { return a + b; }
b. public int sum(long a, long b) { return 0; }
c. abstract int sum();
d. private long sum(long a, long b) { return a + b; }
e. public long sum(long a, int b) { return a + b; }

Q59 The 8859-1 character code for the uppercase letter A is the decimal value 65. Which code fragments declare and initialize a variable of type char with this value?
Select the two correct answers.
a. char ch = 65;
b. char ch = '\65';
c. char ch = '\0041';
d. char ch = 'A';
e. char ch = "A";

Q60 What will be the result of executing the following program code with assertions enabled?
import java.util.*;
public class Q4d3f {
public static void main(String[] args) {
LinkedList lla = new LinkedList();
LinkedList llb = new LinkedList();
assert lla.size() == llb.size() : "empty";
lla.add("Hello");
assert lla.size() == 1 : "size";
llb.add("Hello");
assert llb.contains("Hello") : "contains";
assert lla.get(0).equals(llb.get(0)) : "element";
assert lla.equals(llb) : "collection";
}
}
Select the one correct answer.
a. Execution proceeds normally and produces no output.
b. An AssertionError with the message "size" is thrown.
c. An AssertionError with the message "empty" is thrown.
d. An AssertionError with the message "element" is thrown
e. An IndexOutOfBoundsException is thrown.
f. An AssertionError with the message "container" is thrown.

Q61 Which of these are keywords in Java?
Select the two correct answers.
a. Double
b. native
c. main
d. unsafe
e. default

Wednesday, May 06, 2009

Java Fundamental-II

1. What is a Marker Interface?

An interface with no methods. Example: Serializable, Remote, Cloneable


2. What interface do you implement to do the sorting?

Comparable


3. What is the eligibility for a object to get cloned?

It must implement the Cloneable interface


4. What is the purpose of abstract class?

It is not an instantiable class. It provides the concrete implementation for some/all the methods. So that they can reuse the concrete functionality by inheriting the abstract class.


5. What is the difference between interface and abstract class?

Abstract class defined with methods. Interface will declare only the methods. Abstract classes are very much useful when there is a some functionality across various classes. Interfaces are well suited for the classes which varies in functionality but with the same method signatures.


6. What do you mean by RMI and how it is useful?

RMI is a remote method invocation. Using RMI, you can work with remote object. The function calls are as though you are invoking a local variable. So it gives you a impression that you are working really with a object that resides within your own JVM though it is somewhere.


7. What is the protocol used by RMI?

RMI-IIOP


8. What is a hashCode?

hash code value for this object which is unique for every object.


9. What is a thread?

Thread is a block of code which can execute concurrently with other threads in the JVM.


10. What is the algorithm used in Thread scheduling?

Fixed priority scheduling.


11. What is hash-collision in Hashtable and how it is handled in Java?

Two different keys with the same hash value. Two different entries will be kept in a single hash bucket to avoid the collision.


12. What are the different driver types available in JDBC?

1. A JDBC-ODBC bridge 2. A native-API partly Java technology-enabled driver 3. A net-protocol fully Java technology-enabled driver 4. A native-protocol fully Java technology-enabled driver For more information: Driver Description


13. Is JDBC-ODBC bridge multi-threaded?

No


14. Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection?

No


15. What is the use of serializable?

To persist the state of an object into any perminant storage device.


16. What is the use of transient?

It is an indicator to the JVM that those variables should not be persisted. It is the users responsibility to initialize the value when read back from the storage.


17. What are the different level lockings using the synchronization keyword?

Class level lock Object level lock Method level lock Block level lock

Search Aptipedia