1
Consider the following program:
public class ExceptionType {
public static void main(String
args[]) { String s = null;
try
{ System.out.println(s.le
ngth());
}
catch(Exception e)
{ System.out.println("Excep
tion 1");
}
fnally { try {
generateExcepti
on();
}
catch(Exception e)
{ System.out.println("Excep
tion 2");
}
}
}
static void generateException() throws IllegalArgumentException
{
throw new IllegalArgumentException();
}
}
Which of the following statements are true regarding the above
given program? (Choose 3)
Answer:
a. The output "Exception 2" is because of the
exception thrown programmatically
b. The output "Exception 1" is because of the Exception
thrown
programmatically
c. The output "Exception 1" is because of the Exception
thrown
by JVM
d. The Exception thrown by generateException() method
is an
Unchecked Exception
e. The output "Exception 2" is because of the
Exception thrown by JVM
Consider the following
code:
public class Except {
private void method1() throws Exception {
throw new RuntimeException();
}
public void method2() {
try {
method1();
} catch (RuntimeException e)
{ System.out.println("Caught
Exception");
} catch (Exception e) {
System.out.println("Caught Runtime Exception");
}
}
public static void main(String
args[]) { Except e = new
Except();
e.method2();
}
}
Which of the following gives the correct output for the above
code?
Answer: a. No output
b. Compile time error
c. Prints: Caught Runtime
Exception
d. Prints: Caught Exception
Which of the following statements are true about String
Arrays? (Choose 2) Answer:
a. Array
index can be a long value
b. Array index can be a negative
value
c. String[][] s = new
String[5][];
d. String[][] s;
e. Array decaration: String[6]
strarray;
Consider the following scenario:
The GenericFruit class defines the following method to return a
float value:
public float calories( float serving ) {
// code goes here
}
A junior programmer writing the Apple class, which
extends GenericFruit, proposes to defne the following
overriding method:
public double calories( double serving ) {
// code goes here
}
Which of the following statement is True regarding the
above scenario? Answer: a. It will not compile because of
the different return type.
b. It will not compile because of the different input
type in the parameter list.
c. The double version overrides the float
version.
d. It will compile but will not override the
GenericFruit method because of the different
parameter list.
Consider the following
program:
public class TestStart implements Runnable {
boolean stoper = true;
public void run() {
System.out.println ("Run method Executed");
}
public static void main
(String[] argv) { TestStart
objInt = new TestStart();
Thread threadX = new
Thread(objInt); threadX.start();
threadX.start();
}
}
What will be the output of the above program?
Answer: a. Compiles and executes
successfully
Prints "Run method executed"
b. Compiles and on execution
Prints "Run method executed"
then
throws Runtime exception
c. Compilation Error
d. Compiles and on execution
Prints "Run method executed"
Consider the following program:
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void
main(String[] args) { A a =
new A();
a.start();
System.out.print(
a.i);
}
}
What will be the output of the above program?
Answer:
a. Prints 0
b. Prints:
01
c.
Prints: 10
d.
Prints
Consider the following
code:
e. Compile-time
error
import java.util.*;
public class Code10 {
{
fnal Vector v;
v=new Vector();
}
public Code10() { }
public void codeMethod()
{ System.out.println(v.isE
mpty());
}
public static void main(String args[]) {
new Code10().codeMethod();
}
}
Which of the following will be the output for the
above code?
Answer:
a. Runtime error:
NullPointerException
b. Prints: false
c. Compilation error: cannot fnd the symbol
d. Prints:
true
e. Compilation error: v is not initialised inside the
constructor
Consider the following code:
In the following code methodA has an inner class
1. public class Base {
2. private static fnal int ID = 3;
3. public String name;
4. public void methodA( int nn ){
5. fnal int serialN = 11;
6. class inner {
7. void showResult(){
8.
System.out.println( "Rslt= " + XX );
9. }
10. } // end class inner
11. new inner().showResult();
12. } // end methodA
13. )
Which of the following variables would the statement in line 8
be able to use in place of XX? (Choose 3)
Answer:
a. The String variable 'name'
declared in line 3
b. Invoking
methodA() defined in line 4
c. The int variable 'nn' declared in line 4
d. The int variable 'serialN' declared
in line 5
e. The int variable 'ID' declared in line 2
Consider the following scenario:
A Java application needs to stream a video from a movie fle.
Which of the following options gives the correct combination of
stream classes that can be used to implement the above
requirement?
Answer: a. InputStreamReader and
FileInputStream
b. FileInputStream and FilterInputStream
c. LineInputStream and BufferedInputStream
d. FileReader and BufferedReader
e. FileInputStream and BufferedInputStream
10
Which of the following options are true about abstract
implementations in
Collections?(choose 3)
Answer:
a. It provides static factory class
b. All major implementations like Hashtable,
Vectors are supported
c. They provide hooks for custom
implementations
d. All major interfaces
are supported
e. Map is not supported
11
Consider the following code:
class AT1 {
public static void main (String[] args) {
byte[] a = new byte[1]; long[] b = new long[1];
float[] c = new float[1]; Object[] d = new
Object[1];
System.out.print(a[0]+","+b[0]+","+c[0]
+","+d[0]);
}
}
Which of the following will be the output of the above code?
Answer: a. Prints: 0,0,0,null
b. None of the listed
options
c. Run-time error
d. Prints: 0,0,0.0,null
e. Compile-time error
12
Consider the following code snippet:
interface i1 {
int i = 0;
}
interface i2 {
int i = 0;
}
class inter implements i1,
i2 { public static void
main(String[] a)
{ System.out.println(i);
}
}
Which of the following options will be the output of the above
code snippet?
a. Runtime Error
b. Prints: 0
c. No output
d. Compilation Error
13
The following class defnitions are in separate fles. Note that the
Widget and
BigWidget classes are in different packages:
1.
2.
3.
4.
5.
6.
7.
package conglomo;
public class Widget extends Object{
private int myWidth;
XXXXXX void setWidth( int n ) {
myWidth = n;
}
}
// the following is in a separate fle and in separate package
8. package conglomo.widgets;
9. import conglomo.Widget ;
10. public class BigWidget extends Widget {
11. BigWidget() {
12. setWidth( 204 );
13. }
14. }
Which of the following modifiers, used in line 4 instead of
XXXXXX, would allow the BigWidget class to access the
setWidth method (as in line 12)? (Choose 2)
Answer:
a. fnal
b. default (blank), that is, the method declaration
would read void setWidth( int n )
c. protected
d.private
e.public
14
Which of the following statements are true regarding toString()
method?(Choose 3)
Answer:
a. Declared in the Object
class
b. It is polymorphic
c. Essential for inheriting a
class
d. Defned in the
Object class
e. Gives the String representation of an
Object
It is possible to create a table using JDBC API. State True or False.
Answer:
16
Consider the following code
snippet:
import java.util.*;
public class TestCol8{
public static void main(String
argv[]){ TestCol8 junk = new
TestCol8(); junk.sampleMap();
}
public void sampleMap()
{ TreeMap tm = new
TreeMap();
tm.put("a","Hello");
tm.put("b","Java");
True False
tm.put("c","World");
Iterator it =
tm.keySet().iterator();
while(it.hasNext())
{ System.out.print(it.next()
);
}
}
}
What will be the output of the above code snippet?
Answer:
a. abc
b. Runtime
error c. HWJ
d. HelloJavaWorld
e. Compile error
17
Consider the following
program:
public class Exp4 {
static String s = "smile!..";
public static void
main(String[] args) { new
Exp4().s1();
System.out.println(s);
}
void s1() {
try {
s2();
}
catch (Exception e) {
s += "morning";
}
}
void s2() throws Exception {
s3();
s += "evening";
s3();
s += "good";
}
void s3() throws Exception {
throw new Exception();
}
}
What will be the output of the above program?
Answer: a.
smile!..morningeveni
ng
b. smile!..morning
c. smile!..
d. smile!..eveningmorning
e.
smile!..morningeveninggoo
d
18
Consider the following code snippet:
import java.io.*;
class Test {
int a = 10;
}
class Test2 extends Test implements Serializable {
int b;
public String toString() {
return "a = " + a + ", " + "b = " + b;
}
}
public class IOCode5 {
public static void main(String args[]) throws
FileNotFoundException, IOException,
ClassNotFoundException {
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("C:/ObjectD
ata")); Test2 t1 = new
Test2();
t1.a = 20;
t1.b = 30;
out.writeObject(
t1); out.close();
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("C:/ObjectData"));
Test2 t2 = (Test2) in.readObject(); // Line 1
System.out.println(t2);
}
}
What will be the output of the above code
snippet? Answer: a. a = 10, b = 30
b. a = 0, b = 30
c. a = 20, b =
30
d. a = 10, b = 0
e. throws TransientException at the commented line (//
Line 1)
19
Which of the following are main packages for
Annotations?(Choose
2) Answer:
a. java.io
b. java.util
c. java.lang
d.
java.lang.annotati
on
e.java.sql
20
Consider the following code:
public class LabeledBreak2 {
public static void main(String
args[]) {
loop:
for(int j=0; j<2;
j++) { for(int
i=0; i<10; i++) {
if(i == 5) break
loop;
System.out.print(i + " ");
}
}
}
}
Which of the following will be the output for the above code?
Answer: a. 0 1 2 3 4 0 1 2
34
b. 0 1 2 3 4 5
c. 0 1 2 3 4
21
Consider the following code
snippet:
abstract class
Director {
p
r
o
t
e
c
t
e
d
S
t
r
i
ng name;
d. 1 2 3 4 5
e. Indefnite Loop
Director(String name) {
this.name = name;
}
abstract void occupation();
}
class FilmDirector extends
Director { FilmDirector(String
name) { super(name);
}
void occupation() {
System.out.println("Director " + name + " directs flms");
}
}
public class TestDirector {
public static void main(String[] args) {
FilmDirector fd = new FilmDirector("Manirathnam");
fd.occupation();
new Director("Manirathnam") {
void occupation() {
System.out.println("Director " + name + " also produces flms");
}
}.occupation();
}
}
Which of the following will be the output of the above code
snippet?
Answer: a. Compilation fails at TestDirector class
b. Prints: Director Manirathnam also produces
flms
c. Prints: Director Manirathnam directs flms
Director Manirathnam also produces flms
d. Prints: Director Manirathnam directs flms
e. Runtime
Error
22
False.
Delimiters themselves be considered as tokens. State True or
Answer: True
23
False
Consider the following scenario:
A company decides that it only wants to use the most popular
names for its products. You have to give the number of
employees against each unique frst name.
Which of the following four core interfaces is best-suited for
implementing the above scenario?
Answer:
a. Map
b. Set
c. Queue
d. List
24
Which of the following modifier cannot be applied to the
declaration of a feld
(member of a class)?
Answer: a. protected
b. private
c. fnal
d. public
e. abstract
25
Which of the following class in java.sql package maps the SQL
data types to Java datatypes?
Answer: a. JDBCTypes
b. JDBCSQLTypes
c. No explicit data type mapping. Automatically mapped
on
Query Call.
d. Types
e. SQLTypes
26
Which of the following codes will compile and run properly?
Answer: a. public class Test1 {
public static void main() {
System.out.println("Test1");
}
}
b. public class Test2 {
static public void
main(String[] in)
{ System.out.println("Test2"
);
}
}
c. public class Test3 {
public static void
main(String args)
{ System.out.println("Test3"
);
}
}
d. public class Test4 {
static int main(String args[]) {
System.out.println("Test4");
}
}
e. public class Test5 {
static void main(String[] data)
{
System.out.println("Test5");
}
}
27
Consider the following code:
public class WrapIt {
public static void main(String[] args) {
new WrapIt().testC('a');
}
public void testC(char ch) {
Integer ss = new
Integer(ch);
Character cc = new
Character(ch);
if(ss.equals(cc))
System.out.print("equals ");
if(ss.intValue()==cc.charValue())
{ System.out.println("EQ");
}
}
}
Which of the following gives the valid output for the
above code? Answer: a. Prints: equals
b. Compile-time error: Integer wrapper cannot accept
char type c.Prints: EQ
d. Compile-time error: Wrapper types cannot be
compared using equals
e. Prints: equals EQ
28
Which of the following statements are valid 3 dimensional
character array creations?(Choose 2)
Answer:
a. char[][][] charArray = {{'a', 'b'}, {'c',
'd'}, {'e', 'f'}};
b. char[][][] charArray =
{{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};
c. char[][][] charArray = {{'a', 'b'}, {'c', 'd'},
{'e'}};
d. char[][][] charArray = new char[2][2][];
e. char[2][2][] charArray = {'a', 'b'};
29
Consider the following class defnition:
class InOut{
String s= new
String("Between"); public
void amethod(fnal int iArgs)
{ int iam;
class Bicycle{
public void sayHello(){
...Line 1
}
}//End of bicycle class
}//End of amethod
public void another(){
int iOther;
}
}
Which of the following statements would be correct to be
coded at ...Line 1? (Choose 2)
Answer:
a.
System.out.println(iArgs);
b. System.out.println(iam);
c.
System.out.println(iOther);
d. System.out.println(s);
30
Consider s1 and s2 are sets.
Which of the following options gives the exact meaning of
the method call s1.retainAll(s2)?
Answer: a. transforms s1 into the union of s1 and s2
b. transforms s1 into the intersection of s1 and s2.
c. transforms s1 into the (asymmetric) set
difference of s1 and s2
d. copies elements from s2 to s1
e. returns true if s2 is a subset
of s1
31
Which of the following annotations are defned in java.lang
package? (Choose 3) Answer:
a. @SuppressWarnings
b. @Target
c. @Retention
d. @Override
e. @deprecated
32
Consider the following code:
class Test
{ Test(in
t i) {
System.out.println("Test(" +
i +")");
}
}
public class Question{
static Test t1 = new
Test(1); Test t2 =
new Test(2);
static Test t3 = new Test(3);
public static void
main(String[] args)
{ Question Q = new
Question();
}
}
Which of the following options gives the correct order of
initialization?
Answer: a. Test(3)
Test(2)
Test(1)
b. Test(2)
Test(1)
Test(3)
c. Test(1)
Test(2)
Test(3)
d.
Test(1)
Test(3)
Test(2)
33
Which of the following methods is used to check whether
ResultSet object contains records?
Answer: a. frst()
b. hasRecords()
c. next()
d. last()
e. previous()
34
Which of the following options are true about Associations?
(choose 2) Answer:
a. In Associations, cardinality refers
to the number of related
objects
b. Association refers to binding of related data and
behaviours
into a single entity
c. Associations are bi-directional
d. Association refers to a class reuses the
properties and methods of another class
e. Association refers to an object composed of set of
other
objects
35
Consider the following code
snippet:
class TestString4 {
public static void main(String
args[]) { String s1 = "Its
Great";
String s2 = "Its Tricky";
System.out.print(s1.concat(s2).length() + " ");
System.out.print(s1.concat(s2.substring(1, s1.length())).length());
}
}
What will be the output of the following code snippet?
Answer: a. 18 20
b. 17 19
c. 20 18
d. 17 17
e. 19 17
36
Consider the following
code:
class Planet { }
class Earth extends Planet { }
public class WelcomePlanet {
public static void welcomePlanet(Planet planet) {
if (planet instanceof Earth)
{ System.out.println("Welc
ome!");
} else if (planet instanceof
Planet)
{ System.out.println("Plan
et!");
} else {
System.exit(0);
}
}
public static void main(String args[])
{ WelcomePlanet wp = new
WelcomePlanet(); Planet planet =
new Earth();
welcomePlanet(planet);
}
}
Which of the following will be the output of the above program?
Answer: a. An exception is thrown at
runtime
b. The code runs with no output
c. Welcome!
d.
Planet!
e. Compilation fails
37
What methods does the java.lang.Runtime class provide
related to memory management?(Choose 3)
Answer:
a. to invoke Garbage collector
b. to create new memory locations
c. to query the total memory and free
memory
d. to dump the objects to
storage device
e. to run fnalize methods explicitly
38
Which of the following statement is true?
Answer:
a. To call the wait() method, a thread must own
the lock of the object on which the call is to be
made.
b. To call the yield() method, a thread must own
the lock of the object on which the call is to be
made.
c. To call the sleep() method, a thread must own the
lock of the object which the call is to be made.
d. To call the wait() method, a thread must own the
lock of the current thread.
e. To call the join() method, a thread must own the
lock of the object on which the call is to be made
39
Which of the following statements are true?
(Choose 2) Answer:
a. All exceptions are
thrown by JVM
b. All RuntimeException are thrown by JVM
c. JVM cannot throw user-defned exceptions
d. All exceptions are thrown programmatically from the
code or
API
e. JVM thrown exceptions can be thrown
programmatically
40
Consider the following
code:
public class UnwiseThreads implements Runnable {
public void run() {
while(true)
{}
}
public static void main(String args[])
{ UnwiseThreads ut1 = new
UnwiseThreads(); UnwiseThreads
ut2 = new UnwiseThreads();
UnwiseThreads ut3 = new
UnwiseThreads(); ut1.run();
ut2.run(
);
ut3.run();
}
}
Which of the following is correct for the above given program?
Answer: a. The code compiles and runs 3 non ending non
daemon threads
b. The code compiles but runs only 1 non ending, non
daemon
thread
c. Runtime Error
"IllegalThreadStateException" d.
Compilation error "ut2.run() is never
reached"
41
Which of the following options is true about multi-level
inheritance?
Answer: a. Inheriting from two super classes
b. Inheriting from a class which is already in an
inheritance hierarchy
c. Inheriting from more than one
super class d. Inheriting from a single
class
42
43
Anonymous class can have their own members.
State True or False.
True False
Consider the following partial
code:
interface A { public int getValue(); }
class B implements A {
public int getValue() { return 1; }
}
class C extends B {
// insert code here
}
Which of the following code fragments, when inserted
individually at the commented line (// insert code here), makes
use of polymorphism? (Choose 3)
Answer:
a. public void add(A a) { a.getValue(); }
b. public void add(B b) { b.getValue(); }
c. public void add(C c1, C c2) {
c1.getValue(); }
d. public void add(C c) { c.getValue(); }
e. public void add(A a, B b) {
a.getValue(); }
44
From JDK 1.6, which of the following interfaces is also
implemented by java.util.TreeMap class?
Answer:
a.
NavigableMap
b.
NavigableSe
t
c.
NavigableLis
t d. Deque
45
Consider the following code snippet:
class
Node {
Node
node;
}
class NodeChain {
public static void main(String a[]) {
Node node1 = new Node(); //
Line 1 node1.node = node1;
// Code here
}
}
Which of the following code snippets when replaced at the
comment line (// Code Here) in the above code will make the
object created at Line 1, eligible for garbage collection? (Choose
2)
Answer:
a. node1.node = null;
b. node1 = node1.node;
c. node1.node = new
Node();
d. node1 = null;
e. node1 = new Node();